2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2006, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file util/test_time.c
22 * @brief testcase for time.c
25 #include "gnunet_common.h"
26 #include "gnunet_time_lib.h"
28 #define VERBOSE GNUNET_NO
33 struct GNUNET_TIME_Absolute now;
34 struct GNUNET_TIME_AbsoluteNBO nown;
35 struct GNUNET_TIME_Absolute future;
36 struct GNUNET_TIME_Absolute past;
37 struct GNUNET_TIME_Absolute last;
38 struct GNUNET_TIME_Absolute forever;
39 struct GNUNET_TIME_Absolute zero;
40 struct GNUNET_TIME_Relative rel;
41 struct GNUNET_TIME_Relative relForever;
42 struct GNUNET_TIME_Relative relUnit;
43 struct GNUNET_TIME_RelativeNBO reln;
45 forever = GNUNET_TIME_absolute_get_forever ();
46 relForever = GNUNET_TIME_relative_get_forever ();
47 relUnit = GNUNET_TIME_relative_get_unit ();
50 last = now = GNUNET_TIME_absolute_get ();
51 while (now.value == last.value)
52 now = GNUNET_TIME_absolute_get ();
53 GNUNET_assert (now.value > last.value);
55 /* test overflow checking in multiply */
56 rel = GNUNET_TIME_UNIT_SECONDS;
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.value == GNUNET_TIME_UNIT_FOREVER_REL.value);
63 /* test infinity-check for relative to absolute */
64 last = GNUNET_TIME_relative_to_absolute (rel);
65 GNUNET_assert (last.value == GNUNET_TIME_UNIT_FOREVER_ABS.value);
67 /* check overflow for r2a */
68 rel.value = ((uint64_t) - 1LL) - 1024;
69 GNUNET_log_skip (1, GNUNET_NO);
70 last = GNUNET_TIME_relative_to_absolute (rel);
71 GNUNET_log_skip (0, GNUNET_NO);
72 GNUNET_assert (last.value == GNUNET_TIME_UNIT_FOREVER_ABS.value);
74 /* check overflow for relative add */
75 GNUNET_log_skip (1, GNUNET_NO);
76 rel = GNUNET_TIME_relative_add (rel, rel);
77 GNUNET_log_skip (0, GNUNET_NO);
78 GNUNET_assert (rel.value == GNUNET_TIME_UNIT_FOREVER_REL.value);
80 GNUNET_assert (GNUNET_TIME_relative_add (relForever, relForever).value ==
82 GNUNET_assert (GNUNET_TIME_relative_add (relUnit, relUnit).value ==
85 /* check relation check in get_duration */
86 future.value = now.value + 1000000;
87 GNUNET_assert (GNUNET_TIME_absolute_get_difference (now, future).value ==
89 GNUNET_assert (GNUNET_TIME_absolute_get_difference (future, now).value ==
92 GNUNET_assert (GNUNET_TIME_absolute_get_difference (zero, forever).value ==
95 past.value = now.value - 1000000;
96 rel = GNUNET_TIME_absolute_get_duration (future);
97 GNUNET_assert (rel.value == 0);
98 rel = GNUNET_TIME_absolute_get_duration (past);
99 GNUNET_assert (rel.value >= 1000000);
101 /* check get remaining */
102 rel = GNUNET_TIME_absolute_get_remaining (now);
103 GNUNET_assert (rel.value == 0);
104 rel = GNUNET_TIME_absolute_get_remaining (past);
105 GNUNET_assert (rel.value == 0);
106 rel = GNUNET_TIME_absolute_get_remaining (future);
107 GNUNET_assert (rel.value > 0);
108 GNUNET_assert (rel.value <= 1000000);
110 /* check endianess */
111 reln = GNUNET_TIME_relative_hton (rel);
112 GNUNET_assert (rel.value == GNUNET_TIME_relative_ntoh (reln).value);
113 nown = GNUNET_TIME_absolute_hton (now);
114 GNUNET_assert (now.value == GNUNET_TIME_absolute_ntoh (nown).value);
116 /* check absolute addition */
117 future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_SECONDS);
118 GNUNET_assert (future.value == now.value + 1000);
120 future = GNUNET_TIME_absolute_add (forever, GNUNET_TIME_UNIT_ZERO);
121 GNUNET_assert (future.value == forever.value);
123 rel.value = ((uint64_t) - 1LL) - 1024;
124 now.value = rel.value;
125 future = GNUNET_TIME_absolute_add (now, rel);
126 GNUNET_assert (future.value == forever.value);
129 future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_ZERO);
130 GNUNET_assert (future.value == now.value);
136 main (int argc, char *argv[])
140 GNUNET_log_setup ("test-time", "WARNING", NULL);
146 /* end of test_time.c */