blob: 7b97f4c31af9140c50cb29311d9ae2ededd949f4 [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{
54 u64 src[513];
55 u64 dst[513];
56 u32 i, j;
57
58 for (i = 0; i < ARRAY_LEN (src); i++)
59 src[i] = i;
60
61 for (i = 0; i < ARRAY_LEN (tests); i++)
62 {
63 compress_test_t *t = tests + i;
64 u64 *dp = dst;
65 u32 r;
66
67 for (j = 0; j < ARRAY_LEN (dst); j++)
68 dst[j] = 0xa5a5a5a5a5a5a5a5;
69
70 r = clib_compress_u64_wrapper (dst, src, t->mask, t->n_elts);
71
72 for (j = 0; j < t->n_elts; j++)
73 {
74 if ((t->mask[j >> 6] & (1ULL << (j & 0x3f))) == 0)
75 continue;
76 if (dp[0] != src[j])
77 return clib_error_return (err,
78 "wrong data in testcase %u at "
79 "(dst[%u] = 0x%lx, src[%u] = 0x%lx)",
80 i, dp - dst, dp[0], j, src[j]);
81 dp++;
82 }
83
84 if (dst[dp - dst + 1] != 0xa5a5a5a5a5a5a5a5)
85 return clib_error_return (err, "buffer overrun in testcase %u", i);
86
87 if (dp - dst != r)
88 return clib_error_return (err, "wrong number of elts in testcase %u",
89 i);
90 }
91
92 return err;
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +020093}
94
95static clib_error_t *
Damjan Marione3e35552021-05-06 17:34:49 +020096test_clib_compress_u32 (clib_error_t *err)
97{
98 u32 src[513];
99 u32 dst[513];
100 u32 i, j;
101
102 for (i = 0; i < ARRAY_LEN (src); i++)
103 src[i] = i;
104
105 for (i = 0; i < ARRAY_LEN (tests); i++)
106 {
107 compress_test_t *t = tests + i;
108 u32 *dp = dst;
109 u32 r;
110
111 for (j = 0; j < ARRAY_LEN (dst); j++)
112 dst[j] = 0xa5a5a5a5;
113
114 r = clib_compress_u32_wrapper (dst, src, t->mask, t->n_elts);
115
116 for (j = 0; j < t->n_elts; j++)
117 {
118 if ((t->mask[j >> 6] & (1ULL << (j & 0x3f))) == 0)
119 continue;
120
121 if (dp[0] != src[j])
122 return clib_error_return (err,
123 "wrong data in testcase %u at "
124 "(dst[%u] = 0x%x, src[%u] = 0x%x)",
125 i, dp - dst, dp[0], j, src[j]);
126 dp++;
127 }
128
129 if (dst[dp - dst + 1] != 0xa5a5a5a5)
130 return clib_error_return (err, "buffer overrun in testcase %u", i);
131
132 if (dp - dst != r)
133 return clib_error_return (err, "wrong number of elts in testcase %u",
134 i);
135 }
136
137 return err;
138}
139
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +0200140static clib_error_t *
141test_clib_compress_u16 (clib_error_t *err)
142{
143 u16 src[513];
144 u16 dst[513];
145 u32 i, j;
146
147 for (i = 0; i < ARRAY_LEN (src); i++)
148 src[i] = i;
149
150 for (i = 0; i < ARRAY_LEN (tests); i++)
151 {
152 compress_test_t *t = tests + i;
153 u16 *dp = dst;
154 u32 r;
155
156 for (j = 0; j < ARRAY_LEN (dst); j++)
157 dst[j] = 0xa5a5;
158
159 r = clib_compress_u16_wrapper (dst, src, t->mask, t->n_elts);
160
161 for (j = 0; j < t->n_elts; j++)
162 {
163 if ((t->mask[j >> 6] & (1ULL << (j & 0x3f))) == 0)
164 continue;
165 if (dp[0] != src[j])
166 return clib_error_return (err,
167 "wrong data in testcase %u at "
168 "(dst[%u] = 0x%x, src[%u] = 0x%x)",
169 i, dp - dst, dp[0], j, src[j]);
170 dp++;
171 }
172
173 if (dst[dp - dst + 1] != 0xa5a5)
174 return clib_error_return (err, "buffer overrun in testcase %u", i);
175
176 if (dp - dst != r)
177 return clib_error_return (err, "wrong number of elts in testcase %u",
178 i);
179 }
180
181 return err;
182}
183
184static clib_error_t *
185test_clib_compress_u8 (clib_error_t *err)
186{
187 u8 src[513];
188 u8 dst[513];
189 u32 i, j;
190
191 for (i = 0; i < ARRAY_LEN (src); i++)
192 src[i] = i;
193
194 for (i = 0; i < ARRAY_LEN (tests); i++)
195 {
196 compress_test_t *t = tests + i;
197 u8 *dp = dst;
198 u32 r;
199
200 for (j = 0; j < ARRAY_LEN (dst); j++)
201 dst[j] = 0xa5;
202
203 r = clib_compress_u8_wrapper (dst, src, t->mask, t->n_elts);
204
205 for (j = 0; j < t->n_elts; j++)
206 {
207 if ((t->mask[j >> 6] & (1ULL << (j & 0x3f))) == 0)
208 continue;
209 if (dp[0] != src[j])
210 return clib_error_return (err,
211 "wrong data in testcase %u at "
212 "(dst[%u] = 0x%x, src[%u] = 0x%x)",
213 i, dp - dst, dp[0], j, src[j]);
214 dp++;
215 }
216
217 if (dst[dp - dst + 1] != 0xa5)
218 return clib_error_return (err, "buffer overrun in testcase %u", i);
219
220 if (dp - dst != r)
221 return clib_error_return (err, "wrong number of elts in testcase %u",
222 i);
223 }
224
225 return err;
226}
227
228REGISTER_TEST (clib_compress_u64) = {
229 .name = "clib_compress_u64",
230 .fn = test_clib_compress_u64,
231};
232
Damjan Marione3e35552021-05-06 17:34:49 +0200233REGISTER_TEST (clib_compress_u32) = {
234 .name = "clib_compress_u32",
235 .fn = test_clib_compress_u32,
236};
Mohsin Kazmi7da9b5b2021-08-27 18:57:16 +0200237
238REGISTER_TEST (clib_compress_u16) = {
239 .name = "clib_compress_u16",
240 .fn = test_clib_compress_u16,
241};
242
243REGISTER_TEST (clib_compress_u8) = {
244 .name = "clib_compress_u8",
245 .fn = test_clib_compress_u8,
246};