blob: 738b0082dd7738faf755512f1c9368f33e2dcdfe [file] [log] [blame]
Damjan Marione3e35552021-05-06 17:34:49 +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 Mariond154a172021-07-13 21:12:41 +02007#include <vppinfra/vector/mask_compare.h>
Damjan Marione3e35552021-05-06 17:34:49 +02008
Damjan Marion3323e202021-12-02 11:28:57 +01009__test_funct_fn void
Damjan Marione3e35552021-05-06 17:34:49 +020010clib_mask_compare_u16_wrapper (u16 v, u16 *a, u64 *mask, u32 n_elts)
11{
12 clib_mask_compare_u16 (v, a, mask, n_elts);
13}
14
Damjan Marion3323e202021-12-02 11:28:57 +010015__test_funct_fn void
Damjan Marione3e35552021-05-06 17:34:49 +020016clib_mask_compare_u32_wrapper (u32 v, u32 *a, u64 *mask, u32 n_elts)
17{
18 clib_mask_compare_u32 (v, a, mask, n_elts);
19}
20
Mohsin Kazmi556b5a32022-01-24 23:19:29 +000021__test_funct_fn void
22clib_mask_compare_u64_wrapper (u64 v, u64 *a, u64 *mask, u64 n_elts)
23{
24 clib_mask_compare_u64 (v, a, mask, n_elts);
25}
26
Damjan Marione3e35552021-05-06 17:34:49 +020027static clib_error_t *
28test_clib_mask_compare_u16 (clib_error_t *err)
29{
30 u16 array[513];
31 u64 mask[10];
32 u32 i, j;
33
34 for (i = 0; i < ARRAY_LEN (array); i++)
35 array[i] = i;
36
37 for (i = 0; i < ARRAY_LEN (array); i++)
38 {
39 for (j = 0; j < ARRAY_LEN (mask); j++)
40 mask[j] = 0xa5a5a5a5a5a5a5a5;
41
42 clib_mask_compare_u16_wrapper (i, array, mask, i + 1);
43
44 for (j = 0; j < (i >> 6); j++)
45 {
46 if (mask[j])
47 return clib_error_return (err, "mask at position %u not zero", j);
48 }
49 if (mask[j] != 1ULL << (i & 0x3f))
50 return clib_error_return (err,
51 "mask at position %u is %lx, expected %lx",
52 j, mask[j], 1ULL << (i % 64));
53
54 if (mask[j + 1] != 0xa5a5a5a5a5a5a5a5)
55 return clib_error_return (err, "mask overrun at position %u", j + 1);
56 }
57 return err;
58}
59
60REGISTER_TEST (clib_mask_compare_u16) = {
61 .name = "clib_mask_compare_u16",
62 .fn = test_clib_mask_compare_u16,
63};
64
65static clib_error_t *
66test_clib_mask_compare_u32 (clib_error_t *err)
67{
68 u32 array[513];
69 u64 mask[10];
70 u32 i, j;
71
72 for (i = 0; i < ARRAY_LEN (array); i++)
73 array[i] = i;
74
75 for (i = 0; i < ARRAY_LEN (array); i++)
76 {
77 for (j = 0; j < ARRAY_LEN (mask); j++)
78 mask[j] = 0xa5a5a5a5a5a5a5a5;
79
80 clib_mask_compare_u32_wrapper (i, array, mask, i + 1);
81
82 for (j = 0; j < (i >> 6); j++)
83 {
84 if (mask[j])
85 return clib_error_return (err, "mask at position %u not zero", j);
86 }
87 if (mask[j] != 1ULL << (i & 0x3f))
88 return clib_error_return (err,
89 "mask at position %u is %lx, expected %lx",
90 j, mask[j], 1ULL << (i % 64));
91
92 if (mask[j + 1] != 0xa5a5a5a5a5a5a5a5)
93 return clib_error_return (err, "mask overrun at position %u", j + 1);
94 }
95 return err;
96}
97
98REGISTER_TEST (clib_mask_compare_u32) = {
99 .name = "clib_mask_compare_u32",
100 .fn = test_clib_mask_compare_u32,
101};
Mohsin Kazmi556b5a32022-01-24 23:19:29 +0000102
103static clib_error_t *
104test_clib_mask_compare_u64 (clib_error_t *err)
105{
106 u64 array[513];
107 u64 mask[10];
108 u32 i, j;
109
110 for (i = 0; i < ARRAY_LEN (array); i++)
111 array[i] = i;
112
113 for (i = 0; i < ARRAY_LEN (array); i++)
114 {
115 for (j = 0; j < ARRAY_LEN (mask); j++)
116 mask[j] = 0xa5a5a5a5a5a5a5a5;
117
118 clib_mask_compare_u64_wrapper (i, array, mask, i + 1);
119
120 for (j = 0; j < (i >> 6); j++)
121 {
122 if (mask[j])
123 return clib_error_return (err, "mask at position %u not zero", j);
124 }
125 if (mask[j] != 1ULL << (i & 0x3f))
126 return clib_error_return (err,
127 "mask at position %u is %lx, expected %lx",
128 j, mask[j], 1ULL << (i % 64));
129
130 if (mask[j + 1] != 0xa5a5a5a5a5a5a5a5)
131 return clib_error_return (err, "mask overrun at position %u", j + 1);
132 }
133 return err;
134}
135
136REGISTER_TEST (clib_mask_compare_u64) = {
137 .name = "clib_mask_compare_u64",
138 .fn = test_clib_mask_compare_u64,
139};