SingletonHolderTest.cc 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "SingletonHolder.h"
  2. #include <iostream>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. namespace aria2 {
  5. class SingletonHolderTest : public CppUnit::TestFixture {
  6. CPPUNIT_TEST_SUITE(SingletonHolderTest);
  7. CPPUNIT_TEST(testInstance);
  8. CPPUNIT_TEST_SUITE_END();
  9. private:
  10. public:
  11. void setUp() {
  12. }
  13. void testInstance();
  14. };
  15. CPPUNIT_TEST_SUITE_REGISTRATION( SingletonHolderTest );
  16. class M {
  17. private:
  18. std::string greeting_;
  19. public:
  20. M(const std::string& greeting):greeting_(greeting) {}
  21. const std::string& greeting() const { return greeting_; }
  22. void greeting(const std::string& greeting) {
  23. greeting_ = greeting;
  24. }
  25. };
  26. void SingletonHolderTest::testInstance()
  27. {
  28. std::shared_ptr<M> m(new M("Hello world."));
  29. SingletonHolder<M>::instance(m);
  30. CPPUNIT_ASSERT_EQUAL(std::string("Hello world."),
  31. SingletonHolder<M>::instance()->greeting());
  32. SingletonHolder<M>::instance()->greeting("Yes, it worked!");
  33. CPPUNIT_ASSERT_EQUAL(std::string("Yes, it worked!"),
  34. SingletonHolder<M>::instance()->greeting());
  35. }
  36. } // namespace aria2