New upstream version 18.08
[deb_dpdk.git] / test / 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         return 0;
101 }
102
103 static int test_invalid(void)
104 {
105         struct rte_fbarray dummy;
106
107         /* invalid parameters */
108         TEST_ASSERT_FAIL(rte_fbarray_attach(NULL),
109                         "Call succeeded with invalid parameters\n");
110         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
111         TEST_ASSERT_FAIL(rte_fbarray_detach(NULL),
112                         "Call succeeded with invalid parameters\n");
113         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
114
115         TEST_ASSERT_FAIL(rte_fbarray_destroy(NULL),
116                         "Call succeeded with invalid parameters\n");
117         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno valuey\n");
118         TEST_ASSERT_FAIL(rte_fbarray_init(NULL, "fail", 16, 16),
119                         "Call succeeded with invalid parameters\n");
120         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
121         TEST_ASSERT_FAIL(rte_fbarray_init(&dummy, NULL, 16, 16),
122                         "Call succeeded with invalid parameters\n");
123         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
124         TEST_ASSERT_FAIL(rte_fbarray_init(&dummy, "fail", 0, 16),
125                         "Call succeeded with invalid parameters\n");
126         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
127         TEST_ASSERT_FAIL(rte_fbarray_init(&dummy, "fail", 16, 0),
128                         "Call succeeded with invalid parameters\n");
129         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
130         /* len must not be greater than INT_MAX */
131         TEST_ASSERT_FAIL(rte_fbarray_init(&dummy, "fail", INT_MAX + 1U, 16),
132                         "Call succeeded with invalid parameters\n");
133         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
134
135         TEST_ASSERT_NULL(rte_fbarray_get(NULL, 0),
136                         "Call succeeded with invalid parameters\n");
137         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
138         TEST_ASSERT(rte_fbarray_find_idx(NULL, 0) < 0,
139                         "Call succeeded with invalid parameters\n");
140         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
141         TEST_ASSERT(rte_fbarray_set_free(NULL, 0),
142                         "Call succeeded with invalid parameters\n");
143         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
144         TEST_ASSERT(rte_fbarray_set_used(NULL, 0),
145                         "Call succeeded with invalid parameters\n");
146         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
147         TEST_ASSERT(rte_fbarray_find_contig_free(NULL, 0) < 0,
148                         "Call succeeded with invalid parameters\n");
149         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
150         TEST_ASSERT(rte_fbarray_find_contig_used(NULL, 0) < 0,
151                         "Call succeeded with invalid parameters\n");
152         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
153         TEST_ASSERT(rte_fbarray_find_rev_contig_free(NULL, 0) < 0,
154                         "Call succeeded with invalid parameters\n");
155         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
156         TEST_ASSERT(rte_fbarray_find_rev_contig_used(NULL, 0) < 0,
157                         "Call succeeded with invalid parameters\n");
158         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
159         TEST_ASSERT(rte_fbarray_find_next_free(NULL, 0) < 0,
160                         "Call succeeded with invalid parameters\n");
161         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
162         TEST_ASSERT(rte_fbarray_find_next_used(NULL, 0) < 0,
163                         "Call succeeded with invalid parameters\n");
164         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
165         TEST_ASSERT(rte_fbarray_find_prev_free(NULL, 0) < 0,
166                         "Call succeeded with invalid parameters\n");
167         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
168         TEST_ASSERT(rte_fbarray_find_prev_used(NULL, 0) < 0,
169                         "Call succeeded with invalid parameters\n");
170         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
171         TEST_ASSERT(rte_fbarray_find_next_n_free(NULL, 0, 0) < 0,
172                         "Call succeeded with invalid parameters\n");
173         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
174         TEST_ASSERT(rte_fbarray_find_next_n_used(NULL, 0, 0) < 0,
175                         "Call succeeded with invalid parameters\n");
176         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
177         TEST_ASSERT(rte_fbarray_find_prev_n_free(NULL, 0, 0) < 0,
178                         "Call succeeded with invalid parameters\n");
179         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
180         TEST_ASSERT(rte_fbarray_find_prev_n_used(NULL, 0, 0) < 0,
181                         "Call succeeded with invalid parameters\n");
182         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
183         TEST_ASSERT(rte_fbarray_is_used(NULL, 0) < 0,
184                         "Call succeeded with invalid parameters\n");
185         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
186
187         TEST_ASSERT_SUCCESS(rte_fbarray_init(&dummy, "success",
188                         FBARRAY_TEST_LEN, 8),
189                         "Failed to initialize valid fbarray\n");
190
191         /* test API for handling invalid parameters with a valid fbarray */
192         TEST_ASSERT_NULL(rte_fbarray_get(&dummy, FBARRAY_TEST_LEN),
193                         "Call succeeded with invalid parameters\n");
194         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
195
196         TEST_ASSERT(rte_fbarray_find_idx(&dummy, NULL) < 0,
197                         "Call succeeded with invalid parameters\n");
198         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
199
200         TEST_ASSERT(rte_fbarray_set_free(&dummy, FBARRAY_TEST_LEN),
201                         "Call succeeded with invalid parameters\n");
202         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
203
204         TEST_ASSERT(rte_fbarray_set_used(&dummy, FBARRAY_TEST_LEN),
205                         "Call succeeded with invalid parameters\n");
206         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
207
208         TEST_ASSERT(rte_fbarray_find_contig_free(&dummy, FBARRAY_TEST_LEN) < 0,
209                         "Call succeeded with invalid parameters\n");
210         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
211
212         TEST_ASSERT(rte_fbarray_find_contig_used(&dummy, FBARRAY_TEST_LEN) < 0,
213                         "Call succeeded with invalid parameters\n");
214         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
215
216         TEST_ASSERT(rte_fbarray_find_rev_contig_free(&dummy,
217                         FBARRAY_TEST_LEN) < 0,
218                         "Call succeeded with invalid parameters\n");
219         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
220
221         TEST_ASSERT(rte_fbarray_find_rev_contig_used(&dummy,
222                         FBARRAY_TEST_LEN) < 0,
223                         "Call succeeded with invalid parameters\n");
224         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
225
226         TEST_ASSERT(rte_fbarray_find_next_free(&dummy, FBARRAY_TEST_LEN) < 0,
227                         "Call succeeded with invalid parameters\n");
228         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
229
230         TEST_ASSERT(rte_fbarray_find_next_used(&dummy, FBARRAY_TEST_LEN) < 0,
231                         "Call succeeded with invalid parameters\n");
232         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
233
234         TEST_ASSERT(rte_fbarray_find_prev_free(&dummy, FBARRAY_TEST_LEN) < 0,
235                         "Call succeeded with invalid parameters\n");
236         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
237
238         TEST_ASSERT(rte_fbarray_find_prev_used(&dummy, FBARRAY_TEST_LEN) < 0,
239                         "Call succeeded with invalid parameters\n");
240         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
241
242         TEST_ASSERT(rte_fbarray_find_next_n_free(&dummy,
243                         FBARRAY_TEST_LEN, 1) < 0,
244                         "Call succeeded with invalid parameters\n");
245         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
246         TEST_ASSERT(rte_fbarray_find_next_n_free(&dummy, 0,
247                         FBARRAY_TEST_LEN + 1) < 0,
248                         "Call succeeded with invalid parameters\n");
249         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
250         TEST_ASSERT(rte_fbarray_find_next_n_free(&dummy, 0, 0) < 0,
251                         "Call succeeded with invalid parameters\n");
252         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
253
254         TEST_ASSERT(rte_fbarray_find_next_n_used(&dummy,
255                         FBARRAY_TEST_LEN, 1) < 0,
256                         "Call succeeded with invalid parameters\n");
257         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
258         TEST_ASSERT(rte_fbarray_find_next_n_used(&dummy, 0,
259                         FBARRAY_TEST_LEN + 1) < 0,
260                         "Call succeeded with invalid parameters\n");
261         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
262         TEST_ASSERT(rte_fbarray_find_next_n_used(&dummy, 0, 0) < 0,
263                         "Call succeeded with invalid parameters\n");
264         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
265
266         TEST_ASSERT(rte_fbarray_find_prev_n_free(&dummy,
267                         FBARRAY_TEST_LEN, 1) < 0,
268                         "Call succeeded with invalid parameters\n");
269         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
270         TEST_ASSERT(rte_fbarray_find_prev_n_free(&dummy, 0,
271                         FBARRAY_TEST_LEN + 1) < 0,
272                         "Call succeeded with invalid parameters\n");
273         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
274         TEST_ASSERT(rte_fbarray_find_prev_n_free(&dummy, 0, 0) < 0,
275                         "Call succeeded with invalid parameters\n");
276         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
277
278         TEST_ASSERT(rte_fbarray_find_prev_n_used(&dummy,
279                         FBARRAY_TEST_LEN, 1) < 0,
280                         "Call succeeded with invalid parameters\n");
281         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
282         TEST_ASSERT(rte_fbarray_find_prev_n_used(&dummy, 0,
283                         FBARRAY_TEST_LEN + 1) < 0,
284                         "Call succeeded with invalid parameters\n");
285         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
286         TEST_ASSERT(rte_fbarray_find_prev_n_used(&dummy, 0, 0) < 0,
287                         "Call succeeded with invalid parameters\n");
288         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
289
290         TEST_ASSERT(rte_fbarray_is_used(&dummy, FBARRAY_TEST_LEN) < 0,
291                         "Call succeeded with invalid parameters\n");
292         TEST_ASSERT_EQUAL(rte_errno, EINVAL, "Wrong errno value\n");
293
294         TEST_ASSERT_SUCCESS(rte_fbarray_destroy(&dummy),
295                         "Failed to destroy valid fbarray\n");
296
297         return TEST_SUCCESS;
298 }
299
300 static int check_free(void)
301 {
302         const int idx = 0;
303         const int last_idx = FBARRAY_TEST_LEN - 1;
304
305         /* ensure we can find a free spot */
306         TEST_ASSERT_EQUAL(rte_fbarray_find_next_free(&param.arr, idx), idx,
307                         "Free space not found where expected\n");
308         TEST_ASSERT_EQUAL(rte_fbarray_find_next_n_free(&param.arr, idx, 1), idx,
309                         "Free space not found where expected\n");
310         TEST_ASSERT_EQUAL(rte_fbarray_find_contig_free(&param.arr, idx),
311                         FBARRAY_TEST_LEN,
312                         "Free space not found where expected\n");
313
314         TEST_ASSERT_EQUAL(rte_fbarray_find_prev_free(&param.arr, idx), idx,
315                         "Free space not found where expected\n");
316         TEST_ASSERT_EQUAL(rte_fbarray_find_prev_n_free(&param.arr, idx, 1), idx,
317                         "Free space not found where expected\n");
318         TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_free(&param.arr, idx), 1,
319                         "Free space not found where expected\n");
320
321         TEST_ASSERT_EQUAL(rte_fbarray_find_prev_free(&param.arr, last_idx),
322                         last_idx, "Free space not found where expected\n");
323         TEST_ASSERT_EQUAL(rte_fbarray_find_prev_n_free(&param.arr, last_idx, 1),
324                         last_idx, "Free space not found where expected\n");
325         TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_free(&param.arr,
326                         last_idx), FBARRAY_TEST_LEN,
327                         "Free space not found where expected\n");
328
329         /* ensure we can't find any used spots */
330         TEST_ASSERT(rte_fbarray_find_next_used(&param.arr, idx) < 0,
331                         "Used space found where none was expected\n");
332         TEST_ASSERT_EQUAL(rte_errno, ENOENT, "Wrong errno value\n");
333         TEST_ASSERT(rte_fbarray_find_next_n_used(&param.arr, idx, 1) < 0,
334                         "Used space found where none was expected\n");
335         TEST_ASSERT_EQUAL(rte_errno, ENOENT, "Wrong errno value\n");
336         TEST_ASSERT_EQUAL(rte_fbarray_find_contig_used(&param.arr, idx), 0,
337                         "Used space found where none was expected\n");
338
339         TEST_ASSERT(rte_fbarray_find_prev_used(&param.arr, last_idx) < 0,
340                         "Used space found where none was expected\n");
341         TEST_ASSERT_EQUAL(rte_errno, ENOENT, "Wrong errno value\n");
342         TEST_ASSERT(rte_fbarray_find_prev_n_used(&param.arr, last_idx, 1) < 0,
343                         "Used space found where none was expected\n");
344         TEST_ASSERT_EQUAL(rte_errno, ENOENT, "Wrong errno value\n");
345         TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_used(&param.arr,
346                         last_idx), 0,
347                         "Used space found where none was expected\n");
348
349         return 0;
350 }
351
352 static int check_used_one(void)
353 {
354         const int idx = 0;
355         const int last_idx = FBARRAY_TEST_LEN - 1;
356
357         /* check that we can find used spots now */
358         TEST_ASSERT_EQUAL(rte_fbarray_find_next_used(&param.arr, idx), idx,
359                         "Used space not found where expected\n");
360         TEST_ASSERT_EQUAL(rte_fbarray_find_next_n_used(&param.arr, idx, 1), idx,
361                         "Used space not found where expected\n");
362         TEST_ASSERT_EQUAL(rte_fbarray_find_contig_used(&param.arr, idx), 1,
363                         "Used space not found where expected\n");
364
365         TEST_ASSERT_EQUAL(rte_fbarray_find_prev_used(&param.arr, last_idx), idx,
366                         "Used space not found where expected\n");
367         TEST_ASSERT_EQUAL(rte_fbarray_find_prev_n_used(&param.arr, last_idx, 1),
368                         idx, "Used space not found where expected\n");
369         TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_used(&param.arr, idx), 1,
370                         "Used space not found where expected\n");
371         TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_used(&param.arr,
372                         last_idx), idx,
373                         "Used space not found where expected\n");
374
375         /* check if further indices are still free */
376         TEST_ASSERT(rte_fbarray_find_next_used(&param.arr, idx + 1) < 0,
377                         "Used space not found where none was expected\n");
378         TEST_ASSERT_EQUAL(rte_errno, ENOENT, "Wrong errno value\n");
379         TEST_ASSERT(rte_fbarray_find_next_n_used(&param.arr, idx + 1, 1) < 0,
380                         "Used space not found where none was expected\n");
381         TEST_ASSERT_EQUAL(rte_errno, ENOENT, "Wrong errno value\n");
382         TEST_ASSERT_EQUAL(rte_fbarray_find_contig_used(&param.arr, idx + 1), 0,
383                         "Used space not found where none was expected\n");
384         TEST_ASSERT_EQUAL(rte_fbarray_find_contig_free(&param.arr, idx + 1),
385                         FBARRAY_TEST_LEN - 1,
386                         "Used space not found where none was expected\n");
387
388         TEST_ASSERT_EQUAL(rte_fbarray_find_prev_used(&param.arr, last_idx), 0,
389                         "Used space not found where none was expected\n");
390         TEST_ASSERT_EQUAL(rte_fbarray_find_prev_n_used(&param.arr, last_idx, 1),
391                         0, "Used space not found where none was expected\n");
392         TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_used(&param.arr,
393                         last_idx), 0,
394                         "Used space not found where none was expected\n");
395         TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_free(&param.arr,
396                         last_idx), FBARRAY_TEST_LEN - 1,
397                         "Used space not found where none was expected\n");
398
399         return 0;
400 }
401
402 static int test_basic(void)
403 {
404         const int idx = 0;
405         int i;
406
407         /* check array count */
408         TEST_ASSERT_EQUAL(param.arr.count, 0, "Wrong element count\n");
409
410         /* ensure we can find a free spot */
411         if (check_free())
412                 return TEST_FAILED;
413
414         /* check if used */
415         TEST_ASSERT_EQUAL(rte_fbarray_is_used(&param.arr, idx), 0,
416                         "Used space found where not expected\n");
417
418         /* mark as used */
419         TEST_ASSERT_SUCCESS(rte_fbarray_set_used(&param.arr, idx),
420                         "Failed to set as used\n");
421
422         /* check if used again */
423         TEST_ASSERT_NOT_EQUAL(rte_fbarray_is_used(&param.arr, idx), 0,
424                         "Used space not found where expected\n");
425
426         if (check_used_one())
427                 return TEST_FAILED;
428
429         /* check array count */
430         TEST_ASSERT_EQUAL(param.arr.count, 1, "Wrong element count\n");
431
432         /* check if getting pointers works for every element */
433         for (i = 0; i < FBARRAY_TEST_LEN; i++) {
434                 void *td = rte_fbarray_get(&param.arr, i);
435                 TEST_ASSERT_NOT_NULL(td, "Invalid pointer returned\n");
436                 TEST_ASSERT_EQUAL(rte_fbarray_find_idx(&param.arr, td), i,
437                                 "Wrong index returned\n");
438         }
439
440         /* mark as free */
441         TEST_ASSERT_SUCCESS(rte_fbarray_set_free(&param.arr, idx),
442                         "Failed to set as free\n");
443
444         /* check array count */
445         TEST_ASSERT_EQUAL(param.arr.count, 0, "Wrong element count\n");
446
447         /* check if used */
448         TEST_ASSERT_EQUAL(rte_fbarray_is_used(&param.arr, idx), 0,
449                         "Used space found where not expected\n");
450
451         if (check_free())
452                 return TEST_FAILED;
453
454         reset_array();
455
456         return TEST_SUCCESS;
457 }
458
459 static int ensure_correct(struct rte_fbarray *arr, int first, int last,
460                 bool used)
461 {
462         int i, len = last - first + 1;
463         for (i = 0; i < len; i++) {
464                 int cur = first + i;
465                 int cur_len = len - i;
466
467                 if (used) {
468                         TEST_ASSERT_EQUAL(rte_fbarray_find_contig_used(arr,
469                                         cur), cur_len,
470                                         "Used space length is wrong\n");
471                         TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_used(arr,
472                                         last), len,
473                                         "Used space length is wrong\n");
474                         TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_used(arr,
475                                         cur), i + 1,
476                                         "Used space length is wrong\n");
477
478                         TEST_ASSERT_EQUAL(rte_fbarray_find_next_used(arr, cur),
479                                         cur,
480                                         "Used space not found where expected\n");
481                         TEST_ASSERT_EQUAL(rte_fbarray_find_next_n_used(arr,
482                                         cur, 1), cur,
483                                         "Used space not found where expected\n");
484                         TEST_ASSERT_EQUAL(rte_fbarray_find_next_n_used(arr, cur,
485                                         cur_len), cur,
486                                         "Used space not found where expected\n");
487
488                         TEST_ASSERT_EQUAL(rte_fbarray_find_prev_used(arr, cur),
489                                         cur,
490                                         "Used space not found where expected\n");
491                         TEST_ASSERT_EQUAL(rte_fbarray_find_prev_n_used(arr,
492                                         last, cur_len), cur,
493                                         "Used space not found where expected\n");
494                 } else {
495                         TEST_ASSERT_EQUAL(rte_fbarray_find_contig_free(arr,
496                                         cur), cur_len,
497                                         "Free space length is wrong\n");
498                         TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_free(arr,
499                                         last), len,
500                                         "Free space length is wrong\n");
501                         TEST_ASSERT_EQUAL(rte_fbarray_find_rev_contig_free(arr,
502                                         cur), i + 1,
503                                         "Free space length is wrong\n");
504
505                         TEST_ASSERT_EQUAL(rte_fbarray_find_next_free(arr, cur),
506                                         cur,
507                                         "Free space not found where expected\n");
508                         TEST_ASSERT_EQUAL(rte_fbarray_find_next_n_free(arr, cur,
509                                         1), cur,
510                                         "Free space not found where expected\n");
511                         TEST_ASSERT_EQUAL(rte_fbarray_find_next_n_free(arr, cur,
512                                         cur_len), cur,
513                                         "Free space not found where expected\n");
514
515                         TEST_ASSERT_EQUAL(rte_fbarray_find_prev_free(arr, cur),
516                                         cur,
517                                         "Free space not found where expected\n");
518                         TEST_ASSERT_EQUAL(rte_fbarray_find_prev_n_free(arr,
519                                         last, cur_len), cur,
520                                         "Free space not found where expected\n");
521                 }
522         }
523         return 0;
524 }
525
526 static int test_find(void)
527 {
528         TEST_ASSERT_EQUAL((int)param.arr.count, param.end - param.start + 1,
529                         "Wrong element count\n");
530         /* ensure space is free before start */
531         if (ensure_correct(&param.arr, 0, param.start - 1, false))
532                 return TEST_FAILED;
533         /* ensure space is occupied where it's supposed to be */
534         if (ensure_correct(&param.arr, param.start, param.end, true))
535                 return TEST_FAILED;
536         /* ensure space after end is free as well */
537         if (ensure_correct(&param.arr, param.end + 1, FBARRAY_TEST_LEN - 1,
538                         false))
539                 return TEST_FAILED;
540         return TEST_SUCCESS;
541 }
542
543 static int test_empty(void)
544 {
545         TEST_ASSERT_EQUAL((int)param.arr.count, 0, "Wrong element count\n");
546         /* ensure space is free */
547         if (ensure_correct(&param.arr, 0, FBARRAY_TEST_LEN - 1, false))
548                 return TEST_FAILED;
549         return TEST_SUCCESS;
550 }
551
552
553 static struct unit_test_suite fbarray_test_suite = {
554         .suite_name = "fbarray autotest",
555         .setup = autotest_setup,
556         .teardown = autotest_teardown,
557         .unit_test_cases = {
558                 TEST_CASE(test_invalid),
559                 TEST_CASE(test_basic),
560                 TEST_CASE_ST(first_msk_test_setup, reset_array, test_find),
561                 TEST_CASE_ST(cross_msk_test_setup, reset_array, test_find),
562                 TEST_CASE_ST(multi_msk_test_setup, reset_array, test_find),
563                 TEST_CASE_ST(last_msk_test_setup, reset_array, test_find),
564                 TEST_CASE_ST(full_msk_test_setup, reset_array, test_find),
565                 TEST_CASE_ST(empty_msk_test_setup, reset_array, test_empty),
566                 TEST_CASES_END()
567         }
568 };
569
570 static int
571 test_fbarray(void)
572 {
573         return unit_test_suite_runner(&fbarray_test_suite);
574 }
575
576 REGISTER_TEST_COMMAND(fbarray_autotest, test_fbarray);