blob: 942c2203d3d93a8dc046274ddc13d01742517cbd [file] [log] [blame]
Damjan Marion66b057e2021-11-06 13:26:58 +01001/* 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 Marion66b057e2021-11-06 13:26:58 +01007#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 Marion3323e202021-12-02 11:28:57 +010012 __test_funct_fn uword clib_count_equal_##type##_wrapper (type *a, \
13 uword maxcount) \
Damjan Marion66b057e2021-11-06 13:26:58 +010014 { \
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 Valter923325f2021-11-14 17:05:44 +000038 for (u8 d = 0; d < 255; d++) \
Damjan Marion66b057e2021-11-06 13:26:58 +010039 { \
Dmitry Valter923325f2021-11-14 17:05:44 +000040 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 Marion66b057e2021-11-06 13:26:58 +010043 { \
Dmitry Valter923325f2021-11-14 17:05:44 +000044 uword rv, len = lengths[i]; \
Damjan Marion66b057e2021-11-06 13:26:58 +010045 \
Dmitry Valter923325f2021-11-14 17:05:44 +000046 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 Marion66b057e2021-11-06 13:26:58 +010053 \
Dmitry Valter923325f2021-11-14 17:05:44 +000054 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 Marion66b057e2021-11-06 13:26:58 +010073 } \
Damjan Marion66b057e2021-11-06 13:26:58 +010074 } \
75 \
76 done: \
77 clib_mem_vm_unmap (map); \
78 return err; \
79 }
80
81foreach_clib_count_equal (u8);
82foreach_clib_count_equal (u16);
83foreach_clib_count_equal (u32);
84foreach_clib_count_equal (u64);
85
86REGISTER_TEST (clib_count_equal_u8) = {
87 .name = "clib_count_equal_u8",
88 .fn = test_clib_count_equal_u8,
89};
90
91REGISTER_TEST (clib_count_equal_u16) = {
92 .name = "clib_count_equal_u16",
93 .fn = test_clib_count_equal_u16,
94};
95
96REGISTER_TEST (clib_count_equal_u32) = {
97 .name = "clib_count_equal_u32",
98 .fn = test_clib_count_equal_u32,
99};
100
101REGISTER_TEST (clib_count_equal_u64) = {
102 .name = "clib_count_equal_u64",
103 .fn = test_clib_count_equal_u64,
104};