a2functionalTest.cc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "a2functional.h"
  2. #include <string>
  3. #include <numeric>
  4. #include <cppunit/extensions/HelperMacros.h>
  5. using namespace std;
  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 string sayGreeting() = 0;
  18. virtual string sayGreetingConst() const = 0;
  19. };
  20. typedef SharedHandle<Greeting> GreetingHandle;
  21. class JapaneseGreeting:public Greeting
  22. {
  23. virtual string sayGreeting()
  24. {
  25. return "HAROO WAARUDO";
  26. }
  27. virtual 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(string("HAROO WAARUDO"), mem_fun_sh(&Greeting::sayGreeting)(greeting));
  38. CPPUNIT_ASSERT_EQUAL(string("HAROO WAARUDO"), mem_fun_sh(&Greeting::sayGreetingConst)(greeting));
  39. }
  40. void a2functionalTest::testAdopt2nd()
  41. {
  42. GreetingHandle greeting = new JapaneseGreeting();
  43. CPPUNIT_ASSERT_EQUAL(string("A Japanese said:HAROO WAARUDO"),
  44. adopt2nd(plus<string>(), mem_fun_sh(&Greeting::sayGreeting))("A Japanese said:", greeting));
  45. }