blob: 083065f9bda2a873f4de32cbc3048c0d86cc6233 [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/compress.h>
Damjan Marione3e35552021-05-06 17:34:49 +02008
Damjan Marion3323e202021-12-02 11:28:57 +01009__test_funct_fn u32
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +020010clib_compress_u64_wrapper (u64 *dst, u64 *src, u64 *mask, u32 n_elts)
11{
12 return clib_compress_u64 (dst, src, mask, n_elts);
13}
14
Damjan Marion3323e202021-12-02 11:28:57 +010015__test_funct_fn u32
Damjan Marione3e35552021-05-06 17:34:49 +020016clib_compress_u32_wrapper (u32 *dst, u32 *src, u64 *mask, u32 n_elts)
17{
18 return clib_compress_u32 (dst, src, mask, n_elts);
19}
20
Damjan Marion3323e202021-12-02 11:28:57 +010021__test_funct_fn u32
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +020022clib_compress_u16_wrapper (u16 *dst, u16 *src, u64 *mask, u32 n_elts)
23{
24 return clib_compress_u16 (dst, src, mask, n_elts);
25}
26
Damjan Marion3323e202021-12-02 11:28:57 +010027__test_funct_fn u32
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +020028clib_compress_u8_wrapper (u8 *dst, u8 *src, u64 *mask, u32 n_elts)
29{
30 return clib_compress_u8 (dst, src, mask, n_elts);
31}
32
Damjan Marione3e35552021-05-06 17:34:49 +020033typedef struct
34{
35 u64 mask[10];
36 u32 n_elts;
37} compress_test_t;
38
39static compress_test_t tests[] = {
40 { .mask = { 1 }, .n_elts = 1 },
41 { .mask = { 2 }, .n_elts = 2 },
42 { .mask = { 3 }, .n_elts = 2 },
43 { .mask = { 0, 1 }, .n_elts = 66 },
44 { .mask = { 0, 2 }, .n_elts = 69 },
45 { .mask = { 0, 3 }, .n_elts = 66 },
46 { .mask = { ~0ULL, ~0ULL, ~0ULL, ~0ULL }, .n_elts = 62 },
47 { .mask = { ~0ULL, ~0ULL, ~0ULL, ~0ULL }, .n_elts = 255 },
48 { .mask = { ~0ULL, 1, 1, ~0ULL }, .n_elts = 256 },
49};
50
51static clib_error_t *
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +020052test_clib_compress_u64 (clib_error_t *err)
53{
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +020054 u32 i, j;
55
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +020056 for (i = 0; i < ARRAY_LEN (tests); i++)
57 {
58 compress_test_t *t = tests + i;
Dmitry Valter31502502024-03-06 22:43:27 +000059 u64 src[t->n_elts];
60#ifdef CLIB_SANITIZE_ADDR
61 u64 dst[t->n_elts];
62#else /* CLIB_SANITIZE_ADDR */
63 u64 dst[513];
64#endif /* CLIB_SANITIZE_ADDR */
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +020065 u64 *dp = dst;
66 u32 r;
Dmitry Valter31502502024-03-06 22:43:27 +000067 for (j = 0; j < t->n_elts; j++)
68 src[j] = j;
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +020069
70 for (j = 0; j < ARRAY_LEN (dst); j++)
71 dst[j] = 0xa5a5a5a5a5a5a5a5;
72
73 r = clib_compress_u64_wrapper (dst, src, t->mask, t->n_elts);
74
75 for (j = 0; j < t->n_elts; j++)
76 {
77 if ((t->mask[j >> 6] & (1ULL << (j & 0x3f))) == 0)
78 continue;
79 if (dp[0] != src[j])
80 return clib_error_return (err,
81 "wrong data in testcase %u at "
82 "(dst[%u] = 0x%lx, src[%u] = 0x%lx)",
83 i, dp - dst, dp[0], j, src[j]);
84 dp++;
85 }
86
Dmitry Valter31502502024-03-06 22:43:27 +000087#ifndef CLIB_SANITIZE_ADDR
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +020088 if (dst[dp - dst + 1] != 0xa5a5a5a5a5a5a5a5)
89 return clib_error_return (err, "buffer overrun in testcase %u", i);
Dmitry Valter31502502024-03-06 22:43:27 +000090#endif /* CLIB_SANITIZE_ADDR */
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +020091
92 if (dp - dst != r)
93 return clib_error_return (err, "wrong number of elts in testcase %u",
94 i);
95 }
96
97 return err;
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +020098}
99
100static clib_error_t *
Damjan Marione3e35552021-05-06 17:34:49 +0200101test_clib_compress_u32 (clib_error_t *err)
102{
Damjan Marione3e35552021-05-06 17:34:49 +0200103 u32 i, j;
104
Damjan Marione3e35552021-05-06 17:34:49 +0200105 for (i = 0; i < ARRAY_LEN (tests); i++)
106 {
107 compress_test_t *t = tests + i;
Dmitry Valter31502502024-03-06 22:43:27 +0000108 u32 src[t->n_elts];
109#ifdef CLIB_SANITIZE_ADDR
110 u32 dst[t->n_elts];
111#else /* CLIB_SANITIZE_ADDR */
112 u32 dst[513];
113#endif /* CLIB_SANITIZE_ADDR */
Damjan Marione3e35552021-05-06 17:34:49 +0200114 u32 *dp = dst;
115 u32 r;
Dmitry Valter31502502024-03-06 22:43:27 +0000116 for (j = 0; j < t->n_elts; j++)
117 src[j] = j;
Damjan Marione3e35552021-05-06 17:34:49 +0200118
119 for (j = 0; j < ARRAY_LEN (dst); j++)
120 dst[j] = 0xa5a5a5a5;
121
122 r = clib_compress_u32_wrapper (dst, src, t->mask, t->n_elts);
123
124 for (j = 0; j < t->n_elts; j++)
125 {
126 if ((t->mask[j >> 6] & (1ULL << (j & 0x3f))) == 0)
127 continue;
128
129 if (dp[0] != src[j])
130 return clib_error_return (err,
131 "wrong data in testcase %u at "
132 "(dst[%u] = 0x%x, src[%u] = 0x%x)",
133 i, dp - dst, dp[0], j, src[j]);
134 dp++;
135 }
136
Dmitry Valter31502502024-03-06 22:43:27 +0000137#ifndef CLIB_SANITIZE_ADDR
Damjan Marione3e35552021-05-06 17:34:49 +0200138 if (dst[dp - dst + 1] != 0xa5a5a5a5)
139 return clib_error_return (err, "buffer overrun in testcase %u", i);
Dmitry Valter31502502024-03-06 22:43:27 +0000140#endif /* CLIB_SANITIZE_ADDR */
Damjan Marione3e35552021-05-06 17:34:49 +0200141
142 if (dp - dst != r)
143 return clib_error_return (err, "wrong number of elts in testcase %u",
144 i);
145 }
146
147 return err;
148}
149
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +0200150static clib_error_t *
151test_clib_compress_u16 (clib_error_t *err)
152{
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +0200153 u32 i, j;
154
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +0200155 for (i = 0; i < ARRAY_LEN (tests); i++)
156 {
157 compress_test_t *t = tests + i;
Dmitry Valter31502502024-03-06 22:43:27 +0000158 u16 src[t->n_elts];
159#ifdef CLIB_SANITIZE_ADDR
160 u16 dst[t->n_elts];
161#else /* CLIB_SANITIZE_ADDR */
162 u16 dst[513];
163#endif /* CLIB_SANITIZE_ADDR */
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +0200164 u16 *dp = dst;
165 u32 r;
Dmitry Valter31502502024-03-06 22:43:27 +0000166 for (j = 0; j < t->n_elts; j++)
167 src[j] = j;
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +0200168
169 for (j = 0; j < ARRAY_LEN (dst); j++)
170 dst[j] = 0xa5a5;
171
172 r = clib_compress_u16_wrapper (dst, src, t->mask, t->n_elts);
173
174 for (j = 0; j < t->n_elts; j++)
175 {
176 if ((t->mask[j >> 6] & (1ULL << (j & 0x3f))) == 0)
177 continue;
178 if (dp[0] != src[j])
179 return clib_error_return (err,
180 "wrong data in testcase %u at "
181 "(dst[%u] = 0x%x, src[%u] = 0x%x)",
182 i, dp - dst, dp[0], j, src[j]);
183 dp++;
184 }
185
Dmitry Valter31502502024-03-06 22:43:27 +0000186#ifndef CLIB_SANITIZE_ADDR
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +0200187 if (dst[dp - dst + 1] != 0xa5a5)
188 return clib_error_return (err, "buffer overrun in testcase %u", i);
Dmitry Valter31502502024-03-06 22:43:27 +0000189#endif /* CLIB_SANITIZE_ADDR */
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +0200190
191 if (dp - dst != r)
192 return clib_error_return (err, "wrong number of elts in testcase %u",
193 i);
194 }
195
196 return err;
197}
198
199static clib_error_t *
200test_clib_compress_u8 (clib_error_t *err)
201{
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +0200202 u32 i, j;
203
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +0200204 for (i = 0; i < ARRAY_LEN (tests); i++)
205 {
206 compress_test_t *t = tests + i;
Dmitry Valter31502502024-03-06 22:43:27 +0000207 u8 src[t->n_elts];
208#ifdef CLIB_SANITIZE_ADDR
209 u8 dst[t->n_elts];
210#else /* CLIB_SANITIZE_ADDR */
211 u8 dst[513];
212#endif /* CLIB_SANITIZE_ADDR */
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +0200213 u8 *dp = dst;
214 u32 r;
Dmitry Valter31502502024-03-06 22:43:27 +0000215 for (j = 0; j < t->n_elts; j++)
216 src[j] = j;
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +0200217
218 for (j = 0; j < ARRAY_LEN (dst); j++)
219 dst[j] = 0xa5;
220
221 r = clib_compress_u8_wrapper (dst, src, t->mask, t->n_elts);
222
223 for (j = 0; j < t->n_elts; j++)
224 {
225 if ((t->mask[j >> 6] & (1ULL << (j & 0x3f))) == 0)
226 continue;
227 if (dp[0] != src[j])
228 return clib_error_return (err,
229 "wrong data in testcase %u at "
230 "(dst[%u] = 0x%x, src[%u] = 0x%x)",
231 i, dp - dst, dp[0], j, src[j]);
232 dp++;
233 }
234
Dmitry Valter31502502024-03-06 22:43:27 +0000235#ifndef CLIB_SANITIZE_ADDR
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +0200236 if (dst[dp - dst + 1] != 0xa5)
237 return clib_error_return (err, "buffer overrun in testcase %u", i);
Dmitry Valter31502502024-03-06 22:43:27 +0000238#endif /* CLIB_SANITIZE_ADDR */
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +0200239
240 if (dp - dst != r)
241 return clib_error_return (err, "wrong number of elts in testcase %u",
242 i);
243 }
244
245 return err;
246}
247
248REGISTER_TEST (clib_compress_u64) = {
249 .name = "clib_compress_u64",
250 .fn = test_clib_compress_u64,
251};
252
Damjan Marione3e35552021-05-06 17:34:49 +0200253REGISTER_TEST (clib_compress_u32) = {
254 .name = "clib_compress_u32",
255 .fn = test_clib_compress_u32,
256};
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +0200257
258REGISTER_TEST (clib_compress_u16) = {
259 .name = "clib_compress_u16",
260 .fn = test_clib_compress_u16,
261};
262
263REGISTER_TEST (clib_compress_u8) = {
264 .name = "clib_compress_u8",
265 .fn = test_clib_compress_u8,
266};