]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Refactor RefCount tests to use cppunit (#442)
authorjosepjones <josepjones@expedia.com>
Mon, 29 Jul 2019 12:20:15 +0000 (12:20 +0000)
committerSquid Anubis <squid-anubis@squid-cache.org>
Mon, 29 Jul 2019 12:21:38 +0000 (12:21 +0000)
src/Makefile.am
src/tests/testRefCount.cc
src/tests/testRefCount.h [new file with mode: 0644]

index 3fefaf9706fea6a30afecc19e6de3fe3975eb349..fc03915defed3fcf783d851828251c49f34c392c 100644 (file)
@@ -958,12 +958,13 @@ tests_testLookupTable_LDADD = \
 tests_testLookupTable_LDFLAGS = $(LIBADD_DL)
 
 check_PROGRAMS += tests/testRefCount
-## TODO: make this use cppunit tests
 tests_testRefCount_SOURCES = \
-       tests/testRefCount.cc
+       tests/testRefCount.cc \
+       tests/testRefCount.h
 nodist_tests_testRefCount_SOURCES = \
        base/RefCount.h
 tests_testRefCount_LDADD = \
+       $(LIBCPPUNIT_LIBS) \
        $(COMPAT_LIB) \
        $(XTRA_LIBS)
 tests_testRefCount_LDFLAGS = $(LIBADD_DL)
index 62bb27aaaa5542905fe81dd1836b1d4ae5cdd4c7..e7474c6adadb5185a41160c990395e9eaa3cc600 100644 (file)
 
 #include "squid.h"
 #include "base/RefCount.h"
+#include "testRefCount.h"
+#include "unitTestMain.h"
 
-// XXX: upgrade these tests to CPPUnit testing framework
+CPPUNIT_TEST_SUITE_REGISTRATION( testRefCount );
 
 class _ToRefCount : public RefCountable
 {
@@ -21,7 +23,7 @@ public:
 
     int someMethod() {
         if (!Instances)
-            exit(EXIT_FAILURE);
+            return 0;
 
         return 1;
     }
@@ -31,7 +33,6 @@ public:
 
 typedef RefCount<_ToRefCount> ToRefCount;
 
-/* Must be zero at the end for the test to pass. */
 int _ToRefCount::Instances = 0;
 
 class AlsoRefCountable : public RefCountable, public _ToRefCount
@@ -41,91 +42,132 @@ public:
 
     int doSomething() {
         if (!Instances)
-            exit(EXIT_FAILURE);
+            return 0;
         return 1;
     }
 };
 
-int
-main (int argc, char **argv)
+void
+testRefCount::testCountability()
 {
     {
+        CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
         ToRefCount anObject(new _ToRefCount);
-        anObject->someMethod();
+        CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
+        CPPUNIT_ASSERT_EQUAL(1, anObject->someMethod());
         anObject = anObject;
+        CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
         ToRefCount objectTwo (anObject);
         anObject = objectTwo;
+        CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
         {
             ToRefCount anotherObject(new _ToRefCount);
             anObject = anotherObject;
+            CPPUNIT_ASSERT_EQUAL(2, _ToRefCount::Instances);
         }
 
         {
             ToRefCount aForthObject (anObject);
-            anObject = ToRefCount(NULL);
-            aForthObject->someMethod();
-            aForthObject = NULL;
+            CPPUNIT_ASSERT_EQUAL(2, _ToRefCount::Instances);
+            anObject = ToRefCount(nullptr);
+            CPPUNIT_ASSERT_EQUAL(2, _ToRefCount::Instances);
+            CPPUNIT_ASSERT_EQUAL(1, aForthObject->someMethod());
+            aForthObject = nullptr;
         }
+        CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
     }
+    CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
+}
 
+void
+testRefCount::testObjectToRefCounted()
+{
     /* Test creating an object, using it , and then making available as a
      * refcounted one:
      */
-    {
-        _ToRefCount *aPointer = new _ToRefCount;
-        aPointer->someMethod();
-        ToRefCount anObject(aPointer);
-    }
+    CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
+    _ToRefCount *aPointer = new _ToRefCount;
+    CPPUNIT_ASSERT_EQUAL(1, aPointer->someMethod());
+    ToRefCount anObject(aPointer);
+    CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
+}
+
+void
+testRefCount::testStandalonePointer()
+{
     /* standalone pointers should be usable */
-    {
-        ToRefCount anObject;
-    }
+    ToRefCount anObject;
+    CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
+}
+
+void
+testRefCount::testCheckPointers()
+{
     /* Can we check pointers for equality */
-    {
-        ToRefCount anObject;
-        ToRefCount anotherObject(new _ToRefCount);
+    ToRefCount anObject;
+    CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
+    ToRefCount anotherObject(new _ToRefCount);
 
-        if (anObject == anotherObject)
-            exit(1);
+    CPPUNIT_ASSERT(anObject != anotherObject);
 
-        anotherObject = NULL;
+    CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
+    anotherObject = nullptr;
 
-        if (!(anObject == anotherObject))
-            exit(1);
-    }
+    CPPUNIT_ASSERT_EQUAL(anObject, anotherObject);
+    CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
+}
+
+void
+testRefCount::testPointerConst()
+{
     /* Can we get the pointer for a const object */
-    {
-        ToRefCount anObject (new _ToRefCount);
-        ToRefCount const aConstObject (anObject);
-        _ToRefCount const *aPointer = aConstObject.getRaw();
+    CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
+    ToRefCount anObject (new _ToRefCount);
+    ToRefCount const aConstObject (anObject);
+    _ToRefCount const *aPointer = aConstObject.getRaw();
 
-        if (aPointer != anObject.getRaw())
-            exit(2);
-    }
+    CPPUNIT_ASSERT(aPointer == anObject.getRaw());
+    CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
+}
+
+void testRefCount::testRefCountFromConst()
+{
     /* Can we get a refcounted pointer from a const object */
-    {
-        _ToRefCount const * aPointer = new _ToRefCount;
-        ToRefCount anObject (aPointer);
-    }
+    CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
+    _ToRefCount const * aPointer = new _ToRefCount;
+    ToRefCount anObject (aPointer);
+
+    CPPUNIT_ASSERT(aPointer == anObject.getRaw());
+    CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
+}
+
+void
+testRefCount::testPointerFromRefCounter()
+{
     /* Can we get a pointer to nonconst from a nonconst refcounter */
-    {
-        ToRefCount anObject (new _ToRefCount);
-        _ToRefCount *aPointer = anObject.getRaw();
-        if (aPointer == NULL)
-            exit(3);
-        aPointer = NULL;
-    }
+    CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
+    ToRefCount anObject (new _ToRefCount);
+    CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
+    _ToRefCount *aPointer = anObject.getRaw();
+    CPPUNIT_ASSERT(aPointer != nullptr);
+    CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
+}
+
+void
+testRefCount::testDoubleInheritToSingleInherit()
+{
+
+    CPPUNIT_ASSERT_EQUAL(0, _ToRefCount::Instances);
     /* Create a doubley inheriting refcount instance,
      * cast to a single inheritance instance,
      * then hope :}
      */
+    ToRefCount aBaseObject;
     {
-        ToRefCount aBaseObject;
-        {
-            AlsoRefCountable::Pointer anObject (new AlsoRefCountable);
-            aBaseObject = anObject.getRaw();
-        }
+        AlsoRefCountable::Pointer anObject (new AlsoRefCountable);
+        aBaseObject = anObject.getRaw();
+        CPPUNIT_ASSERT_EQUAL(1, anObject->doSomething());
+        CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
     }
-    return _ToRefCount::Instances == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
+    CPPUNIT_ASSERT_EQUAL(1, _ToRefCount::Instances);
 }
-
diff --git a/src/tests/testRefCount.h b/src/tests/testRefCount.h
new file mode 100644 (file)
index 0000000..8c74cb1
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 1996-2019 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_TEST_REFCOUNT_H
+#define SQUID_SRC_TEST_REFCOUNT_H
+
+#include <cppunit/extensions/HelperMacros.h>
+
+class testRefCount : public CPPUNIT_NS::TestFixture
+{
+    CPPUNIT_TEST_SUITE( testRefCount );
+    CPPUNIT_TEST( testCountability );
+    CPPUNIT_TEST( testObjectToRefCounted );
+    CPPUNIT_TEST( testStandalonePointer );
+    CPPUNIT_TEST( testCheckPointers );
+    CPPUNIT_TEST( testPointerConst );
+    CPPUNIT_TEST( testRefCountFromConst );
+    CPPUNIT_TEST( testPointerFromRefCounter );
+    CPPUNIT_TEST( testDoubleInheritToSingleInherit );
+    CPPUNIT_TEST_SUITE_END();
+
+protected:
+    void testCountability();
+    void testObjectToRefCounted();
+    void testStandalonePointer();
+    void testCheckPointers();
+    void testPointerConst();
+    void testRefCountFromConst();
+    void testPointerFromRefCounter();
+    void testDoubleInheritToSingleInherit();
+};
+
+#endif