Code

Add more string utility functions and unit tests.
[sysdb.git] / t / unit / utils / strings_test.c
1 /*
2  * SysDB - t/unit/utils/strings_test.c
3  * Copyright (C) 2016 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 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif
32 #include "core/data.h"
33 #include "utils/strings.h"
34 #include "testutils.h"
36 #include <check.h>
38 /*
39  * tests
40  */
42 START_TEST(test_stringv)
43 {
44         char **dst = NULL;
45         size_t dst_len = 0;
47         char *src[] = { "a", "b", "c" };
48         size_t src_len = SDB_STATIC_ARRAY_LEN(src);
49         size_t i;
51         int check;
53         /* Test no-op, empty operations. */
54         check = stringv_copy(&dst, &dst_len, NULL, 0);
55         fail_unless(check == 0,
56                         "stringv_copy(&<null>, &<0>, <null>, 0) = %d; expected: 0",
57                         check);
58         fail_unless((dst == NULL) && (dst_len == 0),
59                         "stringv_copy(&<null>, &<0>, <null>, 0) produced %p, %d; "
60                         "expected <null>, 0", dst, dst_len);
61         stringv_free(&dst, &dst_len);
62         fail_unless((dst == NULL) && (dst_len == 0),
63                         "stringv_free(&<null>, &<0>) produced %p, %d; expected <null>, 0",
64                         dst, dst_len);
66         /* Now, append some content. */
67         for (i = 0; i < src_len; i++) {
68                 sdb_data_t d1 = {
69                         SDB_TYPE_STRING | SDB_TYPE_ARRAY,
70                         { .array = { 0, NULL } },
71                 };
72                 sdb_data_t d2 = d1;
74                 char buf1[256], buf2[256];
75                 size_t j;
77                 check = stringv_append(&dst, &dst_len, src[i]);
78                 fail_unless(check == 0,
79                                 "stringv_append(<s>, <len>, '%s') = %d; expected: 0",
80                                 src[i], check);
81                 fail_unless((dst != NULL) && (dst_len == i + 1),
82                                 "stringv_append(<s>, <len>, '%s') produced s=%p, len=%zu; "
83                                 "expected: s=<v>, len=%zu", src[i], dst, dst_len, i + 1);
85                 for (j = 0; j <= i; j++)
86                         if ((! dst[j]) || (strcmp(dst[j], src[j]) != 0))
87                                 break;
89                 if (j <= i) {
90                         d1.data.array.values = dst;
91                         d1.data.array.length = dst_len;
92                         sdb_data_format(&d1, buf1, sizeof(buf1), 0);
94                         d2.data.array.values = src;
95                         d2.data.array.length = dst_len;
96                         sdb_data_format(&d2, buf2, sizeof(buf2), 0);
98                         fail("stringv_append(<s>, <len>, '%s') produced unexpected result: "
99                                         "vectors differ at position %zu: '%s' <-> '%s'",
100                                         src[i], j, buf1, buf2);
101                 }
102         }
103         stringv_free(&dst, &dst_len);
105         /* Copy all in one go. */
106         for (i = 0; i < src_len; i++) {
107                 sdb_data_t d1 = {
108                         SDB_TYPE_STRING | SDB_TYPE_ARRAY,
109                         { .array = { 0, NULL } },
110                 };
111                 sdb_data_t d2 = d1;
113                 char buf1[256], buf2[256];
114                 size_t j;
116                 /* stringv_copy is expected to free memory as needed, so simply copy
117                  * over the old values from the previous iteration. */
118                 check = stringv_copy(&dst, &dst_len, (const char * const *)src, i + 1);
119                 fail_unless(check == 0,
120                                 "stringv_copy(<s>, <len>, <src>, %zu) = %d; expected: 0",
121                                 i, check);
122                 fail_unless((dst != NULL) && (dst_len == i + 1),
123                                 "stringv_copy(<s>, <len>, <src>, %zu) produced s=%p, len=%zu; "
124                                 "expected: s=<v>, len=%zu", i, dst, dst_len, i + 1);
126                 for (j = 0; j <= i; j++)
127                         if ((! dst[j]) || (strcmp(dst[j], src[j]) != 0))
128                                 break;
130                 if (j <= i) {
131                         d1.data.array.values = dst;
132                         d1.data.array.length = dst_len;
133                         sdb_data_format(&d1, buf1, sizeof(buf1), 0);
135                         d2.data.array.values = src;
136                         d2.data.array.length = dst_len;
137                         sdb_data_format(&d2, buf2, sizeof(buf2), 0);
139                         fail("stringv_copy(<s>, <len>, <src>, %zu) produced unexpected result: "
140                                         "vectors differ at position %zu: '%s' <-> '%s'",
141                                         i, j, buf1, buf2);
142                 }
143         }
144         stringv_free(&dst, &dst_len);
146 END_TEST
148 TEST_MAIN("utils::strings")
150         TCase *tc = tcase_create("core");
151         tcase_add_test(tc, test_stringv);
152         ADD_TCASE(tc);
154 TEST_MAIN_END
156 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */