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.
tests/testHttpRequest \
tests/testIcmp \
tests/testIpAddress \
+ tests/testMem \
tests/testNetDb \
tests/testStore \
tests/testString \
$(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
--- /dev/null
+/*
+ * 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 <iostream>
+#include <stdexcept>
+
+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<SomethingToAlloc *>(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<SomethingToAlloc *>(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);
+}
+
--- /dev/null
+/*
+ * 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 <cppunit/extensions/HelperMacros.h>
+
+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 */
+
syntheticoperators \
VirtualDeleteOperator \
splay\
- MemPoolTest\
mem_node_test\
mem_hdr_test\
$(ESI_TESTS) \
## Sort by alpha - any build failures are significant.
check_PROGRAMS += debug \
$(ESI_TESTS) \
- MemPoolTest\
mem_node_test\
mem_hdr_test \
splay \
$(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)
+++ /dev/null
-/*
- * 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 <iostream>
-
-/* 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<SomethingToAlloc *>(Pool->alloc());
- assert (something);
- assert (something->aValue == 0);
- something->aValue = 5;
- Pool->freeOne(something);
- SomethingToAlloc *otherthing = static_cast<SomethingToAlloc *>(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;
-}
-