7e3eba9892d973d64aa9e32accd437a7313f32c6
[vpp.git] / src / vppinfra / vector / test / compress.c
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright(c) 2021 Cisco Systems, Inc.
3  */
4
5 #include <vppinfra/format.h>
6 #include <vppinfra/vector/test/test.h>
7 #include <vppinfra/vector/compress.h>
8
9 __clib_test_fn u32
10 clib_compress_u32_wrapper (u32 *dst, u32 *src, u64 *mask, u32 n_elts)
11 {
12   return clib_compress_u32 (dst, src, mask, n_elts);
13 }
14
15 typedef struct
16 {
17   u64 mask[10];
18   u32 n_elts;
19 } compress_test_t;
20
21 static compress_test_t tests[] = {
22   { .mask = { 1 }, .n_elts = 1 },
23   { .mask = { 2 }, .n_elts = 2 },
24   { .mask = { 3 }, .n_elts = 2 },
25   { .mask = { 0, 1 }, .n_elts = 66 },
26   { .mask = { 0, 2 }, .n_elts = 69 },
27   { .mask = { 0, 3 }, .n_elts = 66 },
28   { .mask = { ~0ULL, ~0ULL, ~0ULL, ~0ULL }, .n_elts = 62 },
29   { .mask = { ~0ULL, ~0ULL, ~0ULL, ~0ULL }, .n_elts = 255 },
30   { .mask = { ~0ULL, 1, 1, ~0ULL }, .n_elts = 256 },
31 };
32
33 static clib_error_t *
34 test_clib_compress_u32 (clib_error_t *err)
35 {
36   u32 src[513];
37   u32 dst[513];
38   u32 i, j;
39
40   for (i = 0; i < ARRAY_LEN (src); i++)
41     src[i] = i;
42
43   for (i = 0; i < ARRAY_LEN (tests); i++)
44     {
45       compress_test_t *t = tests + i;
46       u32 *dp = dst;
47       u32 r;
48
49       for (j = 0; j < ARRAY_LEN (dst); j++)
50         dst[j] = 0xa5a5a5a5;
51
52       r = clib_compress_u32_wrapper (dst, src, t->mask, t->n_elts);
53
54       for (j = 0; j < t->n_elts; j++)
55         {
56           if ((t->mask[j >> 6] & (1ULL << (j & 0x3f))) == 0)
57             continue;
58
59           if (dp[0] != src[j])
60             return clib_error_return (err,
61                                       "wrong data in testcase %u at "
62                                       "(dst[%u] = 0x%x, src[%u] = 0x%x)",
63                                       i, dp - dst, dp[0], j, src[j]);
64           dp++;
65         }
66
67       if (dst[dp - dst + 1] != 0xa5a5a5a5)
68         return clib_error_return (err, "buffer overrun in testcase %u", i);
69
70       if (dp - dst != r)
71         return clib_error_return (err, "wrong number of elts in testcase %u",
72                                   i);
73     }
74
75   return err;
76 }
77
78 REGISTER_TEST (clib_compress_u32) = {
79   .name = "clib_compress_u32",
80   .fn = test_clib_compress_u32,
81 };