2 * Copyright (c) 2011, 2012, 2013 Nicira, Inc.
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.
25 #include "byte-order.h"
26 #include "command-line.h"
35 check_log_2_floor(uint32_t x, int n)
37 if (log_2_floor(x) != n) {
38 fprintf(stderr, "log_2_floor(%"PRIu32") is %d but should be %d\n",
39 x, log_2_floor(x), n);
45 test_log_2_floor(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
49 for (n = 0; n < 32; n++) {
50 /* Check minimum x such that f(x) == n. */
51 check_log_2_floor(1 << n, n);
53 /* Check maximum x such that f(x) == n. */
54 check_log_2_floor((1 << n) | ((1 << n) - 1), n);
56 /* Check a random value in the middle. */
57 check_log_2_floor((random_uint32() & ((1 << n) - 1)) | (1 << n), n);
60 /* log_2_floor(0) is undefined, so don't check it. */
64 check_ctz(uint32_t x, int n)
67 fprintf(stderr, "ctz(%"PRIu32") is %d but should be %d\n",
74 test_ctz(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
78 for (n = 0; n < 32; n++) {
79 /* Check minimum x such that f(x) == n. */
82 /* Check maximum x such that f(x) == n. */
83 check_ctz(UINT32_MAX << n, n);
85 /* Check a random value in the middle. */
86 check_ctz((random_uint32() | 1) << n, n);
93 /* Returns a random number in the range 'min'...'max' inclusive. */
95 random_in_range(uint32_t min, uint32_t max)
97 return min == max ? min : min + random_range(max - min + 1);
101 check_rup2(uint32_t x, int n)
103 uint32_t rup2 = ROUND_UP_POW2(x);
105 fprintf(stderr, "ROUND_UP_POW2(%#"PRIx32") is %#"PRIx32" "
106 "but should be %#"PRIx32"\n", x, rup2, n);
112 test_round_up_pow2(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
116 for (n = 0; n < 32; n++) {
117 /* Min, max value for which ROUND_UP_POW2 should yield (1 << n). */
118 uint32_t min = ((1u << n) >> 1) + 1;
119 uint32_t max = 1u << n;
121 check_rup2(min, 1u << n);
122 check_rup2(max, 1u << n);
123 check_rup2(random_in_range(min, max), 1u << n);
129 check_rdp2(uint32_t x, int n)
131 uint32_t rdp2 = ROUND_DOWN_POW2(x);
133 fprintf(stderr, "ROUND_DOWN_POW2(%#"PRIx32") is %#"PRIx32" "
134 "but should be %#"PRIx32"\n", x, rdp2, n);
140 test_round_down_pow2(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
144 for (n = 0; n < 32; n++) {
145 /* Min, max value for which ROUND_DOWN_POW2 should yield (1 << n). */
146 uint32_t min = 1u << n;
147 uint32_t max = ((1u << n) << 1) - 1;
149 check_rdp2(min, 1u << n);
150 check_rdp2(max, 1u << n);
151 check_rdp2(random_in_range(min, max), 1u << n);
157 shuffle(unsigned int *p, size_t n)
159 for (; n > 1; n--, p++) {
160 unsigned int *q = &p[random_range(n)];
161 unsigned int tmp = *p;
168 check_popcount(uint32_t x, int n)
170 if (popcount(x) != n) {
171 fprintf(stderr, "popcount(%#"PRIx32") is %d but should be %d\n",
178 test_popcount(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
180 unsigned int bits[32];
183 for (i = 0; i < ARRAY_SIZE(bits); i++) {
187 check_popcount(0, 0);
189 for (i = 0; i < 1000; i++) {
193 shuffle(bits, ARRAY_SIZE(bits));
194 for (j = 0; j < 32; j++) {
196 check_popcount(x, j + 1);
198 assert(x == UINT32_MAX);
200 shuffle(bits, ARRAY_SIZE(bits));
201 for (j = 31; j >= 0; j--) {
203 check_popcount(x, j);
209 /* Returns the sum of the squares of the first 'n' positive integers. */
211 sum_of_squares(int n)
213 return n * (n + 1) * (2 * n + 1) / 6;
217 test_bitwise_copy(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
219 unsigned int n_loops;
225 for (n_bits = 0; n_bits <= 64; n_bits++) {
226 for (src_ofs = 0; src_ofs < 64 - n_bits; src_ofs++) {
227 for (dst_ofs = 0; dst_ofs < 64 - n_bits; dst_ofs++) {
228 ovs_be64 src = htonll(random_uint64());
229 ovs_be64 dst = htonll(random_uint64());
230 ovs_be64 orig_dst = dst;
236 uint64_t mask = (UINT64_C(1) << n_bits) - 1;
237 expect = orig_dst & ~htonll(mask << dst_ofs);
238 expect |= htonll(((ntohll(src) >> src_ofs) & mask)
242 bitwise_copy(&src, sizeof src, src_ofs,
243 &dst, sizeof dst, dst_ofs,
246 fprintf(stderr,"copy_bits(0x%016"PRIx64",8,%d, "
247 "0x%016"PRIx64",8,%d, %d) yielded 0x%016"PRIx64" "
248 "instead of the expected 0x%016"PRIx64"\n",
249 ntohll(src), src_ofs,
250 ntohll(orig_dst), dst_ofs,
252 ntohll(dst), ntohll(expect));
261 if (n_loops != sum_of_squares(64)) {
267 test_bitwise_zero(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
269 unsigned int n_loops;
274 for (n_bits = 0; n_bits <= 64; n_bits++) {
275 for (dst_ofs = 0; dst_ofs < 64 - n_bits; dst_ofs++) {
276 ovs_be64 dst = htonll(random_uint64());
277 ovs_be64 orig_dst = dst;
283 uint64_t mask = (UINT64_C(1) << n_bits) - 1;
284 expect = orig_dst & ~htonll(mask << dst_ofs);
287 bitwise_zero(&dst, sizeof dst, dst_ofs, n_bits);
289 fprintf(stderr,"bitwise_zero(0x%016"PRIx64",8,%d, %d) "
290 "yielded 0x%016"PRIx64" "
291 "instead of the expected 0x%016"PRIx64"\n",
292 ntohll(orig_dst), dst_ofs,
294 ntohll(dst), ntohll(expect));
302 if (n_loops != 64 * (64 + 1) / 2) {
308 test_bitwise_one(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
310 unsigned int n_loops;
315 for (n_bits = 0; n_bits <= 64; n_bits++) {
316 for (dst_ofs = 0; dst_ofs < 64 - n_bits; dst_ofs++) {
317 ovs_be64 dst = htonll(random_uint64());
318 ovs_be64 orig_dst = dst;
322 expect = htonll(UINT64_MAX);
324 uint64_t mask = (UINT64_C(1) << n_bits) - 1;
325 expect = orig_dst | htonll(mask << dst_ofs);
328 bitwise_one(&dst, sizeof dst, dst_ofs, n_bits);
330 fprintf(stderr,"bitwise_one(0x%016"PRIx64",8,%d, %d) "
331 "yielded 0x%016"PRIx64" "
332 "instead of the expected 0x%016"PRIx64"\n",
333 ntohll(orig_dst), dst_ofs,
335 ntohll(dst), ntohll(expect));
343 if (n_loops != 64 * (64 + 1) / 2) {
349 test_bitwise_is_all_zeros(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
353 for (n_loops = 0; n_loops < 100; n_loops++) {
354 ovs_be64 x = htonll(0);
357 for (i = 0; i < 64; i++) {
361 /* Change a random 0-bit into a 1-bit. */
363 bit = htonll(UINT64_C(1) << (random_range(64)));
367 for (ofs = 0; ofs < 64; ofs++) {
368 for (n = 0; n <= 64 - ofs; n++) {
374 : !(x & htonll(((UINT64_C(1) << n) - 1)
376 answer = bitwise_is_all_zeros(&x, sizeof x, ofs, n);
377 if (expect != answer) {
379 "bitwise_is_all_zeros(0x%016"PRIx64",8,%d,%d "
380 "returned %s instead of %s\n",
382 answer ? "true" : "false",
383 expect ? "true" : "false");
393 test_follow_symlinks(int argc, char *argv[])
397 for (i = 1; i < argc; i++) {
398 char *target = follow_symlinks(argv[i]);
405 test_assert(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
410 static const struct command commands[] = {
411 {"ctz", 0, 0, test_ctz},
412 {"round_up_pow2", 0, 0, test_round_up_pow2},
413 {"round_down_pow2", 0, 0, test_round_down_pow2},
414 {"popcount", 0, 0, test_popcount},
415 {"log_2_floor", 0, 0, test_log_2_floor},
416 {"bitwise_copy", 0, 0, test_bitwise_copy},
417 {"bitwise_zero", 0, 0, test_bitwise_zero},
418 {"bitwise_one", 0, 0, test_bitwise_one},
419 {"bitwise_is_all_zeros", 0, 0, test_bitwise_is_all_zeros},
420 {"follow-symlinks", 1, INT_MAX, test_follow_symlinks},
421 {"assert", 0, 0, test_assert},
426 parse_options(int argc, char *argv[])
431 static const struct option long_options[] = {
435 char *short_options = long_options_to_short_options(long_options);
438 int c = getopt_long(argc, argv, short_options, long_options, NULL);
457 main(int argc, char *argv[])
459 set_program_name(argv[0]);
460 parse_options(argc, argv);
461 run_command(argc - optind, argv + optind, commands);