Damjan Marion | 66b057e | 2021-11-06 13:26:58 +0100 | [diff] [blame] | 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/count_equal.h> |
| 8 | |
| 9 | #define foreach_clib_count_equal(type) \ |
| 10 | typedef uword (wrapper_fn_##type) (type * a, uword maxcount); \ |
| 11 | \ |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 12 | __test_funct_fn uword clib_count_equal_##type##_wrapper (type *a, \ |
| 13 | uword maxcount) \ |
Damjan Marion | 66b057e | 2021-11-06 13:26:58 +0100 | [diff] [blame] | 14 | { \ |
| 15 | return clib_count_equal_##type (a, maxcount); \ |
| 16 | } \ |
| 17 | \ |
| 18 | static wrapper_fn_##type *wfn_##type = &clib_count_equal_##type##_wrapper; \ |
| 19 | static clib_error_t *test_clib_count_equal_##type (clib_error_t *err) \ |
| 20 | { \ |
| 21 | u32 ps = clib_mem_get_log2_page_size (); \ |
| 22 | void *map; \ |
| 23 | \ |
| 24 | u16 lengths[] = { \ |
| 25 | 1, 2, 3, 5, 7, 9, 15, 16, 17, 31, 32, 33, 255, 256, 257 \ |
| 26 | }; \ |
| 27 | type *data; \ |
| 28 | \ |
| 29 | map = clib_mem_vm_map (0, 2ULL << ps, ps, "test"); \ |
| 30 | if (map == CLIB_MEM_VM_MAP_FAILED) \ |
| 31 | return clib_error_return (err, "clib_mem_vm_map failed"); \ |
| 32 | \ |
| 33 | data = ((type *) (map + (1ULL << ps))); \ |
| 34 | data[-1] = 0xfe; \ |
| 35 | \ |
| 36 | mprotect (data, 1ULL < ps, PROT_NONE); \ |
| 37 | \ |
Dmitry Valter | 923325f | 2021-11-14 17:05:44 +0000 | [diff] [blame] | 38 | for (u8 d = 0; d < 255; d++) \ |
Damjan Marion | 66b057e | 2021-11-06 13:26:58 +0100 | [diff] [blame] | 39 | { \ |
Dmitry Valter | 923325f | 2021-11-14 17:05:44 +0000 | [diff] [blame] | 40 | for (int i = 1; i <= (1 << ps) / sizeof (data[0]); i++) \ |
| 41 | data[-i] = d; \ |
| 42 | for (int i = 0; i < ARRAY_LEN (lengths); i++) \ |
Damjan Marion | 66b057e | 2021-11-06 13:26:58 +0100 | [diff] [blame] | 43 | { \ |
Dmitry Valter | 923325f | 2021-11-14 17:05:44 +0000 | [diff] [blame] | 44 | uword rv, len = lengths[i]; \ |
Damjan Marion | 66b057e | 2021-11-06 13:26:58 +0100 | [diff] [blame] | 45 | \ |
Dmitry Valter | 923325f | 2021-11-14 17:05:44 +0000 | [diff] [blame] | 46 | if ((rv = wfn_##type (data - len, len)) != len) \ |
| 47 | { \ |
| 48 | err = clib_error_return ( \ |
| 49 | err, "testcase 1 failed for len %u data %u(rv %u)", len, d, \ |
| 50 | rv); \ |
| 51 | goto done; \ |
| 52 | } \ |
Damjan Marion | 66b057e | 2021-11-06 13:26:58 +0100 | [diff] [blame] | 53 | \ |
Dmitry Valter | 923325f | 2021-11-14 17:05:44 +0000 | [diff] [blame] | 54 | data[-1] = d + 1; \ |
| 55 | if (len > 1 && ((rv = wfn_##type (data - len, len)) != len - 1)) \ |
| 56 | { \ |
| 57 | err = clib_error_return ( \ |
| 58 | err, "testcase 2 failed for len %u data %u (rv %u)", len, \ |
| 59 | d, rv); \ |
| 60 | goto done; \ |
| 61 | } \ |
| 62 | data[-1] = d; \ |
| 63 | \ |
| 64 | data[-2] = d + 1; \ |
| 65 | if (len > 2 && ((rv = wfn_##type (data - len, len)) != len - 2)) \ |
| 66 | { \ |
| 67 | err = clib_error_return ( \ |
| 68 | err, "testcase 3 failed for len %u data %u (rv %u)", len, \ |
| 69 | d, rv); \ |
| 70 | goto done; \ |
| 71 | } \ |
| 72 | data[-2] = d; \ |
Damjan Marion | 66b057e | 2021-11-06 13:26:58 +0100 | [diff] [blame] | 73 | } \ |
Damjan Marion | 66b057e | 2021-11-06 13:26:58 +0100 | [diff] [blame] | 74 | } \ |
| 75 | \ |
| 76 | done: \ |
| 77 | clib_mem_vm_unmap (map); \ |
| 78 | return err; \ |
| 79 | } |
| 80 | |
| 81 | foreach_clib_count_equal (u8); |
| 82 | foreach_clib_count_equal (u16); |
| 83 | foreach_clib_count_equal (u32); |
| 84 | foreach_clib_count_equal (u64); |
| 85 | |
| 86 | REGISTER_TEST (clib_count_equal_u8) = { |
| 87 | .name = "clib_count_equal_u8", |
| 88 | .fn = test_clib_count_equal_u8, |
| 89 | }; |
| 90 | |
| 91 | REGISTER_TEST (clib_count_equal_u16) = { |
| 92 | .name = "clib_count_equal_u16", |
| 93 | .fn = test_clib_count_equal_u16, |
| 94 | }; |
| 95 | |
| 96 | REGISTER_TEST (clib_count_equal_u32) = { |
| 97 | .name = "clib_count_equal_u32", |
| 98 | .fn = test_clib_count_equal_u32, |
| 99 | }; |
| 100 | |
| 101 | REGISTER_TEST (clib_count_equal_u64) = { |
| 102 | .name = "clib_count_equal_u64", |
| 103 | .fn = test_clib_count_equal_u64, |
| 104 | }; |