2 * Copyright (c) 2014 Kmindg <kmindg@gmail.com>
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "command-line.h"
26 enum { MAX_BITS = 20 * BITMAP_ULONG_BITS };
29 elapsed(const struct timeval *start)
34 return timeval_to_msec(&end) - timeval_to_msec(start);
37 /* Tests bitmap_equal. */
39 test_bitmap_equal(void)
43 a = bitmap_allocate(MAX_BITS);
44 b = bitmap_allocate(MAX_BITS);
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)));
51 bitmap_set_multiple(a, 10 * BITMAP_ULONG_BITS, BITMAP_ULONG_BITS, true);
52 assert(bitmap_equal(a, b, 10 * BITMAP_ULONG_BITS));
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)));
61 /* Tests bitmap_scan. */
63 test_bitmap_scan(void)
67 a = bitmap_allocate(MAX_BITS);
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);
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);
87 assert(bitmap_scan(a, true, 0, MAX_BITS - 1) == 0);
89 bitmap_set_multiple(a, 0, MAX_BITS, true);
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);
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);
109 assert(bitmap_scan(a, false, 0, MAX_BITS - 1) == 0);
113 run_test(void (*function)(void))
120 run_tests(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
122 run_test(test_bitmap_equal);
123 run_test(test_bitmap_scan);
128 run_benchmarks(int argc OVS_UNUSED, char *argv[])
130 int n_iter = strtol(argv[1], NULL, 10);
131 struct timeval start;
133 xgettimeofday(&start);
134 for (int i = 0; i < n_iter; i++) {
137 printf("bitmap equal: %5d ms\n", elapsed(&start));
139 xgettimeofday(&start);
140 for (int i = 0; i < n_iter; i++) {
143 printf("bitmap scan: %5d ms\n", elapsed(&start));
147 static const struct command commands[] = {
148 {"check", 0, 0, run_tests},
149 {"benchmark", 1, 1, run_benchmarks},
154 test_bitmap_main(int argc, char *argv[])
156 set_program_name(argv[0]);
157 run_command(argc - 1, argv + 1, commands);
160 OVSTEST_REGISTER("test-bitmap", test_bitmap_main);