a2functionalTest.cc 2.7 KB

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