-
Notifications
You must be signed in to change notification settings - Fork 6
/
TimeSpecTest.cpp
58 lines (46 loc) · 1.52 KB
/
TimeSpecTest.cpp
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
#include "TimeSpec.h"
#include <iostream>
#include <cassert>
int main() {
TimeSpec ts1;
TimeSpec ts2;
// test fractional seconds initialization
ts1 = TimeSpec(3);
assert(ts1.sec() == 3 && ts1.nsec() == 0);
ts1 = TimeSpec(3.14159265);
assert(ts1.sec() == 3 && ts1.nsec() == 141592650); // I guess this test may fail on some architectures ;/
// test timestruct initialization
ts2 = TimeSpec(ts1.ts);
assert(ts1 == ts2);
// test comparison operators ==, < and >
ts1 = ts2 = TimeSpec(1, 100000000);
assert(ts1 == ts2);
ts2 = TimeSpec(1, 100000001);
assert(ts1 < ts2);
ts2 = TimeSpec(2, 100000000);
assert(ts1 < ts2);
ts1 = TimeSpec(2, 100000001);
assert(ts1 > ts2);
ts1 = TimeSpec(3, 100000000);
assert(ts1 > ts2);
// test arithmetic operators - and + with carry
ts1 = TimeSpec(2, 400000000);
ts2 = TimeSpec(1, 700000000);
ts1 = ts1 - ts2;
assert(ts1.sec() == 0 && ts1.nsec() == 700000000);
ts1 = ts1 + ts2;
assert(ts1.sec() == 2 && ts1.nsec() == 400000000);
ts1 = ts1 + ts2;
assert(ts1.sec() == 4 && ts1.nsec() == 100000000);
// test arithmetic operators -= and += with carry
ts1 = TimeSpec(2, 400000000);
ts2 = TimeSpec(1, 700000000);
ts1 -= ts2;
assert(ts1.sec() == 0 && ts1.nsec() == 700000000);
ts1 += ts2;
assert(ts1.sec() == 2 && ts1.nsec() == 400000000);
ts1 += ts2;
assert(ts1.sec() == 4 && ts1.nsec() == 100000000);
std::cout << "all tests passed" << std::endl;
return 0;
}