blob: cd1c8a5c4d1fbec57ae4d328fada17e4e2c8fa7b [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>
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 \
12 __clib_test_fn uword clib_count_equal_##type##_wrapper (type *a, \
13 uword maxcount) \
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 \
38 for (int i = 1; i <= (1 << ps) / sizeof (data[0]); i++) \
39 data[-i] = 7; \
40 \
41 for (int i = 0; i < ARRAY_LEN (lengths); i++) \
42 { \
43 uword rv, len = lengths[i]; \
44 \
45 if ((rv = wfn_##type (data - len, len)) != len) \
46 { \
47 err = clib_error_return ( \
48 err, "testcase 1 failed for len %u (rv %u)", len, rv); \
49 goto done; \
50 } \
51 \
52 data[-1] = 8; \
53 if (len > 1 && ((rv = wfn_##type (data - len, len)) != len - 1)) \
54 { \
55 err = clib_error_return ( \
56 err, "testcase 2 failed for len %u (rv %u)", len, rv); \
57 goto done; \
58 } \
59 data[-1] = 7; \
60 \
61 data[-2] = 8; \
62 if (len > 2 && ((rv = wfn_##type (data - len, len)) != len - 2)) \
63 { \
64 err = clib_error_return ( \
65 err, "testcase 3 failed for len %u (rv %u)", len, rv); \
66 goto done; \
67 } \
68 data[-2] = 7; \
69 } \
70 \
71 done: \
72 clib_mem_vm_unmap (map); \
73 return err; \
74 }
75
76foreach_clib_count_equal (u8);
77foreach_clib_count_equal (u16);
78foreach_clib_count_equal (u32);
79foreach_clib_count_equal (u64);
80
81REGISTER_TEST (clib_count_equal_u8) = {
82 .name = "clib_count_equal_u8",
83 .fn = test_clib_count_equal_u8,
84};
85
86REGISTER_TEST (clib_count_equal_u16) = {
87 .name = "clib_count_equal_u16",
88 .fn = test_clib_count_equal_u16,
89};
90
91REGISTER_TEST (clib_count_equal_u32) = {
92 .name = "clib_count_equal_u32",
93 .fn = test_clib_count_equal_u32,
94};
95
96REGISTER_TEST (clib_count_equal_u64) = {
97 .name = "clib_count_equal_u64",
98 .fn = test_clib_count_equal_u64,
99};