source: mainline/uspace/lib/c/test/adt/array.c@ fb1015fc

Last change on this file since fb1015fc was 03daabd, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

Renaming dyn_array to array

The term dyn_array is redunant and has therefore been replaced
with the shorter term array

  • Property mode set to 100644
File size: 5.9 KB
Line 
1/*
2 * Copyright (c) 2015 Michal Koutny
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <adt/array.h>
30#include <assert.h>
31#include <pcut/pcut.h>
32
33PCUT_INIT
34
35PCUT_TEST_SUITE(array);
36
37typedef int data_t;
38static array_t da;
39
40PCUT_TEST_BEFORE
41{
42 array_initialize(&da, data_t);
43 errno_t rc = array_reserve(&da, 3);
44 assert(rc == EOK);
45}
46
47PCUT_TEST_AFTER
48{
49 array_destroy(&da);
50}
51
52PCUT_TEST(initialization)
53{
54 PCUT_ASSERT_INT_EQUALS(da.capacity, 3);
55 PCUT_ASSERT_INT_EQUALS(da.size, 0);
56}
57
58PCUT_TEST(append)
59{
60 array_append(&da, data_t, 42);
61 array_append(&da, data_t, 666);
62
63 PCUT_ASSERT_INT_EQUALS(2, da.size);
64 PCUT_ASSERT_INT_EQUALS(42, array_at(&da, data_t, 0));
65 PCUT_ASSERT_INT_EQUALS(666, array_at(&da, data_t, 1));
66}
67
68PCUT_TEST(assign)
69{
70 array_append(&da, data_t, 42);
71 array_at(&da, data_t, 0) = 112;
72
73 PCUT_ASSERT_INT_EQUALS(112, array_at(&da, data_t, 0));
74}
75
76PCUT_TEST(remove)
77{
78 array_append(&da, data_t, 10);
79 array_append(&da, data_t, 11);
80
81 array_remove(&da, 0);
82
83 PCUT_ASSERT_INT_EQUALS(1, da.size);
84 PCUT_ASSERT_INT_EQUALS(11, array_at(&da, data_t, 0));
85}
86
87PCUT_TEST(insert)
88{
89 array_append(&da, data_t, 10);
90 array_append(&da, data_t, 11);
91 array_append(&da, data_t, 12);
92 array_insert(&da, data_t, 1, 99);
93
94 PCUT_ASSERT_INT_EQUALS(4, da.size);
95 PCUT_ASSERT_INT_EQUALS(10, array_at(&da, data_t, 0));
96 PCUT_ASSERT_INT_EQUALS(99, array_at(&da, data_t, 1));
97 PCUT_ASSERT_INT_EQUALS(11, array_at(&da, data_t, 2));
98 PCUT_ASSERT_INT_EQUALS(12, array_at(&da, data_t, 3));
99}
100
101PCUT_TEST(capacity_grow)
102{
103 array_append(&da, data_t, 42);
104 array_append(&da, data_t, 666);
105 array_append(&da, data_t, 42);
106 array_append(&da, data_t, 666);
107
108 PCUT_ASSERT_TRUE(da.capacity > 3);
109}
110
111PCUT_TEST(capacity_shrink)
112{
113 array_append(&da, data_t, 42);
114 array_append(&da, data_t, 666);
115 array_append(&da, data_t, 42);
116
117 array_remove(&da, 0);
118 array_remove(&da, 0);
119 array_remove(&da, 0);
120
121 PCUT_ASSERT_TRUE(da.capacity < 3);
122}
123
124PCUT_TEST(iterator)
125{
126 for (int i = 0; i < 10; ++i) {
127 array_append(&da, data_t, i * i);
128 }
129
130 int i = 0;
131 array_foreach(da, data_t, it) {
132 PCUT_ASSERT_INT_EQUALS(i * i, *it);
133 ++i;
134 }
135}
136
137PCUT_TEST(find)
138{
139 array_append(&da, data_t, 10);
140 array_append(&da, data_t, 11);
141 array_append(&da, data_t, 12);
142 array_append(&da, data_t, 99);
143
144 PCUT_ASSERT_INT_EQUALS(0, array_find(&da, data_t, 10));
145 PCUT_ASSERT_INT_EQUALS(3, array_find(&da, data_t, 99));
146 PCUT_ASSERT_INT_EQUALS(4, array_find(&da, data_t, 666));
147}
148
149PCUT_TEST(clear_range_middle)
150{
151 array_append(&da, data_t, 10);
152 array_append(&da, data_t, 11);
153 array_append(&da, data_t, 12);
154 array_append(&da, data_t, 99);
155
156 array_clear_range(&da, 1, 3);
157 PCUT_ASSERT_INT_EQUALS(2, da.size);
158 PCUT_ASSERT_INT_EQUALS(10, array_at(&da, data_t, 0));
159 PCUT_ASSERT_INT_EQUALS(99, array_at(&da, data_t, 1));
160}
161
162PCUT_TEST(clear_range_begin)
163{
164 array_append(&da, data_t, 10);
165 array_append(&da, data_t, 11);
166 array_append(&da, data_t, 12);
167 array_append(&da, data_t, 99);
168
169 array_clear_range(&da, 0, 2);
170 PCUT_ASSERT_INT_EQUALS(2, da.size);
171 PCUT_ASSERT_INT_EQUALS(12, array_at(&da, data_t, 0));
172 PCUT_ASSERT_INT_EQUALS(99, array_at(&da, data_t, 1));
173}
174
175PCUT_TEST(clear_range_end)
176{
177 array_append(&da, data_t, 10);
178 array_append(&da, data_t, 11);
179 array_append(&da, data_t, 12);
180 array_append(&da, data_t, 99);
181
182 array_clear_range(&da, 2, 4);
183 PCUT_ASSERT_INT_EQUALS(2, da.size);
184 PCUT_ASSERT_INT_EQUALS(10, array_at(&da, data_t, 0));
185 PCUT_ASSERT_INT_EQUALS(11, array_at(&da, data_t, 1));
186}
187
188PCUT_TEST(clear_range_empty)
189{
190 array_append(&da, data_t, 10);
191 array_append(&da, data_t, 99);
192
193 array_clear_range(&da, 0, 0);
194 PCUT_ASSERT_INT_EQUALS(2, da.size);
195 PCUT_ASSERT_INT_EQUALS(10, array_at(&da, data_t, 0));
196 PCUT_ASSERT_INT_EQUALS(99, array_at(&da, data_t, 1));
197}
198
199PCUT_TEST(concat_simple)
200{
201 array_append(&da, data_t, 10);
202 array_append(&da, data_t, 99);
203
204 array_t da2;
205 array_initialize(&da2, data_t);
206 array_append(&da2, data_t, 30);
207 array_append(&da2, data_t, 31);
208
209 array_concat(&da, &da2);
210 PCUT_ASSERT_INT_EQUALS(4, da.size);
211 PCUT_ASSERT_INT_EQUALS(2, da2.size);
212
213 PCUT_ASSERT_INT_EQUALS(10, array_at(&da, data_t, 0));
214 PCUT_ASSERT_INT_EQUALS(99, array_at(&da, data_t, 1));
215 PCUT_ASSERT_INT_EQUALS(30, array_at(&da, data_t, 2));
216 PCUT_ASSERT_INT_EQUALS(31, array_at(&da, data_t, 3));
217
218 array_destroy(&da2);
219}
220
221PCUT_TEST(concat_self)
222{
223 array_append(&da, data_t, 10);
224 array_append(&da, data_t, 99);
225
226 array_concat(&da, &da);
227 PCUT_ASSERT_INT_EQUALS(4, da.size);
228
229 PCUT_ASSERT_INT_EQUALS(10, array_at(&da, data_t, 0));
230 PCUT_ASSERT_INT_EQUALS(99, array_at(&da, data_t, 1));
231 PCUT_ASSERT_INT_EQUALS(10, array_at(&da, data_t, 2));
232 PCUT_ASSERT_INT_EQUALS(99, array_at(&da, data_t, 3));
233}
234
235PCUT_EXPORT(array);
Note: See TracBrowser for help on using the repository browser.