2 * Copyright (c) 2011, 2012 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);
94 shuffle(unsigned int *p, size_t n)
96 for (; n > 1; n--, p++) {
97 unsigned int *q = &p[rand() % n];
98 unsigned int tmp = *p;
105 check_popcount(uint32_t x, int n)
107 if (popcount(x) != n) {
108 fprintf(stderr, "popcount(%#"PRIx32") is %d but should be %d\n",
115 test_popcount(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
117 unsigned int bits[32];
120 for (i = 0; i < ARRAY_SIZE(bits); i++) {
124 check_popcount(0, 0);
126 for (i = 0; i < 1000; i++) {
130 shuffle(bits, ARRAY_SIZE(bits));
131 for (j = 0; j < 32; j++) {
133 check_popcount(x, j + 1);
135 assert(x == UINT32_MAX);
137 shuffle(bits, ARRAY_SIZE(bits));
138 for (j = 31; j >= 0; j--) {
140 check_popcount(x, j);
146 /* Returns the sum of the squares of the first 'n' positive integers. */
148 sum_of_squares(int n)
150 return n * (n + 1) * (2 * n + 1) / 6;
154 test_bitwise_copy(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
156 unsigned int n_loops;
162 for (n_bits = 0; n_bits <= 64; n_bits++) {
163 for (src_ofs = 0; src_ofs < 64 - n_bits; src_ofs++) {
164 for (dst_ofs = 0; dst_ofs < 64 - n_bits; dst_ofs++) {
165 ovs_be64 src = htonll(random_uint64());
166 ovs_be64 dst = htonll(random_uint64());
167 ovs_be64 orig_dst = dst;
173 uint64_t mask = (UINT64_C(1) << n_bits) - 1;
174 expect = orig_dst & ~htonll(mask << dst_ofs);
175 expect |= htonll(((ntohll(src) >> src_ofs) & mask)
179 bitwise_copy(&src, sizeof src, src_ofs,
180 &dst, sizeof dst, dst_ofs,
183 fprintf(stderr,"copy_bits(0x%016"PRIx64",8,%d, "
184 "0x%016"PRIx64",8,%d, %d) yielded 0x%016"PRIx64" "
185 "instead of the expected 0x%016"PRIx64"\n",
186 ntohll(src), src_ofs,
187 ntohll(orig_dst), dst_ofs,
189 ntohll(dst), ntohll(expect));
198 if (n_loops != sum_of_squares(64)) {
204 test_bitwise_zero(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
206 unsigned int n_loops;
211 for (n_bits = 0; n_bits <= 64; n_bits++) {
212 for (dst_ofs = 0; dst_ofs < 64 - n_bits; dst_ofs++) {
213 ovs_be64 dst = htonll(random_uint64());
214 ovs_be64 orig_dst = dst;
220 uint64_t mask = (UINT64_C(1) << n_bits) - 1;
221 expect = orig_dst & ~htonll(mask << dst_ofs);
224 bitwise_zero(&dst, sizeof dst, dst_ofs, n_bits);
226 fprintf(stderr,"bitwise_zero(0x%016"PRIx64",8,%d, %d) "
227 "yielded 0x%016"PRIx64" "
228 "instead of the expected 0x%016"PRIx64"\n",
229 ntohll(orig_dst), dst_ofs,
231 ntohll(dst), ntohll(expect));
239 if (n_loops != 64 * (64 + 1) / 2) {
245 test_bitwise_one(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
247 unsigned int n_loops;
252 for (n_bits = 0; n_bits <= 64; n_bits++) {
253 for (dst_ofs = 0; dst_ofs < 64 - n_bits; dst_ofs++) {
254 ovs_be64 dst = htonll(random_uint64());
255 ovs_be64 orig_dst = dst;
259 expect = htonll(UINT64_MAX);
261 uint64_t mask = (UINT64_C(1) << n_bits) - 1;
262 expect = orig_dst | htonll(mask << dst_ofs);
265 bitwise_one(&dst, sizeof dst, dst_ofs, n_bits);
267 fprintf(stderr,"bitwise_one(0x%016"PRIx64",8,%d, %d) "
268 "yielded 0x%016"PRIx64" "
269 "instead of the expected 0x%016"PRIx64"\n",
270 ntohll(orig_dst), dst_ofs,
272 ntohll(dst), ntohll(expect));
280 if (n_loops != 64 * (64 + 1) / 2) {
286 test_bitwise_is_all_zeros(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
290 for (n_loops = 0; n_loops < 100; n_loops++) {
291 ovs_be64 x = htonll(0);
294 for (i = 0; i < 64; i++) {
298 /* Change a random 0-bit into a 1-bit. */
300 bit = htonll(UINT64_C(1) << (random_uint32() % 64));
304 for (ofs = 0; ofs < 64; ofs++) {
305 for (n = 0; n <= 64 - ofs; n++) {
311 : !(x & htonll(((UINT64_C(1) << n) - 1)
313 answer = bitwise_is_all_zeros(&x, sizeof x, ofs, n);
314 if (expect != answer) {
316 "bitwise_is_all_zeros(0x%016"PRIx64",8,%d,%d "
317 "returned %s instead of %s\n",
319 answer ? "true" : "false",
320 expect ? "true" : "false");
330 test_follow_symlinks(int argc, char *argv[])
334 for (i = 1; i < argc; i++) {
335 char *target = follow_symlinks(argv[i]);
342 test_assert(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
347 static const struct command commands[] = {
348 {"ctz", 0, 0, test_ctz},
349 {"popcount", 0, 0, test_popcount},
350 {"log_2_floor", 0, 0, test_log_2_floor},
351 {"bitwise_copy", 0, 0, test_bitwise_copy},
352 {"bitwise_zero", 0, 0, test_bitwise_zero},
353 {"bitwise_one", 0, 0, test_bitwise_one},
354 {"bitwise_is_all_zeros", 0, 0, test_bitwise_is_all_zeros},
355 {"follow-symlinks", 1, INT_MAX, test_follow_symlinks},
356 {"assert", 0, 0, test_assert},
361 parse_options(int argc, char *argv[])
366 static struct option long_options[] = {
370 char *short_options = long_options_to_short_options(long_options);
373 int c = getopt_long(argc, argv, short_options, long_options, NULL);
392 main(int argc, char *argv[])
394 set_program_name(argv[0]);
395 parse_options(argc, argv);
396 run_command(argc - optind, argv + optind, commands);