From 24aa4519ba3f1e166bd751b2148ab50af3c58700 Mon Sep 17 00:00:00 2001 From: Francesco Chemolli Date: Mon, 27 Jul 2015 18:25:09 +0200 Subject: [PATCH] Implement LookupTable unit tests, documentation, copyright blurb --- src/Makefile.am | 21 +++++++++++++- src/base/LookupTable.h | 8 ++++++ src/tests/testLookupTable.cc | 53 ++++++++++++++++++++++++++++++++++++ src/tests/testLookupTable.h | 23 ++++++++++++++++ 4 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 src/tests/testLookupTable.cc create mode 100644 src/tests/testLookupTable.h diff --git a/src/Makefile.am b/src/Makefile.am index c1c9668cda..6ba5d5e57b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -917,7 +917,8 @@ check_PROGRAMS+=\ tests/testSBuf \ tests/testSBufList \ tests/testConfigParser \ - tests/testStatHist + tests/testStatHist \ + tests/testLookupTable if HAVE_FS_ROCK check_PROGRAMS += tests/testRock @@ -3655,6 +3656,24 @@ tests_testStatHist_LDADD = \ $(COMPAT_LIB) tests_testStatHist_DEPENDENCIES = $(SQUID_CPPUNIT_LA) +tests_testLookupTable_SOURCES = \ + tests/testLookupTable.h \ + tests/testLookupTable.cc \ + tests/stub_debug.cc \ + tests/stub_libmem.cc \ + tests/stub_SBufDetailedStats.cc \ + base/LookupTable.h \ + String.cc \ + $(SBUF_SOURCE) +nodist_tests_testLookupTable_SOURCES = $(TESTSOURCES) +tests_testLookupTable_LDFLAGS = $(LIBADD_DL) +tests_testLookupTable_LDADD = \ + base/libbase.la \ + $(SQUID_CPPUNIT_LIBS) \ + $(COMPAT_LIB) \ + $(XTRA_LIBS) +tests_testLookupTable_DEPENDENCIES = $(SQUID_CPPUNIT_LA) + TESTS += testHeaders ## Special Universal .h dependency test script diff --git a/src/base/LookupTable.h b/src/base/LookupTable.h index 8c5c608ca2..e50130ee6f 100644 --- a/src/base/LookupTable.h +++ b/src/base/LookupTable.h @@ -1,3 +1,11 @@ +/* + * Copyright (C) 1996-2015 The Squid Software Foundation and contributors + * + * Squid software is distributed under GPLv2+ license and includes + * contributions from numerous individuals and organizations. + * Please see the COPYING and CONTRIBUTORS files for details. + */ + #ifndef SQUID_LOOKUPTABLE_H_ #define SQUID_LOOKUPTABLE_H_ diff --git a/src/tests/testLookupTable.cc b/src/tests/testLookupTable.cc new file mode 100644 index 0000000000..268f839451 --- /dev/null +++ b/src/tests/testLookupTable.cc @@ -0,0 +1,53 @@ +/* + * Copyright (C) 1996-2015 The Squid Software Foundation and contributors + * + * Squid software is distributed under GPLv2+ license and includes + * contributions from numerous individuals and organizations. + * Please see the COPYING and CONTRIBUTORS files for details. + */ + +#include "squid.h" +#include "base/LookupTable.h" +#include "testLookupTable.h" +#include "unitTestMain.h" + +CPPUNIT_TEST_SUITE_REGISTRATION( testLookupTable ); + +enum EnumData { + ENUM_1, + ENUM_2, + ENUM_3, + ENUM_4, + ENUM_5, + ENUM_6, + ENUM_7, + ENUM_INVALID +}; + +static const LookupTable::Record tableData[] = { + {"one", ENUM_1}, + {"two", ENUM_2}, + {"three", ENUM_3}, + {"four", ENUM_4}, + {"five", ENUM_5}, + {"six", ENUM_6}, + {"seven", ENUM_7}, + {nullptr, ENUM_INVALID} +}; + +void +testLookupTable::testLookupTableLookup() +{ + LookupTable lt(ENUM_INVALID, tableData); + // element found + CPPUNIT_ASSERT_EQUAL(lt.lookup(SBuf("one")), ENUM_1); + CPPUNIT_ASSERT_EQUAL(lt.lookup(SBuf("two")), ENUM_2); + CPPUNIT_ASSERT_EQUAL(lt.lookup(SBuf("three")), ENUM_3); + CPPUNIT_ASSERT_EQUAL(lt.lookup(SBuf("four")), ENUM_4); + CPPUNIT_ASSERT_EQUAL(lt.lookup(SBuf("five")), ENUM_5); + CPPUNIT_ASSERT_EQUAL(lt.lookup(SBuf("six")), ENUM_6); + CPPUNIT_ASSERT_EQUAL(lt.lookup(SBuf("seven")), ENUM_7); + + // element not found + CPPUNIT_ASSERT_EQUAL(lt.lookup(SBuf("eleventy")), ENUM_INVALID); +} diff --git a/src/tests/testLookupTable.h b/src/tests/testLookupTable.h new file mode 100644 index 0000000000..545db6e49d --- /dev/null +++ b/src/tests/testLookupTable.h @@ -0,0 +1,23 @@ +/* + * Copyright (C) 1996-2015 The Squid Software Foundation and contributors + * + * Squid software is distributed under GPLv2+ license and includes + * contributions from numerous individuals and organizations. + * Please see the COPYING and CONTRIBUTORS files for details. + */ + +#ifndef SQUID_TESTLOOKUPTABLE_H_ +#define SQUID_TESTLOOKUPTABLE_H_ + +#include + +class testLookupTable : public CPPUNIT_NS::TestFixture +{ + CPPUNIT_TEST_SUITE( testLookupTable ); + CPPUNIT_TEST( testLookupTableLookup ); + CPPUNIT_TEST_SUITE_END(); +public: + void testLookupTableLookup(); +}; + +#endif /* SQUID_TESTLOOKUPTABLE_H_ */ -- 2.47.3