vppinfra: clib_count_equal_* tests

Type: improvement
Change-Id: I8f75cd9ce78ce686985e65c75dcddf498cef7621
Signed-off-by: Damjan Marion <damarion@cisco.com>
diff --git a/src/vppinfra/CMakeLists.txt b/src/vppinfra/CMakeLists.txt
index 58ec32f..6900995 100644
--- a/src/vppinfra/CMakeLists.txt
+++ b/src/vppinfra/CMakeLists.txt
@@ -272,9 +272,10 @@
   endforeach()
 
 set(test_files
-  vector/test/compress.c
-  vector/test/mask_compare.c
   vector/test/array_mask.c
+  vector/test/compress.c
+  vector/test/count_equal.c
+  vector/test/mask_compare.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 0000000..cd1c8a5
--- /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,
+};