a2functionalTest.cc 2.5 KB

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