dpdk: Update documentation.
[cascardo/ovs.git] / tests / test-bitmap.c
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>
18 #include "bitmap.h"
19 #include "ovstest.h"
20 #include "timeval.h"
21 #include "command-line.h"
22
23 #undef NDEBUG
24 #include <assert.h>
25
26 enum { MAX_BITS = 20 * BITMAP_ULONG_BITS };
27
28 static int
29 elapsed(const struct timeval *start)
30 {
31     struct timeval end;
32
33     xgettimeofday(&end);
34     return timeval_to_msec(&end) - timeval_to_msec(start);
35 }
36
37 /* Tests bitmap_equal. */
38 static void
39 test_bitmap_equal(void)
40 {
41     unsigned long *a, *b;
42
43     a = bitmap_allocate(MAX_BITS);
44     b = bitmap_allocate(MAX_BITS);
45
46     /* equal test */
47     assert(bitmap_equal(a, b, MAX_BITS));
48     assert(bitmap_equal(a, b, MAX_BITS - 1));
49     assert(bitmap_equal(a, b, MAX_BITS - (BITMAP_ULONG_BITS - 1)));
50
51     bitmap_set_multiple(a, 10 * BITMAP_ULONG_BITS, BITMAP_ULONG_BITS, true);
52     assert(bitmap_equal(a, b, 10 * BITMAP_ULONG_BITS));
53
54     /* non-equal test */
55     assert(!bitmap_equal(a, b, 11 * BITMAP_ULONG_BITS));
56     assert(!bitmap_equal(a, b, 11 * BITMAP_ULONG_BITS - 1));
57     assert(!bitmap_equal(a, b,
58                          11 * BITMAP_ULONG_BITS - (BITMAP_ULONG_BITS - 1)));
59 }
60
61 /* Tests bitmap_scan. */
62 static void
63 test_bitmap_scan(void)
64 {
65     unsigned long *a;
66
67     a = bitmap_allocate(MAX_BITS);
68
69     /* scan for 1 */
70     assert(bitmap_scan(a, true, 1, BITMAP_ULONG_BITS) == BITMAP_ULONG_BITS);
71     assert(bitmap_scan(a, true, BITMAP_ULONG_BITS - 1, BITMAP_ULONG_BITS)
72            == BITMAP_ULONG_BITS);
73     assert(bitmap_scan(a, true, 0, BITMAP_ULONG_BITS) == BITMAP_ULONG_BITS);
74     assert(bitmap_scan(a, true, 0, BITMAP_ULONG_BITS + 1)
75            == BITMAP_ULONG_BITS + 1);
76     assert(bitmap_scan(a, true, 0, 2 * BITMAP_ULONG_BITS - 1)
77            == 2 * BITMAP_ULONG_BITS - 1);
78
79     bitmap_set1(a, MAX_BITS - 1);
80     assert(bitmap_scan(a, true, 0, MAX_BITS) == MAX_BITS - 1);
81     bitmap_set1(a, MAX_BITS - BITMAP_ULONG_BITS + 1);
82     assert(bitmap_scan(a, true, 0, MAX_BITS - 1)
83            == MAX_BITS - BITMAP_ULONG_BITS + 1);
84     bitmap_set1(a, BITMAP_ULONG_BITS - 1);
85     assert(bitmap_scan(a, true, 0, MAX_BITS - 1) == BITMAP_ULONG_BITS - 1);
86     bitmap_set1(a, 0);
87     assert(bitmap_scan(a, true, 0, MAX_BITS - 1) == 0);
88
89     bitmap_set_multiple(a, 0, MAX_BITS, true);
90
91     /* scan for 0 */
92     assert(bitmap_scan(a, false, 1, BITMAP_ULONG_BITS) == BITMAP_ULONG_BITS);
93     assert(bitmap_scan(a, false, BITMAP_ULONG_BITS - 1, BITMAP_ULONG_BITS)
94            == BITMAP_ULONG_BITS);
95     assert(bitmap_scan(a, false, 0, BITMAP_ULONG_BITS) == BITMAP_ULONG_BITS);
96     assert(bitmap_scan(a, false, 0, BITMAP_ULONG_BITS + 1)
97            == BITMAP_ULONG_BITS + 1);
98     assert(bitmap_scan(a, false, 0, 2 * BITMAP_ULONG_BITS - 1)
99            == 2 * BITMAP_ULONG_BITS - 1);
100
101     bitmap_set0(a, MAX_BITS - 1);
102     assert(bitmap_scan(a, false, 0, MAX_BITS) == MAX_BITS - 1);
103     bitmap_set0(a, MAX_BITS - BITMAP_ULONG_BITS + 1);
104     assert(bitmap_scan(a, false, 0, MAX_BITS - 1)
105            == MAX_BITS - BITMAP_ULONG_BITS + 1);
106     bitmap_set0(a, BITMAP_ULONG_BITS - 1);
107     assert(bitmap_scan(a, false, 0, MAX_BITS - 1) == BITMAP_ULONG_BITS - 1);
108     bitmap_set0(a, 0);
109     assert(bitmap_scan(a, false, 0, MAX_BITS - 1) == 0);
110 }
111
112 static void
113 run_test(void (*function)(void))
114 {
115     function();
116     printf(".");
117 }
118
119 static void
120 run_tests(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
121 {
122     run_test(test_bitmap_equal);
123     run_test(test_bitmap_scan);
124     printf("\n");
125 }
126
127 static void
128 run_benchmarks(int argc OVS_UNUSED, char *argv[])
129 {
130     int n_iter = strtol(argv[1], NULL, 10);
131     struct timeval start;
132
133     xgettimeofday(&start);
134     for (int i = 0; i < n_iter; i++) {
135         test_bitmap_equal();
136     }
137     printf("bitmap equal:  %5d ms\n", elapsed(&start));
138
139     xgettimeofday(&start);
140     for (int i = 0; i < n_iter; i++) {
141         test_bitmap_scan();
142     }
143     printf("bitmap scan:  %5d ms\n", elapsed(&start));
144     printf("\n");
145 }
146
147 static const struct command commands[] = {
148     {"check", 0, 0, run_tests},
149     {"benchmark", 1, 1, run_benchmarks},
150     {NULL, 0, 0, NULL},
151 };
152
153 static void
154 test_bitmap_main(int argc, char *argv[])
155 {
156     set_program_name(argv[0]);
157     run_command(argc - 1, argv + 1, commands);
158 }
159
160 OVSTEST_REGISTER("test-bitmap", test_bitmap_main);