api work
[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_Relative rel;
39   struct GNUNET_TIME_RelativeNBO reln;
40   unsigned int i;
41
42   last = now = GNUNET_TIME_absolute_get ();
43   while (now.value == last.value)
44     now = GNUNET_TIME_absolute_get ();
45   GNUNET_assert (now.value > last.value);
46
47   /* test overflow checking in multiply */
48   rel = GNUNET_TIME_UNIT_SECONDS;
49   GNUNET_log_skip (1);
50   for (i = 0; i < 55; i++)
51     rel = GNUNET_TIME_relative_multiply (rel, 2);
52   GNUNET_log_skip (0);
53   GNUNET_assert (rel.value == GNUNET_TIME_UNIT_FOREVER_REL.value);
54
55   /* test infinity-check for relative to absolute */
56   last = GNUNET_TIME_relative_to_absolute (rel);
57   GNUNET_assert (last.value == GNUNET_TIME_UNIT_FOREVER_ABS.value);
58
59   /* check overflow for r2a */
60   rel.value = ((uint64_t) - 1LL) - 1024;
61   GNUNET_log_skip (1);
62   last = GNUNET_TIME_relative_to_absolute (rel);
63   GNUNET_log_skip (0);
64   GNUNET_assert (last.value == GNUNET_TIME_UNIT_FOREVER_ABS.value);
65
66   /* check overflow for relative add */
67   GNUNET_log_skip (1);
68   rel = GNUNET_TIME_relative_add (rel, rel);
69   GNUNET_log_skip (0);
70   GNUNET_assert (rel.value == GNUNET_TIME_UNIT_FOREVER_REL.value);
71
72   /* check relation check in get_duration */
73   future.value = now.value + 1000000;
74   GNUNET_assert (GNUNET_TIME_absolute_get_difference (now, future).value ==
75                  1000000);
76   GNUNET_assert (GNUNET_TIME_absolute_get_difference (future, now).value ==
77                  0);
78
79   past.value = now.value - 1000000;
80   rel = GNUNET_TIME_absolute_get_duration (future);
81   GNUNET_assert (rel.value == 0);
82   rel = GNUNET_TIME_absolute_get_duration (past);
83   GNUNET_assert (rel.value >= 1000000);
84
85   /* check get remaining */
86   rel = GNUNET_TIME_absolute_get_remaining (now);
87   GNUNET_assert (rel.value == 0);
88   rel = GNUNET_TIME_absolute_get_remaining (past);
89   GNUNET_assert (rel.value == 0);
90   rel = GNUNET_TIME_absolute_get_remaining (future);
91   GNUNET_assert (rel.value > 0);
92   GNUNET_assert (rel.value <= 1000000);
93
94   /* check endianess */
95   reln = GNUNET_TIME_relative_hton (rel);
96   GNUNET_assert (rel.value == GNUNET_TIME_relative_ntoh (reln).value);
97   nown = GNUNET_TIME_absolute_hton (now);
98   GNUNET_assert (now.value == GNUNET_TIME_absolute_ntoh (nown).value);
99
100   /* check absolute addition */
101   future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_SECONDS);
102   GNUNET_assert (future.value == now.value + 1000);
103
104   /* check zero */
105   future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_ZERO);
106   GNUNET_assert (future.value == now.value);
107
108   return 0;
109 }
110
111 int
112 main (int argc, char *argv[])
113 {
114   int ret;
115
116   GNUNET_log_setup ("test-time", "WARNING", NULL);
117   ret = check ();
118
119   return ret;
120 }
121
122 /* end of test_time.c */