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