TimeTest.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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(testToHTTPDate);
  16. CPPUNIT_TEST_SUITE_END();
  17. public:
  18. void setUp() {}
  19. void tearDown() {}
  20. void testParseRFC1123();
  21. void testParseRFC1123Alt();
  22. void testParseRFC850();
  23. void testParseRFC850Ext();
  24. void testParseAsctime();
  25. void testParseHTTPDate();
  26. void testOperatorLess();
  27. void testToHTTPDate();
  28. };
  29. CPPUNIT_TEST_SUITE_REGISTRATION(TimeTest);
  30. void TimeTest::testParseRFC1123()
  31. {
  32. Time t1 = Time::parseRFC1123("Sat, 06 Sep 2008 15:26:33 GMT");
  33. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTimeFromEpoch());
  34. }
  35. void TimeTest::testParseRFC1123Alt()
  36. {
  37. Time t1 = Time::parseRFC1123Alt("Sat, 06 Sep 2008 15:26:33 +0000");
  38. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTimeFromEpoch());
  39. }
  40. void TimeTest::testParseRFC850()
  41. {
  42. Time t1 = Time::parseRFC850("Saturday, 06-Sep-08 15:26:33 GMT");
  43. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTimeFromEpoch());
  44. }
  45. void TimeTest::testParseRFC850Ext()
  46. {
  47. Time t1 = Time::parseRFC850Ext("Saturday, 06-Sep-2008 15:26:33 GMT");
  48. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTimeFromEpoch());
  49. }
  50. void TimeTest::testParseAsctime()
  51. {
  52. Time t1 = Time::parseAsctime("Sun Sep 6 15:26:33 2008");
  53. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTimeFromEpoch());
  54. }
  55. void TimeTest::testParseHTTPDate()
  56. {
  57. CPPUNIT_ASSERT_EQUAL(
  58. (time_t)1220714793,
  59. Time::parseHTTPDate("Sat, 06 Sep 2008 15:26:33 GMT").getTimeFromEpoch());
  60. CPPUNIT_ASSERT_EQUAL(
  61. (time_t)1220714793,
  62. Time::parseHTTPDate("Sat, 06-Sep-2008 15:26:33 GMT").getTimeFromEpoch());
  63. CPPUNIT_ASSERT_EQUAL(
  64. (time_t)1220714793,
  65. Time::parseHTTPDate("Sat, 06-Sep-08 15:26:33 GMT").getTimeFromEpoch());
  66. CPPUNIT_ASSERT_EQUAL(
  67. (time_t)1220714793,
  68. Time::parseHTTPDate("Sun Sep 6 15:26:33 2008").getTimeFromEpoch());
  69. CPPUNIT_ASSERT(Time::parseHTTPDate("Sat, 2008-09-06 15:26:33 GMT").bad());
  70. }
  71. void TimeTest::testOperatorLess()
  72. {
  73. CPPUNIT_ASSERT(Time(1) < Time(2));
  74. CPPUNIT_ASSERT(!(Time(1) < Time(1)));
  75. CPPUNIT_ASSERT(!(Time(2) < Time(1)));
  76. }
  77. void TimeTest::testToHTTPDate()
  78. {
  79. // This test disabled for MinGW32, because the garbage will be
  80. // displayed and it hides real errors.
  81. #ifndef __MINGW32__
  82. Time t(1220714793);
  83. CPPUNIT_ASSERT_EQUAL(std::string("Sat, 06 Sep 2008 15:26:33 GMT"),
  84. t.toHTTPDate());
  85. #endif // !__MINGW32__
  86. }
  87. } // namespace aria2