2 * collectd - src/tests/test_common.c
3 * Copyright (C) 2013 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
33 char *ptr = &buffer[4];
36 buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0xff;
37 buffer[12] = buffer[13] = buffer[14] = buffer[15] = 0xff;
39 ret = sstrncpy (ptr, "foobar", 8);
41 STREQ ("foobar", ptr);
42 OK(buffer[3] == buffer[12]);
44 ret = sstrncpy (ptr, "abc", 8);
47 OK(buffer[3] == buffer[12]);
49 ret = sstrncpy (ptr, "collectd", 8);
52 STREQ ("collect", ptr);
53 OK(buffer[3] == buffer[12]);
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 status = ssnprintf (ptr, 8, "%i", 1337);
71 status = ssnprintf (ptr, 8, "%s", "collectd");
74 STREQ ("collect", ptr);
75 OK(buffer[3] == buffer[12]);
84 ptr = sstrdup ("collectd");
86 STREQ ("collectd", ptr);
103 strncpy (buffer, "foo bar", sizeof (buffer));
104 status = strsplit (buffer, fields, 8);
106 STREQ ("foo", fields[0]);
107 STREQ ("bar", fields[1]);
109 strncpy (buffer, "foo \t bar", sizeof (buffer));
110 status = strsplit (buffer, fields, 8);
112 STREQ ("foo", fields[0]);
113 STREQ ("bar", fields[1]);
115 strncpy (buffer, "one two\tthree\rfour\nfive", sizeof (buffer));
116 status = strsplit (buffer, fields, 8);
118 STREQ ("one", fields[0]);
119 STREQ ("two", fields[1]);
120 STREQ ("three", fields[2]);
121 STREQ ("four", fields[3]);
122 STREQ ("five", fields[4]);
124 strncpy (buffer, "\twith trailing\n", sizeof (buffer));
125 status = strsplit (buffer, fields, 8);
127 STREQ ("with", fields[0]);
128 STREQ ("trailing", fields[1]);
130 strncpy (buffer, "1 2 3 4 5 6 7 8 9 10 11 12 13", sizeof (buffer));
131 status = strsplit (buffer, fields, 8);
133 STREQ ("7", fields[6]);
134 STREQ ("8", fields[7]);
136 strncpy (buffer, "single", sizeof (buffer));
137 status = strsplit (buffer, fields, 8);
139 STREQ ("single", fields[0]);
141 strncpy (buffer, "", sizeof (buffer));
142 status = strsplit (buffer, fields, 8);
159 status = strjoin (buffer, sizeof (buffer), fields, 2, "!");
161 STREQ ("foo!bar", buffer);
163 status = strjoin (buffer, sizeof (buffer), fields, 1, "!");
165 STREQ ("foo", buffer);
167 status = strjoin (buffer, sizeof (buffer), fields, 0, "!");
170 status = strjoin (buffer, sizeof (buffer), fields, 2, "rcht");
172 STREQ ("foorchtbar", buffer);
174 status = strjoin (buffer, sizeof (buffer), fields, 4, "");
176 STREQ ("foobarbazqux", buffer);
178 status = strjoin (buffer, sizeof (buffer), fields, 4, "!");
180 STREQ ("foo!bar!baz!qux", buffer);
186 status = strjoin (buffer, sizeof (buffer), fields, 4, "-");
192 DEF_TEST(strunescape)
197 strncpy (buffer, "foo\\tbar", sizeof (buffer));
198 status = strunescape (buffer, sizeof (buffer));
200 STREQ ("foo\tbar", buffer);
202 strncpy (buffer, "\\tfoo\\r\\n", sizeof (buffer));
203 status = strunescape (buffer, sizeof (buffer));
205 STREQ ("\tfoo\r\n", buffer);
207 strncpy (buffer, "With \\\"quotes\\\"", sizeof (buffer));
208 status = strunescape (buffer, sizeof (buffer));
210 STREQ ("With \"quotes\"", buffer);
212 /* Backslash before null byte */
213 strncpy (buffer, "\\tbackslash end\\", sizeof (buffer));
214 status = strunescape (buffer, sizeof (buffer));
216 STREQ ("\tbackslash end", buffer);
219 /* Backslash at buffer end */
220 strncpy (buffer, "\\t3\\56", sizeof (buffer));
221 status = strunescape (buffer, 4);
223 OK(buffer[0] == '\t');
224 OK(buffer[1] == '3');
227 OK(buffer[4] == '5');
228 OK(buffer[5] == '6');
229 OK(buffer[6] == '7');
241 RUN_TEST(strunescape);
246 /* vim: set sw=2 sts=2 et : */