1 /*
2 * SysDB - t/utils/strbuf_test.c
3 * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #include "utils/strbuf.h"
29 #include "libsysdb_test.h"
31 #include <check.h>
33 /*
34 * private data types
35 */
37 static sdb_strbuf_t *buf;
39 static void
40 setup(void)
41 {
42 buf = sdb_strbuf_create(0);
43 fail_unless(buf != NULL,
44 "sdb_strbuf_create() = NULL; expected strbuf object");
45 } /* setup */
47 static void
48 teardown(void)
49 {
50 sdb_strbuf_destroy(buf);
51 buf = NULL;
52 } /* teardown */
54 /*
55 * tests
56 */
58 START_TEST(test_sdb_strbuf_create)
59 {
60 sdb_strbuf_t *s;
61 size_t len;
63 s = sdb_strbuf_create(0);
64 fail_unless(s != NULL,
65 "sdb_strbuf_create() = NULL; expected strbuf object");
66 len = sdb_strbuf_len(s);
67 fail_unless(len == 0,
68 "sdb_strbuf_create() created buffer with len = %zu; "
69 "expected: 0", len);
70 sdb_strbuf_destroy(s);
72 s = sdb_strbuf_create(128);
73 fail_unless(s != NULL,
74 "sdb_strbuf_create() = NULL; expected strbuf object");
75 len = sdb_strbuf_len(s);
76 /* len still has to be 0 -- there's no content */
77 fail_unless(len == 0,
78 "sdb_strbuf_create() created buffer with len = %zu; "
79 "expected: 0", len);
80 sdb_strbuf_destroy(s);
81 }
82 END_TEST
84 START_TEST(test_sdb_strbuf_append)
85 {
86 ssize_t n, expected;
87 size_t len;
88 const char *test;
90 n = sdb_strbuf_append(buf, "1234567890");
91 fail_unless(n == 10,
92 "sdb_strbuf_append() appended %zi bytes; expected: 10", n);
93 len = sdb_strbuf_len(buf);
94 fail_unless(len == 10,
95 "sdb_strbuf_append() left behind buffer with len = %zu; "
96 "expected: 10", len);
98 n = sdb_strbuf_append(buf, "ABCDE");
99 fail_unless(n == 5,
100 "sdb_strbuf_append() appended %zi bytes; expected: 5", n);
101 len = sdb_strbuf_len(buf);
102 fail_unless(len == 15,
103 "sdb_strbuf_append() left behind buffer with len = %zu; "
104 "expected: 15", len);
106 test = sdb_strbuf_string(buf);
107 fail_unless(test[len] == '\0',
108 "sdb_strbuf_append() did not nil-terminate the string");
109 fail_unless(!strcmp(test, "1234567890ABCDE"),
110 "sdb_strbuf_append() did not correctly concatenate two string; "
111 "got: %s; expected: 1234567890ABCDE", test);
113 n = sdb_strbuf_append(buf, "%zu; %5.4f", len, (double)len / 10.0);
114 expected = /* len */ 2 + /* "; " */ 2 + /* decimal len/10 */ 6;
115 fail_unless(n == expected,
116 "sdb_strbuf_append() appended %zi bytes; expected: %zi",
117 n, expected);
118 len = sdb_strbuf_len(buf);
119 fail_unless(len == 15 + (size_t)expected,
120 "sdb_strbuf_append() left behind buffer with len = %zu; "
121 "expected: %zu", len, 15 + (size_t)expected);
123 test = sdb_strbuf_string(buf);
124 fail_unless(test[len] == '\0',
125 "sdb_strbuf_append() did not nil-terminate the string");
126 fail_unless(!strcmp(test, "1234567890ABCDE15; 1.5000"),
127 "sdb_strbuf_append() did not correctly concatenate two string; "
128 "got: %s; expected: 1234567890ABCDE15; 1.5000", test);
129 }
130 END_TEST
132 START_TEST(test_sdb_strbuf_sprintf)
133 {
134 ssize_t n, expected;
135 size_t len;
136 const char *test;
138 n = sdb_strbuf_sprintf(buf, "1234567890");
139 fail_unless(n == 10,
140 "sdb_strbuf_sprintf() wrote %zi bytes; expected: 10", n);
141 len = sdb_strbuf_len(buf);
142 fail_unless(len == 10,
143 "sdb_strbuf_sprintf() left behind buffer with len = %zu; "
144 "expected: 10", len);
146 n = sdb_strbuf_sprintf(buf, "ABCDE");
147 fail_unless(n == 5,
148 "sdb_strbuf_sprintf() wrote %zi bytes; expected: 5", n);
149 len = sdb_strbuf_len(buf);
150 fail_unless(len == 5,
151 "sdb_strbuf_sprintf() left behind buffer with len = %zu; "
152 "expected: 5", len);
154 test = sdb_strbuf_string(buf);
155 fail_unless(test[len] == '\0',
156 "sdb_strbuf_sprintf() did not nil-terminate the string");
157 fail_unless(!strcmp(test, "ABCDE"),
158 "sdb_strbuf_sprintf() did not format string correctly; "
159 "got: %s; expected: ABCDE", test);
161 n = sdb_strbuf_sprintf(buf, "%zu; %5.4f", len, (double)len / 10.0);
162 expected = /* len */ 1 + /* "; " */ 2 + /* decimal len/10 */ 6;
163 fail_unless(n == expected,
164 "sdb_strbuf_sprintf() wrote %zi bytes; expected: %zi",
165 n, expected);
166 len = sdb_strbuf_len(buf);
167 fail_unless(len == (size_t)expected,
168 "sdb_strbuf_sprintf() left behind buffer with len = %zu; "
169 "expected: %zu", len, (size_t)expected);
171 test = sdb_strbuf_string(buf);
172 fail_unless(test[len] == '\0',
173 "sdb_strbuf_sprintf() did not nil-terminate the string");
174 fail_unless(!strcmp(test, "5; 0.5000"),
175 "sdb_strbuf_sprintf() did not format string correctly; "
176 "got: %s; expected: 5; 0.5000", test);
177 }
178 END_TEST
180 static struct {
181 const char *input;
182 ssize_t expected;
183 const char *expected_string;
184 } chomp_golden_data[] = {
185 { NULL, 0, "" },
186 { "\n", 1, "" },
187 { "\n\n", 2, "" },
188 { "12345\n\n\n", 3, "12345" },
189 { "abcd", 0, "abcd" },
190 };
192 START_TEST(test_sdb_strbuf_chomp)
193 {
194 size_t i;
196 for (i = 0; i < SDB_STATIC_ARRAY_LEN(chomp_golden_data); ++i) {
197 ssize_t n;
198 const char *check;
200 if (chomp_golden_data[i].input)
201 sdb_strbuf_sprintf(buf, chomp_golden_data[i].input);
203 /* empty buffer */
204 n = sdb_strbuf_chomp(buf);
205 fail_unless(n == chomp_golden_data[i].expected,
206 "sdb_strbuf_chomp() = %zi; expected: %zi", n,
207 chomp_golden_data[i].expected);
209 check = sdb_strbuf_string(buf);
210 fail_unless(!strcmp(check, chomp_golden_data[i].expected_string),
211 "sdb_strbuf_chomp() did not correctly remove newlines; "
212 "got string '%s'; expected: '%s'", check,
213 chomp_golden_data[i].expected_string);
214 }
215 }
216 END_TEST
218 static struct {
219 const char *input;
220 const char *expected;
221 } string_golden_data[] = {
222 { NULL, "" },
223 { "a", "a" },
224 { "abcdef", "abcdef" },
225 };
227 START_TEST(test_sdb_strbuf_string)
228 {
229 size_t i;
231 for (i = 0; i < SDB_STATIC_ARRAY_LEN(string_golden_data); ++i) {
232 const char *check;
234 if (string_golden_data[i].input)
235 sdb_strbuf_sprintf(buf, string_golden_data[i].input);
236 check = sdb_strbuf_string(buf);
237 fail_unless(!strcmp(check, string_golden_data[i].expected),
238 "sdb_strbuf_string() = '%s'; expected: '%s'",
239 check, string_golden_data[i].expected);
240 }
241 }
242 END_TEST
244 static struct {
245 const char *input;
246 size_t expected;
247 } len_golden_data[] = {
248 { NULL, 0 },
249 { "a", 1 },
250 { "12345", 5 },
251 };
253 START_TEST(test_sdb_strbuf_len)
254 {
255 size_t i;
257 for (i = 0; i < SDB_STATIC_ARRAY_LEN(len_golden_data); ++i) {
258 size_t check;
260 if (len_golden_data[i].input)
261 sdb_strbuf_sprintf(buf, len_golden_data[i].input);
262 check = sdb_strbuf_len(buf);
263 fail_unless(check == len_golden_data[i].expected,
264 "sdb_strbuf_len() = %zu; expected: %zu",
265 check, len_golden_data[i].expected);
266 }
267 }
268 END_TEST
270 Suite *
271 util_strbuf_suite(void)
272 {
273 Suite *s = suite_create("utils::strbuf");
274 TCase *tc;
276 tc = tcase_create("core");
277 tcase_add_checked_fixture(tc, setup, teardown);
278 tcase_add_test(tc, test_sdb_strbuf_create);
279 tcase_add_test(tc, test_sdb_strbuf_append);
280 tcase_add_test(tc, test_sdb_strbuf_sprintf);
281 tcase_add_test(tc, test_sdb_strbuf_chomp);
282 tcase_add_test(tc, test_sdb_strbuf_string);
283 tcase_add_test(tc, test_sdb_strbuf_len);
284 suite_add_tcase(s, tc);
286 return s;
287 } /* util_strbuf_suite */
289 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */