]> git.proxmox.com Git - mirror_ovs.git/blame - tests/test-bitmap.c
ofp-print: Abbreviate lists of fields in table features output.
[mirror_ovs.git] / tests / test-bitmap.c
CommitLineData
f831a472
K
1/*
2 * Copyright (c) 2014 Kmindg <kmindg@gmail.com>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <config.h>
3f636c7e 18#undef NDEBUG
f831a472 19#include "bitmap.h"
3f636c7e
JR
20#include <assert.h>
21#include "command-line.h"
f831a472
K
22#include "ovstest.h"
23#include "timeval.h"
f831a472
K
24
25enum { MAX_BITS = 20 * BITMAP_ULONG_BITS };
26
27static int
28elapsed(const struct timeval *start)
29{
30 struct timeval end;
31
32 xgettimeofday(&end);
33 return timeval_to_msec(&end) - timeval_to_msec(start);
34}
35
36/* Tests bitmap_equal. */
37static void
38test_bitmap_equal(void)
39{
40 unsigned long *a, *b;
41
42 a = bitmap_allocate(MAX_BITS);
43 b = bitmap_allocate(MAX_BITS);
44
45 /* equal test */
46 assert(bitmap_equal(a, b, MAX_BITS));
47 assert(bitmap_equal(a, b, MAX_BITS - 1));
48 assert(bitmap_equal(a, b, MAX_BITS - (BITMAP_ULONG_BITS - 1)));
49
50 bitmap_set_multiple(a, 10 * BITMAP_ULONG_BITS, BITMAP_ULONG_BITS, true);
51 assert(bitmap_equal(a, b, 10 * BITMAP_ULONG_BITS));
52
53 /* non-equal test */
54 assert(!bitmap_equal(a, b, 11 * BITMAP_ULONG_BITS));
55 assert(!bitmap_equal(a, b, 11 * BITMAP_ULONG_BITS - 1));
56 assert(!bitmap_equal(a, b,
57 11 * BITMAP_ULONG_BITS - (BITMAP_ULONG_BITS - 1)));
c44a2a6d
TG
58
59 free(b);
60 free(a);
f831a472
K
61}
62
63/* Tests bitmap_scan. */
64static void
65test_bitmap_scan(void)
66{
67 unsigned long *a;
68
69 a = bitmap_allocate(MAX_BITS);
70
71 /* scan for 1 */
72 assert(bitmap_scan(a, true, 1, BITMAP_ULONG_BITS) == BITMAP_ULONG_BITS);
73 assert(bitmap_scan(a, true, BITMAP_ULONG_BITS - 1, BITMAP_ULONG_BITS)
74 == BITMAP_ULONG_BITS);
75 assert(bitmap_scan(a, true, 0, BITMAP_ULONG_BITS) == BITMAP_ULONG_BITS);
76 assert(bitmap_scan(a, true, 0, BITMAP_ULONG_BITS + 1)
77 == BITMAP_ULONG_BITS + 1);
78 assert(bitmap_scan(a, true, 0, 2 * BITMAP_ULONG_BITS - 1)
79 == 2 * BITMAP_ULONG_BITS - 1);
80
81 bitmap_set1(a, MAX_BITS - 1);
82 assert(bitmap_scan(a, true, 0, MAX_BITS) == MAX_BITS - 1);
83 bitmap_set1(a, MAX_BITS - BITMAP_ULONG_BITS + 1);
795b3288 84 assert(bitmap_scan(a, true, 3, MAX_BITS)
f831a472
K
85 == MAX_BITS - BITMAP_ULONG_BITS + 1);
86 bitmap_set1(a, BITMAP_ULONG_BITS - 1);
795b3288 87 assert(bitmap_scan(a, true, 7, MAX_BITS - 1) == BITMAP_ULONG_BITS - 1);
f831a472 88 bitmap_set1(a, 0);
795b3288 89 assert(bitmap_scan(a, true, 0, MAX_BITS - 7) == 0);
f831a472
K
90
91 bitmap_set_multiple(a, 0, MAX_BITS, true);
92
93 /* scan for 0 */
94 assert(bitmap_scan(a, false, 1, BITMAP_ULONG_BITS) == BITMAP_ULONG_BITS);
95 assert(bitmap_scan(a, false, BITMAP_ULONG_BITS - 1, BITMAP_ULONG_BITS)
96 == BITMAP_ULONG_BITS);
97 assert(bitmap_scan(a, false, 0, BITMAP_ULONG_BITS) == BITMAP_ULONG_BITS);
98 assert(bitmap_scan(a, false, 0, BITMAP_ULONG_BITS + 1)
99 == BITMAP_ULONG_BITS + 1);
100 assert(bitmap_scan(a, false, 0, 2 * BITMAP_ULONG_BITS - 1)
101 == 2 * BITMAP_ULONG_BITS - 1);
102
103 bitmap_set0(a, MAX_BITS - 1);
104 assert(bitmap_scan(a, false, 0, MAX_BITS) == MAX_BITS - 1);
105 bitmap_set0(a, MAX_BITS - BITMAP_ULONG_BITS + 1);
795b3288 106 assert(bitmap_scan(a, false, 3, MAX_BITS)
f831a472
K
107 == MAX_BITS - BITMAP_ULONG_BITS + 1);
108 bitmap_set0(a, BITMAP_ULONG_BITS - 1);
795b3288 109 assert(bitmap_scan(a, false, 7, MAX_BITS - 1) == BITMAP_ULONG_BITS - 1);
f831a472 110 bitmap_set0(a, 0);
795b3288 111 assert(bitmap_scan(a, false, 0, MAX_BITS - 7) == 0);
c44a2a6d
TG
112
113 free(a);
f831a472
K
114}
115
116static void
117run_test(void (*function)(void))
118{
119 function();
120 printf(".");
121}
122
123static void
1636c761 124run_tests(struct ovs_cmdl_context *ctx OVS_UNUSED)
f831a472
K
125{
126 run_test(test_bitmap_equal);
127 run_test(test_bitmap_scan);
128 printf("\n");
129}
130
131static void
1636c761 132run_benchmarks(struct ovs_cmdl_context *ctx)
f831a472 133{
1636c761 134 int n_iter = strtol(ctx->argv[1], NULL, 10);
f831a472
K
135 struct timeval start;
136
137 xgettimeofday(&start);
138 for (int i = 0; i < n_iter; i++) {
139 test_bitmap_equal();
140 }
141 printf("bitmap equal: %5d ms\n", elapsed(&start));
142
143 xgettimeofday(&start);
144 for (int i = 0; i < n_iter; i++) {
145 test_bitmap_scan();
146 }
147 printf("bitmap scan: %5d ms\n", elapsed(&start));
148 printf("\n");
149}
150
5f383751 151static const struct ovs_cmdl_command commands[] = {
1f4a7252
RM
152 {"check", NULL, 0, 0, run_tests, OVS_RO},
153 {"benchmark", NULL, 1, 1, run_benchmarks, OVS_RO},
154 {NULL, NULL, 0, 0, NULL, OVS_RO},
f831a472
K
155};
156
157static void
158test_bitmap_main(int argc, char *argv[])
159{
1636c761
RB
160 struct ovs_cmdl_context ctx = {
161 .argc = argc - 1,
162 .argv = argv + 1,
163 };
164
f831a472 165 set_program_name(argv[0]);
1636c761 166 ovs_cmdl_run_command(&ctx, commands);
f831a472
K
167}
168
169OVSTEST_REGISTER("test-bitmap", test_bitmap_main);