-Merge branch 'master' of ssh://gnunet.org/gnunet into gsoc2018/rest_api
[oweals/gnunet.git] / src / util / test_time.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001-2013 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 /**
19  * @file util/test_time.c
20  * @brief testcase for time.c
21  */
22 #include "platform.h"
23 #include "gnunet_util_lib.h"
24
25
26 int
27 main (int argc, char *argv[])
28 {
29   struct GNUNET_TIME_Absolute now;
30   struct GNUNET_TIME_AbsoluteNBO nown;
31   struct GNUNET_TIME_Absolute future;
32   struct GNUNET_TIME_Absolute past;
33   struct GNUNET_TIME_Absolute last;
34   struct GNUNET_TIME_Absolute forever;
35   struct GNUNET_TIME_Absolute zero;
36   struct GNUNET_TIME_Relative rel;
37   struct GNUNET_TIME_Relative relForever;
38   struct GNUNET_TIME_Relative relUnit;
39   struct GNUNET_TIME_RelativeNBO reln;
40   unsigned int i;
41
42   GNUNET_log_setup ("test-time", "WARNING", NULL);
43   forever = GNUNET_TIME_UNIT_FOREVER_ABS;
44   relForever = GNUNET_TIME_UNIT_FOREVER_REL;
45   relUnit = GNUNET_TIME_UNIT_MILLISECONDS;
46   zero.abs_value_us = 0;
47
48   last = now = GNUNET_TIME_absolute_get ();
49   while (now.abs_value_us == last.abs_value_us)
50     now = GNUNET_TIME_absolute_get ();
51   GNUNET_assert (now.abs_value_us > last.abs_value_us);
52
53   /* test overflow checking in multiply */
54   rel = GNUNET_TIME_UNIT_MILLISECONDS;
55   GNUNET_log_skip (1, GNUNET_NO);
56   for (i = 0; i < 55; i++)
57     rel = GNUNET_TIME_relative_multiply (rel, 2);
58   GNUNET_log_skip (0, GNUNET_NO);
59   GNUNET_assert (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us);
60   /*check zero */
61   rel.rel_value_us = (UINT64_MAX) - 1024;
62   GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us ==
63                  GNUNET_TIME_relative_multiply (rel, 0).rel_value_us);
64
65   /* test infinity-check for relative to absolute */
66   GNUNET_log_skip (1, GNUNET_NO);
67   last = GNUNET_TIME_relative_to_absolute (rel);
68   GNUNET_assert (last.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us);
69   GNUNET_log_skip (0, GNUNET_YES);
70
71   /* check relative to absolute */
72   rel.rel_value_us = 1000000;
73   GNUNET_assert (GNUNET_TIME_absolute_get ().abs_value_us <
74                  GNUNET_TIME_relative_to_absolute (rel).abs_value_us);
75   /*check forever */
76   rel.rel_value_us = UINT64_MAX;
77   GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us ==
78                  GNUNET_TIME_relative_to_absolute (rel).abs_value_us);
79   /* check overflow for r2a */
80   rel.rel_value_us = (UINT64_MAX) - 1024;
81   GNUNET_log_skip (1, GNUNET_NO);
82   last = GNUNET_TIME_relative_to_absolute (rel);
83   GNUNET_log_skip (0, GNUNET_NO);
84   GNUNET_assert (last.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us);
85
86   /* check overflow for relative add */
87   GNUNET_log_skip (1, GNUNET_NO);
88   rel = GNUNET_TIME_relative_add (rel, rel);
89   GNUNET_log_skip (0, GNUNET_NO);
90   GNUNET_assert (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us);
91
92   GNUNET_log_skip (1, GNUNET_NO);
93   rel = GNUNET_TIME_relative_add (relForever, relForever);
94   GNUNET_log_skip (0, GNUNET_NO);
95   GNUNET_assert (rel.rel_value_us == relForever.rel_value_us);
96
97   GNUNET_log_skip (1, GNUNET_NO);
98   rel = GNUNET_TIME_relative_add (relUnit, relUnit);
99   GNUNET_assert (rel.rel_value_us == 2 * relUnit.rel_value_us);
100
101   /* check relation check in get_duration */
102   future.abs_value_us = now.abs_value_us + 1000000;
103   GNUNET_assert (GNUNET_TIME_absolute_get_difference (now, future).rel_value_us ==
104                  1000000);
105   GNUNET_assert (GNUNET_TIME_absolute_get_difference (future, now).rel_value_us ==
106                  0);
107
108   GNUNET_assert (GNUNET_TIME_absolute_get_difference (zero, forever).rel_value_us
109                  == forever.abs_value_us);
110
111   past.abs_value_us = now.abs_value_us - 1000000;
112   rel = GNUNET_TIME_absolute_get_duration (future);
113   GNUNET_assert (rel.rel_value_us == 0);
114   rel = GNUNET_TIME_absolute_get_duration (past);
115   GNUNET_assert (rel.rel_value_us >= 1000000);
116
117   /* check get remaining */
118   rel = GNUNET_TIME_absolute_get_remaining (now);
119   GNUNET_assert (rel.rel_value_us == 0);
120   rel = GNUNET_TIME_absolute_get_remaining (past);
121   GNUNET_assert (rel.rel_value_us == 0);
122   rel = GNUNET_TIME_absolute_get_remaining (future);
123   GNUNET_assert (rel.rel_value_us > 0);
124   GNUNET_assert (rel.rel_value_us <= 1000000);
125   forever = GNUNET_TIME_UNIT_FOREVER_ABS;
126   GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
127                  GNUNET_TIME_absolute_get_remaining (forever).rel_value_us);
128
129   /* check endianess */
130   reln = GNUNET_TIME_relative_hton (rel);
131   GNUNET_assert (rel.rel_value_us == GNUNET_TIME_relative_ntoh (reln).rel_value_us);
132   nown = GNUNET_TIME_absolute_hton (now);
133   GNUNET_assert (now.abs_value_us == GNUNET_TIME_absolute_ntoh (nown).abs_value_us);
134
135   /* check absolute addition */
136   future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_SECONDS);
137   GNUNET_assert (future.abs_value_us == now.abs_value_us + 1000 * 1000LL);
138
139   future = GNUNET_TIME_absolute_add (forever, GNUNET_TIME_UNIT_ZERO);
140   GNUNET_assert (future.abs_value_us == forever.abs_value_us);
141
142   rel.rel_value_us = (UINT64_MAX) - 1024;
143   now.abs_value_us = rel.rel_value_us;
144   future = GNUNET_TIME_absolute_add (now, rel);
145   GNUNET_assert (future.abs_value_us == forever.abs_value_us);
146
147   /* check zero */
148   future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_ZERO);
149   GNUNET_assert (future.abs_value_us == now.abs_value_us);
150
151   GNUNET_assert (forever.abs_value_us ==
152                  GNUNET_TIME_absolute_subtract (forever,
153                                                 GNUNET_TIME_UNIT_MINUTES).abs_value_us);
154   /*check absolute subtract */
155   now.abs_value_us = 50000;
156   rel.rel_value_us = 100000;
157   GNUNET_assert (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us ==
158                  (GNUNET_TIME_absolute_subtract (now, rel)).abs_value_us);
159   rel.rel_value_us = 10000;
160   GNUNET_assert (40000 == (GNUNET_TIME_absolute_subtract (now, rel)).abs_value_us);
161
162   /*check relative divide */
163   GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
164                  (GNUNET_TIME_relative_divide (rel, 0)).rel_value_us);
165
166   rel = GNUNET_TIME_UNIT_FOREVER_REL;
167   GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
168                  (GNUNET_TIME_relative_divide (rel, 2)).rel_value_us);
169
170   rel = GNUNET_TIME_relative_divide (relUnit, 2);
171   GNUNET_assert (rel.rel_value_us == relUnit.rel_value_us / 2);
172
173
174   /* check Return absolute time of 0ms */
175   zero = GNUNET_TIME_UNIT_ZERO_ABS;
176
177   /* check GNUNET_TIME_calculate_eta */
178   last.abs_value_us = GNUNET_TIME_absolute_get ().abs_value_us - 1024;
179   forever = GNUNET_TIME_UNIT_FOREVER_ABS;
180   forever.abs_value_us = forever.abs_value_us - 1024;
181   GNUNET_assert (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us ==
182                  GNUNET_TIME_calculate_eta (forever, 50000, 100000).rel_value_us);
183   /* check zero */
184   GNUNET_log_skip (1, GNUNET_NO);
185   GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us ==
186                  (GNUNET_TIME_calculate_eta (last, 60000, 50000)).rel_value_us);
187   GNUNET_log_skip (0, GNUNET_YES);
188   /*check forever */
189   GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
190                  (GNUNET_TIME_calculate_eta (last, 0, 50000)).rel_value_us);
191
192   /*check relative subtract */
193   now = GNUNET_TIME_absolute_get ();
194   rel.rel_value_us = now.abs_value_us;
195   relForever.rel_value_us = rel.rel_value_us + 1024;
196   GNUNET_assert (1024 ==
197                  GNUNET_TIME_relative_subtract (relForever, rel).rel_value_us);
198   /*check zero */
199   GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us ==
200                  GNUNET_TIME_relative_subtract (rel, relForever).rel_value_us);
201   /*check forever */
202   rel.rel_value_us = UINT64_MAX;
203   GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
204                  GNUNET_TIME_relative_subtract (rel, relForever).rel_value_us);
205
206   /*check GNUNET_TIME_relative_min */
207   now = GNUNET_TIME_absolute_get ();
208   rel.rel_value_us = now.abs_value_us;
209   relForever.rel_value_us = rel.rel_value_us - 1024;
210   GNUNET_assert (relForever.rel_value_us ==
211                  GNUNET_TIME_relative_min (rel, relForever).rel_value_us);
212
213   /*check GNUNET_TIME_relative_max */
214   GNUNET_assert (rel.rel_value_us ==
215                  GNUNET_TIME_relative_max (rel, relForever).rel_value_us);
216
217   /*check GNUNET_TIME_absolute_min */
218   now = GNUNET_TIME_absolute_get ();
219   last.abs_value_us = now.abs_value_us - 1024;
220   GNUNET_assert (last.abs_value_us ==
221                  GNUNET_TIME_absolute_min (now, last).abs_value_us);
222
223   /*check  GNUNET_TIME_absolute_max */
224   GNUNET_assert (now.abs_value_us ==
225                  GNUNET_TIME_absolute_max (now, last).abs_value_us);
226
227   return 0;
228 }
229
230
231 /* end of test_time.c */