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