1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
9 #include <rte_common.h>
10 #include <rte_debug.h>
11 #include <rte_errno.h>
12 #include <rte_fbarray.h>
16 struct fbarray_testsuite_params
{
17 struct rte_fbarray arr
;
22 static struct fbarray_testsuite_params param
;
24 #define FBARRAY_TEST_ARR_NAME "fbarray_autotest"
25 #define FBARRAY_TEST_LEN 256
26 #define FBARRAY_TEST_ELT_SZ (sizeof(int))
28 static int autotest_setup(void)
30 return rte_fbarray_init(¶m
.arr
, FBARRAY_TEST_ARR_NAME
,
31 FBARRAY_TEST_LEN
, FBARRAY_TEST_ELT_SZ
);
34 static void autotest_teardown(void)
36 rte_fbarray_destroy(¶m
.arr
);
39 static int init_array(void)
42 for (i
= param
.start
; i
<= param
.end
; i
++) {
43 if (rte_fbarray_set_used(¶m
.arr
, i
))
49 static void reset_array(void)
52 for (i
= 0; i
< FBARRAY_TEST_LEN
; i
++)
53 rte_fbarray_set_free(¶m
.arr
, i
);
56 static int first_msk_test_setup(void)
58 /* put all within first mask */
64 static int cross_msk_test_setup(void)
66 /* put all within second and third mask */
72 static int multi_msk_test_setup(void)
74 /* put all within first and last mask */
76 param
.end
= FBARRAY_TEST_LEN
- 20;
80 static int last_msk_test_setup(void)
82 /* put all within last mask */
83 param
.start
= FBARRAY_TEST_LEN
- 20;
84 param
.end
= FBARRAY_TEST_LEN
- 1;
88 static int full_msk_test_setup(void)
90 /* fill entire mask */
92 param
.end
= FBARRAY_TEST_LEN
- 1;
96 static int empty_msk_test_setup(void)
98 /* do not fill anything in */
105 static int test_invalid(void)
107 struct rte_fbarray dummy
;
109 /* invalid parameters */
110 TEST_ASSERT_FAIL(rte_fbarray_attach(NULL
),
111 "Call succeeded with invalid parameters\n");
112 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
113 TEST_ASSERT_FAIL(rte_fbarray_detach(NULL
),
114 "Call succeeded with invalid parameters\n");
115 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
117 TEST_ASSERT_FAIL(rte_fbarray_destroy(NULL
),
118 "Call succeeded with invalid parameters\n");
119 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno valuey\n");
120 TEST_ASSERT_FAIL(rte_fbarray_init(NULL
, "fail", 16, 16),
121 "Call succeeded with invalid parameters\n");
122 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
123 TEST_ASSERT_FAIL(rte_fbarray_init(&dummy
, NULL
, 16, 16),
124 "Call succeeded with invalid parameters\n");
125 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
126 TEST_ASSERT_FAIL(rte_fbarray_init(&dummy
, "fail", 0, 16),
127 "Call succeeded with invalid parameters\n");
128 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
129 TEST_ASSERT_FAIL(rte_fbarray_init(&dummy
, "fail", 16, 0),
130 "Call succeeded with invalid parameters\n");
131 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
132 /* len must not be greater than INT_MAX */
133 TEST_ASSERT_FAIL(rte_fbarray_init(&dummy
, "fail", INT_MAX
+ 1U, 16),
134 "Call succeeded with invalid parameters\n");
135 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
137 TEST_ASSERT_NULL(rte_fbarray_get(NULL
, 0),
138 "Call succeeded with invalid parameters\n");
139 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
140 TEST_ASSERT(rte_fbarray_find_idx(NULL
, 0) < 0,
141 "Call succeeded with invalid parameters\n");
142 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
143 TEST_ASSERT(rte_fbarray_set_free(NULL
, 0),
144 "Call succeeded with invalid parameters\n");
145 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
146 TEST_ASSERT(rte_fbarray_set_used(NULL
, 0),
147 "Call succeeded with invalid parameters\n");
148 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
149 TEST_ASSERT(rte_fbarray_find_contig_free(NULL
, 0) < 0,
150 "Call succeeded with invalid parameters\n");
151 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
152 TEST_ASSERT(rte_fbarray_find_contig_used(NULL
, 0) < 0,
153 "Call succeeded with invalid parameters\n");
154 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
155 TEST_ASSERT(rte_fbarray_find_rev_contig_free(NULL
, 0) < 0,
156 "Call succeeded with invalid parameters\n");
157 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
158 TEST_ASSERT(rte_fbarray_find_rev_contig_used(NULL
, 0) < 0,
159 "Call succeeded with invalid parameters\n");
160 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
161 TEST_ASSERT(rte_fbarray_find_next_free(NULL
, 0) < 0,
162 "Call succeeded with invalid parameters\n");
163 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
164 TEST_ASSERT(rte_fbarray_find_next_used(NULL
, 0) < 0,
165 "Call succeeded with invalid parameters\n");
166 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
167 TEST_ASSERT(rte_fbarray_find_prev_free(NULL
, 0) < 0,
168 "Call succeeded with invalid parameters\n");
169 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
170 TEST_ASSERT(rte_fbarray_find_prev_used(NULL
, 0) < 0,
171 "Call succeeded with invalid parameters\n");
172 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
173 TEST_ASSERT(rte_fbarray_find_next_n_free(NULL
, 0, 0) < 0,
174 "Call succeeded with invalid parameters\n");
175 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
176 TEST_ASSERT(rte_fbarray_find_next_n_used(NULL
, 0, 0) < 0,
177 "Call succeeded with invalid parameters\n");
178 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
179 TEST_ASSERT(rte_fbarray_find_prev_n_free(NULL
, 0, 0) < 0,
180 "Call succeeded with invalid parameters\n");
181 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
182 TEST_ASSERT(rte_fbarray_find_prev_n_used(NULL
, 0, 0) < 0,
183 "Call succeeded with invalid parameters\n");
184 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
185 TEST_ASSERT(rte_fbarray_is_used(NULL
, 0) < 0,
186 "Call succeeded with invalid parameters\n");
187 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
189 TEST_ASSERT_SUCCESS(rte_fbarray_init(&dummy
, "success",
190 FBARRAY_TEST_LEN
, 8),
191 "Failed to initialize valid fbarray\n");
193 /* test API for handling invalid parameters with a valid fbarray */
194 TEST_ASSERT_NULL(rte_fbarray_get(&dummy
, FBARRAY_TEST_LEN
),
195 "Call succeeded with invalid parameters\n");
196 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
198 TEST_ASSERT(rte_fbarray_find_idx(&dummy
, NULL
) < 0,
199 "Call succeeded with invalid parameters\n");
200 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
202 TEST_ASSERT(rte_fbarray_set_free(&dummy
, FBARRAY_TEST_LEN
),
203 "Call succeeded with invalid parameters\n");
204 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
206 TEST_ASSERT(rte_fbarray_set_used(&dummy
, FBARRAY_TEST_LEN
),
207 "Call succeeded with invalid parameters\n");
208 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
210 TEST_ASSERT(rte_fbarray_find_contig_free(&dummy
, FBARRAY_TEST_LEN
) < 0,
211 "Call succeeded with invalid parameters\n");
212 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
214 TEST_ASSERT(rte_fbarray_find_contig_used(&dummy
, FBARRAY_TEST_LEN
) < 0,
215 "Call succeeded with invalid parameters\n");
216 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
218 TEST_ASSERT(rte_fbarray_find_rev_contig_free(&dummy
,
219 FBARRAY_TEST_LEN
) < 0,
220 "Call succeeded with invalid parameters\n");
221 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
223 TEST_ASSERT(rte_fbarray_find_rev_contig_used(&dummy
,
224 FBARRAY_TEST_LEN
) < 0,
225 "Call succeeded with invalid parameters\n");
226 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
228 TEST_ASSERT(rte_fbarray_find_next_free(&dummy
, FBARRAY_TEST_LEN
) < 0,
229 "Call succeeded with invalid parameters\n");
230 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
232 TEST_ASSERT(rte_fbarray_find_next_used(&dummy
, FBARRAY_TEST_LEN
) < 0,
233 "Call succeeded with invalid parameters\n");
234 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
236 TEST_ASSERT(rte_fbarray_find_prev_free(&dummy
, FBARRAY_TEST_LEN
) < 0,
237 "Call succeeded with invalid parameters\n");
238 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
240 TEST_ASSERT(rte_fbarray_find_prev_used(&dummy
, FBARRAY_TEST_LEN
) < 0,
241 "Call succeeded with invalid parameters\n");
242 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
244 TEST_ASSERT(rte_fbarray_find_next_n_free(&dummy
,
245 FBARRAY_TEST_LEN
, 1) < 0,
246 "Call succeeded with invalid parameters\n");
247 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
248 TEST_ASSERT(rte_fbarray_find_next_n_free(&dummy
, 0,
249 FBARRAY_TEST_LEN
+ 1) < 0,
250 "Call succeeded with invalid parameters\n");
251 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
252 TEST_ASSERT(rte_fbarray_find_next_n_free(&dummy
, 0, 0) < 0,
253 "Call succeeded with invalid parameters\n");
254 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
256 TEST_ASSERT(rte_fbarray_find_next_n_used(&dummy
,
257 FBARRAY_TEST_LEN
, 1) < 0,
258 "Call succeeded with invalid parameters\n");
259 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
260 TEST_ASSERT(rte_fbarray_find_next_n_used(&dummy
, 0,
261 FBARRAY_TEST_LEN
+ 1) < 0,
262 "Call succeeded with invalid parameters\n");
263 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
264 TEST_ASSERT(rte_fbarray_find_next_n_used(&dummy
, 0, 0) < 0,
265 "Call succeeded with invalid parameters\n");
266 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
268 TEST_ASSERT(rte_fbarray_find_prev_n_free(&dummy
,
269 FBARRAY_TEST_LEN
, 1) < 0,
270 "Call succeeded with invalid parameters\n");
271 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
272 TEST_ASSERT(rte_fbarray_find_prev_n_free(&dummy
, 0,
273 FBARRAY_TEST_LEN
+ 1) < 0,
274 "Call succeeded with invalid parameters\n");
275 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
276 TEST_ASSERT(rte_fbarray_find_prev_n_free(&dummy
, 0, 0) < 0,
277 "Call succeeded with invalid parameters\n");
278 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
280 TEST_ASSERT(rte_fbarray_find_prev_n_used(&dummy
,
281 FBARRAY_TEST_LEN
, 1) < 0,
282 "Call succeeded with invalid parameters\n");
283 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
284 TEST_ASSERT(rte_fbarray_find_prev_n_used(&dummy
, 0,
285 FBARRAY_TEST_LEN
+ 1) < 0,
286 "Call succeeded with invalid parameters\n");
287 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
288 TEST_ASSERT(rte_fbarray_find_prev_n_used(&dummy
, 0, 0) < 0,
289 "Call succeeded with invalid parameters\n");
290 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
292 TEST_ASSERT(rte_fbarray_is_used(&dummy
, FBARRAY_TEST_LEN
) < 0,
293 "Call succeeded with invalid parameters\n");
294 TEST_ASSERT_EQUAL(rte_errno
, EINVAL
, "Wrong errno value\n");
296 TEST_ASSERT_SUCCESS(rte_fbarray_destroy(&dummy
),
297 "Failed to destroy valid fbarray\n");
302 static int check_free(void)
305 const int last_idx
= FBARRAY_TEST_LEN
- 1;
307 /* ensure we can find a free spot */
308 TEST_ASSERT_EQUAL(rte_fbarray_find_next_free(¶m
.arr
, idx
), idx
,
309 "Free space not found where expected\n");
310 TEST_ASSERT_EQUAL(rte_fbarray_find_next_n_free(¶m
.arr
, idx
, 1), idx
,
311 "Free space not found where expected\n");
312 TEST_ASSERT_EQUAL(rte_fbarray_find_contig_free(¶m
.arr
, idx
),
314 "Free space not found where expected\n");
316 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_free(¶m
.arr
, idx
), idx
,
317 "Free space not found where expected\n");
318 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_n_free(¶m
.arr
, idx
, 1), idx
,
319 "Free space not found where expected\n");
320 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_free(¶m
.arr
, idx
), 1,
321 "Free space not found where expected\n");
323 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_free(¶m
.arr
, last_idx
),
324 last_idx
, "Free space not found where expected\n");
325 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_n_free(¶m
.arr
, last_idx
, 1),
326 last_idx
, "Free space not found where expected\n");
327 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_free(¶m
.arr
,
328 last_idx
), FBARRAY_TEST_LEN
,
329 "Free space not found where expected\n");
331 /* ensure we can't find any used spots */
332 TEST_ASSERT(rte_fbarray_find_next_used(¶m
.arr
, idx
) < 0,
333 "Used space found where none was expected\n");
334 TEST_ASSERT_EQUAL(rte_errno
, ENOENT
, "Wrong errno value\n");
335 TEST_ASSERT(rte_fbarray_find_next_n_used(¶m
.arr
, idx
, 1) < 0,
336 "Used space found where none was expected\n");
337 TEST_ASSERT_EQUAL(rte_errno
, ENOENT
, "Wrong errno value\n");
338 TEST_ASSERT_EQUAL(rte_fbarray_find_contig_used(¶m
.arr
, idx
), 0,
339 "Used space found where none was expected\n");
341 TEST_ASSERT(rte_fbarray_find_prev_used(¶m
.arr
, last_idx
) < 0,
342 "Used space found where none was expected\n");
343 TEST_ASSERT_EQUAL(rte_errno
, ENOENT
, "Wrong errno value\n");
344 TEST_ASSERT(rte_fbarray_find_prev_n_used(¶m
.arr
, last_idx
, 1) < 0,
345 "Used space found where none was expected\n");
346 TEST_ASSERT_EQUAL(rte_errno
, ENOENT
, "Wrong errno value\n");
347 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_used(¶m
.arr
,
349 "Used space found where none was expected\n");
354 static int check_used_one(void)
357 const int last_idx
= FBARRAY_TEST_LEN
- 1;
359 /* check that we can find used spots now */
360 TEST_ASSERT_EQUAL(rte_fbarray_find_next_used(¶m
.arr
, idx
), idx
,
361 "Used space not found where expected\n");
362 TEST_ASSERT_EQUAL(rte_fbarray_find_next_n_used(¶m
.arr
, idx
, 1), idx
,
363 "Used space not found where expected\n");
364 TEST_ASSERT_EQUAL(rte_fbarray_find_contig_used(¶m
.arr
, idx
), 1,
365 "Used space not found where expected\n");
367 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_used(¶m
.arr
, last_idx
), idx
,
368 "Used space not found where expected\n");
369 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_n_used(¶m
.arr
, last_idx
, 1),
370 idx
, "Used space not found where expected\n");
371 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_used(¶m
.arr
, idx
), 1,
372 "Used space not found where expected\n");
373 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_used(¶m
.arr
,
375 "Used space not found where expected\n");
377 /* check if further indices are still free */
378 TEST_ASSERT(rte_fbarray_find_next_used(¶m
.arr
, idx
+ 1) < 0,
379 "Used space not found where none was expected\n");
380 TEST_ASSERT_EQUAL(rte_errno
, ENOENT
, "Wrong errno value\n");
381 TEST_ASSERT(rte_fbarray_find_next_n_used(¶m
.arr
, idx
+ 1, 1) < 0,
382 "Used space not found where none was expected\n");
383 TEST_ASSERT_EQUAL(rte_errno
, ENOENT
, "Wrong errno value\n");
384 TEST_ASSERT_EQUAL(rte_fbarray_find_contig_used(¶m
.arr
, idx
+ 1), 0,
385 "Used space not found where none was expected\n");
386 TEST_ASSERT_EQUAL(rte_fbarray_find_contig_free(¶m
.arr
, idx
+ 1),
387 FBARRAY_TEST_LEN
- 1,
388 "Used space not found where none was expected\n");
390 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_used(¶m
.arr
, last_idx
), 0,
391 "Used space not found where none was expected\n");
392 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_n_used(¶m
.arr
, last_idx
, 1),
393 0, "Used space not found where none was expected\n");
394 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_used(¶m
.arr
,
396 "Used space not found where none was expected\n");
397 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_free(¶m
.arr
,
398 last_idx
), FBARRAY_TEST_LEN
- 1,
399 "Used space not found where none was expected\n");
404 static int test_basic(void)
409 /* check array count */
410 TEST_ASSERT_EQUAL(param
.arr
.count
, 0, "Wrong element count\n");
412 /* ensure we can find a free spot */
417 TEST_ASSERT_EQUAL(rte_fbarray_is_used(¶m
.arr
, idx
), 0,
418 "Used space found where not expected\n");
421 TEST_ASSERT_SUCCESS(rte_fbarray_set_used(¶m
.arr
, idx
),
422 "Failed to set as used\n");
424 /* check if used again */
425 TEST_ASSERT_NOT_EQUAL(rte_fbarray_is_used(¶m
.arr
, idx
), 0,
426 "Used space not found where expected\n");
428 if (check_used_one())
431 /* check array count */
432 TEST_ASSERT_EQUAL(param
.arr
.count
, 1, "Wrong element count\n");
434 /* check if getting pointers works for every element */
435 for (i
= 0; i
< FBARRAY_TEST_LEN
; i
++) {
436 void *td
= rte_fbarray_get(¶m
.arr
, i
);
437 TEST_ASSERT_NOT_NULL(td
, "Invalid pointer returned\n");
438 TEST_ASSERT_EQUAL(rte_fbarray_find_idx(¶m
.arr
, td
), i
,
439 "Wrong index returned\n");
443 TEST_ASSERT_SUCCESS(rte_fbarray_set_free(¶m
.arr
, idx
),
444 "Failed to set as free\n");
446 /* check array count */
447 TEST_ASSERT_EQUAL(param
.arr
.count
, 0, "Wrong element count\n");
450 TEST_ASSERT_EQUAL(rte_fbarray_is_used(¶m
.arr
, idx
), 0,
451 "Used space found where not expected\n");
461 static int test_biggest(struct rte_fbarray
*arr
, int first
, int last
)
463 int lo_free_space_first
, lo_free_space_last
, lo_free_space_len
;
464 int hi_free_space_first
, hi_free_space_last
, hi_free_space_len
;
465 int max_free_space_first
, max_free_space_last
, max_free_space_len
;
466 int len
= last
- first
+ 1;
468 /* first and last must either be both -1, or both not -1 */
469 TEST_ASSERT((first
== -1) == (last
== -1),
470 "Invalid arguments provided\n");
472 /* figure out what we expect from the low chunk of free space */
474 /* special case: if there are no occupied elements at all,
475 * consider both free spaces to consume the entire array.
477 lo_free_space_first
= 0;
478 lo_free_space_last
= arr
->len
- 1;
479 lo_free_space_len
= arr
->len
;
480 /* if there's no used space, length should be invalid */
482 } else if (first
== 0) {
483 /* if occupied items start at 0, there's no free space */
484 lo_free_space_first
= -1;
485 lo_free_space_last
= -1;
486 lo_free_space_len
= 0;
488 lo_free_space_first
= 0;
489 lo_free_space_last
= first
- 1;
490 lo_free_space_len
= lo_free_space_last
-
491 lo_free_space_first
+ 1;
494 /* figure out what we expect from the high chunk of free space */
496 /* special case: if there are no occupied elements at all,
497 * consider both free spaces to consume the entire array.
499 hi_free_space_first
= 0;
500 hi_free_space_last
= arr
->len
- 1;
501 hi_free_space_len
= arr
->len
;
502 /* if there's no used space, length should be invalid */
504 } else if (last
== ((int)arr
->len
- 1)) {
505 /* if occupied items end at array len, there's no free space */
506 hi_free_space_first
= -1;
507 hi_free_space_last
= -1;
508 hi_free_space_len
= 0;
510 hi_free_space_first
= last
+ 1;
511 hi_free_space_last
= arr
->len
- 1;
512 hi_free_space_len
= hi_free_space_last
-
513 hi_free_space_first
+ 1;
516 /* find which one will be biggest */
517 if (lo_free_space_len
> hi_free_space_len
) {
518 max_free_space_first
= lo_free_space_first
;
519 max_free_space_last
= lo_free_space_last
;
520 max_free_space_len
= lo_free_space_len
;
522 /* if they are equal, we'll just use the high chunk */
523 max_free_space_first
= hi_free_space_first
;
524 max_free_space_last
= hi_free_space_last
;
525 max_free_space_len
= hi_free_space_len
;
528 /* check used regions - these should produce identical results */
529 TEST_ASSERT_EQUAL(rte_fbarray_find_biggest_used(arr
, 0), first
,
530 "Used space index is wrong\n");
531 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_biggest_used(arr
, arr
->len
- 1),
533 "Used space index is wrong\n");
534 /* len may be -1, but function will return error anyway */
535 TEST_ASSERT_EQUAL(rte_fbarray_find_contig_used(arr
, first
), len
,
536 "Used space length is wrong\n");
538 /* check if biggest free region is the one we expect to find. It can be
539 * -1 if there's no free space - we've made sure we use one or the
540 * other, even if both are invalid.
542 TEST_ASSERT_EQUAL(rte_fbarray_find_biggest_free(arr
, 0),
543 max_free_space_first
,
544 "Biggest free space index is wrong\n");
545 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_biggest_free(arr
, arr
->len
- 1),
546 max_free_space_first
,
547 "Biggest free space index is wrong\n");
549 /* if biggest region exists, check its length */
550 if (max_free_space_first
!= -1) {
551 TEST_ASSERT_EQUAL(rte_fbarray_find_contig_free(arr
,
552 max_free_space_first
),
554 "Biggest free space length is wrong\n");
555 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_free(arr
,
556 max_free_space_last
),
558 "Biggest free space length is wrong\n");
561 /* find if we see what we expect to see in the low region. if there is
562 * no free space, the function should still match expected value, as
563 * we've set it to -1. we're scanning backwards to avoid accidentally
564 * hitting the high free space region. if there is no occupied space,
565 * there's nothing to do.
568 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_biggest_free(arr
, last
),
570 "Low free space index is wrong\n");
573 if (lo_free_space_first
!= -1) {
574 /* if low free region exists, check its length */
575 TEST_ASSERT_EQUAL(rte_fbarray_find_contig_free(arr
,
576 lo_free_space_first
),
578 "Low free space length is wrong\n");
579 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_free(arr
,
582 "Low free space length is wrong\n");
585 /* find if we see what we expect to see in the high region. if there is
586 * no free space, the function should still match expected value, as
587 * we've set it to -1. we're scanning forwards to avoid accidentally
588 * hitting the low free space region. if there is no occupied space,
589 * there's nothing to do.
592 TEST_ASSERT_EQUAL(rte_fbarray_find_biggest_free(arr
, first
),
594 "High free space index is wrong\n");
597 /* if high free region exists, check its length */
598 if (hi_free_space_first
!= -1) {
599 TEST_ASSERT_EQUAL(rte_fbarray_find_contig_free(arr
,
600 hi_free_space_first
),
602 "High free space length is wrong\n");
603 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_free(arr
,
606 "High free space length is wrong\n");
612 static int ensure_correct(struct rte_fbarray
*arr
, int first
, int last
,
615 int i
, len
= last
- first
+ 1;
616 for (i
= 0; i
< len
; i
++) {
618 int cur_len
= len
- i
;
621 TEST_ASSERT_EQUAL(rte_fbarray_find_contig_used(arr
,
623 "Used space length is wrong\n");
624 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_used(arr
,
626 "Used space length is wrong\n");
627 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_used(arr
,
629 "Used space length is wrong\n");
631 TEST_ASSERT_EQUAL(rte_fbarray_find_next_used(arr
, cur
),
633 "Used space not found where expected\n");
634 TEST_ASSERT_EQUAL(rte_fbarray_find_next_n_used(arr
,
636 "Used space not found where expected\n");
637 TEST_ASSERT_EQUAL(rte_fbarray_find_next_n_used(arr
, cur
,
639 "Used space not found where expected\n");
641 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_used(arr
, cur
),
643 "Used space not found where expected\n");
644 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_n_used(arr
,
646 "Used space not found where expected\n");
648 TEST_ASSERT_EQUAL(rte_fbarray_find_contig_free(arr
,
650 "Free space length is wrong\n");
651 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_free(arr
,
653 "Free space length is wrong\n");
654 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_free(arr
,
656 "Free space length is wrong\n");
658 TEST_ASSERT_EQUAL(rte_fbarray_find_next_free(arr
, cur
),
660 "Free space not found where expected\n");
661 TEST_ASSERT_EQUAL(rte_fbarray_find_next_n_free(arr
, cur
,
663 "Free space not found where expected\n");
664 TEST_ASSERT_EQUAL(rte_fbarray_find_next_n_free(arr
, cur
,
666 "Free space not found where expected\n");
668 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_free(arr
, cur
),
670 "Free space not found where expected\n");
671 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_n_free(arr
,
673 "Free space not found where expected\n");
679 static int test_find(void)
681 TEST_ASSERT_EQUAL((int)param
.arr
.count
, param
.end
- param
.start
+ 1,
682 "Wrong element count\n");
683 /* ensure space is free before start */
684 if (ensure_correct(¶m
.arr
, 0, param
.start
- 1, false))
686 /* ensure space is occupied where it's supposed to be */
687 if (ensure_correct(¶m
.arr
, param
.start
, param
.end
, true))
689 /* ensure space after end is free as well */
690 if (ensure_correct(¶m
.arr
, param
.end
+ 1, FBARRAY_TEST_LEN
- 1,
693 /* test if find_biggest API's work correctly */
694 if (test_biggest(¶m
.arr
, param
.start
, param
.end
))
699 static int test_empty(void)
701 TEST_ASSERT_EQUAL((int)param
.arr
.count
, 0, "Wrong element count\n");
702 /* ensure space is free */
703 if (ensure_correct(¶m
.arr
, 0, FBARRAY_TEST_LEN
- 1, false))
705 /* test if find_biggest API's work correctly */
706 if (test_biggest(¶m
.arr
, param
.start
, param
.end
))
712 static struct unit_test_suite fbarray_test_suite
= {
713 .suite_name
= "fbarray autotest",
714 .setup
= autotest_setup
,
715 .teardown
= autotest_teardown
,
717 TEST_CASE(test_invalid
),
718 TEST_CASE(test_basic
),
719 TEST_CASE_ST(first_msk_test_setup
, reset_array
, test_find
),
720 TEST_CASE_ST(cross_msk_test_setup
, reset_array
, test_find
),
721 TEST_CASE_ST(multi_msk_test_setup
, reset_array
, test_find
),
722 TEST_CASE_ST(last_msk_test_setup
, reset_array
, test_find
),
723 TEST_CASE_ST(full_msk_test_setup
, reset_array
, test_find
),
724 TEST_CASE_ST(empty_msk_test_setup
, reset_array
, test_empty
),
732 return unit_test_suite_runner(&fbarray_test_suite
);
735 REGISTER_TEST_COMMAND(fbarray_autotest
, test_fbarray
);