source: filezilla/trunk/fuentes/tests/time.cpp @ 130

Last change on this file since 130 was 130, checked in by jrpelegrina, 3 years ago

First release to xenial

File size: 1.9 KB
Line 
1#include <libfilezilla.h>
2
3#include <timeex.h>
4
5#include <cppunit/extensions/HelperMacros.h>
6
7#include <unistd.h>
8
9class TimeTest final : public CppUnit::TestFixture
10{
11        CPPUNIT_TEST_SUITE(TimeTest);
12        CPPUNIT_TEST(testNow);
13        CPPUNIT_TEST(testPreEpoch);
14        CPPUNIT_TEST_SUITE_END();
15
16public:
17        void setUp() {}
18        void tearDown() {}
19
20        void testNow();
21        void testPreEpoch();
22};
23
24CPPUNIT_TEST_SUITE_REGISTRATION(TimeTest);
25
26void TimeTest::testNow()
27{
28        CDateTime const t1 = CDateTime::Now();
29
30#ifdef __WXMSW__
31        Sleep(2000);
32#else
33        timespec ts{};
34        ts.tv_sec = 2;
35        nanosleep(&ts, 0);
36#endif
37
38        CDateTime const t2 = CDateTime::Now();
39
40        CPPUNIT_ASSERT(t1.IsValid());
41        CPPUNIT_ASSERT(t2.IsValid());
42        CPPUNIT_ASSERT(t2 > t1);
43
44        auto const diff = t2 - t1;
45
46        CPPUNIT_ASSERT(diff.get_seconds() >= 2);
47        CPPUNIT_ASSERT(diff.get_seconds() < 4); // May fail if running on a computer for ants
48
49        CPPUNIT_ASSERT(t1.GetTimeT() > 1431333788); // The time this test was written
50}
51
52void TimeTest::testPreEpoch()
53{
54        CDateTime const now = CDateTime::Now();
55
56        CDateTime const t1(CDateTime::utc, 1957, 10, 4, 19, 28, 34);
57
58        CPPUNIT_ASSERT(t1.IsValid());
59        CPPUNIT_ASSERT(t1 < now);
60
61        CPPUNIT_ASSERT(t1.GetTimeT() < -1);
62
63        auto const tm1 = t1.GetTm(CDateTime::utc);
64        CPPUNIT_ASSERT_EQUAL(57, tm1.tm_year);
65        CPPUNIT_ASSERT_EQUAL(9,  tm1.tm_mon);
66        CPPUNIT_ASSERT_EQUAL(4,  tm1.tm_mday);
67        CPPUNIT_ASSERT_EQUAL(19, tm1.tm_hour);
68        CPPUNIT_ASSERT_EQUAL(28, tm1.tm_min);
69        CPPUNIT_ASSERT_EQUAL(34, tm1.tm_sec);
70
71
72        CDateTime const t2(CDateTime::utc, 1969, 12, 31, 23, 59, 59);
73
74        CPPUNIT_ASSERT(t2.IsValid());
75        CPPUNIT_ASSERT(t2 > t1);
76        CPPUNIT_ASSERT(t2 < now);
77
78        auto const tm2 = t2.GetTm(CDateTime::utc);
79        CPPUNIT_ASSERT_EQUAL(69, tm2.tm_year);
80        CPPUNIT_ASSERT_EQUAL(11, tm2.tm_mon);
81        CPPUNIT_ASSERT_EQUAL(31, tm2.tm_mday);
82        CPPUNIT_ASSERT_EQUAL(23, tm2.tm_hour);
83        CPPUNIT_ASSERT_EQUAL(59, tm2.tm_min);
84        CPPUNIT_ASSERT_EQUAL(59, tm2.tm_sec);
85}
Note: See TracBrowser for help on using the repository browser.