SingletonHolderTest.cc 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "SingletonHolder.h"
  2. #include <iostream>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "a2functional.h"
  5. namespace aria2 {
  6. class SingletonHolderTest : public CppUnit::TestFixture {
  7. CPPUNIT_TEST_SUITE(SingletonHolderTest);
  8. CPPUNIT_TEST(testInstance);
  9. CPPUNIT_TEST_SUITE_END();
  10. private:
  11. public:
  12. void setUp() {
  13. }
  14. void testInstance();
  15. };
  16. CPPUNIT_TEST_SUITE_REGISTRATION( SingletonHolderTest );
  17. class M {
  18. private:
  19. std::string greeting_;
  20. public:
  21. M(const std::string& greeting):greeting_(greeting) {}
  22. const std::string& greeting() const { return greeting_; }
  23. void greeting(const std::string& greeting) {
  24. greeting_ = greeting;
  25. }
  26. };
  27. void SingletonHolderTest::testInstance()
  28. {
  29. SingletonHolder<M>::instance(make_unique<M>("Hello world."));
  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