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 | d154a17 | 2021-07-13 21:12:41 +0200 | [diff] [blame] | 6 | #include <vppinfra/vector/test/test.h> |
| 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 | { |
| 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; |
| 93 | |
| 94 | return err; |
| 95 | } |
| 96 | |
| 97 | static clib_error_t * |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 98 | test_clib_compress_u32 (clib_error_t *err) |
| 99 | { |
| 100 | u32 src[513]; |
| 101 | u32 dst[513]; |
| 102 | u32 i, j; |
| 103 | |
| 104 | for (i = 0; i < ARRAY_LEN (src); i++) |
| 105 | src[i] = i; |
| 106 | |
| 107 | for (i = 0; i < ARRAY_LEN (tests); i++) |
| 108 | { |
| 109 | compress_test_t *t = tests + i; |
| 110 | u32 *dp = dst; |
| 111 | u32 r; |
| 112 | |
| 113 | for (j = 0; j < ARRAY_LEN (dst); j++) |
| 114 | dst[j] = 0xa5a5a5a5; |
| 115 | |
| 116 | r = clib_compress_u32_wrapper (dst, src, t->mask, t->n_elts); |
| 117 | |
| 118 | for (j = 0; j < t->n_elts; j++) |
| 119 | { |
| 120 | if ((t->mask[j >> 6] & (1ULL << (j & 0x3f))) == 0) |
| 121 | continue; |
| 122 | |
| 123 | if (dp[0] != src[j]) |
| 124 | return clib_error_return (err, |
| 125 | "wrong data in testcase %u at " |
| 126 | "(dst[%u] = 0x%x, src[%u] = 0x%x)", |
| 127 | i, dp - dst, dp[0], j, src[j]); |
| 128 | dp++; |
| 129 | } |
| 130 | |
| 131 | if (dst[dp - dst + 1] != 0xa5a5a5a5) |
| 132 | return clib_error_return (err, "buffer overrun in testcase %u", i); |
| 133 | |
| 134 | if (dp - dst != r) |
| 135 | return clib_error_return (err, "wrong number of elts in testcase %u", |
| 136 | i); |
| 137 | } |
| 138 | |
| 139 | return err; |
| 140 | } |
| 141 | |
Mohsin Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 142 | static clib_error_t * |
| 143 | test_clib_compress_u16 (clib_error_t *err) |
| 144 | { |
| 145 | u16 src[513]; |
| 146 | u16 dst[513]; |
| 147 | u32 i, j; |
| 148 | |
| 149 | for (i = 0; i < ARRAY_LEN (src); i++) |
| 150 | src[i] = i; |
| 151 | |
| 152 | for (i = 0; i < ARRAY_LEN (tests); i++) |
| 153 | { |
| 154 | compress_test_t *t = tests + i; |
| 155 | u16 *dp = dst; |
| 156 | u32 r; |
| 157 | |
| 158 | for (j = 0; j < ARRAY_LEN (dst); j++) |
| 159 | dst[j] = 0xa5a5; |
| 160 | |
| 161 | r = clib_compress_u16_wrapper (dst, src, t->mask, t->n_elts); |
| 162 | |
| 163 | for (j = 0; j < t->n_elts; j++) |
| 164 | { |
| 165 | if ((t->mask[j >> 6] & (1ULL << (j & 0x3f))) == 0) |
| 166 | continue; |
| 167 | if (dp[0] != src[j]) |
| 168 | return clib_error_return (err, |
| 169 | "wrong data in testcase %u at " |
| 170 | "(dst[%u] = 0x%x, src[%u] = 0x%x)", |
| 171 | i, dp - dst, dp[0], j, src[j]); |
| 172 | dp++; |
| 173 | } |
| 174 | |
| 175 | if (dst[dp - dst + 1] != 0xa5a5) |
| 176 | return clib_error_return (err, "buffer overrun in testcase %u", i); |
| 177 | |
| 178 | if (dp - dst != r) |
| 179 | return clib_error_return (err, "wrong number of elts in testcase %u", |
| 180 | i); |
| 181 | } |
| 182 | |
| 183 | return err; |
| 184 | } |
| 185 | |
| 186 | static clib_error_t * |
| 187 | test_clib_compress_u8 (clib_error_t *err) |
| 188 | { |
| 189 | u8 src[513]; |
| 190 | u8 dst[513]; |
| 191 | u32 i, j; |
| 192 | |
| 193 | for (i = 0; i < ARRAY_LEN (src); i++) |
| 194 | src[i] = i; |
| 195 | |
| 196 | for (i = 0; i < ARRAY_LEN (tests); i++) |
| 197 | { |
| 198 | compress_test_t *t = tests + i; |
| 199 | u8 *dp = dst; |
| 200 | u32 r; |
| 201 | |
| 202 | for (j = 0; j < ARRAY_LEN (dst); j++) |
| 203 | dst[j] = 0xa5; |
| 204 | |
| 205 | r = clib_compress_u8_wrapper (dst, src, t->mask, t->n_elts); |
| 206 | |
| 207 | for (j = 0; j < t->n_elts; j++) |
| 208 | { |
| 209 | if ((t->mask[j >> 6] & (1ULL << (j & 0x3f))) == 0) |
| 210 | continue; |
| 211 | if (dp[0] != src[j]) |
| 212 | return clib_error_return (err, |
| 213 | "wrong data in testcase %u at " |
| 214 | "(dst[%u] = 0x%x, src[%u] = 0x%x)", |
| 215 | i, dp - dst, dp[0], j, src[j]); |
| 216 | dp++; |
| 217 | } |
| 218 | |
| 219 | if (dst[dp - dst + 1] != 0xa5) |
| 220 | return clib_error_return (err, "buffer overrun in testcase %u", i); |
| 221 | |
| 222 | if (dp - dst != r) |
| 223 | return clib_error_return (err, "wrong number of elts in testcase %u", |
| 224 | i); |
| 225 | } |
| 226 | |
| 227 | return err; |
| 228 | } |
| 229 | |
| 230 | REGISTER_TEST (clib_compress_u64) = { |
| 231 | .name = "clib_compress_u64", |
| 232 | .fn = test_clib_compress_u64, |
| 233 | }; |
| 234 | |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 235 | REGISTER_TEST (clib_compress_u32) = { |
| 236 | .name = "clib_compress_u32", |
| 237 | .fn = test_clib_compress_u32, |
| 238 | }; |
Mohsin Kazmi | 7da9b5b | 2021-08-27 18:57:16 +0200 | [diff] [blame] | 239 | |
| 240 | REGISTER_TEST (clib_compress_u16) = { |
| 241 | .name = "clib_compress_u16", |
| 242 | .fn = test_clib_compress_u16, |
| 243 | }; |
| 244 | |
| 245 | REGISTER_TEST (clib_compress_u8) = { |
| 246 | .name = "clib_compress_u8", |
| 247 | .fn = test_clib_compress_u8, |
| 248 | }; |