use constants instead of casting -1
[oweals/gnunet.git] / src / util / test_time.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2006, 2009 Christian Grothoff (and other contributing authors)
4
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.
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      General Public License for more details.
14
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.
19 */
20 /**
21  * @file util/test_time.c
22  * @brief testcase for time.c
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_time_lib.h"
27
28 #define VERBOSE GNUNET_NO
29
30 static int
31 check ()
32 {
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;
44   unsigned int i;
45   forever = GNUNET_TIME_absolute_get_forever ();
46   relForever = GNUNET_TIME_relative_get_forever ();
47   relUnit = GNUNET_TIME_relative_get_unit ();
48   zero.value = 0;
49
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);
54
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);
62
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);
66
67   /* check overflow for r2a */
68   rel.value = (UINT64_MAX) - 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);
73
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);
79
80   GNUNET_log_skip (1, GNUNET_NO);
81   rel = GNUNET_TIME_relative_add (relForever, relForever);
82   GNUNET_log_skip (0, GNUNET_NO);
83   GNUNET_assert (rel.value == relForever.value);
84
85   GNUNET_log_skip (1, GNUNET_NO);
86   rel = GNUNET_TIME_relative_add (relUnit, relUnit);
87   GNUNET_assert (rel.value == 2 * relUnit.value);
88
89   /* check relation check in get_duration */
90   future.value = now.value + 1000000;
91   GNUNET_assert (GNUNET_TIME_absolute_get_difference (now, future).value ==
92                  1000000);
93   GNUNET_assert (GNUNET_TIME_absolute_get_difference (future, now).value ==
94                  0);
95
96   GNUNET_assert (GNUNET_TIME_absolute_get_difference (zero, forever).value ==
97                  forever.value);
98
99   past.value = now.value - 1000000;
100   rel = GNUNET_TIME_absolute_get_duration (future);
101   GNUNET_assert (rel.value == 0);
102   rel = GNUNET_TIME_absolute_get_duration (past);
103   GNUNET_assert (rel.value >= 1000000);
104
105   /* check get remaining */
106   rel = GNUNET_TIME_absolute_get_remaining (now);
107   GNUNET_assert (rel.value == 0);
108   rel = GNUNET_TIME_absolute_get_remaining (past);
109   GNUNET_assert (rel.value == 0);
110   rel = GNUNET_TIME_absolute_get_remaining (future);
111   GNUNET_assert (rel.value > 0);
112   GNUNET_assert (rel.value <= 1000000);
113
114   /* check endianess */
115   reln = GNUNET_TIME_relative_hton (rel);
116   GNUNET_assert (rel.value == GNUNET_TIME_relative_ntoh (reln).value);
117   nown = GNUNET_TIME_absolute_hton (now);
118   GNUNET_assert (now.value == GNUNET_TIME_absolute_ntoh (nown).value);
119
120   /* check absolute addition */
121   future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_SECONDS);
122   GNUNET_assert (future.value == now.value + 1000);
123
124   future = GNUNET_TIME_absolute_add (forever, GNUNET_TIME_UNIT_ZERO);
125   GNUNET_assert (future.value == forever.value);
126
127   rel.value = (UINT64_MAX) - 1024;
128   now.value = rel.value;
129   future = GNUNET_TIME_absolute_add (now, rel);
130   GNUNET_assert (future.value == forever.value);
131
132   /* check zero */
133   future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_ZERO);
134   GNUNET_assert (future.value == now.value);
135
136   return 0;
137 }
138
139 int
140 main (int argc, char *argv[])
141 {
142   int ret;
143
144   GNUNET_log_setup ("test-time", "WARNING", NULL);
145   ret = check ();
146
147   return ret;
148 }
149
150 /* end of test_time.c */