TimeTest.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 testParseRFC850();
  23. void testParseRFC850Ext();
  24. void testParseAsctime();
  25. void testParseHTTPDate();
  26. void testOperatorLess();
  27. void testElapsed();
  28. void testToHTTPDate();
  29. };
  30. CPPUNIT_TEST_SUITE_REGISTRATION(TimeTest);
  31. void TimeTest::testParseRFC1123()
  32. {
  33. Time t1 = Time::parseRFC1123("Sat, 06 Sep 2008 15:26:33 GMT");
  34. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
  35. }
  36. void TimeTest::testParseRFC850()
  37. {
  38. Time t1 = Time::parseRFC850("Saturday, 06-Sep-08 15:26:33 GMT");
  39. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
  40. }
  41. void TimeTest::testParseRFC850Ext()
  42. {
  43. Time t1 = Time::parseRFC850Ext("Saturday, 06-Sep-2008 15:26:33 GMT");
  44. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
  45. }
  46. void TimeTest::testParseAsctime()
  47. {
  48. Time t1 = Time::parseAsctime("Sun Sep 6 15:26:33 2008");
  49. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
  50. }
  51. void TimeTest::testParseHTTPDate()
  52. {
  53. CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
  54. Time::parseHTTPDate
  55. ("Sat, 06 Sep 2008 15:26:33 GMT").getTime());
  56. CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
  57. Time::parseHTTPDate
  58. ("Sat, 06-Sep-2008 15:26:33 GMT").getTime());
  59. CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
  60. Time::parseHTTPDate
  61. ("Sat, 06-Sep-08 15:26:33 GMT").getTime());
  62. CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
  63. Time::parseHTTPDate
  64. ("Sun Sep 6 15:26:33 2008").getTime());
  65. CPPUNIT_ASSERT(Time::parseHTTPDate
  66. ("Sat, 2008-09-06 15:26:33 GMT").bad());
  67. }
  68. void TimeTest::testOperatorLess()
  69. {
  70. CPPUNIT_ASSERT(Time(1) < Time(2));
  71. CPPUNIT_ASSERT(!(Time(1) < Time(1)));
  72. CPPUNIT_ASSERT(!(Time(2) < Time(1)));
  73. struct timeval tv1;
  74. tv1.tv_sec = 0;
  75. tv1.tv_usec = 1;
  76. struct timeval tv2;
  77. tv2.tv_sec = 1;
  78. tv2.tv_usec = 0;
  79. CPPUNIT_ASSERT(Time(tv1) < Time(tv2));
  80. tv2.tv_sec = 0;
  81. CPPUNIT_ASSERT(Time(tv2) < Time(tv1));
  82. }
  83. void TimeTest::testToHTTPDate()
  84. {
  85. Time t(1220714793);
  86. CPPUNIT_ASSERT_EQUAL(std::string("Sat, 06 Sep 2008 15:26:33 GMT"),
  87. t.toHTTPDate());
  88. }
  89. void TimeTest::testElapsed()
  90. {
  91. struct timeval now;
  92. gettimeofday(&now, 0);
  93. {
  94. struct timeval tv = now;
  95. CPPUNIT_ASSERT(!Time(tv).elapsed(1));
  96. }
  97. {
  98. struct timeval tv;
  99. suseconds_t usec = now.tv_usec+500000;
  100. if(usec > 999999) {
  101. tv.tv_sec = now.tv_sec+usec/1000000;
  102. tv.tv_usec = usec%1000000;
  103. } else {
  104. tv.tv_sec = now.tv_sec;
  105. tv.tv_usec = usec;
  106. }
  107. CPPUNIT_ASSERT(!Time(tv).elapsed(1));
  108. }
  109. {
  110. struct timeval tv = { now.tv_sec-1, now.tv_usec };
  111. CPPUNIT_ASSERT(Time(tv).elapsed(1));
  112. }
  113. }
  114. } // namespace aria2