2 * collectd - src/common_test.c
4 * Copyright (C) 2013 Florian octo Forster
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 * Florian octo Forster <octo at collectd.org>
30 static int fail_count = 0;
31 static int check_count = 0;
33 #define TEST(func) do { \
35 printf ("Testing %s ...\n", #func); \
36 status = test_ ## func (); \
37 printf ("%s.\n", (status == 0) ? "Success" : "FAILURE"); \
38 if (status != 0) { fail_count++; } \
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); } \
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); \
53 printf ("ok %i - %s evaluates to \"%s\"\n", ++check_count, #actual, expect); \
56 #define OK(cond) OK1(cond, #cond)
58 static int test_sstrncpy (void)
61 char *ptr = &buffer[4];
64 buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0xff;
65 buffer[12] = buffer[13] = buffer[14] = buffer[15] = 0xff;
67 ret = sstrncpy (ptr, "foobar", 8);
69 STREQ ("foobar", ptr);
70 OK(buffer[3] == buffer[12]);
72 ret = sstrncpy (ptr, "abc", 8);
75 OK(buffer[3] == buffer[12]);
77 ret = sstrncpy (ptr, "collectd", 8);
80 STREQ ("collect", ptr);
81 OK(buffer[3] == buffer[12]);
86 static int test_ssnprintf (void)
89 char *ptr = &buffer[4];
92 buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0xff;
93 buffer[12] = buffer[13] = buffer[14] = buffer[15] = 0xff;
95 status = ssnprintf (ptr, 8, "%i", 1337);
99 status = ssnprintf (ptr, 8, "%s", "collectd");
102 STREQ ("collect", ptr);
103 OK(buffer[3] == buffer[12]);
108 static int test_sstrdup (void)
112 ptr = sstrdup ("collectd");
114 STREQ ("collectd", ptr);
119 ptr = sstrdup (NULL);
125 static int test_strsplit (void)
131 strncpy (buffer, "foo bar", sizeof (buffer));
132 status = strsplit (buffer, fields, 8);
134 STREQ ("foo", fields[0]);
135 STREQ ("bar", fields[1]);
137 strncpy (buffer, "foo \t bar", sizeof (buffer));
138 status = strsplit (buffer, fields, 8);
140 STREQ ("foo", fields[0]);
141 STREQ ("bar", fields[1]);
143 strncpy (buffer, "one two\tthree\rfour\nfive", sizeof (buffer));
144 status = strsplit (buffer, fields, 8);
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]);
152 strncpy (buffer, "\twith trailing\n", sizeof (buffer));
153 status = strsplit (buffer, fields, 8);
155 STREQ ("with", fields[0]);
156 STREQ ("trailing", fields[1]);
158 strncpy (buffer, "1 2 3 4 5 6 7 8 9 10 11 12 13", sizeof (buffer));
159 status = strsplit (buffer, fields, 8);
161 STREQ ("7", fields[6]);
162 STREQ ("8", fields[7]);
164 strncpy (buffer, "single", sizeof (buffer));
165 status = strsplit (buffer, fields, 8);
167 STREQ ("single", fields[0]);
169 strncpy (buffer, "", sizeof (buffer));
170 status = strsplit (buffer, fields, 8);
176 int test_strjoin (void)
187 status = strjoin (buffer, sizeof (buffer), fields, 2, "!");
189 STREQ ("foo!bar", buffer);
191 status = strjoin (buffer, sizeof (buffer), fields, 1, "!");
193 STREQ ("foo", buffer);
195 status = strjoin (buffer, sizeof (buffer), fields, 0, "!");
198 status = strjoin (buffer, sizeof (buffer), fields, 2, "rcht");
200 STREQ ("foorchtbar", buffer);
202 status = strjoin (buffer, sizeof (buffer), fields, 4, "");
204 STREQ ("foobarbazqux", buffer);
206 status = strjoin (buffer, sizeof (buffer), fields, 4, "!");
208 STREQ ("foo!bar!baz!qux", buffer);
214 status = strjoin (buffer, sizeof (buffer), fields, 4, "-");
220 static int test_strunescape ()
225 strncpy (buffer, "foo\\tbar", sizeof (buffer));
226 status = strunescape (buffer, sizeof (buffer));
228 STREQ ("foo\tbar", buffer);
230 strncpy (buffer, "\\tfoo\\r\\n", sizeof (buffer));
231 status = strunescape (buffer, sizeof (buffer));
233 STREQ ("\tfoo\r\n", buffer);
235 strncpy (buffer, "With \\\"quotes\\\"", sizeof (buffer));
236 status = strunescape (buffer, sizeof (buffer));
238 STREQ ("With \"quotes\"", buffer);
240 /* Backslash before null byte */
241 strncpy (buffer, "\\tbackslash end\\", sizeof (buffer));
242 status = strunescape (buffer, sizeof (buffer));
244 STREQ ("\tbackslash end", buffer);
247 /* Backslash at buffer end */
248 strncpy (buffer, "\\t3\\56", sizeof (buffer));
249 status = strunescape (buffer, 4);
251 OK(buffer[0] == '\t');
252 OK(buffer[1] == '3');
255 OK(buffer[4] == '5');
256 OK(buffer[5] == '6');
257 OK(buffer[6] == '7');
276 /* vim: set sw=2 sts=2 et : */