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 START_TEST(test_sdb_strbuf_chomp)
181 {
182 ssize_t n;
183 const char *check;
185 /* empty buffer */
186 n = sdb_strbuf_chomp(buf);
187 fail_unless(n == 0,
188 "sdb_strbuf_chomp() = %zi; expected: 0", n);
190 sdb_strbuf_sprintf(buf, "\n");
191 n = sdb_strbuf_chomp(buf);
192 fail_unless(n == 1,
193 "sdb_strbuf_chomp() = %zi; expected: 1", n);
194 check = sdb_strbuf_string(buf);
195 fail_unless(!strcmp(check, ""),
196 "sdb_strbuf_chomp() did not correctly remove newlines; "
197 "got string '%s'; expected: ''", check);
199 sdb_strbuf_sprintf(buf, "12345\n\n");
200 n = sdb_strbuf_chomp(buf);
201 fail_unless(n == 2,
202 "sdb_strbuf_chomp() = %zi; expected: 2", n);
203 check = sdb_strbuf_string(buf);
204 fail_unless(!strcmp(check, "12345"),
205 "sdb_strbuf_chomp() did not correctly remove newlines; "
206 "got string '%s'; expected: '12345'", check);
207 }
208 END_TEST
210 START_TEST(test_sdb_strbuf_string)
211 {
212 const char *check;
214 check = sdb_strbuf_string(buf);
215 fail_unless(!strcmp(check, ""),
216 "sdb_strbuf_string() = '%s'; expected: ''", check);
218 sdb_strbuf_sprintf(buf, "abcdef");
219 check = sdb_strbuf_string(buf);
220 fail_unless(!strcmp(check, "abcdef"),
221 "sdb_strbuf_string() = '%s'; expected: 'abcdef'", check);
222 }
223 END_TEST
225 START_TEST(test_sdb_strbuf_len)
226 {
227 size_t check;
229 check = sdb_strbuf_len(buf);
230 fail_unless(check == 0,
231 "sdb_strbuf_len() = %zu; expected: 0", check);
233 sdb_strbuf_sprintf(buf, "a");
234 check = sdb_strbuf_len(buf);
235 fail_unless(check == 1,
236 "sdb_strbuf_len() = %zu; expected: 1", check);
237 }
238 END_TEST
240 Suite *
241 util_strbuf_suite(void)
242 {
243 Suite *s = suite_create("utils::strbuf");
244 TCase *tc;
246 tc = tcase_create("core");
247 tcase_add_checked_fixture(tc, setup, teardown);
248 tcase_add_test(tc, test_sdb_strbuf_create);
249 tcase_add_test(tc, test_sdb_strbuf_append);
250 tcase_add_test(tc, test_sdb_strbuf_sprintf);
251 tcase_add_test(tc, test_sdb_strbuf_chomp);
252 tcase_add_test(tc, test_sdb_strbuf_string);
253 tcase_add_test(tc, test_sdb_strbuf_len);
254 suite_add_tcase(s, tc);
256 return s;
257 } /* util_strbuf_suite */
259 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */