-remove async ecc key generation, not needed
[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 3, 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
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 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)
31 #define WANTB(a,b,l) if (0 != memcmp(a,b,l)) { GNUNET_break(0); return 1;} else { }
32
33 int
34 main (int argc, char *argv[])
35 {
36   char buf[128];
37   char *r;
38   char *b;
39   const char *bc;
40   struct GNUNET_TIME_Absolute at;
41   struct GNUNET_TIME_Absolute atx;
42   struct GNUNET_TIME_Relative rt;
43   struct GNUNET_TIME_Relative rtx;
44   const char *hdir;
45
46   GNUNET_log_setup ("test_strings", "ERROR", NULL);
47   sprintf (buf, "4 %s", _( /* size unit */ "b"));
48   b = GNUNET_STRINGS_byte_size_fancy (4);
49   WANT (buf, b);
50   sprintf (buf, "10 %s", _( /* size unit */ "KiB"));
51   b = GNUNET_STRINGS_byte_size_fancy (10240);
52   WANT (buf, b);
53   sprintf (buf, "10 %s", _( /* size unit */ "TiB"));
54   b = GNUNET_STRINGS_byte_size_fancy (10240LL * 1024LL * 1024LL * 1024LL);
55   WANT (buf, b);
56   sprintf (buf, "4 %s", _( /* time unit */ "ms"));
57   bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
58                                                (GNUNET_TIME_UNIT_MILLISECONDS,
59                                                 4), GNUNET_YES);
60   WANTNF (buf, bc);
61   sprintf (buf, "7 %s", _( /* time unit */ "s"));
62   bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
63                                                (GNUNET_TIME_UNIT_MILLISECONDS,
64                                                 7 * 1000), GNUNET_YES);
65   WANTNF (buf, bc);
66   sprintf (buf, "7 %s", _( /* time unit */ "h"));
67   bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
68                                               (GNUNET_TIME_UNIT_MILLISECONDS,
69                                                7 * 60 * 60 * 1000), GNUNET_YES);
70   WANTNF (buf, bc);
71 #ifndef MINGW
72   hdir = getenv ("HOME");
73 #else
74   hdir = getenv ("USERPROFILE");
75 #endif
76   GNUNET_snprintf (buf, sizeof (buf), "%s%s", hdir, DIR_SEPARATOR_STR);
77   b = GNUNET_STRINGS_filename_expand ("~");
78   GNUNET_assert (b != NULL);
79   WANT (buf, b);
80   GNUNET_STRINGS_buffer_fill (buf, sizeof (buf), 3, "a", "btx", "c");
81   WANTB ("a\0btx\0c", buf, 8);
82   if (6 != GNUNET_STRINGS_buffer_tokenize (buf, sizeof (buf), 2, &r, &b))
83     return 1;
84   r = GNUNET_strdup (r);
85   WANT ("a", r);
86   b = GNUNET_strdup (b);
87   WANT ("btx", b);
88   if (0 != GNUNET_STRINGS_buffer_tokenize (buf, 2, 2, &r, &b))
89     return 1;
90   at.abs_value = 5000;
91   bc = GNUNET_STRINGS_absolute_time_to_string (at);
92   /* bc should be something like "Wed Dec 31 17:00:05 1969"
93    * where the details of the day and hour depend on the timezone;
94    * however, the "0:05 19" should always be there; hence: */
95   if (NULL == strstr (bc, "0:05 19"))
96   {
97     FPRINTF (stderr, "Got %s\n", bc);
98     GNUNET_break (0);
99     return 1;
100   }
101   b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "ASCII");
102   WANT ("TEST", b);
103   
104   at = GNUNET_TIME_UNIT_FOREVER_ABS;
105   bc = GNUNET_STRINGS_absolute_time_to_string (at);
106   GNUNET_assert (GNUNET_OK ==
107                  GNUNET_STRINGS_fancy_time_to_absolute (bc, &atx));
108   GNUNET_assert (atx.abs_value == at.abs_value);
109
110   GNUNET_log_skip (2, GNUNET_NO);
111   b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "unknown");
112   GNUNET_log_skip (0, GNUNET_YES);
113   WANT ("TEST", b);
114
115   GNUNET_assert (GNUNET_OK ==
116       GNUNET_STRINGS_fancy_time_to_relative ("15m", &rt));
117   GNUNET_assert (GNUNET_OK ==
118       GNUNET_STRINGS_fancy_time_to_relative ("15 m", &rtx));
119   GNUNET_assert (rt.rel_value == rtx.rel_value);
120
121   return 0;
122 }
123
124
125 /* end of test_strings.c */