SharedHandleTest.cc 1.6 KB

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