use NULL value in load_path_suffix to NOT load any files
[oweals/gnunet.git] / src / util / test_time.c
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 #include "platform.h"
25 #include "gnunet_util_lib.h"
26
27
28 int
29 main (int argc, char *argv[])
30 {
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;
42   unsigned int i;
43
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;
49
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);
54
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);
62   /*check zero */
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);
66
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 ==
71                  GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us);
72   GNUNET_log_skip (0, GNUNET_YES);
73
74   /* check relative to absolute */
75   rel.rel_value_us = 1000000;
76   GNUNET_assert (GNUNET_TIME_absolute_get ().abs_value_us <
77                  GNUNET_TIME_relative_to_absolute (rel).abs_value_us);
78   /*check forever */
79   rel.rel_value_us = UINT64_MAX;
80   GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us ==
81                  GNUNET_TIME_relative_to_absolute (rel).abs_value_us);
82   /* check overflow for r2a */
83   rel.rel_value_us = (UINT64_MAX) -1024;
84   GNUNET_log_skip (1, GNUNET_NO);
85   last = GNUNET_TIME_relative_to_absolute (rel);
86   GNUNET_log_skip (0, GNUNET_NO);
87   GNUNET_assert (last.abs_value_us ==
88                  GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us);
89
90   /* check overflow for relative add */
91   GNUNET_log_skip (1, GNUNET_NO);
92   rel = GNUNET_TIME_relative_add (rel, rel);
93   GNUNET_log_skip (0, GNUNET_NO);
94   GNUNET_assert (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us);
95
96   GNUNET_log_skip (1, GNUNET_NO);
97   rel = GNUNET_TIME_relative_add (relForever, relForever);
98   GNUNET_log_skip (0, GNUNET_NO);
99   GNUNET_assert (rel.rel_value_us == relForever.rel_value_us);
100
101   GNUNET_log_skip (1, GNUNET_NO);
102   rel = GNUNET_TIME_relative_add (relUnit, relUnit);
103   GNUNET_assert (rel.rel_value_us == 2 * relUnit.rel_value_us);
104
105   /* check relation check in get_duration */
106   future.abs_value_us = now.abs_value_us + 1000000;
107   GNUNET_assert (GNUNET_TIME_absolute_get_difference (now,
108                                                       future).rel_value_us ==
109                  1000000);
110   GNUNET_assert (GNUNET_TIME_absolute_get_difference (future,
111                                                       now).rel_value_us ==
112                  0);
113
114   GNUNET_assert (GNUNET_TIME_absolute_get_difference (zero,
115                                                       forever).rel_value_us
116                  == forever.abs_value_us);
117
118   past.abs_value_us = now.abs_value_us - 1000000;
119   rel = GNUNET_TIME_absolute_get_duration (future);
120   GNUNET_assert (rel.rel_value_us == 0);
121   rel = GNUNET_TIME_absolute_get_duration (past);
122   GNUNET_assert (rel.rel_value_us >= 1000000);
123
124   /* check get remaining */
125   rel = GNUNET_TIME_absolute_get_remaining (now);
126   GNUNET_assert (rel.rel_value_us == 0);
127   rel = GNUNET_TIME_absolute_get_remaining (past);
128   GNUNET_assert (rel.rel_value_us == 0);
129   rel = GNUNET_TIME_absolute_get_remaining (future);
130   GNUNET_assert (rel.rel_value_us > 0);
131   GNUNET_assert (rel.rel_value_us <= 1000000);
132   forever = GNUNET_TIME_UNIT_FOREVER_ABS;
133   GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
134                  GNUNET_TIME_absolute_get_remaining (forever).rel_value_us);
135
136   /* check endianess */
137   reln = GNUNET_TIME_relative_hton (rel);
138   GNUNET_assert (rel.rel_value_us == GNUNET_TIME_relative_ntoh (
139                    reln).rel_value_us);
140   nown = GNUNET_TIME_absolute_hton (now);
141   GNUNET_assert (now.abs_value_us == GNUNET_TIME_absolute_ntoh (
142                    nown).abs_value_us);
143
144   /* check absolute addition */
145   future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_SECONDS);
146   GNUNET_assert (future.abs_value_us == now.abs_value_us + 1000 * 1000LL);
147
148   future = GNUNET_TIME_absolute_add (forever, GNUNET_TIME_UNIT_ZERO);
149   GNUNET_assert (future.abs_value_us == forever.abs_value_us);
150
151   rel.rel_value_us = (UINT64_MAX) -1024;
152   now.abs_value_us = rel.rel_value_us;
153   future = GNUNET_TIME_absolute_add (now, rel);
154   GNUNET_assert (future.abs_value_us == forever.abs_value_us);
155
156   /* check zero */
157   future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_ZERO);
158   GNUNET_assert (future.abs_value_us == now.abs_value_us);
159
160   GNUNET_assert (forever.abs_value_us ==
161                  GNUNET_TIME_absolute_subtract (forever,
162                                                 GNUNET_TIME_UNIT_MINUTES).
163                  abs_value_us);
164   /*check absolute subtract */
165   now.abs_value_us = 50000;
166   rel.rel_value_us = 100000;
167   GNUNET_assert (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us ==
168                  (GNUNET_TIME_absolute_subtract (now, rel)).abs_value_us);
169   rel.rel_value_us = 10000;
170   GNUNET_assert (40000 == (GNUNET_TIME_absolute_subtract (now,
171                                                           rel)).abs_value_us);
172
173   /*check relative divide */
174   GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
175                  (GNUNET_TIME_relative_divide (rel, 0)).rel_value_us);
176
177   rel = GNUNET_TIME_UNIT_FOREVER_REL;
178   GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
179                  (GNUNET_TIME_relative_divide (rel, 2)).rel_value_us);
180
181   rel = GNUNET_TIME_relative_divide (relUnit, 2);
182   GNUNET_assert (rel.rel_value_us == relUnit.rel_value_us / 2);
183
184
185   /* check Return absolute time of 0ms */
186   zero = GNUNET_TIME_UNIT_ZERO_ABS;
187
188   /* check GNUNET_TIME_calculate_eta */
189   last.abs_value_us = GNUNET_TIME_absolute_get ().abs_value_us - 1024;
190   forever = GNUNET_TIME_UNIT_FOREVER_ABS;
191   forever.abs_value_us = forever.abs_value_us - 1024;
192   GNUNET_assert (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us ==
193                  GNUNET_TIME_calculate_eta (forever, 50000,
194                                             100000).rel_value_us);
195   /* check zero */
196   GNUNET_log_skip (1, GNUNET_NO);
197   GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us ==
198                  (GNUNET_TIME_calculate_eta (last, 60000, 50000)).rel_value_us);
199   GNUNET_log_skip (0, GNUNET_YES);
200   /*check forever */
201   GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
202                  (GNUNET_TIME_calculate_eta (last, 0, 50000)).rel_value_us);
203
204   /*check relative subtract */
205   now = GNUNET_TIME_absolute_get ();
206   rel.rel_value_us = now.abs_value_us;
207   relForever.rel_value_us = rel.rel_value_us + 1024;
208   GNUNET_assert (1024 ==
209                  GNUNET_TIME_relative_subtract (relForever, rel).rel_value_us);
210   /*check zero */
211   GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us ==
212                  GNUNET_TIME_relative_subtract (rel, relForever).rel_value_us);
213   /*check forever */
214   rel.rel_value_us = UINT64_MAX;
215   GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
216                  GNUNET_TIME_relative_subtract (rel, relForever).rel_value_us);
217
218   /*check GNUNET_TIME_relative_min */
219   now = GNUNET_TIME_absolute_get ();
220   rel.rel_value_us = now.abs_value_us;
221   relForever.rel_value_us = rel.rel_value_us - 1024;
222   GNUNET_assert (relForever.rel_value_us ==
223                  GNUNET_TIME_relative_min (rel, relForever).rel_value_us);
224
225   /*check GNUNET_TIME_relative_max */
226   GNUNET_assert (rel.rel_value_us ==
227                  GNUNET_TIME_relative_max (rel, relForever).rel_value_us);
228
229   /*check GNUNET_TIME_absolute_min */
230   now = GNUNET_TIME_absolute_get ();
231   last.abs_value_us = now.abs_value_us - 1024;
232   GNUNET_assert (last.abs_value_us ==
233                  GNUNET_TIME_absolute_min (now, last).abs_value_us);
234
235   /*check  GNUNET_TIME_absolute_max */
236   GNUNET_assert (now.abs_value_us ==
237                  GNUNET_TIME_absolute_max (now, last).abs_value_us);
238   for (unsigned int i = 0; i < 30; i++)
239   {
240     struct GNUNET_CONFIGURATION_Handle *cfg;
241
242     cfg = GNUNET_CONFIGURATION_create ();
243     last = GNUNET_TIME_absolute_get_monotonic (cfg);
244     now = GNUNET_TIME_absolute_get_monotonic (cfg);
245     GNUNET_assert (now.abs_value_us > last.abs_value_us);
246     (void) GNUNET_TIME_absolute_get_monotonic (NULL);
247     GNUNET_CONFIGURATION_set_value_string (cfg,
248                                            "util",
249                                            "MONOTONIC_TIME_FILENAME",
250                                            "monotonic-time.dat");
251     last = GNUNET_TIME_absolute_get_monotonic (cfg);
252     now = GNUNET_TIME_absolute_get_monotonic (cfg);
253     (void) GNUNET_TIME_absolute_get_monotonic (NULL);
254     GNUNET_assert (now.abs_value_us > last.abs_value_us);
255     GNUNET_CONFIGURATION_destroy (cfg);
256   }
257   GNUNET_break (GNUNET_OK ==
258                 GNUNET_DISK_directory_remove ("monotonic-time.dat"));
259
260   return 0;
261 }
262
263
264 /* end of test_time.c */