doc: Update doc about talloc vs malloc speed
[Samba.git] / lib / util / tests / strv_util.c
blobb1496c74384e1004cc1a8f4a91f7b85d99c858ce
1 /*
2 * Tests for strv_util
4 * Copyright Martin Schwenke <martin@meltin.net> 2016
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <talloc.h>
22 #include "replace.h"
24 #include "libcli/util/ntstatus.h"
25 #include "torture/torture.h"
26 #include "lib/util/data_blob.h"
27 #include "torture/local/proto.h"
29 #include "lib/util/strv.h"
30 #include "lib/util/strv_util.h"
32 static bool test_strv_split_none(struct torture_context *tctx)
34 char *strv = NULL;
35 int ret;
37 /* NULL has 0 entries */
38 ret = strv_split(tctx, &strv, NULL, " ");
39 torture_assert(tctx, ret == 0, "strv_split() on NULL failed");
40 torture_assert_int_equal(tctx,
41 strv_count(strv),
43 "strv_split() on NULL failed");
44 TALLOC_FREE(strv);
46 /* Empty string has 0 entries */
47 ret = strv_split(tctx, &strv, "", " ");
48 torture_assert(tctx, ret == 0, "strv_split() on NULL failed");
49 torture_assert_int_equal(tctx,
50 strv_count(strv),
52 "strv_split() on \"\" failed");
53 TALLOC_FREE(strv);
55 /* String containing only separators has 0 entries */
56 ret = strv_split(tctx, &strv, "abcabcabc", "cba ");
57 torture_assert(tctx, ret == 0, "strv_split() on NULL failed");
58 torture_assert_int_equal(tctx,
59 strv_count(strv),
61 "strv_split() on seps-only failed");
62 TALLOC_FREE(strv);
64 return true;
67 struct test_str_split_data {
68 const char *in;
69 const char *sep;
70 const char *out[10]; /* Hardcoded maximum! */
73 static bool test_strv_split_some(struct torture_context *tctx)
75 const struct test_str_split_data data[] = {
77 /* Single string */
78 .in = "foo",
79 .sep = " \t",
80 .out = { "foo" }
83 /* Single string, single leading separator */
84 .in = " foo",
85 .sep = " \t",
86 .out = { "foo" }
89 /* Single string, single trailing separator */
90 .in = " foo",
91 .sep = " \t",
92 .out = { "foo" }
95 /* Single string, lots of separators */
96 .in = " \t foo\t ",
97 .sep = " \t",
98 .out = { "foo" }
101 /* Multiple strings, many separators */
102 .in = " \t foo bar\t\tx\t samba\t ",
103 .sep = " \t",
104 .out = { "foo", "bar", "x", "samba" }
107 const char *t;
108 char *strv = NULL;
109 size_t j;
111 for (j = 0; j < ARRAY_SIZE(data); j++) {
112 size_t i, num;
113 int ret;
114 const struct test_str_split_data *d = &data[j];
116 num = 0;
117 while (num < ARRAY_SIZE(d->out) && d->out[num] != NULL) {
118 num++;
120 ret = strv_split(tctx, &strv, d->in, d->sep);
121 torture_assert(tctx, ret == 0, "strv_split() on NULL failed");
122 torture_assert_int_equal(tctx,
123 strv_count(strv),
124 num,
125 "strv_split() failed");
126 t = NULL;
127 for (i = 0; i < num; i++) {
128 t = strv_next(strv, t);
129 torture_assert(tctx,
130 strcmp(t, d->out[i]) == 0,
131 "strv_split() failed");
133 TALLOC_FREE(strv);
135 return true;
138 struct torture_suite *torture_local_util_strv_util(TALLOC_CTX *mem_ctx)
140 struct torture_suite *suite =
141 torture_suite_create(mem_ctx, "strv_util");
143 torture_suite_add_simple_test(suite,
144 "strv_split_none",
145 test_strv_split_none);
146 torture_suite_add_simple_test(suite,
147 "strv_split_some",
148 test_strv_split_some);
149 return suite;