lib/util: Add tests for strv
[Samba.git] / lib / util / tests / strv.c
blob4030c44467c3f8dd9501fa9b8643433822b0d2a7
1 /*
2 * Tests for strv
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/>.
21 #include <talloc.h>
23 #include "replace.h"
25 #include "libcli/util/ntstatus.h"
26 #include "torture/torture.h"
27 #include "lib/util/data_blob.h"
28 #include "torture/local/proto.h"
30 #include "lib/util/strv.h"
32 static bool test_strv_empty(struct torture_context *tctx)
34 /* NULL strv contains 0 entries */
35 torture_assert_int_equal(tctx,
36 strv_count(NULL),
38 "strv_count() on NULL failed");
40 /* NULL strv has no next entry */
41 torture_assert(tctx,
42 strv_next(NULL, NULL) == NULL,
43 "strv_next() on NULL failed");
45 return true;
48 static bool test_strv_single(struct torture_context *tctx)
50 const char *data = "foo";
51 char *strv = NULL;
52 char *t;
53 int ret;
55 /* Add an item */
56 ret = strv_add(tctx, &strv, data);
57 torture_assert(tctx, ret == 0, "strv_add() failed");
59 /* Is there 1 item? */
60 torture_assert_int_equal(tctx,
61 strv_count(strv), 1,
62 "strv_count() failed");
64 /* Is the expected item the first one? */
65 t = strv_next(strv, NULL);
66 torture_assert(tctx,
67 strcmp(t, data) == 0,
68 "strv_next() failed");
70 /* Can the expected item be found? */
71 t = strv_find(strv, data);
72 torture_assert(tctx,
73 strcmp(t, data) == 0,
74 "strv_next() failed");
76 /* Delete it */
77 strv_delete(&strv, t);
79 /* Should have no items */
80 torture_assert_int_equal(tctx,
81 strv_count(strv), 0,
82 "strv_count() failed");
83 return true;
86 static bool test_strv_multi(struct torture_context *tctx)
88 const char *data[5] = { "foo", "bar", "", "samba", "x"};
89 char *strv = NULL;
90 char *t;
91 int i, ret;
92 const int num = sizeof(data) / sizeof(data[0]);
94 /* Add items */
95 for (i = 0; i < num; i++) {
96 ret = strv_add(tctx, &strv, data[i]);
97 torture_assert(tctx, ret == 0, "strv_add() failed");
100 torture_assert_int_equal(tctx,
101 strv_count(strv), num,
102 "strv_count() failed");
104 /* Check that strv_next() finds the expected values */
105 t = NULL;
106 for (i = 0; i < num; i++) {
107 t = strv_next(strv, t);
108 torture_assert(tctx,
109 strcmp(t, data[i]) == 0,
110 "strv_next() failed");
114 /* Check that strv_next() finds the expected values */
115 t = NULL;
116 for (i = 0; i < num; i++) {
117 t = strv_next(strv, t);
118 torture_assert(tctx,
119 strcmp(t, data[i]) == 0,
120 "strv_next() failed");
123 /* Find each item, delete it, check count */
124 for (i = 0; i < num; i++) {
125 t = strv_find(strv, data[i]);
126 torture_assert(tctx,
127 strcmp(t, data[i]) == 0,
128 "strv_next() failed");
129 strv_delete(&strv, t);
130 torture_assert_int_equal(tctx,
131 strv_count(strv), num - i - 1,
132 "strv_delete() failed");
135 /* Add items */
136 for (i = 0; i < num; i++) {
137 ret = strv_add(tctx, &strv, data[i]);
138 torture_assert(tctx, ret == 0, "strv_add() failed");
141 torture_assert_int_equal(tctx,
142 strv_count(strv), num,
143 "strv_count() failed");
145 /* Find items in reverse, delete, check count */
146 for (i = num - 1; i >= 0; i--) {
147 t = strv_find(strv, data[i]);
148 torture_assert(tctx,
149 strcmp(t, data[i]) == 0,
150 "strv_next() failed");
151 strv_delete(&strv, t);
152 torture_assert_int_equal(tctx,
153 strv_count(strv), i,
154 "strv_delete() failed");
157 return true;
160 struct torture_suite *torture_local_util_strv(TALLOC_CTX *mem_ctx)
162 struct torture_suite *suite = torture_suite_create(mem_ctx, "strv");
164 torture_suite_add_simple_test(suite, "strv_empty", test_strv_empty);
165 torture_suite_add_simple_test(suite, "strv_single", test_strv_single);
166 torture_suite_add_simple_test(suite, "strv_multi", test_strv_multi);
168 return suite;