TimeTest.cc 2.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#include "TimeA2.h"

#include <iostream>

#include <cppunit/extensions/HelperMacros.h>

#include "Exception.h"
#include "util.h"

namespace aria2 {

12
class TimeTest : public CppUnit::TestFixture {
13 14 15 16 17 18 19 20 21 22

  CPPUNIT_TEST_SUITE(TimeTest);
  CPPUNIT_TEST(testParseRFC1123);
  CPPUNIT_TEST(testParseRFC850);
  CPPUNIT_TEST(testParseRFC850Ext);
  CPPUNIT_TEST(testParseAsctime);
  CPPUNIT_TEST(testParseHTTPDate);
  CPPUNIT_TEST(testOperatorLess);
  CPPUNIT_TEST(testToHTTPDate);
  CPPUNIT_TEST_SUITE_END();
23

24 25 26 27 28 29
public:
  void setUp() {}

  void tearDown() {}

  void testParseRFC1123();
30
  void testParseRFC1123Alt();
31 32 33 34 35 36 37 38 39 40 41 42 43
  void testParseRFC850();
  void testParseRFC850Ext();
  void testParseAsctime();
  void testParseHTTPDate();
  void testOperatorLess();
  void testToHTTPDate();
};

CPPUNIT_TEST_SUITE_REGISTRATION(TimeTest);

void TimeTest::testParseRFC1123()
{
  Time t1 = Time::parseRFC1123("Sat, 06 Sep 2008 15:26:33 GMT");
44 45 46 47 48 49 50
  CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTimeFromEpoch());
}

void TimeTest::testParseRFC1123Alt()
{
  Time t1 = Time::parseRFC1123Alt("Sat, 06 Sep 2008 15:26:33 +0000");
  CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTimeFromEpoch());
51 52 53 54 55
}

void TimeTest::testParseRFC850()
{
  Time t1 = Time::parseRFC850("Saturday, 06-Sep-08 15:26:33 GMT");
56
  CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTimeFromEpoch());
57 58 59 60 61
}

void TimeTest::testParseRFC850Ext()
{
  Time t1 = Time::parseRFC850Ext("Saturday, 06-Sep-2008 15:26:33 GMT");
62
  CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTimeFromEpoch());
63 64 65 66 67
}

void TimeTest::testParseAsctime()
{
  Time t1 = Time::parseAsctime("Sun Sep  6 15:26:33 2008");
68
  CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTimeFromEpoch());
69 70 71 72
}

void TimeTest::testParseHTTPDate()
{
73 74 75 76 77 78 79 80 81 82 83 84 85
  CPPUNIT_ASSERT_EQUAL(
      (time_t)1220714793,
      Time::parseHTTPDate("Sat, 06 Sep 2008 15:26:33 GMT").getTimeFromEpoch());
  CPPUNIT_ASSERT_EQUAL(
      (time_t)1220714793,
      Time::parseHTTPDate("Sat, 06-Sep-2008 15:26:33 GMT").getTimeFromEpoch());
  CPPUNIT_ASSERT_EQUAL(
      (time_t)1220714793,
      Time::parseHTTPDate("Sat, 06-Sep-08 15:26:33 GMT").getTimeFromEpoch());
  CPPUNIT_ASSERT_EQUAL(
      (time_t)1220714793,
      Time::parseHTTPDate("Sun Sep  6 15:26:33 2008").getTimeFromEpoch());
  CPPUNIT_ASSERT(Time::parseHTTPDate("Sat, 2008-09-06 15:26:33 GMT").bad());
86 87 88 89 90 91 92 93 94 95 96
}

void TimeTest::testOperatorLess()
{
  CPPUNIT_ASSERT(Time(1) < Time(2));
  CPPUNIT_ASSERT(!(Time(1) < Time(1)));
  CPPUNIT_ASSERT(!(Time(2) < Time(1)));
}

void TimeTest::testToHTTPDate()
{
97 98
// This test disabled for MinGW32, because the garbage will be
// displayed and it hides real errors.
99
#ifndef __MINGW32__
100 101 102
  Time t(1220714793);
  CPPUNIT_ASSERT_EQUAL(std::string("Sat, 06 Sep 2008 15:26:33 GMT"),
                       t.toHTTPDate());
103
#endif // !__MINGW32__
104 105 106
}

} // namespace aria2