TimeTest.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "TimeA2.h"
  2. #include "Exception.h"
  3. #include "Util.h"
  4. #include <iostream>
  5. #include <cppunit/extensions/HelperMacros.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_SUITE_END();
  15. public:
  16. void setUp() {}
  17. void tearDown() {}
  18. void testParseRFC1123();
  19. void testParseRFC850();
  20. void testParseRFC850Ext();
  21. void testParseHTTPDate();
  22. void testOperatorLess();
  23. };
  24. CPPUNIT_TEST_SUITE_REGISTRATION(TimeTest);
  25. void TimeTest::testParseRFC1123()
  26. {
  27. Time t1 = Time::parseRFC1123("Sat, 06 Sep 2008 15:26:33 GMT");
  28. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
  29. }
  30. void TimeTest::testParseRFC850()
  31. {
  32. Time t1 = Time::parseRFC850("Saturday, 06-Sep-08 15:26:33 GMT");
  33. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
  34. }
  35. void TimeTest::testParseRFC850Ext()
  36. {
  37. Time t1 = Time::parseRFC850Ext("Saturday, 06-Sep-2008 15:26:33 GMT");
  38. CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
  39. }
  40. void TimeTest::testParseHTTPDate()
  41. {
  42. CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
  43. Time::parseHTTPDate
  44. ("Sat, 06 Sep 2008 15:26:33 GMT").getTime());
  45. CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
  46. Time::parseHTTPDate
  47. ("Sat, 06-Sep-2008 15:26:33 GMT").getTime());
  48. CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
  49. Time::parseHTTPDate
  50. ("Sat, 06-Sep-08 15:26:33 GMT").getTime());
  51. CPPUNIT_ASSERT_EQUAL((time_t)-1,
  52. Time::parseHTTPDate
  53. ("Sat, 2008-09-06 15:26:33 GMT").getTime());
  54. }
  55. void TimeTest::testOperatorLess()
  56. {
  57. CPPUNIT_ASSERT(Time(1) < Time(2));
  58. CPPUNIT_ASSERT(!(Time(1) < Time(1)));
  59. CPPUNIT_ASSERT(!(Time(2) < Time(1)));
  60. struct timeval tv1;
  61. tv1.tv_sec = 0;
  62. tv1.tv_usec = 1;
  63. struct timeval tv2;
  64. tv2.tv_sec = 1;
  65. tv2.tv_usec = 0;
  66. CPPUNIT_ASSERT(Time(tv1) < Time(tv2));
  67. tv2.tv_sec = 0;
  68. CPPUNIT_ASSERT(Time(tv2) < Time(tv1));
  69. }
  70. } // namespace aria2