TimegmTest.cc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include "timegm.h"
  2. #include <cstring>
  3. #include <iostream>
  4. #include <cppunit/extensions/HelperMacros.h>
  5. namespace aria2 {
  6. class TimegmTest : public CppUnit::TestFixture {
  7. CPPUNIT_TEST_SUITE(TimegmTest);
  8. CPPUNIT_TEST(testTimegm);
  9. CPPUNIT_TEST_SUITE_END();
  10. public:
  11. void setUp() {}
  12. void tearDown() {}
  13. void testTimegm();
  14. };
  15. CPPUNIT_TEST_SUITE_REGISTRATION(TimegmTest);
  16. namespace {
  17. void setTime(struct tm* tm, int yr, int mon, int day, int h, int m, int s)
  18. {
  19. tm->tm_year = yr - 1900;
  20. tm->tm_mon = mon - 1;
  21. tm->tm_mday = day;
  22. tm->tm_hour = h;
  23. tm->tm_min = m;
  24. tm->tm_sec = s;
  25. }
  26. } // namespace
  27. void TimegmTest::testTimegm()
  28. {
  29. struct tm tm;
  30. memset(&tm, 0, sizeof(tm));
  31. setTime(&tm, 1970, 1, 1, 0, 0, 0);
  32. CPPUNIT_ASSERT_EQUAL((time_t)0, timegm(&tm));
  33. setTime(&tm, 2000, 1, 2, 1, 2, 3);
  34. CPPUNIT_ASSERT_EQUAL((time_t)946774923, timegm(&tm));
  35. setTime(&tm, 2000, 2, 2, 1, 2, 3);
  36. CPPUNIT_ASSERT_EQUAL((time_t)949453323, timegm(&tm));
  37. setTime(&tm, 2015, 10, 21, 10, 19, 30);
  38. CPPUNIT_ASSERT_EQUAL((time_t)1445422770, timegm(&tm));
  39. setTime(&tm, 1970, 13, 1, 0, 0, 0);
  40. CPPUNIT_ASSERT_EQUAL((time_t)-1, timegm(&tm));
  41. setTime(&tm, 2039, 1, 1, 0, 0, 0);
  42. if (sizeof(time_t) == 4) {
  43. CPPUNIT_ASSERT_EQUAL((time_t)-1, timegm(&tm));
  44. }
  45. else if (sizeof(time_t) == 8) {
  46. CPPUNIT_ASSERT_EQUAL((time_t)2177452800LL, timegm(&tm));
  47. }
  48. }
  49. } // namespace aria2