aboutsummaryrefslogtreecommitdiffstats
path: root/src/vppinfra/test/count_equal.c
blob: 942c2203d3d93a8dc046274ddc13d01742517cbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* SPDX-License-Identifier: Apache-2.0
 * Copyright(c) 2021 Cisco Systems, Inc.
 */

#include <vppinfra/format.h>
#include <vppinfra/test/test.h>
#include <vppinfra/vector/count_equal.h>

#define foreach_clib_count_equal(type)                                        \
  typedef uword (wrapper_fn_##type) (type * a, uword maxcount);               \
                                                                              \
  __test_funct_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 (u8 d = 0; d < 255; d++)                                              \
      {                                                                       \
	for (int i = 1; i <= (1 << ps) / sizeof (data[0]); i++)               \
	  data[-i] = d;                                                       \
	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 data %u(rv %u)", len, d, \
		  rv);                                                        \
		goto done;                                                    \
	      }                                                               \
                                                                              \
	    data[-1] = d + 1;                                                 \
	    if (len > 1 && ((rv = wfn_##type (data - len, len)) != len - 1))  \
	      {                                                               \
		err = clib_error_return (                                     \
		  err, "testcase 2 failed for len %u data %u (rv %u)", len,   \
		  d, rv);                                                     \
		goto done;                                                    \
	      }                                                               \
	    data[-1] = d;                                                     \
                                                                              \
	    data[-2] = d + 1;                                                 \
	    if (len > 2 && ((rv = wfn_##type (data - len, len)) != len - 2))  \
	      {                                                               \
		err = clib_error_return (                                     \
		  err, "testcase 3 failed for len %u data %u (rv %u)", len,   \
		  d, rv);                                                     \
		goto done;                                                    \
	      }                                                               \
	    data[-2] = d;                                                     \
	  }                                                                   \
      }                                                                       \
                                                                              \
  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,
};