humotion / test / timestamp.cpp @ e19ad051
History | View | Annotate | Download (3.675 KB)
1 | e19ad051 | Simon Schulz | // Bring in my package's API, which is what I'm testing
|
---|---|---|---|
2 | #include "timestamp.h" |
||
3 | #include <gtest/gtest.h> |
||
4 | #include <string> |
||
5 | #include <cstdio> |
||
6 | #include <stdio.h> |
||
7 | #include <unistd.h> |
||
8 | |||
9 | using namespace std; |
||
10 | using namespace humotion; |
||
11 | |||
12 | namespace {
|
||
13 | |||
14 | //the fixture for testing class Foo.
|
||
15 | class timestamp_Test : public ::testing::Test { |
||
16 | protected:
|
||
17 | timestamp_Test() { |
||
18 | //set-up work for EACH test here
|
||
19 | } |
||
20 | |||
21 | virtual ~timestamp_Test() {
|
||
22 | //clean-up work that doesn't throw exceptions here
|
||
23 | } |
||
24 | |||
25 | //if the constructor and destructor are not enough for setting up
|
||
26 | //and cleaning up each test, you can define the following methods:
|
||
27 | virtual void SetUp() { |
||
28 | //code here will be called immediately after the constructor (right before each test).
|
||
29 | } |
||
30 | |||
31 | virtual void TearDown() { |
||
32 | //code here will be called immediately after each test (right
|
||
33 | //before the destructor).
|
||
34 | } |
||
35 | |||
36 | //ojects declared here can be used by all tests in the test case for Foo
|
||
37 | }; |
||
38 | |||
39 | // Tests that the Foo::Bar() method does Abc.
|
||
40 | TEST_F(timestamp_Test, init_from_values) { |
||
41 | |||
42 | //check sec/nsec initialiser
|
||
43 | Timestamp a(1.0, 0.0); |
||
44 | ASSERT_EQ(a.to_seconds(), 1.0); |
||
45 | |||
46 | Timestamp b(999, 123); |
||
47 | ASSERT_EQ(b.to_seconds(), 999.0 + 123 / 1000000000.0); |
||
48 | |||
49 | Timestamp c(666.777); |
||
50 | ASSERT_EQ(c.to_seconds(), 666.777); |
||
51 | ASSERT_EQ(c.sec, 666);
|
||
52 | ASSERT_EQ(c.nsec, 0.777 / 1E-9);; |
||
53 | |||
54 | //test initializer with now()
|
||
55 | struct timespec tp;
|
||
56 | clock_gettime(CLOCK_REALTIME, &tp); |
||
57 | |||
58 | Timestamp ts_d; |
||
59 | sleep(1);
|
||
60 | Timestamp ts_e; |
||
61 | |||
62 | //they should be at least 1 second apart:
|
||
63 | double diff = ts_e.to_seconds() - ts_d.to_seconds();
|
||
64 | ASSERT_GE(diff, 1.0); |
||
65 | ASSERT_LE(diff, 2.0); |
||
66 | |||
67 | //d should also be greater equal that timespec values:
|
||
68 | ASSERT_GE(ts_d.sec, tp.tv_sec); |
||
69 | |||
70 | SUCCEED(); |
||
71 | } |
||
72 | |||
73 | TEST_F(timestamp_Test, comparison) { |
||
74 | |||
75 | //check sec/nsec initialiser
|
||
76 | Timestamp a(1234567, 89101213); |
||
77 | Timestamp b(1234567, 89101213); |
||
78 | ASSERT_TRUE(a == b); |
||
79 | ASSERT_TRUE(a >= b); |
||
80 | ASSERT_TRUE(a <= b); |
||
81 | ASSERT_FALSE(a != b); |
||
82 | ASSERT_FALSE(a < b); |
||
83 | ASSERT_FALSE(a > b); |
||
84 | |||
85 | //check double initializer
|
||
86 | Timestamp e(1.234567); |
||
87 | Timestamp f(1.234568); |
||
88 | ASSERT_FALSE(e == f); |
||
89 | ASSERT_FALSE(e >= f); |
||
90 | ASSERT_TRUE(e <= f); |
||
91 | ASSERT_TRUE(e != f); |
||
92 | ASSERT_TRUE(e < f); |
||
93 | ASSERT_FALSE(e > f); |
||
94 | |||
95 | //check now() initializer
|
||
96 | Timestamp c; |
||
97 | sleep(1);
|
||
98 | Timestamp d; |
||
99 | ASSERT_FALSE(c == d); |
||
100 | ASSERT_FALSE(c >= d); |
||
101 | ASSERT_TRUE(c <= d); |
||
102 | ASSERT_TRUE(c != d); |
||
103 | ASSERT_TRUE(c < d); |
||
104 | ASSERT_FALSE(c > d); |
||
105 | |||
106 | //ok now test comparisons based on sec:
|
||
107 | Timestamp g(100, 1234567); |
||
108 | Timestamp h(101, 1234567); |
||
109 | ASSERT_FALSE(g == h); |
||
110 | ASSERT_FALSE(g >= h); |
||
111 | ASSERT_TRUE(g <= h); |
||
112 | ASSERT_TRUE(g != h); |
||
113 | ASSERT_TRUE(g < h); |
||
114 | ASSERT_FALSE(g > h); |
||
115 | |||
116 | //ok now test comparisons based on nsec:
|
||
117 | { |
||
118 | Timestamp i(100, 1234567); |
||
119 | Timestamp j(100, 1234568); |
||
120 | ASSERT_FALSE(i == j); |
||
121 | ASSERT_FALSE(i >= j); |
||
122 | ASSERT_TRUE(i <= j); |
||
123 | ASSERT_TRUE(i != j); |
||
124 | ASSERT_TRUE(i < j); |
||
125 | ASSERT_FALSE(i > j); |
||
126 | } |
||
127 | |||
128 | //ok now test comparisons based on sec + nsec:
|
||
129 | { |
||
130 | Timestamp i(100, 1234568); |
||
131 | Timestamp j(101, 1234567); |
||
132 | ASSERT_FALSE(i == j); |
||
133 | ASSERT_FALSE(i >= j); |
||
134 | ASSERT_TRUE(i <= j); |
||
135 | ASSERT_TRUE(i != j); |
||
136 | ASSERT_TRUE(i < j); |
||
137 | ASSERT_FALSE(i > j); |
||
138 | } |
||
139 | |||
140 | SUCCEED(); |
||
141 | } |
||
142 | |||
143 | // Tests that Foo does Xyz.
|
||
144 | // TEST_F(xsc3_client_cpp_Test, DoesXyz) {
|
||
145 | // CheckConnection(xsc3_client);
|
||
146 | // }
|
||
147 | |||
148 | } // namespace
|
||
149 | |||
150 | // Run all the tests that were declared with TEST()
|
||
151 | int main(int argc, char **argv){ |
||
152 | testing::InitGoogleTest(&argc, argv); |
||
153 | return RUN_ALL_TESTS();
|
||
154 | } |