TimeTest.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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(testParseHTTPDate);
  13. CPPUNIT_TEST(testOperatorLess);
  14. CPPUNIT_TEST(testElapsed);
  15. CPPUNIT_TEST_SUITE_END();
  16. public:
  17. void setUp() {}
  18. void tearDown() {}
  19. void testParseRFC1123();
  20. void testParseRFC850();
  21. void testParseRFC850Ext();
  22. void testParseHTTPDate();
  23. void testOperatorLess();
  24. void testElapsed();
  25. };
  26. CPPUNIT_TEST_SUITE_REGISTRATION(TimeTest);
  27. void TimeTest::testParseRFC1123()
  28. {
  29. Time t1 = Time::parseRFC1123("Sat, 06 Sep 2008 15:26:33 GMT");
  30. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
  31. }
  32. void TimeTest::testParseRFC850()
  33. {
  34. Time t1 = Time::parseRFC850("Saturday, 06-Sep-08 15:26:33 GMT");
  35. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
  36. }
  37. void TimeTest::testParseRFC850Ext()
  38. {
  39. Time t1 = Time::parseRFC850Ext("Saturday, 06-Sep-2008 15:26:33 GMT");
  40. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
  41. }
  42. void TimeTest::testParseHTTPDate()
  43. {
  44. CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
  45. Time::parseHTTPDate
  46. ("Sat, 06 Sep 2008 15:26:33 GMT").getTime());
  47. CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
  48. Time::parseHTTPDate
  49. ("Sat, 06-Sep-2008 15:26:33 GMT").getTime());
  50. CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
  51. Time::parseHTTPDate
  52. ("Sat, 06-Sep-08 15:26:33 GMT").getTime());
  53. CPPUNIT_ASSERT_EQUAL((time_t)-1,
  54. Time::parseHTTPDate
  55. ("Sat, 2008-09-06 15:26:33 GMT").getTime());
  56. }
  57. void TimeTest::testOperatorLess()
  58. {
  59. CPPUNIT_ASSERT(Time(1) < Time(2));
  60. CPPUNIT_ASSERT(!(Time(1) < Time(1)));
  61. CPPUNIT_ASSERT(!(Time(2) < Time(1)));
  62. struct timeval tv1;
  63. tv1.tv_sec = 0;
  64. tv1.tv_usec = 1;
  65. struct timeval tv2;
  66. tv2.tv_sec = 1;
  67. tv2.tv_usec = 0;
  68. CPPUNIT_ASSERT(Time(tv1) < Time(tv2));
  69. tv2.tv_sec = 0;
  70. CPPUNIT_ASSERT(Time(tv2) < Time(tv1));
  71. }
  72. void TimeTest::testElapsed()
  73. {
  74. struct timeval now;
  75. gettimeofday(&now, 0);
  76. {
  77. struct timeval tv = now;
  78. CPPUNIT_ASSERT(!Time(tv).elapsed(1));
  79. }
  80. {
  81. struct timeval tv;
  82. suseconds_t usec = now.tv_usec+500000;
  83. if(usec > 999999) {
  84. tv.tv_sec = now.tv_sec+usec/1000000;
  85. tv.tv_usec = usec%1000000;
  86. } else {
  87. tv.tv_sec = now.tv_sec;
  88. tv.tv_usec = usec;
  89. }
  90. CPPUNIT_ASSERT(!Time(tv).elapsed(1));
  91. }
  92. {
  93. struct timeval tv = { now.tv_sec-1, now.tv_usec };
  94. CPPUNIT_ASSERT(Time(tv).elapsed(1));
  95. }
  96. }
  97. } // namespace aria2