2 This file is part of GNUnet.
3 Copyright (C) 2001-2013, 2018 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file util/test_time.c
22 * @brief testcase for time.c
25 #include "gnunet_util_lib.h"
29 main (int argc, char *argv[])
31 struct GNUNET_TIME_Absolute now;
32 struct GNUNET_TIME_AbsoluteNBO nown;
33 struct GNUNET_TIME_Absolute future;
34 struct GNUNET_TIME_Absolute past;
35 struct GNUNET_TIME_Absolute last;
36 struct GNUNET_TIME_Absolute forever;
37 struct GNUNET_TIME_Absolute zero;
38 struct GNUNET_TIME_Relative rel;
39 struct GNUNET_TIME_Relative relForever;
40 struct GNUNET_TIME_Relative relUnit;
41 struct GNUNET_TIME_RelativeNBO reln;
44 GNUNET_log_setup ("test-time", "WARNING", NULL);
45 forever = GNUNET_TIME_UNIT_FOREVER_ABS;
46 relForever = GNUNET_TIME_UNIT_FOREVER_REL;
47 relUnit = GNUNET_TIME_UNIT_MILLISECONDS;
48 zero.abs_value_us = 0;
50 last = now = GNUNET_TIME_absolute_get ();
51 while (now.abs_value_us == last.abs_value_us)
52 now = GNUNET_TIME_absolute_get ();
53 GNUNET_assert (now.abs_value_us > last.abs_value_us);
55 /* test overflow checking in multiply */
56 rel = GNUNET_TIME_UNIT_MILLISECONDS;
57 GNUNET_log_skip (1, GNUNET_NO);
58 for (i = 0; i < 55; i++)
59 rel = GNUNET_TIME_relative_multiply (rel, 2);
60 GNUNET_log_skip (0, GNUNET_NO);
61 GNUNET_assert (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us);
63 rel.rel_value_us = (UINT64_MAX) - 1024;
64 GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us ==
65 GNUNET_TIME_relative_multiply (rel, 0).rel_value_us);
67 /* test infinity-check for relative to absolute */
68 GNUNET_log_skip (1, GNUNET_NO);
69 last = GNUNET_TIME_relative_to_absolute (rel);
70 GNUNET_assert (last.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us);
71 GNUNET_log_skip (0, GNUNET_YES);
73 /* check relative to absolute */
74 rel.rel_value_us = 1000000;
75 GNUNET_assert (GNUNET_TIME_absolute_get ().abs_value_us <
76 GNUNET_TIME_relative_to_absolute (rel).abs_value_us);
78 rel.rel_value_us = UINT64_MAX;
79 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us ==
80 GNUNET_TIME_relative_to_absolute (rel).abs_value_us);
81 /* check overflow for r2a */
82 rel.rel_value_us = (UINT64_MAX) - 1024;
83 GNUNET_log_skip (1, GNUNET_NO);
84 last = GNUNET_TIME_relative_to_absolute (rel);
85 GNUNET_log_skip (0, GNUNET_NO);
86 GNUNET_assert (last.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us);
88 /* check overflow for relative add */
89 GNUNET_log_skip (1, GNUNET_NO);
90 rel = GNUNET_TIME_relative_add (rel, rel);
91 GNUNET_log_skip (0, GNUNET_NO);
92 GNUNET_assert (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us);
94 GNUNET_log_skip (1, GNUNET_NO);
95 rel = GNUNET_TIME_relative_add (relForever, relForever);
96 GNUNET_log_skip (0, GNUNET_NO);
97 GNUNET_assert (rel.rel_value_us == relForever.rel_value_us);
99 GNUNET_log_skip (1, GNUNET_NO);
100 rel = GNUNET_TIME_relative_add (relUnit, relUnit);
101 GNUNET_assert (rel.rel_value_us == 2 * relUnit.rel_value_us);
103 /* check relation check in get_duration */
104 future.abs_value_us = now.abs_value_us + 1000000;
105 GNUNET_assert (GNUNET_TIME_absolute_get_difference (now, future).rel_value_us ==
107 GNUNET_assert (GNUNET_TIME_absolute_get_difference (future, now).rel_value_us ==
110 GNUNET_assert (GNUNET_TIME_absolute_get_difference (zero, forever).rel_value_us
111 == forever.abs_value_us);
113 past.abs_value_us = now.abs_value_us - 1000000;
114 rel = GNUNET_TIME_absolute_get_duration (future);
115 GNUNET_assert (rel.rel_value_us == 0);
116 rel = GNUNET_TIME_absolute_get_duration (past);
117 GNUNET_assert (rel.rel_value_us >= 1000000);
119 /* check get remaining */
120 rel = GNUNET_TIME_absolute_get_remaining (now);
121 GNUNET_assert (rel.rel_value_us == 0);
122 rel = GNUNET_TIME_absolute_get_remaining (past);
123 GNUNET_assert (rel.rel_value_us == 0);
124 rel = GNUNET_TIME_absolute_get_remaining (future);
125 GNUNET_assert (rel.rel_value_us > 0);
126 GNUNET_assert (rel.rel_value_us <= 1000000);
127 forever = GNUNET_TIME_UNIT_FOREVER_ABS;
128 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
129 GNUNET_TIME_absolute_get_remaining (forever).rel_value_us);
131 /* check endianess */
132 reln = GNUNET_TIME_relative_hton (rel);
133 GNUNET_assert (rel.rel_value_us == GNUNET_TIME_relative_ntoh (reln).rel_value_us);
134 nown = GNUNET_TIME_absolute_hton (now);
135 GNUNET_assert (now.abs_value_us == GNUNET_TIME_absolute_ntoh (nown).abs_value_us);
137 /* check absolute addition */
138 future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_SECONDS);
139 GNUNET_assert (future.abs_value_us == now.abs_value_us + 1000 * 1000LL);
141 future = GNUNET_TIME_absolute_add (forever, GNUNET_TIME_UNIT_ZERO);
142 GNUNET_assert (future.abs_value_us == forever.abs_value_us);
144 rel.rel_value_us = (UINT64_MAX) - 1024;
145 now.abs_value_us = rel.rel_value_us;
146 future = GNUNET_TIME_absolute_add (now, rel);
147 GNUNET_assert (future.abs_value_us == forever.abs_value_us);
150 future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_ZERO);
151 GNUNET_assert (future.abs_value_us == now.abs_value_us);
153 GNUNET_assert (forever.abs_value_us ==
154 GNUNET_TIME_absolute_subtract (forever,
155 GNUNET_TIME_UNIT_MINUTES).abs_value_us);
156 /*check absolute subtract */
157 now.abs_value_us = 50000;
158 rel.rel_value_us = 100000;
159 GNUNET_assert (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us ==
160 (GNUNET_TIME_absolute_subtract (now, rel)).abs_value_us);
161 rel.rel_value_us = 10000;
162 GNUNET_assert (40000 == (GNUNET_TIME_absolute_subtract (now, rel)).abs_value_us);
164 /*check relative divide */
165 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
166 (GNUNET_TIME_relative_divide (rel, 0)).rel_value_us);
168 rel = GNUNET_TIME_UNIT_FOREVER_REL;
169 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
170 (GNUNET_TIME_relative_divide (rel, 2)).rel_value_us);
172 rel = GNUNET_TIME_relative_divide (relUnit, 2);
173 GNUNET_assert (rel.rel_value_us == relUnit.rel_value_us / 2);
176 /* check Return absolute time of 0ms */
177 zero = GNUNET_TIME_UNIT_ZERO_ABS;
179 /* check GNUNET_TIME_calculate_eta */
180 last.abs_value_us = GNUNET_TIME_absolute_get ().abs_value_us - 1024;
181 forever = GNUNET_TIME_UNIT_FOREVER_ABS;
182 forever.abs_value_us = forever.abs_value_us - 1024;
183 GNUNET_assert (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us ==
184 GNUNET_TIME_calculate_eta (forever, 50000, 100000).rel_value_us);
186 GNUNET_log_skip (1, GNUNET_NO);
187 GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us ==
188 (GNUNET_TIME_calculate_eta (last, 60000, 50000)).rel_value_us);
189 GNUNET_log_skip (0, GNUNET_YES);
191 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
192 (GNUNET_TIME_calculate_eta (last, 0, 50000)).rel_value_us);
194 /*check relative subtract */
195 now = GNUNET_TIME_absolute_get ();
196 rel.rel_value_us = now.abs_value_us;
197 relForever.rel_value_us = rel.rel_value_us + 1024;
198 GNUNET_assert (1024 ==
199 GNUNET_TIME_relative_subtract (relForever, rel).rel_value_us);
201 GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us ==
202 GNUNET_TIME_relative_subtract (rel, relForever).rel_value_us);
204 rel.rel_value_us = UINT64_MAX;
205 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
206 GNUNET_TIME_relative_subtract (rel, relForever).rel_value_us);
208 /*check GNUNET_TIME_relative_min */
209 now = GNUNET_TIME_absolute_get ();
210 rel.rel_value_us = now.abs_value_us;
211 relForever.rel_value_us = rel.rel_value_us - 1024;
212 GNUNET_assert (relForever.rel_value_us ==
213 GNUNET_TIME_relative_min (rel, relForever).rel_value_us);
215 /*check GNUNET_TIME_relative_max */
216 GNUNET_assert (rel.rel_value_us ==
217 GNUNET_TIME_relative_max (rel, relForever).rel_value_us);
219 /*check GNUNET_TIME_absolute_min */
220 now = GNUNET_TIME_absolute_get ();
221 last.abs_value_us = now.abs_value_us - 1024;
222 GNUNET_assert (last.abs_value_us ==
223 GNUNET_TIME_absolute_min (now, last).abs_value_us);
225 /*check GNUNET_TIME_absolute_max */
226 GNUNET_assert (now.abs_value_us ==
227 GNUNET_TIME_absolute_max (now, last).abs_value_us);
228 for (unsigned int i=0;i<30;i++)
230 struct GNUNET_CONFIGURATION_Handle *cfg;
232 cfg = GNUNET_CONFIGURATION_create ();
233 last = GNUNET_TIME_absolute_get_monotonic (cfg);
234 now = GNUNET_TIME_absolute_get_monotonic (cfg);
235 GNUNET_assert (now.abs_value_us > last.abs_value_us);
236 (void) GNUNET_TIME_absolute_get_monotonic (NULL);
237 GNUNET_CONFIGURATION_set_value_string (cfg,
239 "MONOTONIC_TIME_FILENAME",
240 "monotonic-time.dat");
241 last = GNUNET_TIME_absolute_get_monotonic (cfg);
242 now = GNUNET_TIME_absolute_get_monotonic (cfg);
243 (void) GNUNET_TIME_absolute_get_monotonic (NULL);
244 GNUNET_assert (now.abs_value_us > last.abs_value_us);
245 GNUNET_CONFIGURATION_destroy (cfg);
247 GNUNET_break (GNUNET_OK ==
248 GNUNET_DISK_directory_remove ("monotonic-time.dat"));
254 /* end of test_time.c */