summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/vppinfra/CMakeLists.txt3
-rw-r--r--src/vppinfra/vector/test/count_equal.c99
2 files changed, 101 insertions, 1 deletions
diff --git a/src/vppinfra/CMakeLists.txt b/src/vppinfra/CMakeLists.txt
index 58ec32fbfeb..6900995e644 100644
--- a/src/vppinfra/CMakeLists.txt
+++ b/src/vppinfra/CMakeLists.txt
@@ -272,9 +272,10 @@ if(VPP_BUILD_VPPINFRA_TESTS)
endforeach()
set(test_files
+ vector/test/array_mask.c
vector/test/compress.c
+ vector/test/count_equal.c
vector/test/mask_compare.c
- vector/test/array_mask.c
)
add_vpp_executable(test_vector_funcs
diff --git a/src/vppinfra/vector/test/count_equal.c b/src/vppinfra/vector/test/count_equal.c
new file mode 100644
index 00000000000..cd1c8a5c4d1
--- /dev/null
+++ b/src/vppinfra/vector/test/count_equal.c
@@ -0,0 +1,99 @@
+/* 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/count_equal.h>
+
+#define foreach_clib_count_equal(type) \
+ typedef uword (wrapper_fn_##type) (type * a, uword maxcount); \
+ \
+ __clib_test_fn uword clib_count_equal_##type##_wrapper (type *a, \
+ uword maxcount) \
+ { \
+ return clib_count_equal_##type (a, maxcount); \
+ } \
+ \
+ static wrapper_fn_##type *wfn_##type = &clib_count_equal_##type##_wrapper; \
+ static clib_error_t *test_clib_count_equal_##type (clib_error_t *err) \
+ { \
+ u32 ps = clib_mem_get_log2_page_size (); \
+ void *map; \
+ \
+ u16 lengths[] = { \
+ 1, 2, 3, 5, 7, 9, 15, 16, 17, 31, 32, 33, 255, 256, 257 \
+ }; \
+ type *data; \
+ \
+ map = clib_mem_vm_map (0, 2ULL << ps, ps, "test"); \
+ if (map == CLIB_MEM_VM_MAP_FAILED) \
+ return clib_error_return (err, "clib_mem_vm_map failed"); \
+ \
+ data = ((type *) (map + (1ULL << ps))); \
+ data[-1] = 0xfe; \
+ \
+ mprotect (data, 1ULL < ps, PROT_NONE); \
+ \
+ for (int i = 1; i <= (1 << ps) / sizeof (data[0]); i++) \
+ data[-i] = 7; \
+ \
+ for (int i = 0; i < ARRAY_LEN (lengths); i++) \
+ { \
+ uword rv, len = lengths[i]; \
+ \
+ if ((rv = wfn_##type (data - len, len)) != len) \
+ { \
+ err = clib_error_return ( \
+ err, "testcase 1 failed for len %u (rv %u)", len, rv); \
+ goto done; \
+ } \
+ \
+ data[-1] = 8; \
+ if (len > 1 && ((rv = wfn_##type (data - len, len)) != len - 1)) \
+ { \
+ err = clib_error_return ( \
+ err, "testcase 2 failed for len %u (rv %u)", len, rv); \
+ goto done; \
+ } \
+ data[-1] = 7; \
+ \
+ data[-2] = 8; \
+ if (len > 2 && ((rv = wfn_##type (data - len, len)) != len - 2)) \
+ { \
+ err = clib_error_return ( \
+ err, "testcase 3 failed for len %u (rv %u)", len, rv); \
+ goto done; \
+ } \
+ data[-2] = 7; \
+ } \
+ \
+ done: \
+ clib_mem_vm_unmap (map); \
+ return err; \
+ }
+
+foreach_clib_count_equal (u8);
+foreach_clib_count_equal (u16);
+foreach_clib_count_equal (u32);
+foreach_clib_count_equal (u64);
+
+REGISTER_TEST (clib_count_equal_u8) = {
+ .name = "clib_count_equal_u8",
+ .fn = test_clib_count_equal_u8,
+};
+
+REGISTER_TEST (clib_count_equal_u16) = {
+ .name = "clib_count_equal_u16",
+ .fn = test_clib_count_equal_u16,
+};
+
+REGISTER_TEST (clib_count_equal_u32) = {
+ .name = "clib_count_equal_u32",
+ .fn = test_clib_count_equal_u32,
+};
+
+REGISTER_TEST (clib_count_equal_u64) = {
+ .name = "clib_count_equal_u64",
+ .fn = test_clib_count_equal_u64,
+};