2 This file is part of GNUnet.
3 Copyright (C) 2001-2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU 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.
16 * @file util/test_time.c
17 * @brief testcase for time.c
20 #include "gnunet_util_lib.h"
24 main (int argc, char *argv[])
26 struct GNUNET_TIME_Absolute now;
27 struct GNUNET_TIME_AbsoluteNBO nown;
28 struct GNUNET_TIME_Absolute future;
29 struct GNUNET_TIME_Absolute past;
30 struct GNUNET_TIME_Absolute last;
31 struct GNUNET_TIME_Absolute forever;
32 struct GNUNET_TIME_Absolute zero;
33 struct GNUNET_TIME_Relative rel;
34 struct GNUNET_TIME_Relative relForever;
35 struct GNUNET_TIME_Relative relUnit;
36 struct GNUNET_TIME_RelativeNBO reln;
39 GNUNET_log_setup ("test-time", "WARNING", NULL);
40 forever = GNUNET_TIME_UNIT_FOREVER_ABS;
41 relForever = GNUNET_TIME_UNIT_FOREVER_REL;
42 relUnit = GNUNET_TIME_UNIT_MILLISECONDS;
43 zero.abs_value_us = 0;
45 last = now = GNUNET_TIME_absolute_get ();
46 while (now.abs_value_us == last.abs_value_us)
47 now = GNUNET_TIME_absolute_get ();
48 GNUNET_assert (now.abs_value_us > last.abs_value_us);
50 /* test overflow checking in multiply */
51 rel = GNUNET_TIME_UNIT_MILLISECONDS;
52 GNUNET_log_skip (1, GNUNET_NO);
53 for (i = 0; i < 55; i++)
54 rel = GNUNET_TIME_relative_multiply (rel, 2);
55 GNUNET_log_skip (0, GNUNET_NO);
56 GNUNET_assert (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us);
58 rel.rel_value_us = (UINT64_MAX) - 1024;
59 GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us ==
60 GNUNET_TIME_relative_multiply (rel, 0).rel_value_us);
62 /* test infinity-check for relative to absolute */
63 GNUNET_log_skip (1, GNUNET_NO);
64 last = GNUNET_TIME_relative_to_absolute (rel);
65 GNUNET_assert (last.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us);
66 GNUNET_log_skip (0, GNUNET_YES);
68 /* check relative to absolute */
69 rel.rel_value_us = 1000000;
70 GNUNET_assert (GNUNET_TIME_absolute_get ().abs_value_us <
71 GNUNET_TIME_relative_to_absolute (rel).abs_value_us);
73 rel.rel_value_us = UINT64_MAX;
74 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us ==
75 GNUNET_TIME_relative_to_absolute (rel).abs_value_us);
76 /* check overflow for r2a */
77 rel.rel_value_us = (UINT64_MAX) - 1024;
78 GNUNET_log_skip (1, GNUNET_NO);
79 last = GNUNET_TIME_relative_to_absolute (rel);
80 GNUNET_log_skip (0, GNUNET_NO);
81 GNUNET_assert (last.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us);
83 /* check overflow for relative add */
84 GNUNET_log_skip (1, GNUNET_NO);
85 rel = GNUNET_TIME_relative_add (rel, rel);
86 GNUNET_log_skip (0, GNUNET_NO);
87 GNUNET_assert (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us);
89 GNUNET_log_skip (1, GNUNET_NO);
90 rel = GNUNET_TIME_relative_add (relForever, relForever);
91 GNUNET_log_skip (0, GNUNET_NO);
92 GNUNET_assert (rel.rel_value_us == relForever.rel_value_us);
94 GNUNET_log_skip (1, GNUNET_NO);
95 rel = GNUNET_TIME_relative_add (relUnit, relUnit);
96 GNUNET_assert (rel.rel_value_us == 2 * relUnit.rel_value_us);
98 /* check relation check in get_duration */
99 future.abs_value_us = now.abs_value_us + 1000000;
100 GNUNET_assert (GNUNET_TIME_absolute_get_difference (now, future).rel_value_us ==
102 GNUNET_assert (GNUNET_TIME_absolute_get_difference (future, now).rel_value_us ==
105 GNUNET_assert (GNUNET_TIME_absolute_get_difference (zero, forever).rel_value_us
106 == forever.abs_value_us);
108 past.abs_value_us = now.abs_value_us - 1000000;
109 rel = GNUNET_TIME_absolute_get_duration (future);
110 GNUNET_assert (rel.rel_value_us == 0);
111 rel = GNUNET_TIME_absolute_get_duration (past);
112 GNUNET_assert (rel.rel_value_us >= 1000000);
114 /* check get remaining */
115 rel = GNUNET_TIME_absolute_get_remaining (now);
116 GNUNET_assert (rel.rel_value_us == 0);
117 rel = GNUNET_TIME_absolute_get_remaining (past);
118 GNUNET_assert (rel.rel_value_us == 0);
119 rel = GNUNET_TIME_absolute_get_remaining (future);
120 GNUNET_assert (rel.rel_value_us > 0);
121 GNUNET_assert (rel.rel_value_us <= 1000000);
122 forever = GNUNET_TIME_UNIT_FOREVER_ABS;
123 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
124 GNUNET_TIME_absolute_get_remaining (forever).rel_value_us);
126 /* check endianess */
127 reln = GNUNET_TIME_relative_hton (rel);
128 GNUNET_assert (rel.rel_value_us == GNUNET_TIME_relative_ntoh (reln).rel_value_us);
129 nown = GNUNET_TIME_absolute_hton (now);
130 GNUNET_assert (now.abs_value_us == GNUNET_TIME_absolute_ntoh (nown).abs_value_us);
132 /* check absolute addition */
133 future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_SECONDS);
134 GNUNET_assert (future.abs_value_us == now.abs_value_us + 1000 * 1000LL);
136 future = GNUNET_TIME_absolute_add (forever, GNUNET_TIME_UNIT_ZERO);
137 GNUNET_assert (future.abs_value_us == forever.abs_value_us);
139 rel.rel_value_us = (UINT64_MAX) - 1024;
140 now.abs_value_us = rel.rel_value_us;
141 future = GNUNET_TIME_absolute_add (now, rel);
142 GNUNET_assert (future.abs_value_us == forever.abs_value_us);
145 future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_ZERO);
146 GNUNET_assert (future.abs_value_us == now.abs_value_us);
148 GNUNET_assert (forever.abs_value_us ==
149 GNUNET_TIME_absolute_subtract (forever,
150 GNUNET_TIME_UNIT_MINUTES).abs_value_us);
151 /*check absolute subtract */
152 now.abs_value_us = 50000;
153 rel.rel_value_us = 100000;
154 GNUNET_assert (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us ==
155 (GNUNET_TIME_absolute_subtract (now, rel)).abs_value_us);
156 rel.rel_value_us = 10000;
157 GNUNET_assert (40000 == (GNUNET_TIME_absolute_subtract (now, rel)).abs_value_us);
159 /*check relative divide */
160 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
161 (GNUNET_TIME_relative_divide (rel, 0)).rel_value_us);
163 rel = GNUNET_TIME_UNIT_FOREVER_REL;
164 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
165 (GNUNET_TIME_relative_divide (rel, 2)).rel_value_us);
167 rel = GNUNET_TIME_relative_divide (relUnit, 2);
168 GNUNET_assert (rel.rel_value_us == relUnit.rel_value_us / 2);
171 /* check Return absolute time of 0ms */
172 zero = GNUNET_TIME_UNIT_ZERO_ABS;
174 /* check GNUNET_TIME_calculate_eta */
175 last.abs_value_us = GNUNET_TIME_absolute_get ().abs_value_us - 1024;
176 forever = GNUNET_TIME_UNIT_FOREVER_ABS;
177 forever.abs_value_us = forever.abs_value_us - 1024;
178 GNUNET_assert (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us ==
179 GNUNET_TIME_calculate_eta (forever, 50000, 100000).rel_value_us);
181 GNUNET_log_skip (1, GNUNET_NO);
182 GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us ==
183 (GNUNET_TIME_calculate_eta (last, 60000, 50000)).rel_value_us);
184 GNUNET_log_skip (0, GNUNET_YES);
186 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
187 (GNUNET_TIME_calculate_eta (last, 0, 50000)).rel_value_us);
189 /*check relative subtract */
190 now = GNUNET_TIME_absolute_get ();
191 rel.rel_value_us = now.abs_value_us;
192 relForever.rel_value_us = rel.rel_value_us + 1024;
193 GNUNET_assert (1024 ==
194 GNUNET_TIME_relative_subtract (relForever, rel).rel_value_us);
196 GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us ==
197 GNUNET_TIME_relative_subtract (rel, relForever).rel_value_us);
199 rel.rel_value_us = UINT64_MAX;
200 GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
201 GNUNET_TIME_relative_subtract (rel, relForever).rel_value_us);
203 /*check GNUNET_TIME_relative_min */
204 now = GNUNET_TIME_absolute_get ();
205 rel.rel_value_us = now.abs_value_us;
206 relForever.rel_value_us = rel.rel_value_us - 1024;
207 GNUNET_assert (relForever.rel_value_us ==
208 GNUNET_TIME_relative_min (rel, relForever).rel_value_us);
210 /*check GNUNET_TIME_relative_max */
211 GNUNET_assert (rel.rel_value_us ==
212 GNUNET_TIME_relative_max (rel, relForever).rel_value_us);
214 /*check GNUNET_TIME_absolute_min */
215 now = GNUNET_TIME_absolute_get ();
216 last.abs_value_us = now.abs_value_us - 1024;
217 GNUNET_assert (last.abs_value_us ==
218 GNUNET_TIME_absolute_min (now, last).abs_value_us);
220 /*check GNUNET_TIME_absolute_max */
221 GNUNET_assert (now.abs_value_us ==
222 GNUNET_TIME_absolute_max (now, last).abs_value_us);
228 /* end of test_time.c */