]> git.ipfire.org Git - thirdparty/kmod.git/blob - testsuite/test-array.c
testsuite: check for ill-formed kcmdline
[thirdparty/kmod.git] / testsuite / test-array.c
1 /*
2 * Copyright (C) 2014 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <errno.h>
19 #include <stddef.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include <shared/array.h>
26
27 #include "testsuite.h"
28
29 static int test_array_append1(const struct test *t)
30 {
31 struct array array;
32 const char *c1 = "test1";
33
34 array_init(&array, 2);
35 array_append(&array, c1);
36 assert_return(array.count == 1, EXIT_FAILURE);
37 assert_return(array.array[0] == c1, EXIT_FAILURE);
38 array_free_array(&array);
39
40 return 0;
41 }
42 DEFINE_TEST(test_array_append1,
43 .description = "test simple array append");
44
45
46 static int test_array_append2(const struct test *t)
47 {
48 struct array array;
49 const char *c1 = "test1";
50 const char *c2 = "test2";
51 const char *c3 = "test3";
52
53 array_init(&array, 2);
54 array_append(&array, c1);
55 array_append(&array, c2);
56 array_append(&array, c3);
57 assert_return(array.count == 3, EXIT_FAILURE);
58 assert_return(array.array[0] == c1, EXIT_FAILURE);
59 assert_return(array.array[1] == c2, EXIT_FAILURE);
60 assert_return(array.array[2] == c3, EXIT_FAILURE);
61 array_free_array(&array);
62
63 return 0;
64 }
65 DEFINE_TEST(test_array_append2,
66 .description = "test array append over step");
67
68 static int test_array_append_unique(const struct test *t)
69 {
70 struct array array;
71 const char *c1 = "test1";
72 const char *c2 = "test2";
73 const char *c3 = "test3";
74
75 array_init(&array, 2);
76 array_append_unique(&array, c1);
77 array_append_unique(&array, c2);
78 array_append_unique(&array, c3);
79 array_append_unique(&array, c3);
80 array_append_unique(&array, c2);
81 array_append_unique(&array, c1);
82 assert_return(array.count == 3, EXIT_FAILURE);
83 assert_return(array.array[0] == c1, EXIT_FAILURE);
84 assert_return(array.array[1] == c2, EXIT_FAILURE);
85 assert_return(array.array[2] == c3, EXIT_FAILURE);
86 array_free_array(&array);
87
88 return 0;
89 }
90 DEFINE_TEST(test_array_append_unique,
91 .description = "test array append unique");
92
93 static int strptrcmp(const void *pa, const void *pb) {
94 const char *a = *(const char **)pa;
95 const char *b = *(const char **)pb;
96
97 return strcmp(a, b);
98 }
99
100 static int test_array_sort(const struct test *t)
101 {
102 struct array array;
103 const char *c1 = "test1";
104 const char *c2 = "test2";
105 const char *c3 = "test3";
106
107 array_init(&array, 2);
108 array_append(&array, c1);
109 array_append(&array, c2);
110 array_append(&array, c3);
111 array_append(&array, c2);
112 array_append(&array, c3);
113 array_append(&array, c1);
114 array_sort(&array, strptrcmp);
115 assert_return(array.count == 6, EXIT_FAILURE);
116 assert_return(array.array[0] == c1, EXIT_FAILURE);
117 assert_return(array.array[1] == c1, EXIT_FAILURE);
118 assert_return(array.array[2] == c2, EXIT_FAILURE);
119 assert_return(array.array[3] == c2, EXIT_FAILURE);
120 assert_return(array.array[4] == c3, EXIT_FAILURE);
121 assert_return(array.array[5] == c3, EXIT_FAILURE);
122 array_free_array(&array);
123
124 return 0;
125 }
126 DEFINE_TEST(test_array_sort,
127 .description = "test array sort");
128
129 static int test_array_remove_at(const struct test *t)
130 {
131 struct array array;
132 const char *c1 = "test1";
133 const char *c2 = "test2";
134 const char *c3 = "test3";
135
136 array_init(&array, 2);
137 array_append(&array, c1);
138 array_append(&array, c2);
139 array_append(&array, c3);
140
141 array_remove_at(&array, 2);
142 assert_return(array.count == 2, EXIT_FAILURE);
143 assert_return(array.array[0] == c1, EXIT_FAILURE);
144 assert_return(array.array[1] == c2, EXIT_FAILURE);
145
146 array_remove_at(&array, 0);
147 assert_return(array.count == 1, EXIT_FAILURE);
148 assert_return(array.array[0] == c2, EXIT_FAILURE);
149
150 array_remove_at(&array, 0);
151 assert_return(array.count == 0, EXIT_FAILURE);
152
153 array_append(&array, c1);
154 array_append(&array, c2);
155 array_append(&array, c3);
156
157 array_remove_at(&array, 1);
158 assert_return(array.count == 2, EXIT_FAILURE);
159 assert_return(array.array[0] == c1, EXIT_FAILURE);
160 assert_return(array.array[1] == c3, EXIT_FAILURE);
161
162 array_free_array(&array);
163
164 return 0;
165 }
166 DEFINE_TEST(test_array_remove_at,
167 .description = "test array remove at");
168
169 static int test_array_pop(const struct test *t)
170 {
171 struct array array;
172 const char *c1 = "test1";
173 const char *c2 = "test2";
174 const char *c3 = "test3";
175
176 array_init(&array, 2);
177 array_append(&array, c1);
178 array_append(&array, c2);
179 array_append(&array, c3);
180
181
182 array_pop(&array);
183
184 assert_return(array.count == 2, EXIT_FAILURE);
185 assert_return(array.array[0] == c1, EXIT_FAILURE);
186 assert_return(array.array[1] == c2, EXIT_FAILURE);
187
188 array_pop(&array);
189 array_pop(&array);
190
191 assert_return(array.count == 0, EXIT_FAILURE);
192
193 array_free_array(&array);
194
195 return 0;
196 }
197
198 DEFINE_TEST(test_array_pop,
199 .description = "test array pop");
200
201 TESTSUITE_MAIN();