File: TimegmTest.cc

package info (click to toggle)
aria2 1.37.0%2Bdebian-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,128 kB
  • sloc: cpp: 115,006; ansic: 9,140; makefile: 1,466; ruby: 475; python: 373; sh: 260; xml: 176
file content (61 lines) | stat: -rw-r--r-- 1,400 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "timegm.h"

#include <cstring>
#include <iostream>

#include <cppunit/extensions/HelperMacros.h>

namespace aria2 {

class TimegmTest : public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE(TimegmTest);
  CPPUNIT_TEST(testTimegm);
  CPPUNIT_TEST_SUITE_END();

public:
  void setUp() {}

  void tearDown() {}

  void testTimegm();
};

CPPUNIT_TEST_SUITE_REGISTRATION(TimegmTest);

namespace {
void setTime(struct tm* tm, int yr, int mon, int day, int h, int m, int s)
{
  tm->tm_year = yr - 1900;
  tm->tm_mon = mon - 1;
  tm->tm_mday = day;
  tm->tm_hour = h;
  tm->tm_min = m;
  tm->tm_sec = s;
}
} // namespace

void TimegmTest::testTimegm()
{
  struct tm tm;
  memset(&tm, 0, sizeof(tm));
  setTime(&tm, 1970, 1, 1, 0, 0, 0);
  CPPUNIT_ASSERT_EQUAL((time_t)0, timegm(&tm));
  setTime(&tm, 2000, 1, 2, 1, 2, 3);
  CPPUNIT_ASSERT_EQUAL((time_t)946774923, timegm(&tm));
  setTime(&tm, 2000, 2, 2, 1, 2, 3);
  CPPUNIT_ASSERT_EQUAL((time_t)949453323, timegm(&tm));
  setTime(&tm, 2015, 10, 21, 10, 19, 30);
  CPPUNIT_ASSERT_EQUAL((time_t)1445422770, timegm(&tm));
  setTime(&tm, 1970, 13, 1, 0, 0, 0);
  CPPUNIT_ASSERT_EQUAL((time_t)-1, timegm(&tm));
  setTime(&tm, 2039, 1, 1, 0, 0, 0);
  if (sizeof(time_t) == 4) {
    CPPUNIT_ASSERT_EQUAL((time_t)-1, timegm(&tm));
  }
  else if (sizeof(time_t) == 8) {
    CPPUNIT_ASSERT_EQUAL((time_t)2177452800LL, timegm(&tm));
  }
}

} // namespace aria2