1 #include "test/jemalloc_test.h"
2
3 #define BILLION UINT64_C(1000000000)
4
TEST_BEGIN(test_nstime_init)5 TEST_BEGIN(test_nstime_init) {
6 nstime_t nst;
7
8 nstime_init(&nst, 42000000043);
9 assert_u64_eq(nstime_ns(&nst), 42000000043, "ns incorrectly read");
10 assert_u64_eq(nstime_sec(&nst), 42, "sec incorrectly read");
11 assert_u64_eq(nstime_nsec(&nst), 43, "nsec incorrectly read");
12 }
13 TEST_END
14
TEST_BEGIN(test_nstime_init2)15 TEST_BEGIN(test_nstime_init2) {
16 nstime_t nst;
17
18 nstime_init2(&nst, 42, 43);
19 assert_u64_eq(nstime_sec(&nst), 42, "sec incorrectly read");
20 assert_u64_eq(nstime_nsec(&nst), 43, "nsec incorrectly read");
21 }
22 TEST_END
23
TEST_BEGIN(test_nstime_copy)24 TEST_BEGIN(test_nstime_copy) {
25 nstime_t nsta, nstb;
26
27 nstime_init2(&nsta, 42, 43);
28 nstime_init(&nstb, 0);
29 nstime_copy(&nstb, &nsta);
30 assert_u64_eq(nstime_sec(&nstb), 42, "sec incorrectly copied");
31 assert_u64_eq(nstime_nsec(&nstb), 43, "nsec incorrectly copied");
32 }
33 TEST_END
34
TEST_BEGIN(test_nstime_compare)35 TEST_BEGIN(test_nstime_compare) {
36 nstime_t nsta, nstb;
37
38 nstime_init2(&nsta, 42, 43);
39 nstime_copy(&nstb, &nsta);
40 assert_d_eq(nstime_compare(&nsta, &nstb), 0, "Times should be equal");
41 assert_d_eq(nstime_compare(&nstb, &nsta), 0, "Times should be equal");
42
43 nstime_init2(&nstb, 42, 42);
44 assert_d_eq(nstime_compare(&nsta, &nstb), 1,
45 "nsta should be greater than nstb");
46 assert_d_eq(nstime_compare(&nstb, &nsta), -1,
47 "nstb should be less than nsta");
48
49 nstime_init2(&nstb, 42, 44);
50 assert_d_eq(nstime_compare(&nsta, &nstb), -1,
51 "nsta should be less than nstb");
52 assert_d_eq(nstime_compare(&nstb, &nsta), 1,
53 "nstb should be greater than nsta");
54
55 nstime_init2(&nstb, 41, BILLION - 1);
56 assert_d_eq(nstime_compare(&nsta, &nstb), 1,
57 "nsta should be greater than nstb");
58 assert_d_eq(nstime_compare(&nstb, &nsta), -1,
59 "nstb should be less than nsta");
60
61 nstime_init2(&nstb, 43, 0);
62 assert_d_eq(nstime_compare(&nsta, &nstb), -1,
63 "nsta should be less than nstb");
64 assert_d_eq(nstime_compare(&nstb, &nsta), 1,
65 "nstb should be greater than nsta");
66 }
67 TEST_END
68
TEST_BEGIN(test_nstime_add)69 TEST_BEGIN(test_nstime_add) {
70 nstime_t nsta, nstb;
71
72 nstime_init2(&nsta, 42, 43);
73 nstime_copy(&nstb, &nsta);
74 nstime_add(&nsta, &nstb);
75 nstime_init2(&nstb, 84, 86);
76 assert_d_eq(nstime_compare(&nsta, &nstb), 0,
77 "Incorrect addition result");
78
79 nstime_init2(&nsta, 42, BILLION - 1);
80 nstime_copy(&nstb, &nsta);
81 nstime_add(&nsta, &nstb);
82 nstime_init2(&nstb, 85, BILLION - 2);
83 assert_d_eq(nstime_compare(&nsta, &nstb), 0,
84 "Incorrect addition result");
85 }
86 TEST_END
87
TEST_BEGIN(test_nstime_iadd)88 TEST_BEGIN(test_nstime_iadd) {
89 nstime_t nsta, nstb;
90
91 nstime_init2(&nsta, 42, BILLION - 1);
92 nstime_iadd(&nsta, 1);
93 nstime_init2(&nstb, 43, 0);
94 assert_d_eq(nstime_compare(&nsta, &nstb), 0,
95 "Incorrect addition result");
96
97 nstime_init2(&nsta, 42, 1);
98 nstime_iadd(&nsta, BILLION + 1);
99 nstime_init2(&nstb, 43, 2);
100 assert_d_eq(nstime_compare(&nsta, &nstb), 0,
101 "Incorrect addition result");
102 }
103 TEST_END
104
TEST_BEGIN(test_nstime_subtract)105 TEST_BEGIN(test_nstime_subtract) {
106 nstime_t nsta, nstb;
107
108 nstime_init2(&nsta, 42, 43);
109 nstime_copy(&nstb, &nsta);
110 nstime_subtract(&nsta, &nstb);
111 nstime_init(&nstb, 0);
112 assert_d_eq(nstime_compare(&nsta, &nstb), 0,
113 "Incorrect subtraction result");
114
115 nstime_init2(&nsta, 42, 43);
116 nstime_init2(&nstb, 41, 44);
117 nstime_subtract(&nsta, &nstb);
118 nstime_init2(&nstb, 0, BILLION - 1);
119 assert_d_eq(nstime_compare(&nsta, &nstb), 0,
120 "Incorrect subtraction result");
121 }
122 TEST_END
123
TEST_BEGIN(test_nstime_isubtract)124 TEST_BEGIN(test_nstime_isubtract) {
125 nstime_t nsta, nstb;
126
127 nstime_init2(&nsta, 42, 43);
128 nstime_isubtract(&nsta, 42*BILLION + 43);
129 nstime_init(&nstb, 0);
130 assert_d_eq(nstime_compare(&nsta, &nstb), 0,
131 "Incorrect subtraction result");
132
133 nstime_init2(&nsta, 42, 43);
134 nstime_isubtract(&nsta, 41*BILLION + 44);
135 nstime_init2(&nstb, 0, BILLION - 1);
136 assert_d_eq(nstime_compare(&nsta, &nstb), 0,
137 "Incorrect subtraction result");
138 }
139 TEST_END
140
TEST_BEGIN(test_nstime_imultiply)141 TEST_BEGIN(test_nstime_imultiply) {
142 nstime_t nsta, nstb;
143
144 nstime_init2(&nsta, 42, 43);
145 nstime_imultiply(&nsta, 10);
146 nstime_init2(&nstb, 420, 430);
147 assert_d_eq(nstime_compare(&nsta, &nstb), 0,
148 "Incorrect multiplication result");
149
150 nstime_init2(&nsta, 42, 666666666);
151 nstime_imultiply(&nsta, 3);
152 nstime_init2(&nstb, 127, 999999998);
153 assert_d_eq(nstime_compare(&nsta, &nstb), 0,
154 "Incorrect multiplication result");
155 }
156 TEST_END
157
TEST_BEGIN(test_nstime_idivide)158 TEST_BEGIN(test_nstime_idivide) {
159 nstime_t nsta, nstb;
160
161 nstime_init2(&nsta, 42, 43);
162 nstime_copy(&nstb, &nsta);
163 nstime_imultiply(&nsta, 10);
164 nstime_idivide(&nsta, 10);
165 assert_d_eq(nstime_compare(&nsta, &nstb), 0,
166 "Incorrect division result");
167
168 nstime_init2(&nsta, 42, 666666666);
169 nstime_copy(&nstb, &nsta);
170 nstime_imultiply(&nsta, 3);
171 nstime_idivide(&nsta, 3);
172 assert_d_eq(nstime_compare(&nsta, &nstb), 0,
173 "Incorrect division result");
174 }
175 TEST_END
176
TEST_BEGIN(test_nstime_divide)177 TEST_BEGIN(test_nstime_divide) {
178 nstime_t nsta, nstb, nstc;
179
180 nstime_init2(&nsta, 42, 43);
181 nstime_copy(&nstb, &nsta);
182 nstime_imultiply(&nsta, 10);
183 assert_u64_eq(nstime_divide(&nsta, &nstb), 10,
184 "Incorrect division result");
185
186 nstime_init2(&nsta, 42, 43);
187 nstime_copy(&nstb, &nsta);
188 nstime_imultiply(&nsta, 10);
189 nstime_init(&nstc, 1);
190 nstime_add(&nsta, &nstc);
191 assert_u64_eq(nstime_divide(&nsta, &nstb), 10,
192 "Incorrect division result");
193
194 nstime_init2(&nsta, 42, 43);
195 nstime_copy(&nstb, &nsta);
196 nstime_imultiply(&nsta, 10);
197 nstime_init(&nstc, 1);
198 nstime_subtract(&nsta, &nstc);
199 assert_u64_eq(nstime_divide(&nsta, &nstb), 9,
200 "Incorrect division result");
201 }
202 TEST_END
203
TEST_BEGIN(test_nstime_monotonic)204 TEST_BEGIN(test_nstime_monotonic) {
205 nstime_monotonic();
206 }
207 TEST_END
208
TEST_BEGIN(test_nstime_update)209 TEST_BEGIN(test_nstime_update) {
210 nstime_t nst;
211
212 nstime_init(&nst, 0);
213
214 assert_false(nstime_update(&nst), "Basic time update failed.");
215
216 /* Only Rip Van Winkle sleeps this long. */
217 {
218 nstime_t addend;
219 nstime_init2(&addend, 631152000, 0);
220 nstime_add(&nst, &addend);
221 }
222 {
223 nstime_t nst0;
224 nstime_copy(&nst0, &nst);
225 assert_true(nstime_update(&nst),
226 "Update should detect time roll-back.");
227 assert_d_eq(nstime_compare(&nst, &nst0), 0,
228 "Time should not have been modified");
229 }
230 }
231 TEST_END
232
233 int
main(void)234 main(void) {
235 return test(
236 test_nstime_init,
237 test_nstime_init2,
238 test_nstime_copy,
239 test_nstime_compare,
240 test_nstime_add,
241 test_nstime_iadd,
242 test_nstime_subtract,
243 test_nstime_isubtract,
244 test_nstime_imultiply,
245 test_nstime_idivide,
246 test_nstime_divide,
247 test_nstime_monotonic,
248 test_nstime_update);
249 }
250