blob: 06b621c10ff7c7eac73ffc8aadffaeeaaa2d2242 [file] [log] [blame]
Damjan Marion4c53ff42021-10-28 23:03:04 +02001/* SPDX-License-Identifier: Apache-2.0
2 * Copyright(c) 2021 Cisco Systems, Inc.
3 */
4
5#include <vppinfra/format.h>
Damjan Marionc3542e12023-03-15 11:42:06 +00006#include <vppinfra/test/test.h>
Damjan Marion4c53ff42021-10-28 23:03:04 +02007#include <vppinfra/vector/index_to_ptr.h>
8
9typedef void (wrapper_fn) (u32 *indices, void *base, u8 shift, void **ptrs,
10 u32 n_elts);
11
Damjan Marion3323e202021-12-02 11:28:57 +010012__test_funct_fn void
Damjan Marion4c53ff42021-10-28 23:03:04 +020013clib_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
19static wrapper_fn *wfn = &clib_index_to_ptr_u32_wrapper;
20
21static clib_error_t *
22test_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
55REGISTER_TEST (clib_index_to_ptr_u32) = {
56 .name = "clib_index_to_ptr_u32",
57 .fn = test_clib_index_to_ptr_u32,
58};