a2functionalTest.cc 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "a2functional.h"
  2. #include <string>
  3. #include <numeric>
  4. #include <cppunit/extensions/HelperMacros.h>
  5. namespace aria2 {
  6. class a2functionalTest:public CppUnit::TestFixture {
  7. CPPUNIT_TEST_SUITE(a2functionalTest);
  8. CPPUNIT_TEST(testMemFunSh);
  9. CPPUNIT_TEST(testAdopt2nd);
  10. CPPUNIT_TEST_SUITE_END();
  11. public:
  12. void testMemFunSh();
  13. void testAdopt2nd();
  14. class Greeting {
  15. public:
  16. virtual ~Greeting() {}
  17. virtual std::string sayGreeting() = 0;
  18. virtual std::string sayGreetingConst() const = 0;
  19. };
  20. typedef SharedHandle<Greeting> GreetingHandle;
  21. class JapaneseGreeting:public Greeting
  22. {
  23. virtual std::string sayGreeting()
  24. {
  25. return "HAROO WAARUDO";
  26. }
  27. virtual std::string sayGreetingConst() const
  28. {
  29. return "HAROO WAARUDO";
  30. }
  31. };
  32. };
  33. CPPUNIT_TEST_SUITE_REGISTRATION(a2functionalTest);
  34. void a2functionalTest::testMemFunSh()
  35. {
  36. GreetingHandle greeting(new JapaneseGreeting());
  37. CPPUNIT_ASSERT_EQUAL(std::string("HAROO WAARUDO"), mem_fun_sh(&Greeting::sayGreeting)(greeting));
  38. CPPUNIT_ASSERT_EQUAL(std::string("HAROO WAARUDO"), mem_fun_sh(&Greeting::sayGreetingConst)(greeting));
  39. }
  40. void a2functionalTest::testAdopt2nd()
  41. {
  42. GreetingHandle greeting(new JapaneseGreeting());
  43. CPPUNIT_ASSERT_EQUAL(std::string("A Japanese said:HAROO WAARUDO"),
  44. adopt2nd(std::plus<std::string>(), mem_fun_sh(&Greeting::sayGreeting))("A Japanese said:", greeting));
  45. }
  46. } // namespace aria2