TimeTest.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "TimeA2.h"
  2. #include <iostream>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "Exception.h"
  5. #include "util.h"
  6. namespace aria2 {
  7. class TimeTest:public CppUnit::TestFixture {
  8. CPPUNIT_TEST_SUITE(TimeTest);
  9. CPPUNIT_TEST(testParseRFC1123);
  10. CPPUNIT_TEST(testParseRFC850);
  11. CPPUNIT_TEST(testParseRFC850Ext);
  12. CPPUNIT_TEST(testParseAsctime);
  13. CPPUNIT_TEST(testParseHTTPDate);
  14. CPPUNIT_TEST(testOperatorLess);
  15. CPPUNIT_TEST(testElapsed);
  16. CPPUNIT_TEST(testToHTTPDate);
  17. CPPUNIT_TEST_SUITE_END();
  18. public:
  19. void setUp() {}
  20. void tearDown() {}
  21. void testParseRFC1123();
  22. void testParseRFC1123Alt();
  23. void testParseRFC850();
  24. void testParseRFC850Ext();
  25. void testParseAsctime();
  26. void testParseHTTPDate();
  27. void testOperatorLess();
  28. void testElapsed();
  29. void testToHTTPDate();
  30. };
  31. CPPUNIT_TEST_SUITE_REGISTRATION(TimeTest);
  32. void TimeTest::testParseRFC1123()
  33. {
  34. Time t1 = Time::parseRFC1123("Sat, 06 Sep 2008 15:26:33 GMT");
  35. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
  36. }
  37. void TimeTest::testParseRFC1123Alt()
  38. {
  39. Time t1 = Time::parseRFC1123Alt("Sat, 06 Sep 2008 15:26:33 +0000");
  40. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
  41. }
  42. void TimeTest::testParseRFC850()
  43. {
  44. Time t1 = Time::parseRFC850("Saturday, 06-Sep-08 15:26:33 GMT");
  45. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
  46. }
  47. void TimeTest::testParseRFC850Ext()
  48. {
  49. Time t1 = Time::parseRFC850Ext("Saturday, 06-Sep-2008 15:26:33 GMT");
  50. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
  51. }
  52. void TimeTest::testParseAsctime()
  53. {
  54. Time t1 = Time::parseAsctime("Sun Sep 6 15:26:33 2008");
  55. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
  56. }
  57. void TimeTest::testParseHTTPDate()
  58. {
  59. CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
  60. Time::parseHTTPDate
  61. ("Sat, 06 Sep 2008 15:26:33 GMT").getTime());
  62. CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
  63. Time::parseHTTPDate
  64. ("Sat, 06-Sep-2008 15:26:33 GMT").getTime());
  65. CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
  66. Time::parseHTTPDate
  67. ("Sat, 06-Sep-08 15:26:33 GMT").getTime());
  68. CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
  69. Time::parseHTTPDate
  70. ("Sun Sep 6 15:26:33 2008").getTime());
  71. CPPUNIT_ASSERT(Time::parseHTTPDate
  72. ("Sat, 2008-09-06 15:26:33 GMT").bad());
  73. }
  74. void TimeTest::testOperatorLess()
  75. {
  76. CPPUNIT_ASSERT(Time(1) < Time(2));
  77. CPPUNIT_ASSERT(!(Time(1) < Time(1)));
  78. CPPUNIT_ASSERT(!(Time(2) < Time(1)));
  79. struct timeval tv1;
  80. tv1.tv_sec = 0;
  81. tv1.tv_usec = 1;
  82. struct timeval tv2;
  83. tv2.tv_sec = 1;
  84. tv2.tv_usec = 0;
  85. CPPUNIT_ASSERT(Time(tv1) < Time(tv2));
  86. tv2.tv_sec = 0;
  87. CPPUNIT_ASSERT(Time(tv2) < Time(tv1));
  88. }
  89. void TimeTest::testToHTTPDate()
  90. {
  91. // This test disabled for MinGW32, because the garbage will be
  92. // displayed and it hides real errors.
  93. #ifndef __MINGW32__
  94. Time t(1220714793);
  95. CPPUNIT_ASSERT_EQUAL(std::string("Sat, 06 Sep 2008 15:26:33 GMT"),
  96. t.toHTTPDate());
  97. #endif // !__MINGW32__
  98. }
  99. void TimeTest::testElapsed()
  100. {
  101. struct timeval now;
  102. gettimeofday(&now, nullptr);
  103. {
  104. struct timeval tv = now;
  105. CPPUNIT_ASSERT(!Time(tv).elapsed(1));
  106. }
  107. {
  108. struct timeval tv;
  109. suseconds_t usec = now.tv_usec+500000;
  110. if(usec > 999999) {
  111. tv.tv_sec = now.tv_sec+usec/1000000;
  112. tv.tv_usec = usec%1000000;
  113. } else {
  114. tv.tv_sec = now.tv_sec;
  115. tv.tv_usec = usec;
  116. }
  117. CPPUNIT_ASSERT(!Time(tv).elapsed(1));
  118. }
  119. {
  120. struct timeval tv = { now.tv_sec-2, now.tv_usec };
  121. CPPUNIT_ASSERT(Time(tv).elapsed(1));
  122. }
  123. }
  124. } // namespace aria2