utils_vl_lookup_test: Fix dependencies.
[collectd.git] / src / common_test.c
1 /**
2  * collectd - src/common_test.c
3  *
4  * Copyright (C) 2013       Florian octo Forster
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *   Florian octo Forster <octo at collectd.org>
26  */
27
28 #include "common.h"
29
30 static int fail_count = 0;
31 static int check_count = 0;
32
33 #define TEST(func) do { \
34   int status; \
35   printf ("Testing %s ...\n", #func); \
36   status = test_ ## func (); \
37   printf ("%s.\n", (status == 0) ? "Success" : "FAILURE"); \
38   if (status != 0) { fail_count++; } \
39 } while (0);
40
41 #define OK1(cond, text) do { \
42   _Bool result = (cond); \
43   printf ("%s %i - %s\n", result ? "ok" : "not ok", ++check_count, text); \
44   if (!result) { return (-1); } \
45 } while (0);
46
47 #define STREQ(expect, actual) do { \
48   if (strcmp (expect, actual) != 0) { \
49     printf ("not ok %i - %s incorrect: expected \"%s\", got \"%s\"\n", \
50         ++check_count, #actual, expect, actual); \
51     return (-1); \
52   } \
53   printf ("ok %i - %s evaluates to \"%s\"\n", ++check_count, #actual, expect); \
54 } while (0)
55
56 #define OK(cond) OK1(cond, #cond)
57
58 static int test_sstrncpy (void)
59 {
60   char buffer[16] = "";
61   char *ptr = &buffer[4];
62   char *ret;
63
64   buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0xff;
65   buffer[12] = buffer[13] = buffer[14] = buffer[15] = 0xff;
66
67   ret = sstrncpy (ptr, "foobar", 8);
68   OK(ret == ptr);
69   STREQ ("foobar", ptr);
70   OK(buffer[3] == buffer[12]);
71
72   ret = sstrncpy (ptr, "abc", 8);
73   OK(ret == ptr);
74   STREQ ("abc", ptr);
75   OK(buffer[3] == buffer[12]);
76
77   ret = sstrncpy (ptr, "collectd", 8);
78   OK(ret == ptr);
79   OK(ptr[7] == 0);
80   STREQ ("collect", ptr);
81   OK(buffer[3] == buffer[12]);
82
83   return (0);
84 }
85
86 static int test_ssnprintf (void)
87 {
88   char buffer[16] = "";
89   char *ptr = &buffer[4];
90   int status;
91
92   buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0xff;
93   buffer[12] = buffer[13] = buffer[14] = buffer[15] = 0xff;
94
95   status = ssnprintf (ptr, 8, "%i", 1337);
96   OK(status == 4);
97   STREQ ("1337", ptr);
98
99   status = ssnprintf (ptr, 8, "%s", "collectd");
100   OK(status == 8);
101   OK(ptr[7] == 0);
102   STREQ ("collect", ptr);
103   OK(buffer[3] == buffer[12]);
104
105   return (0);
106 }
107
108 static int test_sstrdup (void)
109 {
110   char *ptr;
111
112   ptr = sstrdup ("collectd");
113   OK(ptr != NULL);
114   STREQ ("collectd", ptr);
115
116   sfree(ptr);
117   OK(ptr == NULL);
118
119   ptr = sstrdup (NULL);
120   OK(ptr == NULL);
121
122   return (0);
123 }
124
125 static int test_strsplit (void)
126 {
127   char buffer[32];
128   char *fields[8];
129   int status;
130
131   strncpy (buffer, "foo bar", sizeof (buffer));
132   status = strsplit (buffer, fields, 8);
133   OK(status == 2);
134   STREQ ("foo", fields[0]);
135   STREQ ("bar", fields[1]);
136
137   strncpy (buffer, "foo \t bar", sizeof (buffer));
138   status = strsplit (buffer, fields, 8);
139   OK(status == 2);
140   STREQ ("foo", fields[0]);
141   STREQ ("bar", fields[1]);
142
143   strncpy (buffer, "one two\tthree\rfour\nfive", sizeof (buffer));
144   status = strsplit (buffer, fields, 8);
145   OK(status == 5);
146   STREQ ("one", fields[0]);
147   STREQ ("two", fields[1]);
148   STREQ ("three", fields[2]);
149   STREQ ("four", fields[3]);
150   STREQ ("five", fields[4]);
151
152   strncpy (buffer, "\twith trailing\n", sizeof (buffer));
153   status = strsplit (buffer, fields, 8);
154   OK(status == 2);
155   STREQ ("with", fields[0]);
156   STREQ ("trailing", fields[1]);
157
158   strncpy (buffer, "1 2 3 4 5 6 7 8 9 10 11 12 13", sizeof (buffer));
159   status = strsplit (buffer, fields, 8);
160   OK(status == 8);
161   STREQ ("7", fields[6]);
162   STREQ ("8", fields[7]);
163
164   strncpy (buffer, "single", sizeof (buffer));
165   status = strsplit (buffer, fields, 8);
166   OK(status == 1);
167   STREQ ("single", fields[0]);
168
169   strncpy (buffer, "", sizeof (buffer));
170   status = strsplit (buffer, fields, 8);
171   OK(status == 0);
172
173   return (0);
174 }
175
176 int test_strjoin (void)
177 {
178   char buffer[16];
179   char *fields[4];
180   int status;
181
182   fields[0] = "foo";
183   fields[1] = "bar";
184   fields[2] = "baz";
185   fields[3] = "qux";
186
187   status = strjoin (buffer, sizeof (buffer), fields, 2, "!");
188   OK(status == 7);
189   STREQ ("foo!bar", buffer);
190
191   status = strjoin (buffer, sizeof (buffer), fields, 1, "!");
192   OK(status == 3);
193   STREQ ("foo", buffer);
194
195   status = strjoin (buffer, sizeof (buffer), fields, 0, "!");
196   OK(status < 0);
197
198   status = strjoin (buffer, sizeof (buffer), fields, 2, "rcht");
199   OK(status == 10);
200   STREQ ("foorchtbar", buffer);
201
202   status = strjoin (buffer, sizeof (buffer), fields, 4, "");
203   OK(status == 12);
204   STREQ ("foobarbazqux", buffer);
205
206   status = strjoin (buffer, sizeof (buffer), fields, 4, "!");
207   OK(status == 15);
208   STREQ ("foo!bar!baz!qux", buffer);
209
210   fields[0] = "0123";
211   fields[1] = "4567";
212   fields[2] = "8901";
213   fields[3] = "2345";
214   status = strjoin (buffer, sizeof (buffer), fields, 4, "-");
215   OK(status < 0);
216
217   return (0);
218 }
219
220 static int test_strunescape ()
221 {
222   char buffer[16];
223   int status;
224
225   strncpy (buffer, "foo\\tbar", sizeof (buffer));
226   status = strunescape (buffer, sizeof (buffer));
227   OK(status == 0);
228   STREQ ("foo\tbar", buffer);
229
230   strncpy (buffer, "\\tfoo\\r\\n", sizeof (buffer));
231   status = strunescape (buffer, sizeof (buffer));
232   OK(status == 0);
233   STREQ ("\tfoo\r\n", buffer);
234
235   strncpy (buffer, "With \\\"quotes\\\"", sizeof (buffer));
236   status = strunescape (buffer, sizeof (buffer));
237   OK(status == 0);
238   STREQ ("With \"quotes\"", buffer);
239
240   /* Backslash before null byte */
241   strncpy (buffer, "\\tbackslash end\\", sizeof (buffer));
242   status = strunescape (buffer, sizeof (buffer));
243   OK(status != 0);
244   STREQ ("\tbackslash end", buffer);
245   return (0);
246
247   /* Backslash at buffer end */
248   strncpy (buffer, "\\t3\\56", sizeof (buffer));
249   status = strunescape (buffer, 4);
250   OK(status != 0);
251   OK(buffer[0] == '\t');
252   OK(buffer[1] == '3');
253   OK(buffer[2] == 0);
254   OK(buffer[3] == 0);
255   OK(buffer[4] == '5');
256   OK(buffer[5] == '6');
257   OK(buffer[6] == '7');
258
259   return (0);
260 }
261
262 int main (void)
263 {
264   TEST(sstrncpy);
265   TEST(ssnprintf);
266   TEST(sstrdup);
267   TEST(strsplit);
268   TEST(strjoin);
269   TEST(strunescape);
270
271   if (fail_count != 0)
272     exit (EXIT_FAILURE);
273   exit (EXIT_SUCCESS);
274 }
275
276 /* vim: set sw=2 sts=2 et : */