complete Test_time.c
[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_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);
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_assert (GNUNET_TIME_relative_add (relForever,relForever).value == relForever.value);
81   GNUNET_assert (GNUNET_TIME_relative_add (relUnit,relUnit).value == 2*relUnit.value);
82
83   /* check relation check in get_duration */
84   future.value = now.value + 1000000;
85   GNUNET_assert (GNUNET_TIME_absolute_get_difference (now, future).value ==
86                  1000000);
87   GNUNET_assert (GNUNET_TIME_absolute_get_difference (future, now).value ==
88                  0);
89
90   GNUNET_assert (GNUNET_TIME_absolute_get_difference (zero, forever).value == forever.value);
91
92   past.value = now.value - 1000000;
93   rel = GNUNET_TIME_absolute_get_duration (future);
94   GNUNET_assert (rel.value == 0);
95   rel = GNUNET_TIME_absolute_get_duration (past);
96   GNUNET_assert (rel.value >= 1000000);
97
98   /* check get remaining */
99   rel = GNUNET_TIME_absolute_get_remaining (now);
100   GNUNET_assert (rel.value == 0);
101   rel = GNUNET_TIME_absolute_get_remaining (past);
102   GNUNET_assert (rel.value == 0);
103   rel = GNUNET_TIME_absolute_get_remaining (future);
104   GNUNET_assert (rel.value > 0);
105   GNUNET_assert (rel.value <= 1000000);
106
107   /* check endianess */
108   reln = GNUNET_TIME_relative_hton (rel);
109   GNUNET_assert (rel.value == GNUNET_TIME_relative_ntoh (reln).value);
110   nown = GNUNET_TIME_absolute_hton (now);
111   GNUNET_assert (now.value == GNUNET_TIME_absolute_ntoh (nown).value);
112
113   /* check absolute addition */
114   future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_SECONDS);
115   GNUNET_assert (future.value == now.value + 1000);
116
117   future = GNUNET_TIME_absolute_add (forever,GNUNET_TIME_UNIT_ZERO);
118   GNUNET_assert (future.value == forever.value);
119
120   rel.value = ((uint64_t) - 1LL) - 1024;
121   now.value = rel.value;
122   future = GNUNET_TIME_absolute_add (now,rel);
123   GNUNET_assert (future.value == forever.value);
124
125   /* check zero */
126   future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_ZERO);
127   GNUNET_assert (future.value == now.value);
128
129   return 0;
130 }
131
132 int
133 main (int argc, char *argv[])
134 {
135   int ret;
136
137   GNUNET_log_setup ("test-time", "WARNING", NULL);
138   ret = check ();
139
140   return ret;
141 }
142
143 /* end of test_time.c */