2 This file is part of GNUnet.
3 (C) 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 3, 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_strings.c
22 * @brief testcase for strings.c
25 #include "gnunet_common.h"
26 #include "gnunet_strings_lib.h"
29 #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); }
30 #define WANTB(a,b,l) if (0 != memcmp(a,b,l)) { GNUNET_break(0); return 1;} else { }
33 main (int argc, char *argv[])
38 struct GNUNET_TIME_Absolute at;
39 struct GNUNET_TIME_Absolute atx;
42 GNUNET_log_setup ("test_strings", "ERROR", NULL);
43 sprintf (buf, "4 %s", _( /* size unit */ "b"));
44 b = GNUNET_STRINGS_byte_size_fancy (4);
46 sprintf (buf, "10 %s", _( /* size unit */ "KiB"));
47 b = GNUNET_STRINGS_byte_size_fancy (10240);
49 sprintf (buf, "10 %s", _( /* size unit */ "TiB"));
50 b = GNUNET_STRINGS_byte_size_fancy (10240LL * 1024LL * 1024LL * 1024LL);
52 sprintf (buf, "4 %s", _( /* time unit */ "ms"));
53 b = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
54 (GNUNET_TIME_UNIT_MILLISECONDS,
57 sprintf (buf, "7 %s", _( /* time unit */ "s"));
58 b = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
59 (GNUNET_TIME_UNIT_MILLISECONDS,
62 sprintf (buf, "7 %s", _( /* time unit */ "h"));
63 b = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
64 (GNUNET_TIME_UNIT_MILLISECONDS,
68 hdir = getenv ("HOME");
70 hdir = getenv ("USERPROFILE");
72 GNUNET_snprintf (buf, sizeof (buf), "%s%s", hdir, DIR_SEPARATOR_STR);
73 b = GNUNET_STRINGS_filename_expand ("~");
74 GNUNET_assert (b != NULL);
76 GNUNET_STRINGS_buffer_fill (buf, sizeof (buf), 3, "a", "btx", "c");
77 WANTB ("a\0btx\0c", buf, 8);
78 if (6 != GNUNET_STRINGS_buffer_tokenize (buf, sizeof (buf), 2, &r, &b))
80 r = GNUNET_strdup (r);
82 b = GNUNET_strdup (b);
84 if (0 != GNUNET_STRINGS_buffer_tokenize (buf, 2, 2, &r, &b))
87 r = GNUNET_STRINGS_absolute_time_to_string (at);
88 /* r should be something like "Wed Dec 31 17:00:05 1969"
89 * where the details of the day and hour depend on the timezone;
90 * however, the "0:05 19" should always be there; hence: */
91 if (NULL == strstr (r, "0:05 19"))
93 FPRINTF (stderr, "Got %s\n", r);
99 b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "ASCII");
102 at = GNUNET_TIME_UNIT_FOREVER_ABS;
103 b = GNUNET_STRINGS_absolute_time_to_string (at);
104 GNUNET_assert (GNUNET_OK ==
105 GNUNET_STRINGS_fancy_time_to_absolute (b, &atx));
106 GNUNET_assert (atx.abs_value == at.abs_value);
109 #if ENABLE_NLS && HAVE_ICONV
110 GNUNET_log_skip (2, GNUNET_NO);
111 b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "unknown");
112 GNUNET_log_skip (0, GNUNET_YES);
119 /* end of test_strings.c */