a2functionalTest.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "a2functional.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include <string>
  4. #include <numeric>
  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(testStrjoin);
  11. CPPUNIT_TEST(testStrconcat);
  12. CPPUNIT_TEST(testStrappend);
  13. CPPUNIT_TEST_SUITE_END();
  14. public:
  15. void testMemFunSh();
  16. void testAdopt2nd();
  17. void testStrjoin();
  18. void testStrconcat();
  19. void testStrappend();
  20. class Greeting {
  21. public:
  22. virtual ~Greeting() {}
  23. virtual std::string sayGreeting() = 0;
  24. virtual std::string sayGreetingConst() const = 0;
  25. };
  26. typedef SharedHandle<Greeting> GreetingHandle;
  27. class JapaneseGreeting:public Greeting
  28. {
  29. virtual std::string sayGreeting()
  30. {
  31. return "HAROO WAARUDO";
  32. }
  33. virtual std::string sayGreetingConst() const
  34. {
  35. return "HAROO WAARUDO";
  36. }
  37. };
  38. };
  39. CPPUNIT_TEST_SUITE_REGISTRATION(a2functionalTest);
  40. void a2functionalTest::testMemFunSh()
  41. {
  42. GreetingHandle greeting(new JapaneseGreeting());
  43. CPPUNIT_ASSERT_EQUAL(std::string("HAROO WAARUDO"), mem_fun_sh(&Greeting::sayGreeting)(greeting));
  44. CPPUNIT_ASSERT_EQUAL(std::string("HAROO WAARUDO"), mem_fun_sh(&Greeting::sayGreetingConst)(greeting));
  45. }
  46. void a2functionalTest::testAdopt2nd()
  47. {
  48. GreetingHandle greeting(new JapaneseGreeting());
  49. CPPUNIT_ASSERT_EQUAL(std::string("A Japanese said:HAROO WAARUDO"),
  50. adopt2nd(std::plus<std::string>(), mem_fun_sh(&Greeting::sayGreeting))("A Japanese said:", greeting));
  51. }
  52. void a2functionalTest::testStrjoin()
  53. {
  54. std::vector<std::string> v;
  55. CPPUNIT_ASSERT_EQUAL(std::string(""), strjoin(v.begin(), v.end(), " "));
  56. v.push_back("A");
  57. CPPUNIT_ASSERT_EQUAL(std::string("A"), strjoin(v.begin(), v.end(), " "));
  58. v.push_back("hero");
  59. v.push_back("is");
  60. v.push_back("lonely");
  61. CPPUNIT_ASSERT_EQUAL(std::string("A hero is lonely"),
  62. strjoin(v.begin(), v.end(), " "));
  63. }
  64. void a2functionalTest::testStrconcat()
  65. {
  66. CPPUNIT_ASSERT_EQUAL(std::string("X=3"), strconcat("X=", "3"));
  67. }
  68. void a2functionalTest::testStrappend()
  69. {
  70. std::string str = "X=";
  71. strappend(str, "3", ",Y=", "5");
  72. CPPUNIT_ASSERT_EQUAL(std::string("X=3,Y=5"), str);
  73. }
  74. } // namespace aria2