]> git.ipfire.org Git - thirdparty/kmod.git/blame - testsuite/test-array.c
testsuite: use a section to put tests in instead of array
[thirdparty/kmod.git] / testsuite / test-array.c
CommitLineData
fdafa6b6
LDM
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, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <errno.h>
20#include <stddef.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25
26#include <shared/array.h>
27
28#include "testsuite.h"
29
30static int test_array_append1(const struct test *t)
31{
32 struct array array;
33 const char *c1 = "test1";
34
35 array_init(&array, 2);
36 array_append(&array, c1);
37 assert_return(array.count == 1, EXIT_FAILURE);
38 assert_return(array.array[0] == c1, EXIT_FAILURE);
39 array_free_array(&array);
40
41 return 0;
42}
f1155c15 43DEFINE_TEST(test_array_append1,
fdafa6b6
LDM
44 .description = "test simple array append");
45
46
47static int test_array_append2(const struct test *t)
48{
49 struct array array;
50 const char *c1 = "test1";
51 const char *c2 = "test2";
52 const char *c3 = "test3";
53
54 array_init(&array, 2);
55 array_append(&array, c1);
56 array_append(&array, c2);
57 array_append(&array, c3);
58 assert_return(array.count == 3, EXIT_FAILURE);
59 assert_return(array.array[0] == c1, EXIT_FAILURE);
60 assert_return(array.array[1] == c2, EXIT_FAILURE);
61 assert_return(array.array[2] == c3, EXIT_FAILURE);
62 array_free_array(&array);
63
64 return 0;
65}
f1155c15 66DEFINE_TEST(test_array_append2,
fdafa6b6
LDM
67 .description = "test array append over step");
68
69static int test_array_append_unique(const struct test *t)
70{
71 struct array array;
72 const char *c1 = "test1";
73 const char *c2 = "test2";
74 const char *c3 = "test3";
75
76 array_init(&array, 2);
77 array_append_unique(&array, c1);
78 array_append_unique(&array, c2);
79 array_append_unique(&array, c3);
80 array_append_unique(&array, c3);
81 array_append_unique(&array, c2);
82 array_append_unique(&array, c1);
83 assert_return(array.count == 3, EXIT_FAILURE);
84 assert_return(array.array[0] == c1, EXIT_FAILURE);
85 assert_return(array.array[1] == c2, EXIT_FAILURE);
86 assert_return(array.array[2] == c3, EXIT_FAILURE);
87 array_free_array(&array);
88
89 return 0;
90}
f1155c15 91DEFINE_TEST(test_array_append_unique,
fdafa6b6
LDM
92 .description = "test array append unique");
93
94static int test_array_sort(const struct test *t)
95{
96 struct array array;
97 const char *c1 = "test1";
98 const char *c2 = "test2";
99 const char *c3 = "test3";
100
101 array_init(&array, 2);
102 array_append(&array, c1);
103 array_append(&array, c2);
104 array_append(&array, c3);
105 array_append(&array, c2);
106 array_append(&array, c3);
107 array_append(&array, c1);
108 array_sort(&array, (int (*)(const void *a, const void *b)) strcmp);
109 assert_return(array.count == 6, EXIT_FAILURE);
110 assert_return(array.array[0] == c1, EXIT_FAILURE);
111 assert_return(array.array[1] == c1, EXIT_FAILURE);
112 assert_return(array.array[2] == c2, EXIT_FAILURE);
113 assert_return(array.array[3] == c2, EXIT_FAILURE);
114 assert_return(array.array[4] == c3, EXIT_FAILURE);
115 assert_return(array.array[5] == c3, EXIT_FAILURE);
116 array_free_array(&array);
117
118 return 0;
119}
f1155c15 120DEFINE_TEST(test_array_sort,
fdafa6b6
LDM
121 .description = "test array sort");
122
123static int test_array_remove_at(const struct test *t)
124{
125 struct array array;
126 const char *c1 = "test1";
127 const char *c2 = "test2";
128 const char *c3 = "test3";
129
130 array_init(&array, 2);
131 array_append(&array, c1);
132 array_append(&array, c2);
133 array_append(&array, c3);
134
135 array_remove_at(&array, 2);
136 assert_return(array.count == 2, EXIT_FAILURE);
137 assert_return(array.array[0] == c1, EXIT_FAILURE);
138 assert_return(array.array[1] == c2, EXIT_FAILURE);
139
140 array_remove_at(&array, 0);
141 assert_return(array.count == 1, EXIT_FAILURE);
142 assert_return(array.array[0] == c2, EXIT_FAILURE);
143
144 array_remove_at(&array, 0);
145 assert_return(array.count == 0, EXIT_FAILURE);
146
147 array_append(&array, c1);
148 array_append(&array, c2);
149 array_append(&array, c3);
150
151 array_remove_at(&array, 1);
152 assert_return(array.count == 2, EXIT_FAILURE);
153 assert_return(array.array[0] == c1, EXIT_FAILURE);
154 assert_return(array.array[1] == c3, EXIT_FAILURE);
155
156 array_free_array(&array);
157
158 return 0;
159}
f1155c15 160DEFINE_TEST(test_array_remove_at,
fdafa6b6
LDM
161 .description = "test array remove at");
162
163static const struct test *tests[] = {
164 &stest_array_append1,
165 &stest_array_append2,
166 &stest_array_append_unique,
167 &stest_array_sort,
168 &stest_array_remove_at,
169 NULL,
170};
171
172TESTSUITE_MAIN(tests);