]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Tests: shuffle libmem Pool unit test to src/
authorAmos Jeffries <squid3@treenet.co.nz>
Tue, 3 Jan 2017 03:29:53 +0000 (16:29 +1300)
committerAmos Jeffries <squid3@treenet.co.nz>
Tue, 3 Jan 2017 03:29:53 +0000 (16:29 +1300)
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
src/tests/testMem.cc [new file with mode: 0644]
src/tests/testMem.h [new file with mode: 0644]
test-suite/Makefile.am
test-suite/MemPoolTest.cc [deleted file]

index 535c290fadeaaa3d836b26070f8900236a82ba6a..3044cc9c276e4d67bad9c306ed224a0bdedb2a85 100644 (file)
@@ -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 (file)
index 0000000..32dbe07
--- /dev/null
@@ -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 <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);
+}
+
diff --git a/src/tests/testMem.h b/src/tests/testMem.h
new file mode 100644 (file)
index 0000000..551f7b6
--- /dev/null
@@ -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 <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 */
+
index 39e9126eeb268dd64f8cddaf5c2e2f08a2a80d1c..df2fbca8ba31dd370de9f1e7965ec39ad07a3a14 100644 (file)
@@ -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 (file)
index 2beb514..0000000
+++ /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 <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;
-}
-