1//
2// TimestampTest.cpp
3//
4// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "TimestampTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Timestamp.h"
15#include "Poco/Thread.h"
16
17
18using Poco::Timestamp;
19using Poco::Thread;
20
21
22TimestampTest::TimestampTest(const std::string& rName): CppUnit::TestCase(rName)
23{
24}
25
26
27TimestampTest::~TimestampTest()
28{
29}
30
31
32void TimestampTest::testTimestamp()
33{
34 Timestamp t1;
35 Thread::sleep(200);
36 Timestamp t2;
37 Timestamp t3 = t2;
38 assertTrue (t1 != t2);
39 assertTrue (!(t1 == t2));
40 assertTrue (t2 > t1);
41 assertTrue (t2 >= t1);
42 assertTrue (!(t1 > t2));
43 assertTrue (!(t1 >= t2));
44 assertTrue (t2 == t3);
45 assertTrue (!(t2 != t3));
46 assertTrue (t2 >= t3);
47 assertTrue (t2 <= t3);
48 Timestamp::TimeDiff d = (t2 - t1);
49 assertTrue (d >= 180000 && d <= 300000);
50
51 t1.swap(t2);
52 assertTrue (t1 > t2);
53 t2.swap(t1);
54
55 Timestamp::UtcTimeVal tv = t1.utcTime();
56 Timestamp t4 = Timestamp::fromUtcTime(tv);
57 assertTrue (t1 == t4);
58
59 Timestamp epoch(0);
60 tv = epoch.utcTime();
61 assertTrue (tv >> 32 == 0x01B21DD2);
62 assertTrue ((tv & 0xFFFFFFFF) == 0x13814000);
63
64 Timestamp now;
65 Thread::sleep(201);
66 assertTrue (now.elapsed() >= 200000);
67 assertTrue (now.isElapsed(200000));
68 assertTrue (!now.isElapsed(2000000));
69
70#if defined(_WIN32)
71 {
72 Timestamp now;
73 Poco::UInt32 low;
74 Poco::UInt32 high;
75 now.toFileTimeNP(low, high);
76 Timestamp ts = Timestamp::fromFileTimeNP(low, high);
77 assertTrue (now == ts);
78 }
79#endif
80}
81
82
83void TimestampTest::setUp()
84{
85}
86
87
88void TimestampTest::tearDown()
89{
90}
91
92
93CppUnit::Test* TimestampTest::suite()
94{
95 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("TimestampTest");
96
97 CppUnit_addTest(pSuite, TimestampTest, testTimestamp);
98
99 return pSuite;
100}
101