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