From 11e14d639b6979de936aad35a04be4cf82c6519b Mon Sep 17 00:00:00 2001 From: Amos Jeffries Date: Tue, 3 Jan 2017 16:29:53 +1300 Subject: [PATCH] Tests: shuffle libmem Pool unit test to src/ Refactor for cppunit framework instead of custom code. Duplicate the basic create-free-realloc cycle test for both Pool and MemProxy but do not add missing tests for other API calls at this time. --- src/Makefile.am | 16 +++++++++ src/tests/testMem.cc | 69 +++++++++++++++++++++++++++++++++++++++ src/tests/testMem.h | 30 +++++++++++++++++ test-suite/Makefile.am | 7 ---- test-suite/MemPoolTest.cc | 68 -------------------------------------- 5 files changed, 115 insertions(+), 75 deletions(-) create mode 100644 src/tests/testMem.cc create mode 100644 src/tests/testMem.h delete mode 100644 test-suite/MemPoolTest.cc diff --git a/src/Makefile.am b/src/Makefile.am index 535c290fad..3044cc9c27 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -876,6 +876,7 @@ check_PROGRAMS+=\ tests/testHttpRequest \ tests/testIcmp \ tests/testIpAddress \ + tests/testMem \ tests/testNetDb \ tests/testStore \ tests/testString \ @@ -3645,6 +3646,21 @@ tests_testYesNoNone_LDADD= \ $(XTRA_LIBS) tests_testYesNoNone_LDFLAGS = $(LIBADD_DL) +tests_testMem_SOURCES = \ + tests/testMem.cc \ + tests/testMem.h +nodist_tests_testMem_SOURCES = \ + tests/stub_debug.cc \ + tests/stub_time.cc \ + $(TESTSOURCES) +tests_testMem_LDADD= \ + base/libbase.la \ + mem/libmem.la \ + $(LIBCPPUNIT_LIBS) \ + $(COMPAT_LIB) \ + $(XTRA_LIBS) +tests_testMem_LDFLAGS = $(LIBADD_DL) + TESTS += testHeaders ## Special Universal .h dependency test script diff --git a/src/tests/testMem.cc b/src/tests/testMem.cc new file mode 100644 index 0000000000..32dbe077a7 --- /dev/null +++ b/src/tests/testMem.cc @@ -0,0 +1,69 @@ +/* + * Copyright (C) 1996-2017 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 "mem/forward.h" +#include "mem/Pool.h" +#include "tests/testMem.h" +#include "unitTestMain.h" + +#include +#include + +CPPUNIT_TEST_SUITE_REGISTRATION( testMem ); + +class SomethingToAlloc +{ +public: + int aValue; +}; + +class MoreToAlloc +{ + MEMPROXY_CLASS(MoreToAlloc); + +public: + int aValue = 0; +}; + +void +testMem::testMemPool() +{ + MemAllocator *Pool = memPoolCreate("Test Pool", sizeof(SomethingToAlloc)); + CPPUNIT_ASSERT(Pool); + + auto *something = static_cast(Pool->alloc()); + CPPUNIT_ASSERT(something); + CPPUNIT_ASSERT_EQUAL(something->aValue, 0); + something->aValue = 5; + Pool->freeOne(something); + + // Pool should use the FreeList to allocate next object + auto *otherthing = static_cast(Pool->alloc()); + CPPUNIT_ASSERT_EQUAL(otherthing, something); + CPPUNIT_ASSERT_EQUAL(otherthing->aValue, 0); + Pool->freeOne(otherthing); + + delete Pool; +} + +void +testMem::testMemProxy() +{ + auto *something = new MoreToAlloc; + CPPUNIT_ASSERT(something); + CPPUNIT_ASSERT_EQUAL(something->aValue, 0); + something->aValue = 5; + delete something; + + // The MEMPROXY pool should use its FreeList to allocate next object + auto *otherthing = new MoreToAlloc; + CPPUNIT_ASSERT_EQUAL(otherthing, something); + CPPUNIT_ASSERT_EQUAL(otherthing->aValue, 0); +} + diff --git a/src/tests/testMem.h b/src/tests/testMem.h new file mode 100644 index 0000000000..551f7b6f39 --- /dev/null +++ b/src/tests/testMem.h @@ -0,0 +1,30 @@ +/* + * Copyright (C) 1996-2017 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_SRC_TESTS_TESTMEM_H +#define SQUID_SRC_TESTS_TESTMEM_H + +#include + +class testMem : public CPPUNIT_NS::TestFixture +{ + CPPUNIT_TEST_SUITE( testMem ); + /* note the statement here and then the actual prototype below */ + CPPUNIT_TEST( testMemPool ); + CPPUNIT_TEST( testMemProxy ); + CPPUNIT_TEST_SUITE_END(); + +public: + +protected: + void testMemPool(); + void testMemProxy(); +}; + +#endif /* SQUID_SRC_TESTS_TESTMEM_H */ + diff --git a/test-suite/Makefile.am b/test-suite/Makefile.am index 39e9126eeb..df2fbca8ba 100644 --- a/test-suite/Makefile.am +++ b/test-suite/Makefile.am @@ -38,7 +38,6 @@ TESTS += debug \ syntheticoperators \ VirtualDeleteOperator \ splay\ - MemPoolTest\ mem_node_test\ mem_hdr_test\ $(ESI_TESTS) \ @@ -47,7 +46,6 @@ TESTS += debug \ ## Sort by alpha - any build failures are significant. check_PROGRAMS += debug \ $(ESI_TESTS) \ - MemPoolTest\ mem_node_test\ mem_hdr_test \ splay \ @@ -116,11 +114,6 @@ mem_hdr_test_LDADD = \ $(top_builddir)/src/mem/libmem.la \ $(LDADD) -MemPoolTest_SOURCES = MemPoolTest.cc $(DEBUG_SOURCE) -MemPoolTest_LDADD = \ - $(top_builddir)/src/mem/libmem.la \ - $(LDADD) - splay_SOURCES = splay.cc stub_libmem.cc $(DEBUG_SOURCE) syntheticoperators_SOURCES = syntheticoperators.cc stub_libmem.cc $(DEBUG_SOURCE) diff --git a/test-suite/MemPoolTest.cc b/test-suite/MemPoolTest.cc deleted file mode 100644 index 2beb5143f2..0000000000 --- a/test-suite/MemPoolTest.cc +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (C) 1996-2017 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" - -#if USE_MEMPOOLS - -#include "MemPool.h" - -#include - -/* TODO: put this in a libTest */ -void -xassert(const char *msg, const char *file, int line) -{ - std::cout << "Assertion failed: (" << msg << ") at " << file << ":" << line << std::endl; - exit (1); -} - -class MemPoolTest -{ -public: - void run(); -private: - class SomethingToAlloc - { - public: - int aValue; - }; - static MemAllocator *Pool; -}; -MemAllocator *MemPoolTest::Pool = NULL; - -void -MemPoolTest::run() -{ - assert (Pool == NULL); - Pool = memPoolCreate("Test Pool", sizeof(SomethingToAlloc)); - assert (Pool); - SomethingToAlloc *something = static_cast(Pool->alloc()); - assert (something); - assert (something->aValue == 0); - something->aValue = 5; - Pool->freeOne(something); - SomethingToAlloc *otherthing = static_cast(Pool->alloc()); - assert (otherthing == something); - assert (otherthing->aValue == 0); - Pool->freeOne(otherthing); - delete Pool; -} - -#endif /* USE_MEMPOOLS */ - -int -main (int argc, char **argv) -{ -#if USE_MEMPOOLS - MemPoolTest aTest; - aTest.run(); -#endif - return 0; -} - -- 2.47.3