ng
[oweals/gnunet.git] / src / util / test_strings.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
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 2, or (at your
8      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      General Public License for more details.
14
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.
19 */
20 /**
21  * @file util/test_strings.c
22  * @brief testcase for strings.c
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_strings_lib.h"
27
28 #define VERBOSE GNUNET_NO
29
30 #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); }
31 #define WANTB(a,b,l) if (0 != memcmp(a,b,l)) { GNUNET_break(0); return 1;} else { }
32
33 static int
34 check ()
35 {
36   char buf[128];
37   char *r;
38   char *b;
39   struct GNUNET_TIME_Absolute at;
40
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   b =
52     GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
53                                             (GNUNET_TIME_UNIT_MILLISECONDS,
54                                              4));
55   WANT (buf, b);
56   sprintf (buf, "7%s", _( /* time unit */ "s"));
57   b =
58     GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
59                                             (GNUNET_TIME_UNIT_MILLISECONDS,
60                                              7 * 1000));
61   WANT (buf, b);
62   sprintf (buf, "7%s", _( /* time unit */ "h"));
63   b =
64     GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
65                                             (GNUNET_TIME_UNIT_MILLISECONDS,
66                                              7 * 60 * 60 * 1000));
67   WANT (buf, b);
68   sprintf (buf, "%s%s", getenv ("HOME"), DIR_SEPARATOR_STR);
69   b = GNUNET_STRINGS_filename_expand ("~");
70   WANT (buf, b);
71   GNUNET_STRINGS_buffer_fill (buf, sizeof (buf), 3, "a", "btx", "c");
72   WANTB ("a\0btx\0c", buf, 8);
73   if (6 != GNUNET_STRINGS_buffer_tokenize (buf, sizeof (buf), 2, &r, &b))
74     return 1;
75   r = GNUNET_strdup (r);
76   WANT ("a", r);
77   b = GNUNET_strdup (b);
78   WANT ("btx", b);
79   if (0 != GNUNET_STRINGS_buffer_tokenize (buf, 2, 2, &r, &b))
80     return 1;
81   at.value = 5000;
82   r = GNUNET_STRINGS_absolute_time_to_string (at);
83   /* r should be something like "Wed Dec 31 17:00:05 1969"
84      where the details of the day and hour depend on the timezone;
85      however, the "0:05 19" should always be there; hence: */
86   if (NULL == strstr (r, "0:05 19"))
87     {
88       fprintf (stderr, "Got %s\n", r);
89       GNUNET_break (0);
90       GNUNET_free (r);
91       return 1;
92     }
93   b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "ASCII");
94   WANT ("TEST", b);
95   b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "unknown");
96   WANT ("TEST", b);
97   GNUNET_free (r);
98   return 0;
99 }
100
101 int
102 main (int argc, char *argv[])
103 {
104   int ret;
105
106   GNUNET_log_setup ("test_strings", "ERROR", NULL);
107   ret = check ();
108   return ret;
109 }
110
111 /* end of test_strings.c */