VERSION: Disable GIT_SNAPSHOT for the 4.12.0rc3 release.
[Samba.git] / lib / util / tests / strv_util.c
blob3dd93d2eb4690853da788692eafd8df598a4d9a6
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 int j;
111 for (j = 0; j < ARRAY_SIZE(data); j++) {
112 int i, num, ret;
113 const struct test_str_split_data *d = &data[j];
115 num = 0;
116 while (num < ARRAY_SIZE(d->out) && d->out[num] != NULL) {
117 num++;
119 ret = strv_split(tctx, &strv, d->in, d->sep);
120 torture_assert(tctx, ret == 0, "strv_split() on NULL failed");
121 torture_assert_int_equal(tctx,
122 strv_count(strv),
123 num,
124 "strv_split() failed");
125 t = NULL;
126 for (i = 0; i < num; i++) {
127 t = strv_next(strv, t);
128 torture_assert(tctx,
129 strcmp(t, d->out[i]) == 0,
130 "strv_split() failed");
132 TALLOC_FREE(strv);
134 return true;
137 struct torture_suite *torture_local_util_strv_util(TALLOC_CTX *mem_ctx)
139 struct torture_suite *suite =
140 torture_suite_create(mem_ctx, "strv_util");
142 torture_suite_add_simple_test(suite,
143 "strv_split_none",
144 test_strv_split_none);
145 torture_suite_add_simple_test(suite,
146 "strv_split_some",
147 test_strv_split_some);
148 return suite;