Damjan Marion | 4c53ff4 | 2021-10-28 23:03:04 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright(c) 2021 Cisco Systems, Inc. |
| 3 | */ |
| 4 | |
| 5 | #include <vppinfra/format.h> |
Damjan Marion | c3542e1 | 2023-03-15 11:42:06 +0000 | [diff] [blame] | 6 | #include <vppinfra/test/test.h> |
Damjan Marion | 4c53ff4 | 2021-10-28 23:03:04 +0200 | [diff] [blame] | 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 | |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 12 | __test_funct_fn void |
Damjan Marion | 4c53ff4 | 2021-10-28 23:03:04 +0200 | [diff] [blame] | 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 | }; |