]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/dpdk/app/test/test_fbarray.c
import 15.2.0 Octopus source
[ceph.git] / ceph / src / spdk / dpdk / app / test / test_fbarray.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
3 */
4
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <limits.h>
8
9 #include <rte_common.h>
10 #include <rte_debug.h>
11 #include <rte_errno.h>
12 #include <rte_fbarray.h>
13
14 #include "test.h"
15
16 struct fbarray_testsuite_params {
17 struct rte_fbarray arr;
18 int start;
19 int end;
20 };
21
22 static struct fbarray_testsuite_params param;
23
24 #define FBARRAY_TEST_ARR_NAME "fbarray_autotest"
25 #define FBARRAY_TEST_LEN 256
26 #define FBARRAY_TEST_ELT_SZ (sizeof(int))
27
28 static int autotest_setup(void)
29 {
30 return rte_fbarray_init(&param.arr, FBARRAY_TEST_ARR_NAME,
31 FBARRAY_TEST_LEN, FBARRAY_TEST_ELT_SZ);
32 }
33
34 static void autotest_teardown(void)
35 {
36 rte_fbarray_destroy(&param.arr);
37 }
38
39 static int init_array(void)
40 {
41 int i;
42 for (i = param.start; i <= param.end; i++) {
43 if (rte_fbarray_set_used(&param.arr, i))
44 return -1;
45 }
46 return 0;
47 }
48
49 static void reset_array(void)
50 {
51 int i;
52 for (i = 0; i < FBARRAY_TEST_LEN; i++)
53 rte_fbarray_set_free(&param.arr, i);
54 }
55
56 static int first_msk_test_setup(void)
57 {
58 /* put all within first mask */
59 param.start = 3;
60 param.end = 10;
61 return init_array();
62 }
63
64 static int cross_msk_test_setup(void)
65 {
66 /* put all within second and third mask */
67 param.start = 70;
68 param.end = 160;
69 return init_array();
70 }
71
72 static int multi_msk_test_setup(void)
73 {
74 /* put all within first and last mask */
75 param.start = 3;
76 param.end = FBARRAY_TEST_LEN - 20;
77 return init_array();
78 }
79
80 static int last_msk_test_setup(void)
81 {
82 /* put all within last mask */
83 param.start = FBARRAY_TEST_LEN - 20;
84 param.end = FBARRAY_TEST_LEN - 1;
85 return init_array();
86 }
87
88 static int full_msk_test_setup(void)
89 {
90 /* fill entire mask */
91 param.start = 0;
92 param.end = FBARRAY_TEST_LEN - 1;
93 return init_array();
94 }
95
96 static int empty_msk_test_setup(void)
97 {
98 /* do not fill anything in */
99 reset_array();
100 param.start = -1;
101 param.end = -1;
102 return 0;
103 }
104
105 static int test_invalid(void)
106 {
107 struct rte_fbarray dummy;
108
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");
116
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");
136
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");
188
189 TEST_ASSERT_SUCCESS(rte_fbarray_init(&dummy, "success",
190 FBARRAY_TEST_LEN, 8),
191 "Failed to initialize valid fbarray\n");
192
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");
197
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");
201
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");
205
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");
209
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");
213
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");
217
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");
222
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");
227
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");
231
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");
235
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");
239
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");
243
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");
255
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");
267
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");
279
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");
291
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");
295
296 TEST_ASSERT_SUCCESS(rte_fbarray_destroy(&dummy),
297 "Failed to destroy valid fbarray\n");
298
299 return TEST_SUCCESS;
300 }
301
302 static int check_free(void)
303 {
304 const int idx = 0;
305 const int last_idx = FBARRAY_TEST_LEN - 1;
306
307 /* ensure we can find a free spot */
308 TEST_ASSERT_EQUAL(rte_fbarray_find_next_free(&param.arr, idx), idx,
309 "Free space not found where expected\n");
310 TEST_ASSERT_EQUAL(rte_fbarray_find_next_n_free(&param.arr, idx, 1), idx,
311 "Free space not found where expected\n");
312 TEST_ASSERT_EQUAL(rte_fbarray_find_contig_free(&param.arr, idx),
313 FBARRAY_TEST_LEN,
314 "Free space not found where expected\n");
315
316 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_free(&param.arr, idx), idx,
317 "Free space not found where expected\n");
318 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_n_free(&param.arr, idx, 1), idx,
319 "Free space not found where expected\n");
320 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_free(&param.arr, idx), 1,
321 "Free space not found where expected\n");
322
323 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_free(&param.arr, last_idx),
324 last_idx, "Free space not found where expected\n");
325 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_n_free(&param.arr, last_idx, 1),
326 last_idx, "Free space not found where expected\n");
327 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_free(&param.arr,
328 last_idx), FBARRAY_TEST_LEN,
329 "Free space not found where expected\n");
330
331 /* ensure we can't find any used spots */
332 TEST_ASSERT(rte_fbarray_find_next_used(&param.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(&param.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(&param.arr, idx), 0,
339 "Used space found where none was expected\n");
340
341 TEST_ASSERT(rte_fbarray_find_prev_used(&param.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(&param.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(&param.arr,
348 last_idx), 0,
349 "Used space found where none was expected\n");
350
351 return 0;
352 }
353
354 static int check_used_one(void)
355 {
356 const int idx = 0;
357 const int last_idx = FBARRAY_TEST_LEN - 1;
358
359 /* check that we can find used spots now */
360 TEST_ASSERT_EQUAL(rte_fbarray_find_next_used(&param.arr, idx), idx,
361 "Used space not found where expected\n");
362 TEST_ASSERT_EQUAL(rte_fbarray_find_next_n_used(&param.arr, idx, 1), idx,
363 "Used space not found where expected\n");
364 TEST_ASSERT_EQUAL(rte_fbarray_find_contig_used(&param.arr, idx), 1,
365 "Used space not found where expected\n");
366
367 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_used(&param.arr, last_idx), idx,
368 "Used space not found where expected\n");
369 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_n_used(&param.arr, last_idx, 1),
370 idx, "Used space not found where expected\n");
371 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_used(&param.arr, idx), 1,
372 "Used space not found where expected\n");
373 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_used(&param.arr,
374 last_idx), idx,
375 "Used space not found where expected\n");
376
377 /* check if further indices are still free */
378 TEST_ASSERT(rte_fbarray_find_next_used(&param.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(&param.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(&param.arr, idx + 1), 0,
385 "Used space not found where none was expected\n");
386 TEST_ASSERT_EQUAL(rte_fbarray_find_contig_free(&param.arr, idx + 1),
387 FBARRAY_TEST_LEN - 1,
388 "Used space not found where none was expected\n");
389
390 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_used(&param.arr, last_idx), 0,
391 "Used space not found where none was expected\n");
392 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_n_used(&param.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(&param.arr,
395 last_idx), 0,
396 "Used space not found where none was expected\n");
397 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_free(&param.arr,
398 last_idx), FBARRAY_TEST_LEN - 1,
399 "Used space not found where none was expected\n");
400
401 return 0;
402 }
403
404 static int test_basic(void)
405 {
406 const int idx = 0;
407 int i;
408
409 /* check array count */
410 TEST_ASSERT_EQUAL(param.arr.count, 0, "Wrong element count\n");
411
412 /* ensure we can find a free spot */
413 if (check_free())
414 return TEST_FAILED;
415
416 /* check if used */
417 TEST_ASSERT_EQUAL(rte_fbarray_is_used(&param.arr, idx), 0,
418 "Used space found where not expected\n");
419
420 /* mark as used */
421 TEST_ASSERT_SUCCESS(rte_fbarray_set_used(&param.arr, idx),
422 "Failed to set as used\n");
423
424 /* check if used again */
425 TEST_ASSERT_NOT_EQUAL(rte_fbarray_is_used(&param.arr, idx), 0,
426 "Used space not found where expected\n");
427
428 if (check_used_one())
429 return TEST_FAILED;
430
431 /* check array count */
432 TEST_ASSERT_EQUAL(param.arr.count, 1, "Wrong element count\n");
433
434 /* check if getting pointers works for every element */
435 for (i = 0; i < FBARRAY_TEST_LEN; i++) {
436 void *td = rte_fbarray_get(&param.arr, i);
437 TEST_ASSERT_NOT_NULL(td, "Invalid pointer returned\n");
438 TEST_ASSERT_EQUAL(rte_fbarray_find_idx(&param.arr, td), i,
439 "Wrong index returned\n");
440 }
441
442 /* mark as free */
443 TEST_ASSERT_SUCCESS(rte_fbarray_set_free(&param.arr, idx),
444 "Failed to set as free\n");
445
446 /* check array count */
447 TEST_ASSERT_EQUAL(param.arr.count, 0, "Wrong element count\n");
448
449 /* check if used */
450 TEST_ASSERT_EQUAL(rte_fbarray_is_used(&param.arr, idx), 0,
451 "Used space found where not expected\n");
452
453 if (check_free())
454 return TEST_FAILED;
455
456 reset_array();
457
458 return TEST_SUCCESS;
459 }
460
461 static int test_biggest(struct rte_fbarray *arr, int first, int last)
462 {
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;
467
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");
471
472 /* figure out what we expect from the low chunk of free space */
473 if (first == -1) {
474 /* special case: if there are no occupied elements at all,
475 * consider both free spaces to consume the entire array.
476 */
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 */
481 len = -1;
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;
487 } else {
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;
492 }
493
494 /* figure out what we expect from the high chunk of free space */
495 if (last == -1) {
496 /* special case: if there are no occupied elements at all,
497 * consider both free spaces to consume the entire array.
498 */
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 */
503 len = -1;
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;
509 } else {
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;
514 }
515
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;
521 } else {
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;
526 }
527
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),
532 first,
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");
537
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.
541 */
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");
548
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),
553 max_free_space_len,
554 "Biggest free space length is wrong\n");
555 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_free(arr,
556 max_free_space_last),
557 max_free_space_len,
558 "Biggest free space length is wrong\n");
559 }
560
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.
566 */
567 if (last != -1) {
568 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_biggest_free(arr, last),
569 lo_free_space_first,
570 "Low free space index is wrong\n");
571 }
572
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),
577 lo_free_space_len,
578 "Low free space length is wrong\n");
579 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_free(arr,
580 lo_free_space_last),
581 lo_free_space_len,
582 "Low free space length is wrong\n");
583 }
584
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.
590 */
591 if (first != -1) {
592 TEST_ASSERT_EQUAL(rte_fbarray_find_biggest_free(arr, first),
593 hi_free_space_first,
594 "High free space index is wrong\n");
595 }
596
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),
601 hi_free_space_len,
602 "High free space length is wrong\n");
603 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_free(arr,
604 hi_free_space_last),
605 hi_free_space_len,
606 "High free space length is wrong\n");
607 }
608
609 return 0;
610 }
611
612 static int ensure_correct(struct rte_fbarray *arr, int first, int last,
613 bool used)
614 {
615 int i, len = last - first + 1;
616 for (i = 0; i < len; i++) {
617 int cur = first + i;
618 int cur_len = len - i;
619
620 if (used) {
621 TEST_ASSERT_EQUAL(rte_fbarray_find_contig_used(arr,
622 cur), cur_len,
623 "Used space length is wrong\n");
624 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_used(arr,
625 last), len,
626 "Used space length is wrong\n");
627 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_used(arr,
628 cur), i + 1,
629 "Used space length is wrong\n");
630
631 TEST_ASSERT_EQUAL(rte_fbarray_find_next_used(arr, cur),
632 cur,
633 "Used space not found where expected\n");
634 TEST_ASSERT_EQUAL(rte_fbarray_find_next_n_used(arr,
635 cur, 1), cur,
636 "Used space not found where expected\n");
637 TEST_ASSERT_EQUAL(rte_fbarray_find_next_n_used(arr, cur,
638 cur_len), cur,
639 "Used space not found where expected\n");
640
641 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_used(arr, cur),
642 cur,
643 "Used space not found where expected\n");
644 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_n_used(arr,
645 last, cur_len), cur,
646 "Used space not found where expected\n");
647 } else {
648 TEST_ASSERT_EQUAL(rte_fbarray_find_contig_free(arr,
649 cur), cur_len,
650 "Free space length is wrong\n");
651 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_free(arr,
652 last), len,
653 "Free space length is wrong\n");
654 TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_free(arr,
655 cur), i + 1,
656 "Free space length is wrong\n");
657
658 TEST_ASSERT_EQUAL(rte_fbarray_find_next_free(arr, cur),
659 cur,
660 "Free space not found where expected\n");
661 TEST_ASSERT_EQUAL(rte_fbarray_find_next_n_free(arr, cur,
662 1), cur,
663 "Free space not found where expected\n");
664 TEST_ASSERT_EQUAL(rte_fbarray_find_next_n_free(arr, cur,
665 cur_len), cur,
666 "Free space not found where expected\n");
667
668 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_free(arr, cur),
669 cur,
670 "Free space not found where expected\n");
671 TEST_ASSERT_EQUAL(rte_fbarray_find_prev_n_free(arr,
672 last, cur_len), cur,
673 "Free space not found where expected\n");
674 }
675 }
676 return 0;
677 }
678
679 static int test_find(void)
680 {
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(&param.arr, 0, param.start - 1, false))
685 return TEST_FAILED;
686 /* ensure space is occupied where it's supposed to be */
687 if (ensure_correct(&param.arr, param.start, param.end, true))
688 return TEST_FAILED;
689 /* ensure space after end is free as well */
690 if (ensure_correct(&param.arr, param.end + 1, FBARRAY_TEST_LEN - 1,
691 false))
692 return TEST_FAILED;
693 /* test if find_biggest API's work correctly */
694 if (test_biggest(&param.arr, param.start, param.end))
695 return TEST_FAILED;
696 return TEST_SUCCESS;
697 }
698
699 static int test_empty(void)
700 {
701 TEST_ASSERT_EQUAL((int)param.arr.count, 0, "Wrong element count\n");
702 /* ensure space is free */
703 if (ensure_correct(&param.arr, 0, FBARRAY_TEST_LEN - 1, false))
704 return TEST_FAILED;
705 /* test if find_biggest API's work correctly */
706 if (test_biggest(&param.arr, param.start, param.end))
707 return TEST_FAILED;
708 return TEST_SUCCESS;
709 }
710
711
712 static struct unit_test_suite fbarray_test_suite = {
713 .suite_name = "fbarray autotest",
714 .setup = autotest_setup,
715 .teardown = autotest_teardown,
716 .unit_test_cases = {
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),
725 TEST_CASES_END()
726 }
727 };
728
729 static int
730 test_fbarray(void)
731 {
732 return unit_test_suite_runner(&fbarray_test_suite);
733 }
734
735 REGISTER_TEST_COMMAND(fbarray_autotest, test_fbarray);