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"
25 enum { MAX_BITS
= 20 * BITMAP_ULONG_BITS
};
28 elapsed(const struct timeval
*start
)
33 return timeval_to_msec(&end
) - timeval_to_msec(start
);
36 /* Tests bitmap_equal. */
38 test_bitmap_equal(void)
42 a
= bitmap_allocate(MAX_BITS
);
43 b
= bitmap_allocate(MAX_BITS
);
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)));
50 bitmap_set_multiple(a
, 10 * BITMAP_ULONG_BITS
, BITMAP_ULONG_BITS
, true);
51 assert(bitmap_equal(a
, b
, 10 * BITMAP_ULONG_BITS
));
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)));
63 /* Tests bitmap_scan. */
65 test_bitmap_scan(void)
69 a
= bitmap_allocate(MAX_BITS
);
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);
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);
84 assert(bitmap_scan(a
, true, 3, MAX_BITS
)
85 == MAX_BITS
- BITMAP_ULONG_BITS
+ 1);
86 bitmap_set1(a
, BITMAP_ULONG_BITS
- 1);
87 assert(bitmap_scan(a
, true, 7, MAX_BITS
- 1) == BITMAP_ULONG_BITS
- 1);
89 assert(bitmap_scan(a
, true, 0, MAX_BITS
- 7) == 0);
91 bitmap_set_multiple(a
, 0, MAX_BITS
, true);
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);
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);
106 assert(bitmap_scan(a
, false, 3, MAX_BITS
)
107 == MAX_BITS
- BITMAP_ULONG_BITS
+ 1);
108 bitmap_set0(a
, BITMAP_ULONG_BITS
- 1);
109 assert(bitmap_scan(a
, false, 7, MAX_BITS
- 1) == BITMAP_ULONG_BITS
- 1);
111 assert(bitmap_scan(a
, false, 0, MAX_BITS
- 7) == 0);
117 run_test(void (*function
)(void))
124 run_tests(struct ovs_cmdl_context
*ctx OVS_UNUSED
)
126 run_test(test_bitmap_equal
);
127 run_test(test_bitmap_scan
);
132 run_benchmarks(struct ovs_cmdl_context
*ctx
)
134 int n_iter
= strtol(ctx
->argv
[1], NULL
, 10);
135 struct timeval start
;
137 xgettimeofday(&start
);
138 for (int i
= 0; i
< n_iter
; i
++) {
141 printf("bitmap equal: %5d ms\n", elapsed(&start
));
143 xgettimeofday(&start
);
144 for (int i
= 0; i
< n_iter
; i
++) {
147 printf("bitmap scan: %5d ms\n", elapsed(&start
));
151 static const struct ovs_cmdl_command commands
[] = {
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
},
158 test_bitmap_main(int argc
, char *argv
[])
160 struct ovs_cmdl_context ctx
= {
165 set_program_name(argv
[0]);
166 ovs_cmdl_run_command(&ctx
, commands
);
169 OVSTEST_REGISTER("test-bitmap", test_bitmap_main
);