Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright(c) 2021 Cisco Systems, Inc. |
| 3 | */ |
| 4 | |
| 5 | #include <vppinfra/format.h> |
Damjan Marion | c3542e1 | 2023-03-15 11:42:06 +0000 | [diff] [blame] | 6 | #include <vppinfra/test/test.h> |
Damjan Marion | d154a17 | 2021-07-13 21:12:41 +0200 | [diff] [blame] | 7 | #include <vppinfra/vector/compress.h> |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 8 | |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 9 | __test_funct_fn u32 |
Mohsin Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 10 | clib_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 Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 15 | __test_funct_fn u32 |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 16 | clib_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 Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 21 | __test_funct_fn u32 |
Mohsin Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 22 | clib_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 Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 27 | __test_funct_fn u32 |
Mohsin Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 28 | clib_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 Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 33 | typedef struct |
| 34 | { |
| 35 | u64 mask[10]; |
| 36 | u32 n_elts; |
| 37 | } compress_test_t; |
| 38 | |
| 39 | static 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 | |
| 51 | static clib_error_t * |
Mohsin Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 52 | test_clib_compress_u64 (clib_error_t *err) |
| 53 | { |
Mohsin Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 54 | u32 i, j; |
| 55 | |
Mohsin Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 56 | for (i = 0; i < ARRAY_LEN (tests); i++) |
| 57 | { |
| 58 | compress_test_t *t = tests + i; |
Dmitry Valter | 3150250 | 2024-03-06 22:43:27 +0000 | [diff] [blame] | 59 | 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 Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 65 | u64 *dp = dst; |
| 66 | u32 r; |
Dmitry Valter | 3150250 | 2024-03-06 22:43:27 +0000 | [diff] [blame] | 67 | for (j = 0; j < t->n_elts; j++) |
| 68 | src[j] = j; |
Mohsin Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 69 | |
| 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 Valter | 3150250 | 2024-03-06 22:43:27 +0000 | [diff] [blame] | 87 | #ifndef CLIB_SANITIZE_ADDR |
Mohsin Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 88 | if (dst[dp - dst + 1] != 0xa5a5a5a5a5a5a5a5) |
| 89 | return clib_error_return (err, "buffer overrun in testcase %u", i); |
Dmitry Valter | 3150250 | 2024-03-06 22:43:27 +0000 | [diff] [blame] | 90 | #endif /* CLIB_SANITIZE_ADDR */ |
Mohsin Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 91 | |
| 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 Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static clib_error_t * |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 101 | test_clib_compress_u32 (clib_error_t *err) |
| 102 | { |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 103 | u32 i, j; |
| 104 | |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 105 | for (i = 0; i < ARRAY_LEN (tests); i++) |
| 106 | { |
| 107 | compress_test_t *t = tests + i; |
Dmitry Valter | 3150250 | 2024-03-06 22:43:27 +0000 | [diff] [blame] | 108 | 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 Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 114 | u32 *dp = dst; |
| 115 | u32 r; |
Dmitry Valter | 3150250 | 2024-03-06 22:43:27 +0000 | [diff] [blame] | 116 | for (j = 0; j < t->n_elts; j++) |
| 117 | src[j] = j; |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 118 | |
| 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 Valter | 3150250 | 2024-03-06 22:43:27 +0000 | [diff] [blame] | 137 | #ifndef CLIB_SANITIZE_ADDR |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 138 | if (dst[dp - dst + 1] != 0xa5a5a5a5) |
| 139 | return clib_error_return (err, "buffer overrun in testcase %u", i); |
Dmitry Valter | 3150250 | 2024-03-06 22:43:27 +0000 | [diff] [blame] | 140 | #endif /* CLIB_SANITIZE_ADDR */ |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 141 | |
| 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 Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 150 | static clib_error_t * |
| 151 | test_clib_compress_u16 (clib_error_t *err) |
| 152 | { |
Mohsin Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 153 | u32 i, j; |
| 154 | |
Mohsin Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 155 | for (i = 0; i < ARRAY_LEN (tests); i++) |
| 156 | { |
| 157 | compress_test_t *t = tests + i; |
Dmitry Valter | 3150250 | 2024-03-06 22:43:27 +0000 | [diff] [blame] | 158 | 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 Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 164 | u16 *dp = dst; |
| 165 | u32 r; |
Dmitry Valter | 3150250 | 2024-03-06 22:43:27 +0000 | [diff] [blame] | 166 | for (j = 0; j < t->n_elts; j++) |
| 167 | src[j] = j; |
Mohsin Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 168 | |
| 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 Valter | 3150250 | 2024-03-06 22:43:27 +0000 | [diff] [blame] | 186 | #ifndef CLIB_SANITIZE_ADDR |
Mohsin Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 187 | if (dst[dp - dst + 1] != 0xa5a5) |
| 188 | return clib_error_return (err, "buffer overrun in testcase %u", i); |
Dmitry Valter | 3150250 | 2024-03-06 22:43:27 +0000 | [diff] [blame] | 189 | #endif /* CLIB_SANITIZE_ADDR */ |
Mohsin Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 190 | |
| 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 | |
| 199 | static clib_error_t * |
| 200 | test_clib_compress_u8 (clib_error_t *err) |
| 201 | { |
Mohsin Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 202 | u32 i, j; |
| 203 | |
Mohsin Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 204 | for (i = 0; i < ARRAY_LEN (tests); i++) |
| 205 | { |
| 206 | compress_test_t *t = tests + i; |
Dmitry Valter | 3150250 | 2024-03-06 22:43:27 +0000 | [diff] [blame] | 207 | 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 Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 213 | u8 *dp = dst; |
| 214 | u32 r; |
Dmitry Valter | 3150250 | 2024-03-06 22:43:27 +0000 | [diff] [blame] | 215 | for (j = 0; j < t->n_elts; j++) |
| 216 | src[j] = j; |
Mohsin Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 217 | |
| 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 Valter | 3150250 | 2024-03-06 22:43:27 +0000 | [diff] [blame] | 235 | #ifndef CLIB_SANITIZE_ADDR |
Mohsin Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 236 | if (dst[dp - dst + 1] != 0xa5) |
| 237 | return clib_error_return (err, "buffer overrun in testcase %u", i); |
Dmitry Valter | 3150250 | 2024-03-06 22:43:27 +0000 | [diff] [blame] | 238 | #endif /* CLIB_SANITIZE_ADDR */ |
Mohsin Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 239 | |
| 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 | |
| 248 | REGISTER_TEST (clib_compress_u64) = { |
| 249 | .name = "clib_compress_u64", |
| 250 | .fn = test_clib_compress_u64, |
| 251 | }; |
| 252 | |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 253 | REGISTER_TEST (clib_compress_u32) = { |
| 254 | .name = "clib_compress_u32", |
| 255 | .fn = test_clib_compress_u32, |
| 256 | }; |
Mohsin Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 257 | |
| 258 | REGISTER_TEST (clib_compress_u16) = { |
| 259 | .name = "clib_compress_u16", |
| 260 | .fn = test_clib_compress_u16, |
| 261 | }; |
| 262 | |
| 263 | REGISTER_TEST (clib_compress_u8) = { |
| 264 | .name = "clib_compress_u8", |
| 265 | .fn = test_clib_compress_u8, |
| 266 | }; |