a2functionalTest.cc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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(testArrayLength);
  11. CPPUNIT_TEST_SUITE_END();
  12. public:
  13. void testMemFunSh();
  14. void testAdopt2nd();
  15. void testArrayLength();
  16. class Greeting {
  17. public:
  18. virtual ~Greeting() {}
  19. virtual string sayGreeting() = 0;
  20. virtual string sayGreetingConst() const = 0;
  21. };
  22. typedef SharedHandle<Greeting> GreetingHandle;
  23. class JapaneseGreeting:public Greeting
  24. {
  25. virtual string sayGreeting()
  26. {
  27. return "HAROO WAARUDO";
  28. }
  29. virtual string sayGreetingConst() const
  30. {
  31. return "HAROO WAARUDO";
  32. }
  33. };
  34. };
  35. CPPUNIT_TEST_SUITE_REGISTRATION(a2functionalTest);
  36. void a2functionalTest::testMemFunSh()
  37. {
  38. GreetingHandle greeting = new JapaneseGreeting();
  39. CPPUNIT_ASSERT_EQUAL(string("HAROO WAARUDO"), mem_fun_sh(&Greeting::sayGreeting)(greeting));
  40. CPPUNIT_ASSERT_EQUAL(string("HAROO WAARUDO"), mem_fun_sh(&Greeting::sayGreetingConst)(greeting));
  41. }
  42. void a2functionalTest::testAdopt2nd()
  43. {
  44. GreetingHandle greeting = new JapaneseGreeting();
  45. CPPUNIT_ASSERT_EQUAL(string("A Japanese said:HAROO WAARUDO"),
  46. adopt2nd(plus<string>(), mem_fun_sh(&Greeting::sayGreeting))("A Japanese said:", greeting));
  47. }
  48. void a2functionalTest::testArrayLength()
  49. {
  50. int64_t ia[] = { 1, 2, 3, 4, 5 };
  51. int64_t zeroLengthArray[] = {};
  52. CPPUNIT_ASSERT_EQUAL((size_t)5, arrayLength(ia));
  53. CPPUNIT_ASSERT_EQUAL((size_t)0, arrayLength(zeroLengthArray));
  54. }