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_Relative rel;
39 struct GNUNET_TIME_RelativeNBO reln;
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);
47 /* test overflow checking in multiply */
48 rel = GNUNET_TIME_UNIT_SECONDS;
49 GNUNET_log_skip (1, GNUNET_NO);
50 for (i = 0; i < 55; i++)
51 rel = GNUNET_TIME_relative_multiply (rel, 2);
52 GNUNET_log_skip (0, GNUNET_NO);
53 GNUNET_assert (rel.value == GNUNET_TIME_UNIT_FOREVER_REL.value);
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);
59 /* check overflow for r2a */
60 rel.value = ((uint64_t) - 1LL) - 1024;
61 GNUNET_log_skip (1, GNUNET_NO);
62 last = GNUNET_TIME_relative_to_absolute (rel);
63 GNUNET_log_skip (0, GNUNET_NO);
64 GNUNET_assert (last.value == GNUNET_TIME_UNIT_FOREVER_ABS.value);
66 /* check overflow for relative add */
67 GNUNET_log_skip (1, GNUNET_NO);
68 rel = GNUNET_TIME_relative_add (rel, rel);
69 GNUNET_log_skip (0, GNUNET_NO);
70 GNUNET_assert (rel.value == GNUNET_TIME_UNIT_FOREVER_REL.value);
72 /* check relation check in get_duration */
73 future.value = now.value + 1000000;
74 GNUNET_assert (GNUNET_TIME_absolute_get_difference (now, future).value ==
76 GNUNET_assert (GNUNET_TIME_absolute_get_difference (future, now).value ==
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);
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);
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);
100 /* check absolute addition */
101 future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_SECONDS);
102 GNUNET_assert (future.value == now.value + 1000);
105 future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_ZERO);
106 GNUNET_assert (future.value == now.value);
112 main (int argc, char *argv[])
116 GNUNET_log_setup ("test-time", "WARNING", NULL);
122 /* end of test_time.c */