diff options
Diffstat (limited to 'src/util/test_time.c')
-rw-r--r-- | src/util/test_time.c | 265 |
1 files changed, 0 insertions, 265 deletions
diff --git a/src/util/test_time.c b/src/util/test_time.c deleted file mode 100644 index 35f270a44..000000000 --- a/src/util/test_time.c +++ /dev/null | |||
@@ -1,265 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2001-2013, 2018 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file util/test_time.c | ||
22 | * @brief testcase for time.c | ||
23 | */ | ||
24 | |||
25 | #include "platform.h" | ||
26 | #include "gnunet_util_lib.h" | ||
27 | |||
28 | |||
29 | int | ||
30 | main (int argc, char *argv[]) | ||
31 | { | ||
32 | struct GNUNET_TIME_Absolute now; | ||
33 | struct GNUNET_TIME_AbsoluteNBO nown; | ||
34 | struct GNUNET_TIME_Absolute future; | ||
35 | struct GNUNET_TIME_Absolute past; | ||
36 | struct GNUNET_TIME_Absolute last; | ||
37 | struct GNUNET_TIME_Absolute forever; | ||
38 | struct GNUNET_TIME_Absolute zero; | ||
39 | struct GNUNET_TIME_Relative rel; | ||
40 | struct GNUNET_TIME_Relative relForever; | ||
41 | struct GNUNET_TIME_Relative relUnit; | ||
42 | struct GNUNET_TIME_RelativeNBO reln; | ||
43 | unsigned int i; | ||
44 | |||
45 | GNUNET_log_setup ("test-time", "WARNING", NULL); | ||
46 | forever = GNUNET_TIME_UNIT_FOREVER_ABS; | ||
47 | relForever = GNUNET_TIME_UNIT_FOREVER_REL; | ||
48 | relUnit = GNUNET_TIME_UNIT_MILLISECONDS; | ||
49 | zero.abs_value_us = 0; | ||
50 | |||
51 | last = now = GNUNET_TIME_absolute_get (); | ||
52 | while (now.abs_value_us == last.abs_value_us) | ||
53 | now = GNUNET_TIME_absolute_get (); | ||
54 | GNUNET_assert (now.abs_value_us > last.abs_value_us); | ||
55 | |||
56 | /* test overflow checking in multiply */ | ||
57 | rel = GNUNET_TIME_UNIT_MILLISECONDS; | ||
58 | GNUNET_log_skip (1, GNUNET_NO); | ||
59 | for (i = 0; i < 55; i++) | ||
60 | rel = GNUNET_TIME_relative_multiply (rel, 2); | ||
61 | GNUNET_log_skip (0, GNUNET_NO); | ||
62 | GNUNET_assert (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us); | ||
63 | /*check zero */ | ||
64 | rel.rel_value_us = (UINT64_MAX) -1024; | ||
65 | GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us == | ||
66 | GNUNET_TIME_relative_multiply (rel, 0).rel_value_us); | ||
67 | |||
68 | /* test infinity-check for relative to absolute */ | ||
69 | GNUNET_log_skip (1, GNUNET_NO); | ||
70 | last = GNUNET_TIME_relative_to_absolute (rel); | ||
71 | GNUNET_assert (last.abs_value_us == | ||
72 | GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us); | ||
73 | GNUNET_log_skip (0, GNUNET_YES); | ||
74 | |||
75 | /* check relative to absolute */ | ||
76 | rel.rel_value_us = 1000000; | ||
77 | GNUNET_assert (GNUNET_TIME_absolute_get ().abs_value_us < | ||
78 | GNUNET_TIME_relative_to_absolute (rel).abs_value_us); | ||
79 | /*check forever */ | ||
80 | rel.rel_value_us = UINT64_MAX; | ||
81 | GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us == | ||
82 | GNUNET_TIME_relative_to_absolute (rel).abs_value_us); | ||
83 | /* check overflow for r2a */ | ||
84 | rel.rel_value_us = (UINT64_MAX) -1024; | ||
85 | GNUNET_log_skip (1, GNUNET_NO); | ||
86 | last = GNUNET_TIME_relative_to_absolute (rel); | ||
87 | GNUNET_log_skip (0, GNUNET_NO); | ||
88 | GNUNET_assert (last.abs_value_us == | ||
89 | GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us); | ||
90 | |||
91 | /* check overflow for relative add */ | ||
92 | GNUNET_log_skip (1, GNUNET_NO); | ||
93 | rel = GNUNET_TIME_relative_add (rel, rel); | ||
94 | GNUNET_log_skip (0, GNUNET_NO); | ||
95 | GNUNET_assert (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us); | ||
96 | |||
97 | GNUNET_log_skip (1, GNUNET_NO); | ||
98 | rel = GNUNET_TIME_relative_add (relForever, relForever); | ||
99 | GNUNET_log_skip (0, GNUNET_NO); | ||
100 | GNUNET_assert (rel.rel_value_us == relForever.rel_value_us); | ||
101 | |||
102 | GNUNET_log_skip (1, GNUNET_NO); | ||
103 | rel = GNUNET_TIME_relative_add (relUnit, relUnit); | ||
104 | GNUNET_assert (rel.rel_value_us == 2 * relUnit.rel_value_us); | ||
105 | |||
106 | /* check relation check in get_duration */ | ||
107 | future.abs_value_us = now.abs_value_us + 1000000; | ||
108 | GNUNET_assert (GNUNET_TIME_absolute_get_difference (now, | ||
109 | future).rel_value_us == | ||
110 | 1000000); | ||
111 | GNUNET_assert (GNUNET_TIME_absolute_get_difference (future, | ||
112 | now).rel_value_us == | ||
113 | 0); | ||
114 | |||
115 | GNUNET_assert (GNUNET_TIME_absolute_get_difference (zero, | ||
116 | forever).rel_value_us | ||
117 | == forever.abs_value_us); | ||
118 | |||
119 | past.abs_value_us = now.abs_value_us - 1000000; | ||
120 | rel = GNUNET_TIME_absolute_get_duration (future); | ||
121 | GNUNET_assert (rel.rel_value_us == 0); | ||
122 | rel = GNUNET_TIME_absolute_get_duration (past); | ||
123 | GNUNET_assert (rel.rel_value_us >= 1000000); | ||
124 | |||
125 | /* check get remaining */ | ||
126 | rel = GNUNET_TIME_absolute_get_remaining (now); | ||
127 | GNUNET_assert (rel.rel_value_us == 0); | ||
128 | rel = GNUNET_TIME_absolute_get_remaining (past); | ||
129 | GNUNET_assert (rel.rel_value_us == 0); | ||
130 | rel = GNUNET_TIME_absolute_get_remaining (future); | ||
131 | GNUNET_assert (rel.rel_value_us > 0); | ||
132 | GNUNET_assert (rel.rel_value_us <= 1000000); | ||
133 | forever = GNUNET_TIME_UNIT_FOREVER_ABS; | ||
134 | GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == | ||
135 | GNUNET_TIME_absolute_get_remaining (forever).rel_value_us); | ||
136 | |||
137 | /* check endianness */ | ||
138 | reln = GNUNET_TIME_relative_hton (rel); | ||
139 | GNUNET_assert (rel.rel_value_us == GNUNET_TIME_relative_ntoh ( | ||
140 | reln).rel_value_us); | ||
141 | nown = GNUNET_TIME_absolute_hton (now); | ||
142 | GNUNET_assert (now.abs_value_us == GNUNET_TIME_absolute_ntoh ( | ||
143 | nown).abs_value_us); | ||
144 | |||
145 | /* check absolute addition */ | ||
146 | future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_SECONDS); | ||
147 | GNUNET_assert (future.abs_value_us == now.abs_value_us + 1000 * 1000LL); | ||
148 | |||
149 | future = GNUNET_TIME_absolute_add (forever, GNUNET_TIME_UNIT_ZERO); | ||
150 | GNUNET_assert (future.abs_value_us == forever.abs_value_us); | ||
151 | |||
152 | rel.rel_value_us = (UINT64_MAX) -1024; | ||
153 | now.abs_value_us = rel.rel_value_us; | ||
154 | future = GNUNET_TIME_absolute_add (now, rel); | ||
155 | GNUNET_assert (future.abs_value_us == forever.abs_value_us); | ||
156 | |||
157 | /* check zero */ | ||
158 | future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_ZERO); | ||
159 | GNUNET_assert (future.abs_value_us == now.abs_value_us); | ||
160 | |||
161 | GNUNET_assert (forever.abs_value_us == | ||
162 | GNUNET_TIME_absolute_subtract (forever, | ||
163 | GNUNET_TIME_UNIT_MINUTES). | ||
164 | abs_value_us); | ||
165 | /*check absolute subtract */ | ||
166 | now.abs_value_us = 50000; | ||
167 | rel.rel_value_us = 100000; | ||
168 | GNUNET_assert (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == | ||
169 | (GNUNET_TIME_absolute_subtract (now, rel)).abs_value_us); | ||
170 | rel.rel_value_us = 10000; | ||
171 | GNUNET_assert (40000 == (GNUNET_TIME_absolute_subtract (now, | ||
172 | rel)).abs_value_us); | ||
173 | |||
174 | /*check relative divide */ | ||
175 | GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == | ||
176 | (GNUNET_TIME_relative_divide (rel, 0)).rel_value_us); | ||
177 | |||
178 | rel = GNUNET_TIME_UNIT_FOREVER_REL; | ||
179 | GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == | ||
180 | (GNUNET_TIME_relative_divide (rel, 2)).rel_value_us); | ||
181 | |||
182 | rel = GNUNET_TIME_relative_divide (relUnit, 2); | ||
183 | GNUNET_assert (rel.rel_value_us == relUnit.rel_value_us / 2); | ||
184 | |||
185 | |||
186 | /* check Return absolute time of 0ms */ | ||
187 | zero = GNUNET_TIME_UNIT_ZERO_ABS; | ||
188 | |||
189 | /* check GNUNET_TIME_calculate_eta */ | ||
190 | last.abs_value_us = GNUNET_TIME_absolute_get ().abs_value_us - 1024; | ||
191 | forever = GNUNET_TIME_UNIT_FOREVER_ABS; | ||
192 | forever.abs_value_us = forever.abs_value_us - 1024; | ||
193 | GNUNET_assert (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == | ||
194 | GNUNET_TIME_calculate_eta (forever, 50000, | ||
195 | 100000).rel_value_us); | ||
196 | /* check zero */ | ||
197 | GNUNET_log_skip (1, GNUNET_NO); | ||
198 | GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us == | ||
199 | (GNUNET_TIME_calculate_eta (last, 60000, 50000)).rel_value_us); | ||
200 | GNUNET_log_skip (0, GNUNET_YES); | ||
201 | /*check forever */ | ||
202 | GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == | ||
203 | (GNUNET_TIME_calculate_eta (last, 0, 50000)).rel_value_us); | ||
204 | |||
205 | /*check relative subtract */ | ||
206 | now = GNUNET_TIME_absolute_get (); | ||
207 | rel.rel_value_us = now.abs_value_us; | ||
208 | relForever.rel_value_us = rel.rel_value_us + 1024; | ||
209 | GNUNET_assert (1024 == | ||
210 | GNUNET_TIME_relative_subtract (relForever, rel).rel_value_us); | ||
211 | /*check zero */ | ||
212 | GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us == | ||
213 | GNUNET_TIME_relative_subtract (rel, relForever).rel_value_us); | ||
214 | /*check forever */ | ||
215 | rel.rel_value_us = UINT64_MAX; | ||
216 | GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == | ||
217 | GNUNET_TIME_relative_subtract (rel, relForever).rel_value_us); | ||
218 | |||
219 | /*check GNUNET_TIME_relative_min */ | ||
220 | now = GNUNET_TIME_absolute_get (); | ||
221 | rel.rel_value_us = now.abs_value_us; | ||
222 | relForever.rel_value_us = rel.rel_value_us - 1024; | ||
223 | GNUNET_assert (relForever.rel_value_us == | ||
224 | GNUNET_TIME_relative_min (rel, relForever).rel_value_us); | ||
225 | |||
226 | /*check GNUNET_TIME_relative_max */ | ||
227 | GNUNET_assert (rel.rel_value_us == | ||
228 | GNUNET_TIME_relative_max (rel, relForever).rel_value_us); | ||
229 | |||
230 | /*check GNUNET_TIME_absolute_min */ | ||
231 | now = GNUNET_TIME_absolute_get (); | ||
232 | last.abs_value_us = now.abs_value_us - 1024; | ||
233 | GNUNET_assert (last.abs_value_us == | ||
234 | GNUNET_TIME_absolute_min (now, last).abs_value_us); | ||
235 | |||
236 | /*check GNUNET_TIME_absolute_max */ | ||
237 | GNUNET_assert (now.abs_value_us == | ||
238 | GNUNET_TIME_absolute_max (now, last).abs_value_us); | ||
239 | for (unsigned int i = 0; i < 30; i++) | ||
240 | { | ||
241 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
242 | |||
243 | cfg = GNUNET_CONFIGURATION_create (); | ||
244 | last = GNUNET_TIME_absolute_get_monotonic (cfg); | ||
245 | now = GNUNET_TIME_absolute_get_monotonic (cfg); | ||
246 | GNUNET_assert (now.abs_value_us > last.abs_value_us); | ||
247 | (void) GNUNET_TIME_absolute_get_monotonic (NULL); | ||
248 | GNUNET_CONFIGURATION_set_value_string (cfg, | ||
249 | "util", | ||
250 | "MONOTONIC_TIME_FILENAME", | ||
251 | "monotonic-time.dat"); | ||
252 | last = GNUNET_TIME_absolute_get_monotonic (cfg); | ||
253 | now = GNUNET_TIME_absolute_get_monotonic (cfg); | ||
254 | (void) GNUNET_TIME_absolute_get_monotonic (NULL); | ||
255 | GNUNET_assert (now.abs_value_us > last.abs_value_us); | ||
256 | GNUNET_CONFIGURATION_destroy (cfg); | ||
257 | } | ||
258 | GNUNET_break (GNUNET_OK == | ||
259 | GNUNET_DISK_directory_remove ("monotonic-time.dat")); | ||
260 | |||
261 | return 0; | ||
262 | } | ||
263 | |||
264 | |||
265 | /* end of test_time.c */ | ||