123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- #include "TimeA2.h"
- #include <iostream>
- #include <cppunit/extensions/HelperMacros.h>
- #include "Exception.h"
- #include "util.h"
- namespace aria2 {
- class TimeTest:public CppUnit::TestFixture {
- CPPUNIT_TEST_SUITE(TimeTest);
- CPPUNIT_TEST(testParseRFC1123);
- CPPUNIT_TEST(testParseRFC850);
- CPPUNIT_TEST(testParseRFC850Ext);
- CPPUNIT_TEST(testParseAsctime);
- CPPUNIT_TEST(testParseHTTPDate);
- CPPUNIT_TEST(testOperatorLess);
- CPPUNIT_TEST(testElapsed);
- CPPUNIT_TEST(testToHTTPDate);
- CPPUNIT_TEST_SUITE_END();
- public:
- void setUp() {}
- void tearDown() {}
- void testParseRFC1123();
- void testParseRFC1123Alt();
- void testParseRFC850();
- void testParseRFC850Ext();
- void testParseAsctime();
- void testParseHTTPDate();
- void testOperatorLess();
- void testElapsed();
- void testToHTTPDate();
- };
- CPPUNIT_TEST_SUITE_REGISTRATION(TimeTest);
- void TimeTest::testParseRFC1123()
- {
- Time t1 = Time::parseRFC1123("Sat, 06 Sep 2008 15:26:33 GMT");
- CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
- }
- void TimeTest::testParseRFC1123Alt()
- {
- Time t1 = Time::parseRFC1123Alt("Sat, 06 Sep 2008 15:26:33 +0000");
- CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
- }
- void TimeTest::testParseRFC850()
- {
- Time t1 = Time::parseRFC850("Saturday, 06-Sep-08 15:26:33 GMT");
- CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
- }
- void TimeTest::testParseRFC850Ext()
- {
- Time t1 = Time::parseRFC850Ext("Saturday, 06-Sep-2008 15:26:33 GMT");
- CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
- }
- void TimeTest::testParseAsctime()
- {
- Time t1 = Time::parseAsctime("Sun Sep 6 15:26:33 2008");
- CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
- }
- void TimeTest::testParseHTTPDate()
- {
- CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
- Time::parseHTTPDate
- ("Sat, 06 Sep 2008 15:26:33 GMT").getTime());
- CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
- Time::parseHTTPDate
- ("Sat, 06-Sep-2008 15:26:33 GMT").getTime());
- CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
- Time::parseHTTPDate
- ("Sat, 06-Sep-08 15:26:33 GMT").getTime());
- CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
- Time::parseHTTPDate
- ("Sun Sep 6 15:26:33 2008").getTime());
- CPPUNIT_ASSERT(Time::parseHTTPDate
- ("Sat, 2008-09-06 15:26:33 GMT").bad());
- }
- void TimeTest::testOperatorLess()
- {
- CPPUNIT_ASSERT(Time(1) < Time(2));
- CPPUNIT_ASSERT(!(Time(1) < Time(1)));
- CPPUNIT_ASSERT(!(Time(2) < Time(1)));
- struct timeval tv1;
- tv1.tv_sec = 0;
- tv1.tv_usec = 1;
- struct timeval tv2;
- tv2.tv_sec = 1;
- tv2.tv_usec = 0;
- CPPUNIT_ASSERT(Time(tv1) < Time(tv2));
- tv2.tv_sec = 0;
- CPPUNIT_ASSERT(Time(tv2) < Time(tv1));
- }
- void TimeTest::testToHTTPDate()
- {
- // This test disabled for MinGW32, because the garbage will be
- // displayed and it hides real errors.
- #ifndef __MINGW32__
- Time t(1220714793);
- CPPUNIT_ASSERT_EQUAL(std::string("Sat, 06 Sep 2008 15:26:33 GMT"),
- t.toHTTPDate());
- #endif // !__MINGW32__
- }
- void TimeTest::testElapsed()
- {
- struct timeval now;
- gettimeofday(&now, nullptr);
- {
- struct timeval tv = now;
- CPPUNIT_ASSERT(!Time(tv).elapsed(1));
- }
- {
- struct timeval tv;
- suseconds_t usec = now.tv_usec+500000;
- if(usec > 999999) {
- tv.tv_sec = now.tv_sec+usec/1000000;
- tv.tv_usec = usec%1000000;
- } else {
- tv.tv_sec = now.tv_sec;
- tv.tv_usec = usec;
- }
- CPPUNIT_ASSERT(!Time(tv).elapsed(1));
- }
- {
- struct timeval tv = { now.tv_sec-2, now.tv_usec };
- CPPUNIT_ASSERT(Time(tv).elapsed(1));
- }
- }
- } // namespace aria2
|