vppinfra: vectorized index to pointer function
[vpp.git] / src / vppinfra / vector / test / index_to_ptr.c
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright(c) 2021 Cisco Systems, Inc.
3  */
4
5 #include <vppinfra/format.h>
6 #include <vppinfra/vector/test/test.h>
7 #include <vppinfra/vector/index_to_ptr.h>
8
9 typedef void (wrapper_fn) (u32 *indices, void *base, u8 shift, void **ptrs,
10                            u32 n_elts);
11
12 __clib_test_fn void
13 clib_index_to_ptr_u32_wrapper (u32 *indices, void *base, u8 shift, void **ptrs,
14                                u32 n_elts)
15 {
16   clib_index_to_ptr_u32 (indices, base, shift, ptrs, n_elts);
17 }
18
19 static wrapper_fn *wfn = &clib_index_to_ptr_u32_wrapper;
20
21 static clib_error_t *
22 test_clib_index_to_ptr_u32 (clib_error_t *err)
23 {
24   void *_ptrs[512 + 128], **ptrs = _ptrs + 64;
25   u32 _indices[512 + 128], *indices = _indices + 64;
26   u16 lengths[] = { 1,  3,  5,  7,  9,  15, 16, 17,  31, 32,
27                     33, 40, 41, 42, 63, 64, 65, 511, 512 };
28
29   for (int i = 0; i < ARRAY_LEN (_indices); i++)
30     _indices[i] = i;
31
32   for (int i = 0; i < ARRAY_LEN (lengths); i++)
33     {
34       u16 len = lengths[i];
35       u8 shift = 6;
36       void *base = (void *) 0x100000000 + i;
37
38       for (int j = -64; j < len + 64; j++)
39         ptrs[j] = (void *) 0xfefefefefefefefe;
40
41       wfn (indices, base, shift, ptrs, len);
42       for (int j = 0; j < len; j++)
43         {
44           void *expected = base + ((u64) indices[j] << shift);
45           if (ptrs[j] != expected)
46             return clib_error_return (err,
47                                       "testcase failed for length %u "
48                                       "(offset %u, expected %p, found %p)",
49                                       len, j, expected, ptrs[j]);
50         }
51     }
52   return err;
53 }
54
55 REGISTER_TEST (clib_index_to_ptr_u32) = {
56   .name = "clib_index_to_ptr_u32",
57   .fn = test_clib_index_to_ptr_u32,
58 };