glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / util / test_strings.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15 /**
16  * @file util/test_strings.c
17  * @brief testcase for strings.c
18  */
19 #include "platform.h"
20 #include "gnunet_util_lib.h"
21
22
23 #define WANT(a,b) if (0 != strcmp(a,b)) { fprintf(stderr, "Got `%s', wanted `%s'\n", b, a); GNUNET_free(b); GNUNET_break(0); return 1;} else { GNUNET_free (b); }
24 #define WANTNF(a,b) do { if (0 != strcmp(a,b)) { fprintf(stderr, "Got `%s', wanted `%s'\n", b, a); GNUNET_break(0); return 1;} } while (0)
25 #define WANTB(a,b,l) if (0 != memcmp(a,b,l)) { GNUNET_break(0); return 1;} else { }
26
27 int
28 main (int argc, char *argv[])
29 {
30   char buf[128];
31   char *r;
32   char *b;
33   const char *bc;
34   struct GNUNET_TIME_Absolute at;
35   struct GNUNET_TIME_Absolute atx;
36   struct GNUNET_TIME_Relative rt;
37   struct GNUNET_TIME_Relative rtx;
38   const char *hdir;
39
40   GNUNET_log_setup ("test_strings", "ERROR", NULL);
41   sprintf (buf, "4 %s", _( /* size unit */ "b"));
42   b = GNUNET_STRINGS_byte_size_fancy (4);
43   WANT (buf, b);
44   sprintf (buf, "10 %s", _( /* size unit */ "KiB"));
45   b = GNUNET_STRINGS_byte_size_fancy (10240);
46   WANT (buf, b);
47   sprintf (buf, "10 %s", _( /* size unit */ "TiB"));
48   b = GNUNET_STRINGS_byte_size_fancy (10240LL * 1024LL * 1024LL * 1024LL);
49   WANT (buf, b);
50   sprintf (buf, "4 %s", _( /* time unit */ "ms"));
51   bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
52                                                (GNUNET_TIME_UNIT_MILLISECONDS,
53                                                 4), GNUNET_YES);
54   WANTNF (buf, bc);
55   sprintf (buf, "7 %s", _( /* time unit */ "s"));
56   bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
57                                                (GNUNET_TIME_UNIT_MILLISECONDS,
58                                                 7 * 1000), GNUNET_YES);
59   WANTNF (buf, bc);
60   sprintf (buf, "7 %s", _( /* time unit */ "h"));
61   bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
62                                               (GNUNET_TIME_UNIT_MILLISECONDS,
63                                                7 * 60 * 60 * 1000), GNUNET_YES);
64   WANTNF (buf, bc);
65 #ifndef MINGW
66   hdir = getenv ("HOME");
67 #else
68   hdir = getenv ("USERPROFILE");
69 #endif
70   GNUNET_snprintf (buf, sizeof (buf), "%s%s", hdir, DIR_SEPARATOR_STR);
71   b = GNUNET_STRINGS_filename_expand ("~");
72   GNUNET_assert (b != NULL);
73   WANT (buf, b);
74   GNUNET_STRINGS_buffer_fill (buf, sizeof (buf), 3, "a", "btx", "c");
75   WANTB ("a\0btx\0c", buf, 8);
76   if (6 != GNUNET_STRINGS_buffer_tokenize (buf, sizeof (buf), 2, &r, &b))
77     return 1;
78   r = GNUNET_strdup (r);
79   WANT ("a", r);
80   b = GNUNET_strdup (b);
81   WANT ("btx", b);
82   if (0 != GNUNET_STRINGS_buffer_tokenize (buf, 2, 2, &r, &b))
83     return 1;
84   at.abs_value_us = 5000000;
85   bc = GNUNET_STRINGS_absolute_time_to_string (at);
86   /* bc should be something like "Wed Dec 31 17:00:05 1969"
87    * where the details of the day and hour depend on the timezone;
88    * however, the "0:05 19" should always be there; hence: */
89   if (NULL == strstr (bc, "0:05 19"))
90   {
91     FPRINTF (stderr, "Got %s\n", bc);
92     GNUNET_break (0);
93     return 1;
94   }
95   b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "ASCII");
96   WANT ("TEST", b);
97
98   at = GNUNET_TIME_UNIT_FOREVER_ABS;
99   bc = GNUNET_STRINGS_absolute_time_to_string (at);
100   GNUNET_assert (GNUNET_OK ==
101                  GNUNET_STRINGS_fancy_time_to_absolute (bc, &atx));
102   GNUNET_assert (atx.abs_value_us == at.abs_value_us);
103
104   at.abs_value_us = 50000000000;
105   bc = GNUNET_STRINGS_absolute_time_to_string (at);
106
107   GNUNET_assert (GNUNET_OK ==
108                  GNUNET_STRINGS_fancy_time_to_absolute (bc, &atx));
109
110   if (atx.abs_value_us != at.abs_value_us)
111   {
112 #ifdef WINDOWS
113     DWORD tzv;
114     TIME_ZONE_INFORMATION tzi;
115     tzv = GetTimeZoneInformation (&tzi);
116     if (TIME_ZONE_ID_INVALID != tzv)
117     {
118       atx.abs_value_us -= 1000LL * 1000LL * tzi.Bias * 60LL;
119     }
120     if (atx.abs_value_us == at.abs_value_us)
121       fprintf (stderr,
122                "WARNING:  GNUNET_STRINGS_fancy_time_to_absolute() miscalculates timezone!\n");
123 #endif
124     GNUNET_assert (0);
125   }
126
127   GNUNET_log_skip (2, GNUNET_NO);
128   b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "unknown");
129   GNUNET_log_skip (0, GNUNET_YES);
130   WANT ("TEST", b);
131
132   GNUNET_assert (GNUNET_OK ==
133       GNUNET_STRINGS_fancy_time_to_relative ("15m", &rt));
134   GNUNET_assert (GNUNET_OK ==
135       GNUNET_STRINGS_fancy_time_to_relative ("15 m", &rtx));
136   GNUNET_assert (rt.rel_value_us == rtx.rel_value_us);
137
138   return 0;
139 }
140
141
142 /* end of test_strings.c */