SharedHandleTest.cc 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "SharedHandle.h"
  2. #include "common.h"
  3. #include <string>
  4. #include <cppunit/extensions/HelperMacros.h>
  5. using namespace std;
  6. class SharedHandleTest:public CppUnit::TestFixture {
  7. CPPUNIT_TEST_SUITE(SharedHandleTest);
  8. CPPUNIT_TEST(testSharedHandle);
  9. CPPUNIT_TEST_SUITE_END();
  10. static SharedHandle<int> staticHandle;
  11. public:
  12. void setUp() {
  13. }
  14. static SharedHandle<int> getInstance() {
  15. if(staticHandle.isNull()) {
  16. staticHandle = new int(1);
  17. }
  18. return staticHandle;
  19. }
  20. void testSharedHandle();
  21. };
  22. SharedHandle<int> SharedHandleTest::staticHandle = 0;
  23. CPPUNIT_TEST_SUITE_REGISTRATION( SharedHandleTest );
  24. void SharedHandleTest::testSharedHandle() {
  25. cerr << "xh:" << endl;
  26. SharedHandle<int> xh = new int(1);
  27. CPPUNIT_ASSERT_EQUAL(1, xh.getRefCount()->totalRefCount);
  28. CPPUNIT_ASSERT_EQUAL(1, xh.getRefCount()->strongRefCount);
  29. cerr << "nullHandle:" << endl;
  30. SharedHandle<int> nullHandle = 0;
  31. CPPUNIT_ASSERT_EQUAL(1, nullHandle.getRefCount()->totalRefCount);
  32. CPPUNIT_ASSERT_EQUAL(1, nullHandle.getRefCount()->strongRefCount);
  33. cerr << "staticHandle:" << endl;
  34. CPPUNIT_ASSERT_EQUAL(1, staticHandle.getRefCount()->totalRefCount);
  35. CPPUNIT_ASSERT_EQUAL(1, staticHandle.getRefCount()->strongRefCount);
  36. SharedHandle<int> localStaticHandle = getInstance();
  37. CPPUNIT_ASSERT_EQUAL(2, localStaticHandle.getRefCount()->totalRefCount);
  38. CPPUNIT_ASSERT_EQUAL(2, localStaticHandle.getRefCount()->strongRefCount);
  39. }