1 /* SPDX-License-Identifier: Apache-2.0
2 * Copyright(c) 2021 Cisco Systems, Inc.
5 #include <vppinfra/format.h>
6 #include <vppinfra/test/test.h>
7 #include <vppinfra/vector/index_to_ptr.h>
9 typedef void (wrapper_fn) (u32 *indices, void *base, u8 shift, void **ptrs,
13 clib_index_to_ptr_u32_wrapper (u32 *indices, void *base, u8 shift, void **ptrs,
16 clib_index_to_ptr_u32 (indices, base, shift, ptrs, n_elts);
19 static wrapper_fn *wfn = &clib_index_to_ptr_u32_wrapper;
22 test_clib_index_to_ptr_u32 (clib_error_t *err)
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 };
29 for (int i = 0; i < ARRAY_LEN (_indices); i++)
32 for (int i = 0; i < ARRAY_LEN (lengths); i++)
36 void *base = (void *) 0x100000000 + i;
38 for (int j = -64; j < len + 64; j++)
39 ptrs[j] = (void *) 0xfefefefefefefefe;
41 wfn (indices, base, shift, ptrs, len);
42 for (int j = 0; j < len; j++)
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]);
55 REGISTER_TEST (clib_index_to_ptr_u32) = {
56 .name = "clib_index_to_ptr_u32",
57 .fn = test_clib_index_to_ptr_u32,