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