a2functionalTest.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "a2functional.h"
  2. #include <string>
  3. #include <numeric>
  4. #include <algorithm>
  5. #include <cppunit/extensions/HelperMacros.h>
  6. namespace aria2 {
  7. class a2functionalTest:public CppUnit::TestFixture {
  8. CPPUNIT_TEST_SUITE(a2functionalTest);
  9. CPPUNIT_TEST(testMemFunSh);
  10. CPPUNIT_TEST(testAdopt2nd);
  11. CPPUNIT_TEST(testStrjoin);
  12. CPPUNIT_TEST(testStrconcat);
  13. CPPUNIT_TEST(testStrappend);
  14. CPPUNIT_TEST(testLeastRecentAccess);
  15. CPPUNIT_TEST_SUITE_END();
  16. public:
  17. void testMemFunSh();
  18. void testAdopt2nd();
  19. void testStrjoin();
  20. void testStrconcat();
  21. void testStrappend();
  22. void testLeastRecentAccess();
  23. class Greeting {
  24. public:
  25. virtual ~Greeting() {}
  26. virtual std::string sayGreeting() = 0;
  27. virtual std::string sayGreetingConst() const = 0;
  28. };
  29. typedef SharedHandle<Greeting> GreetingHandle;
  30. class JapaneseGreeting:public Greeting
  31. {
  32. virtual std::string sayGreeting()
  33. {
  34. return "HAROO WAARUDO";
  35. }
  36. virtual std::string sayGreetingConst() const
  37. {
  38. return "HAROO WAARUDO";
  39. }
  40. };
  41. struct LastAccess {
  42. time_t lastAccess_;
  43. LastAccess(time_t lastAccess):lastAccess_(lastAccess) {}
  44. time_t getLastAccessTime() const
  45. {
  46. return lastAccess_;
  47. }
  48. };
  49. };
  50. CPPUNIT_TEST_SUITE_REGISTRATION(a2functionalTest);
  51. void a2functionalTest::testMemFunSh()
  52. {
  53. GreetingHandle greeting(new JapaneseGreeting());
  54. CPPUNIT_ASSERT_EQUAL(std::string("HAROO WAARUDO"), mem_fun_sh(&Greeting::sayGreeting)(greeting));
  55. CPPUNIT_ASSERT_EQUAL(std::string("HAROO WAARUDO"), mem_fun_sh(&Greeting::sayGreetingConst)(greeting));
  56. }
  57. void a2functionalTest::testAdopt2nd()
  58. {
  59. GreetingHandle greeting(new JapaneseGreeting());
  60. CPPUNIT_ASSERT_EQUAL(std::string("A Japanese said:HAROO WAARUDO"),
  61. adopt2nd(std::plus<std::string>(), mem_fun_sh(&Greeting::sayGreeting))("A Japanese said:", greeting));
  62. }
  63. void a2functionalTest::testStrjoin()
  64. {
  65. std::vector<std::string> v;
  66. CPPUNIT_ASSERT_EQUAL(std::string(""), strjoin(v.begin(), v.end(), " "));
  67. v.push_back("A");
  68. CPPUNIT_ASSERT_EQUAL(std::string("A"), strjoin(v.begin(), v.end(), " "));
  69. v.push_back("hero");
  70. v.push_back("is");
  71. v.push_back("lonely");
  72. CPPUNIT_ASSERT_EQUAL(std::string("A hero is lonely"),
  73. strjoin(v.begin(), v.end(), " "));
  74. }
  75. void a2functionalTest::testStrconcat()
  76. {
  77. CPPUNIT_ASSERT_EQUAL(std::string("X=3"), strconcat("X=", "3"));
  78. }
  79. void a2functionalTest::testStrappend()
  80. {
  81. std::string str = "X=";
  82. strappend(str, "3", ",Y=", "5");
  83. CPPUNIT_ASSERT_EQUAL(std::string("X=3,Y=5"), str);
  84. }
  85. void a2functionalTest::testLeastRecentAccess()
  86. {
  87. std::vector<LastAccess> v;
  88. for(int i = 99; i >= 0; --i) {
  89. v.push_back(LastAccess(i));
  90. }
  91. std::sort(v.begin(), v.end(), LeastRecentAccess<LastAccess>());
  92. for(int i = 0; i < 100; ++i) {
  93. CPPUNIT_ASSERT_EQUAL((time_t)i, v[i].lastAccess_);
  94. }
  95. }
  96. } // namespace aria2