From 50921dfc2aba018c57c522e7281069e9c2632e49 Mon Sep 17 00:00:00 2001 From: josepjones Date: Mon, 29 Jul 2019 12:20:15 +0000 Subject: [PATCH] Refactor RefCount tests to use cppunit (#442) --- src/Makefile.am | 5 +- src/tests/testRefCount.cc | 146 ++++++++++++++++++++++++-------------- src/tests/testRefCount.h | 38 ++++++++++ 3 files changed, 135 insertions(+), 54 deletions(-) create mode 100644 src/tests/testRefCount.h diff --git a/src/Makefile.am b/src/Makefile.am index 3fefaf9706..fc03915def 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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) diff --git a/src/tests/testRefCount.cc b/src/tests/testRefCount.cc index 62bb27aaaa..e7474c6ada 100644 --- a/src/tests/testRefCount.cc +++ b/src/tests/testRefCount.cc @@ -10,8 +10,10 @@ #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 index 0000000000..8c74cb133f --- /dev/null +++ b/src/tests/testRefCount.h @@ -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 + +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 -- 2.47.2