aboutsummaryrefslogtreecommitdiffstats
path: root/src/vppinfra/vector/test
diff options
context:
space:
mode:
Diffstat (limited to 'src/vppinfra/vector/test')
-rw-r--r--src/vppinfra/vector/test/compress.c81
-rw-r--r--src/vppinfra/vector/test/mask_compare.c95
-rw-r--r--src/vppinfra/vector/test/test.c42
-rw-r--r--src/vppinfra/vector/test/test.h35
4 files changed, 253 insertions, 0 deletions
diff --git a/src/vppinfra/vector/test/compress.c b/src/vppinfra/vector/test/compress.c
new file mode 100644
index 00000000000..7e3eba9892d
--- /dev/null
+++ b/src/vppinfra/vector/test/compress.c
@@ -0,0 +1,81 @@
+/* SPDX-License-Identifier: Apache-2.0
+ * Copyright(c) 2021 Cisco Systems, Inc.
+ */
+
+#include <vppinfra/format.h>
+#include <vppinfra/vector/test/test.h>
+#include <vppinfra/vector/compress.h>
+
+__clib_test_fn u32
+clib_compress_u32_wrapper (u32 *dst, u32 *src, u64 *mask, u32 n_elts)
+{
+ return clib_compress_u32 (dst, src, mask, n_elts);
+}
+
+typedef struct
+{
+ u64 mask[10];
+ u32 n_elts;
+} compress_test_t;
+
+static compress_test_t tests[] = {
+ { .mask = { 1 }, .n_elts = 1 },
+ { .mask = { 2 }, .n_elts = 2 },
+ { .mask = { 3 }, .n_elts = 2 },
+ { .mask = { 0, 1 }, .n_elts = 66 },
+ { .mask = { 0, 2 }, .n_elts = 69 },
+ { .mask = { 0, 3 }, .n_elts = 66 },
+ { .mask = { ~0ULL, ~0ULL, ~0ULL, ~0ULL }, .n_elts = 62 },
+ { .mask = { ~0ULL, ~0ULL, ~0ULL, ~0ULL }, .n_elts = 255 },
+ { .mask = { ~0ULL, 1, 1, ~0ULL }, .n_elts = 256 },
+};
+
+static clib_error_t *
+test_clib_compress_u32 (clib_error_t *err)
+{
+ u32 src[513];
+ u32 dst[513];
+ u32 i, j;
+
+ for (i = 0; i < ARRAY_LEN (src); i++)
+ src[i] = i;
+
+ for (i = 0; i < ARRAY_LEN (tests); i++)
+ {
+ compress_test_t *t = tests + i;
+ u32 *dp = dst;
+ u32 r;
+
+ for (j = 0; j < ARRAY_LEN (dst); j++)
+ dst[j] = 0xa5a5a5a5;
+
+ r = clib_compress_u32_wrapper (dst, src, t->mask, t->n_elts);
+
+ for (j = 0; j < t->n_elts; j++)
+ {
+ if ((t->mask[j >> 6] & (1ULL << (j & 0x3f))) == 0)
+ continue;
+
+ if (dp[0] != src[j])
+ return clib_error_return (err,
+ "wrong data in testcase %u at "
+ "(dst[%u] = 0x%x, src[%u] = 0x%x)",
+ i, dp - dst, dp[0], j, src[j]);
+ dp++;
+ }
+
+ if (dst[dp - dst + 1] != 0xa5a5a5a5)
+ return clib_error_return (err, "buffer overrun in testcase %u", i);
+
+ if (dp - dst != r)
+ return clib_error_return (err, "wrong number of elts in testcase %u",
+ i);
+ }
+
+ return err;
+}
+
+REGISTER_TEST (clib_compress_u32) = {
+ .name = "clib_compress_u32",
+ .fn = test_clib_compress_u32,
+};
diff --git a/src/vppinfra/vector/test/mask_compare.c b/src/vppinfra/vector/test/mask_compare.c
new file mode 100644
index 00000000000..64df0ee084a
--- /dev/null
+++ b/src/vppinfra/vector/test/mask_compare.c
@@ -0,0 +1,95 @@
+/* SPDX-License-Identifier: Apache-2.0
+ * Copyright(c) 2021 Cisco Systems, Inc.
+ */
+
+#include <vppinfra/format.h>
+#include <vppinfra/vector/test/test.h>
+#include <vppinfra/vector/mask_compare.h>
+
+__clib_test_fn void
+clib_mask_compare_u16_wrapper (u16 v, u16 *a, u64 *mask, u32 n_elts)
+{
+ clib_mask_compare_u16 (v, a, mask, n_elts);
+}
+
+__clib_test_fn void
+clib_mask_compare_u32_wrapper (u32 v, u32 *a, u64 *mask, u32 n_elts)
+{
+ clib_mask_compare_u32 (v, a, mask, n_elts);
+}
+
+static clib_error_t *
+test_clib_mask_compare_u16 (clib_error_t *err)
+{
+ u16 array[513];
+ u64 mask[10];
+ u32 i, j;
+
+ for (i = 0; i < ARRAY_LEN (array); i++)
+ array[i] = i;
+
+ for (i = 0; i < ARRAY_LEN (array); i++)
+ {
+ for (j = 0; j < ARRAY_LEN (mask); j++)
+ mask[j] = 0xa5a5a5a5a5a5a5a5;
+
+ clib_mask_compare_u16_wrapper (i, array, mask, i + 1);
+
+ for (j = 0; j < (i >> 6); j++)
+ {
+ if (mask[j])
+ return clib_error_return (err, "mask at position %u not zero", j);
+ }
+ if (mask[j] != 1ULL << (i & 0x3f))
+ return clib_error_return (err,
+ "mask at position %u is %lx, expected %lx",
+ j, mask[j], 1ULL << (i % 64));
+
+ if (mask[j + 1] != 0xa5a5a5a5a5a5a5a5)
+ return clib_error_return (err, "mask overrun at position %u", j + 1);
+ }
+ return err;
+}
+
+REGISTER_TEST (clib_mask_compare_u16) = {
+ .name = "clib_mask_compare_u16",
+ .fn = test_clib_mask_compare_u16,
+};
+
+static clib_error_t *
+test_clib_mask_compare_u32 (clib_error_t *err)
+{
+ u32 array[513];
+ u64 mask[10];
+ u32 i, j;
+
+ for (i = 0; i < ARRAY_LEN (array); i++)
+ array[i] = i;
+
+ for (i = 0; i < ARRAY_LEN (array); i++)
+ {
+ for (j = 0; j < ARRAY_LEN (mask); j++)
+ mask[j] = 0xa5a5a5a5a5a5a5a5;
+
+ clib_mask_compare_u32_wrapper (i, array, mask, i + 1);
+
+ for (j = 0; j < (i >> 6); j++)
+ {
+ if (mask[j])
+ return clib_error_return (err, "mask at position %u not zero", j);
+ }
+ if (mask[j] != 1ULL << (i & 0x3f))
+ return clib_error_return (err,
+ "mask at position %u is %lx, expected %lx",
+ j, mask[j], 1ULL << (i % 64));
+
+ if (mask[j + 1] != 0xa5a5a5a5a5a5a5a5)
+ return clib_error_return (err, "mask overrun at position %u", j + 1);
+ }
+ return err;
+}
+
+REGISTER_TEST (clib_mask_compare_u32) = {
+ .name = "clib_mask_compare_u32",
+ .fn = test_clib_mask_compare_u32,
+};
diff --git a/src/vppinfra/vector/test/test.c b/src/vppinfra/vector/test/test.c
new file mode 100644
index 00000000000..0e90bacce49
--- /dev/null
+++ b/src/vppinfra/vector/test/test.c
@@ -0,0 +1,42 @@
+/* SPDX-License-Identifier: Apache-2.0
+ * Copyright(c) 2021 Cisco Systems, Inc.
+ */
+
+#include <vppinfra/format.h>
+#include <vppinfra/vector/test/test.h>
+
+test_registration_t *test_registrations[CLIB_MARCH_TYPE_N_VARIANTS] = {};
+
+int
+main (int argc, char *argv[])
+{
+ clib_mem_init (0, 64ULL << 20);
+
+ for (int i = 0; i < CLIB_MARCH_TYPE_N_VARIANTS; i++)
+ {
+ test_registration_t *r = test_registrations[i];
+
+ if (r == 0)
+ continue;
+
+ fformat (stdout, "\nMultiarch Variant: %U\n", format_march_variant, i);
+ fformat (stdout,
+ "-------------------------------------------------------\n");
+ while (r)
+ {
+ clib_error_t *err;
+ err = (r->fn) (0);
+ fformat (stdout, "%-50s %s\n", r->name, err ? "FAIL" : "PASS");
+ if (err)
+ {
+ clib_error_report (err);
+ fformat (stdout, "\n");
+ }
+
+ r = r->next;
+ }
+ }
+
+ fformat (stdout, "\n");
+ return 0;
+}
diff --git a/src/vppinfra/vector/test/test.h b/src/vppinfra/vector/test/test.h
new file mode 100644
index 00000000000..bc499fb24e8
--- /dev/null
+++ b/src/vppinfra/vector/test/test.h
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: Apache-2.0
+ * Copyright(c) 2021 Cisco Systems, Inc.
+ */
+
+#ifndef included_test_test_h
+#define included_test_test_h
+
+#include <vppinfra/cpu.h>
+
+typedef clib_error_t *(test_fn_t) (clib_error_t *);
+
+typedef struct test_registration_
+{
+ char *name;
+ u8 multiarch : 1;
+ test_fn_t *fn;
+ struct test_registration_ *next;
+} test_registration_t;
+
+extern test_registration_t *test_registrations[CLIB_MARCH_TYPE_N_VARIANTS];
+
+#define __clib_test_fn static __clib_noinline __clib_section (".test_wrapper")
+
+#define REGISTER_TEST(x) \
+ test_registration_t CLIB_MARCH_SFX (__test_##x); \
+ static void __clib_constructor CLIB_MARCH_SFX (__test_registration_##x) ( \
+ void) \
+ { \
+ test_registration_t *r = &CLIB_MARCH_SFX (__test_##x); \
+ r->next = test_registrations[CLIB_MARCH_SFX (CLIB_MARCH_VARIANT_TYPE)]; \
+ test_registrations[CLIB_MARCH_SFX (CLIB_MARCH_VARIANT_TYPE)] = r; \
+ } \
+ test_registration_t CLIB_MARCH_SFX (__test_##x)
+
+#endif