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_ctz32(uint32_t x, int n)
67 fprintf(stderr, "ctz32(%"PRIu32") is %d but should be %d\n",
74 check_ctz64(uint64_t x, int n)
77 fprintf(stderr, "ctz64(%"PRIu64") is %d but should be %d\n",
84 test_ctz(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
88 for (n = 0; n < 32; n++) {
89 /* Check minimum x such that f(x) == n. */
90 check_ctz32(1 << n, n);
92 /* Check maximum x such that f(x) == n. */
93 check_ctz32(UINT32_MAX << n, n);
95 /* Check a random value in the middle. */
96 check_ctz32((random_uint32() | 1) << n, n);
100 for (n = 0; n < 64; n++) {
101 /* Check minimum x such that f(x) == n. */
102 check_ctz64(UINT64_C(1) << n, n);
104 /* Check maximum x such that f(x) == n. */
105 check_ctz64(UINT64_MAX << n, n);
107 /* Check a random value in the middle. */
108 check_ctz64((random_uint64() | UINT64_C(1)) << n, n);
117 check_clz32(uint32_t x, int n)
120 fprintf(stderr, "clz32(%"PRIu32") is %d but should be %d\n",
127 check_clz64(uint64_t x, int n)
130 fprintf(stderr, "clz64(%"PRIu64") is %d but should be %d\n",
137 test_clz(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
141 for (n = 0; n < 32; n++) {
142 /* Check minimum x such that f(x) == n. */
143 check_clz32((1u << 31) >> n, n);
145 /* Check maximum x such that f(x) == n. */
146 check_clz32(UINT32_MAX >> n, n);
148 /* Check a random value in the middle. */
149 check_clz32((random_uint32() | 1u << 31) >> n, n);
152 for (n = 0; n < 64; n++) {
153 /* Check minimum x such that f(x) == n. */
154 check_clz64((UINT64_C(1) << 63) >> n, n);
156 /* Check maximum x such that f(x) == n. */
157 check_clz64(UINT64_MAX >> n, n);
159 /* Check a random value in the middle. */
160 check_clz64((random_uint64() | UINT64_C(1) << 63) >> n, n);
168 /* Returns a random number in the range 'min'...'max' inclusive. */
170 random_in_range(uint32_t min, uint32_t max)
172 return min == max ? min : min + random_range(max - min + 1);
176 check_rup2(uint32_t x, int n)
178 uint32_t rup2 = ROUND_UP_POW2(x);
180 fprintf(stderr, "ROUND_UP_POW2(%#"PRIx32") is %#"PRIx32" "
181 "but should be %#"PRIx32"\n", x, rup2, n);
187 test_round_up_pow2(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
191 for (n = 0; n < 32; n++) {
192 /* Min, max value for which ROUND_UP_POW2 should yield (1 << n). */
193 uint32_t min = ((1u << n) >> 1) + 1;
194 uint32_t max = 1u << n;
196 check_rup2(min, 1u << n);
197 check_rup2(max, 1u << n);
198 check_rup2(random_in_range(min, max), 1u << n);
204 check_rdp2(uint32_t x, int n)
206 uint32_t rdp2 = ROUND_DOWN_POW2(x);
208 fprintf(stderr, "ROUND_DOWN_POW2(%#"PRIx32") is %#"PRIx32" "
209 "but should be %#"PRIx32"\n", x, rdp2, n);
215 test_round_down_pow2(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
219 for (n = 0; n < 32; n++) {
220 /* Min, max value for which ROUND_DOWN_POW2 should yield (1 << n). */
221 uint32_t min = 1u << n;
222 uint32_t max = ((1u << n) << 1) - 1;
224 check_rdp2(min, 1u << n);
225 check_rdp2(max, 1u << n);
226 check_rdp2(random_in_range(min, max), 1u << n);
232 shuffle(uint64_t *p, size_t n)
234 for (; n > 1; n--, p++) {
235 uint64_t *q = &p[random_range(n)];
243 check_count_1bits(uint64_t x, int n)
245 if (count_1bits(x) != n) {
246 fprintf(stderr, "count_1bits(%#"PRIx64") is %d but should be %d\n",
247 x, count_1bits(x), n);
253 test_count_1bits(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
258 for (i = 0; i < ARRAY_SIZE(bits); i++) {
259 bits[i] = UINT64_C(1) << i;
262 check_count_1bits(0, 0);
264 for (i = 0; i < 1000; i++) {
268 shuffle(bits, ARRAY_SIZE(bits));
269 for (j = 0; j < 64; j++) {
271 check_count_1bits(x, j + 1);
273 assert(x == UINT64_MAX);
275 shuffle(bits, ARRAY_SIZE(bits));
276 for (j = 63; j >= 0; j--) {
278 check_count_1bits(x, j);
284 /* Returns the sum of the squares of the first 'n' positive integers. */
286 sum_of_squares(int n)
288 return n * (n + 1) * (2 * n + 1) / 6;
292 test_bitwise_copy(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
294 unsigned int n_loops;
300 for (n_bits = 0; n_bits <= 64; n_bits++) {
301 for (src_ofs = 0; src_ofs < 64 - n_bits; src_ofs++) {
302 for (dst_ofs = 0; dst_ofs < 64 - n_bits; dst_ofs++) {
303 ovs_be64 src = htonll(random_uint64());
304 ovs_be64 dst = htonll(random_uint64());
305 ovs_be64 orig_dst = dst;
311 uint64_t mask = (UINT64_C(1) << n_bits) - 1;
312 expect = orig_dst & ~htonll(mask << dst_ofs);
313 expect |= htonll(((ntohll(src) >> src_ofs) & mask)
317 bitwise_copy(&src, sizeof src, src_ofs,
318 &dst, sizeof dst, dst_ofs,
321 fprintf(stderr,"copy_bits(0x%016"PRIx64",8,%d, "
322 "0x%016"PRIx64",8,%d, %d) yielded 0x%016"PRIx64" "
323 "instead of the expected 0x%016"PRIx64"\n",
324 ntohll(src), src_ofs,
325 ntohll(orig_dst), dst_ofs,
327 ntohll(dst), ntohll(expect));
336 if (n_loops != sum_of_squares(64)) {
342 test_bitwise_zero(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
344 unsigned int n_loops;
349 for (n_bits = 0; n_bits <= 64; n_bits++) {
350 for (dst_ofs = 0; dst_ofs < 64 - n_bits; dst_ofs++) {
351 ovs_be64 dst = htonll(random_uint64());
352 ovs_be64 orig_dst = dst;
358 uint64_t mask = (UINT64_C(1) << n_bits) - 1;
359 expect = orig_dst & ~htonll(mask << dst_ofs);
362 bitwise_zero(&dst, sizeof dst, dst_ofs, n_bits);
364 fprintf(stderr,"bitwise_zero(0x%016"PRIx64",8,%d, %d) "
365 "yielded 0x%016"PRIx64" "
366 "instead of the expected 0x%016"PRIx64"\n",
367 ntohll(orig_dst), dst_ofs,
369 ntohll(dst), ntohll(expect));
377 if (n_loops != 64 * (64 + 1) / 2) {
383 test_bitwise_one(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
385 unsigned int n_loops;
390 for (n_bits = 0; n_bits <= 64; n_bits++) {
391 for (dst_ofs = 0; dst_ofs < 64 - n_bits; dst_ofs++) {
392 ovs_be64 dst = htonll(random_uint64());
393 ovs_be64 orig_dst = dst;
397 expect = OVS_BE64_MAX;
399 uint64_t mask = (UINT64_C(1) << n_bits) - 1;
400 expect = orig_dst | htonll(mask << dst_ofs);
403 bitwise_one(&dst, sizeof dst, dst_ofs, n_bits);
405 fprintf(stderr,"bitwise_one(0x%016"PRIx64",8,%d, %d) "
406 "yielded 0x%016"PRIx64" "
407 "instead of the expected 0x%016"PRIx64"\n",
408 ntohll(orig_dst), dst_ofs,
410 ntohll(dst), ntohll(expect));
418 if (n_loops != 64 * (64 + 1) / 2) {
424 test_bitwise_is_all_zeros(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
428 for (n_loops = 0; n_loops < 100; n_loops++) {
429 ovs_be64 x = htonll(0);
432 for (i = 0; i < 64; i++) {
436 /* Change a random 0-bit into a 1-bit. */
438 bit = htonll(UINT64_C(1) << (random_range(64)));
442 for (ofs = 0; ofs < 64; ofs++) {
443 for (n = 0; n <= 64 - ofs; n++) {
449 : !(x & htonll(((UINT64_C(1) << n) - 1)
451 answer = bitwise_is_all_zeros(&x, sizeof x, ofs, n);
452 if (expect != answer) {
454 "bitwise_is_all_zeros(0x%016"PRIx64",8,%d,%d "
455 "returned %s instead of %s\n",
457 answer ? "true" : "false",
458 expect ? "true" : "false");
468 test_follow_symlinks(int argc, char *argv[])
472 for (i = 1; i < argc; i++) {
473 char *target = follow_symlinks(argv[i]);
480 test_assert(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
486 test_ovs_scan(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
488 char str[16], str2[16], str3[16];
502 ovs_assert(ovs_scan("", ""));
503 ovs_assert(ovs_scan("", " "));
504 ovs_assert(ovs_scan(" ", " "));
505 ovs_assert(ovs_scan(" ", " "));
506 ovs_assert(ovs_scan(" \t ", " "));
508 ovs_assert(ovs_scan("xyzzy", "xyzzy"));
509 ovs_assert(ovs_scan("xy%zzy", "xy%%zzy"));
510 ovs_assert(!ovs_scan(" xy%zzy", "xy%%zzy"));
511 ovs_assert(ovs_scan(" xy%\tzzy", " xy%% zzy"));
513 ovs_assert(ovs_scan("123", "%d", &i));
514 ovs_assert(i == 123);
515 ovs_assert(ovs_scan("0", "%d", &i));
517 ovs_assert(!ovs_scan("123", "%d%d", &i, &i2));
518 ovs_assert(ovs_scan("+123", "%d", &i));
519 ovs_assert(i == 123);
520 ovs_assert(ovs_scan("-123", "%d", &i));
521 ovs_assert(i == -123);
522 ovs_assert(ovs_scan("0123", "%d", &i));
523 ovs_assert(i == 123);
524 ovs_assert(ovs_scan(" 123", "%d", &i));
525 ovs_assert(i == 123);
526 ovs_assert(ovs_scan("0x123", "%d", &i));
528 ovs_assert(ovs_scan("123", "%2d %d", &i, &i2));
531 ovs_assert(ovs_scan("+123", "%2d %d", &i, &i2));
533 ovs_assert(i2 == 23);
534 ovs_assert(ovs_scan("-123", "%2d %d", &i, &i2));
536 ovs_assert(i2 == 23);
537 ovs_assert(ovs_scan("0123", "%2d %d", &i, &i2));
539 ovs_assert(i2 == 23);
540 ovs_assert(ovs_scan("123", "%*2d %d", &i));
542 ovs_assert(ovs_scan("+123", "%2d %*d", &i));
544 ovs_assert(i2 == 23);
545 ovs_assert(ovs_scan("-123", "%*2d %*d"));
547 ovs_assert(ovs_scan("123", "%u", &i));
548 ovs_assert(i == 123);
549 ovs_assert(ovs_scan("0", "%u", &i));
551 ovs_assert(!ovs_scan("123", "%u%u", &i, &i2));
552 ovs_assert(ovs_scan("+123", "%u", &i));
553 ovs_assert(i == 123);
554 ovs_assert(ovs_scan("-123", "%u", &i));
555 ovs_assert(i == -123);
556 ovs_assert(ovs_scan("0123", "%u", &i));
557 ovs_assert(i == 123);
558 ovs_assert(ovs_scan(" 123", "%u", &i));
559 ovs_assert(i == 123);
560 ovs_assert(ovs_scan("0x123", "%u", &i));
562 ovs_assert(ovs_scan("123", "%2u %u", &i, &i2));
565 ovs_assert(ovs_scan("+123", "%2u %u", &i, &i2));
567 ovs_assert(i2 == 23);
568 ovs_assert(ovs_scan("-123", "%2u %u", &i, &i2));
570 ovs_assert(i2 == 23);
571 ovs_assert(ovs_scan("0123", "%2u %u", &i, &i2));
573 ovs_assert(i2 == 23);
574 ovs_assert(ovs_scan("123", "%*2u %u", &i));
576 ovs_assert(ovs_scan("+123", "%2u %*u", &i));
578 ovs_assert(i2 == 23);
579 ovs_assert(ovs_scan("-123", "%*2u %*u"));
581 ovs_assert(ovs_scan("123", "%i", &i));
582 ovs_assert(i == 123);
583 ovs_assert(ovs_scan("0", "%i", &i));
585 ovs_assert(!ovs_scan("123", "%i%i", &i, &i2));
586 ovs_assert(ovs_scan("+123", "%i", &i));
587 ovs_assert(i == 123);
588 ovs_assert(ovs_scan("-123", "%i", &i));
589 ovs_assert(i == -123);
590 ovs_assert(ovs_scan("0123", "%i", &i));
591 ovs_assert(i == 0123);
592 ovs_assert(ovs_scan(" 123", "%i", &i));
593 ovs_assert(i == 123);
594 ovs_assert(ovs_scan("0x123", "%i", &i));
595 ovs_assert(i == 0x123);
596 ovs_assert(ovs_scan("123", "%2i %i", &i, &i2));
599 ovs_assert(ovs_scan("+123", "%2i %i", &i, &i2));
601 ovs_assert(i2 == 23);
602 ovs_assert(ovs_scan("-123", "%2i %i", &i, &i2));
604 ovs_assert(i2 == 23);
605 ovs_assert(ovs_scan("0123", "%2i %i", &i, &i2));
607 ovs_assert(i2 == 23);
608 ovs_assert(ovs_scan("123", "%*2i %i", &i));
610 ovs_assert(ovs_scan("+123", "%2i %*i", &i));
612 ovs_assert(i2 == 23);
613 ovs_assert(ovs_scan("-123", "%*2i %*i"));
615 ovs_assert(ovs_scan("123", "%o", &i));
616 ovs_assert(i == 0123);
617 ovs_assert(ovs_scan("0", "%o", &i));
619 ovs_assert(!ovs_scan("123", "%o%o", &i, &i2));
620 ovs_assert(ovs_scan("+123", "%o", &i));
621 ovs_assert(i == 0123);
622 ovs_assert(ovs_scan("-123", "%o", &i));
623 ovs_assert(i == -0123);
624 ovs_assert(ovs_scan("0123", "%o", &i));
625 ovs_assert(i == 0123);
626 ovs_assert(ovs_scan(" 123", "%o", &i));
627 ovs_assert(i == 0123);
628 ovs_assert(ovs_scan("0x123", "%o", &i));
630 ovs_assert(ovs_scan("123", "%2o %o", &i, &i2));
631 ovs_assert(i == 012);
633 ovs_assert(ovs_scan("+123", "%2o %o", &i, &i2));
635 ovs_assert(i2 == 023);
636 ovs_assert(ovs_scan("-123", "%2o %o", &i, &i2));
638 ovs_assert(i2 == 023);
639 ovs_assert(ovs_scan("0123", "%2o %o", &i, &i2));
641 ovs_assert(i2 == 023);
642 ovs_assert(ovs_scan("123", "%*2o %o", &i));
644 ovs_assert(ovs_scan("+123", "%2o %*o", &i));
646 ovs_assert(i2 == 023);
647 ovs_assert(ovs_scan("-123", "%*2o %*o"));
649 ovs_assert(ovs_scan("123", "%x", &i));
650 ovs_assert(i == 0x123);
651 ovs_assert(ovs_scan("0", "%x", &i));
653 ovs_assert(!ovs_scan("123", "%x%x", &i, &i2));
654 ovs_assert(ovs_scan("+123", "%x", &i));
655 ovs_assert(i == 0x123);
656 ovs_assert(ovs_scan("-123", "%x", &i));
657 ovs_assert(i == -0x123);
658 ovs_assert(ovs_scan("0123", "%x", &i));
659 ovs_assert(i == 0x123);
660 ovs_assert(ovs_scan(" 123", "%x", &i));
661 ovs_assert(i == 0x123);
662 ovs_assert(ovs_scan("0x123", "%x", &i));
663 ovs_assert(i == 0x123);
664 ovs_assert(ovs_scan("123", "%2x %x", &i, &i2));
665 ovs_assert(i == 0x12);
667 ovs_assert(ovs_scan("+123", "%2x %x", &i, &i2));
669 ovs_assert(i2 == 0x23);
670 ovs_assert(ovs_scan("-123", "%2x %x", &i, &i2));
672 ovs_assert(i2 == 0x23);
673 ovs_assert(ovs_scan("0123", "%2x %x", &i, &i2));
675 ovs_assert(i2 == 0x23);
676 ovs_assert(ovs_scan("123", "%*2x %x", &i));
678 ovs_assert(ovs_scan("+123", "%2x %*x", &i));
680 ovs_assert(i2 == 0x23);
681 ovs_assert(ovs_scan("-123", "%*2x %*x"));
683 ovs_assert(ovs_scan("123", "%hd", &s));
684 ovs_assert(s == 123);
685 ovs_assert(!ovs_scan("123", "%hd%hd", &s, &s2));
686 ovs_assert(ovs_scan("+123", "%hd", &s));
687 ovs_assert(s == 123);
688 ovs_assert(ovs_scan("-123", "%hd", &s));
689 ovs_assert(s == -123);
690 ovs_assert(ovs_scan("0123", "%hd", &s));
691 ovs_assert(s == 123);
692 ovs_assert(ovs_scan(" 123", "%hd", &s));
693 ovs_assert(s == 123);
694 ovs_assert(ovs_scan("0x123", "%hd", &s));
696 ovs_assert(ovs_scan("123", "%2hd %hd", &s, &s2));
699 ovs_assert(ovs_scan("+123", "%2hd %hd", &s, &s2));
701 ovs_assert(s2 == 23);
702 ovs_assert(ovs_scan("-123", "%2hd %hd", &s, &s2));
704 ovs_assert(s2 == 23);
705 ovs_assert(ovs_scan("0123", "%2hd %hd", &s, &s2));
707 ovs_assert(s2 == 23);
709 ovs_assert(ovs_scan("123", "%hhd", &c));
710 ovs_assert(c == 123);
711 ovs_assert(ovs_scan("0", "%hhd", &c));
713 ovs_assert(!ovs_scan("123", "%hhd%hhd", &c, &c2));
714 ovs_assert(ovs_scan("+123", "%hhd", &c));
715 ovs_assert(c == 123);
716 ovs_assert(ovs_scan("-123", "%hhd", &c));
717 ovs_assert(c == -123);
718 ovs_assert(ovs_scan("0123", "%hhd", &c));
719 ovs_assert(c == 123);
720 ovs_assert(ovs_scan(" 123", "%hhd", &c));
721 ovs_assert(c == 123);
722 ovs_assert(ovs_scan("0x123", "%hhd", &c));
724 ovs_assert(ovs_scan("123", "%2hhd %hhd", &c, &c2));
727 ovs_assert(ovs_scan("+123", "%2hhd %hhd", &c, &c2));
729 ovs_assert(c2 == 23);
730 ovs_assert(ovs_scan("-123", "%2hhd %hhd", &c, &c2));
732 ovs_assert(c2 == 23);
733 ovs_assert(ovs_scan("0123", "%2hhd %hhd", &c, &c2));
735 ovs_assert(c2 == 23);
737 ovs_assert(ovs_scan("123", "%ld", &l));
738 ovs_assert(l == 123);
739 ovs_assert(ovs_scan("0", "%ld", &l));
741 ovs_assert(!ovs_scan("123", "%ld%ld", &l, &l2));
742 ovs_assert(ovs_scan("+123", "%ld", &l));
743 ovs_assert(l == 123);
744 ovs_assert(ovs_scan("-123", "%ld", &l));
745 ovs_assert(l == -123);
746 ovs_assert(ovs_scan("0123", "%ld", &l));
747 ovs_assert(l == 123);
748 ovs_assert(ovs_scan(" 123", "%ld", &l));
749 ovs_assert(l == 123);
750 ovs_assert(ovs_scan("0x123", "%ld", &l));
752 ovs_assert(ovs_scan("123", "%2ld %ld", &l, &l2));
755 ovs_assert(ovs_scan("+123", "%2ld %ld", &l, &l2));
757 ovs_assert(l2 == 23);
758 ovs_assert(ovs_scan("-123", "%2ld %ld", &l, &l2));
760 ovs_assert(l2 == 23);
761 ovs_assert(ovs_scan("0123", "%2ld %ld", &l, &l2));
763 ovs_assert(l2 == 23);
765 ovs_assert(ovs_scan("123", "%lld", &ll));
766 ovs_assert(ll == 123);
767 ovs_assert(ovs_scan("0", "%lld", &ll));
769 ovs_assert(!ovs_scan("123", "%lld%lld", &ll, &ll2));
770 ovs_assert(ovs_scan("+123", "%lld", &ll));
771 ovs_assert(ll == 123);
772 ovs_assert(ovs_scan("-123", "%lld", &ll));
773 ovs_assert(ll == -123);
774 ovs_assert(ovs_scan("0123", "%lld", &ll));
775 ovs_assert(ll == 123);
776 ovs_assert(ovs_scan(" 123", "%lld", &ll));
777 ovs_assert(ll == 123);
778 ovs_assert(ovs_scan("0x123", "%lld", &ll));
780 ovs_assert(ovs_scan("123", "%2lld %lld", &ll, &ll2));
781 ovs_assert(ll == 12);
782 ovs_assert(ll2 == 3);
783 ovs_assert(ovs_scan("+123", "%2lld %lld", &ll, &ll2));
785 ovs_assert(ll2 == 23);
786 ovs_assert(ovs_scan("-123", "%2lld %lld", &ll, &ll2));
787 ovs_assert(ll == -1);
788 ovs_assert(ll2 == 23);
789 ovs_assert(ovs_scan("0123", "%2lld %lld", &ll, &ll2));
791 ovs_assert(ll2 == 23);
793 ovs_assert(ovs_scan("123", "%jd", &im));
794 ovs_assert(im == 123);
795 ovs_assert(ovs_scan("0", "%jd", &im));
797 ovs_assert(!ovs_scan("123", "%jd%jd", &im, &im2));
798 ovs_assert(ovs_scan("+123", "%jd", &im));
799 ovs_assert(im == 123);
800 ovs_assert(ovs_scan("-123", "%jd", &im));
801 ovs_assert(im == -123);
802 ovs_assert(ovs_scan("0123", "%jd", &im));
803 ovs_assert(im == 123);
804 ovs_assert(ovs_scan(" 123", "%jd", &im));
805 ovs_assert(im == 123);
806 ovs_assert(ovs_scan("0x123", "%jd", &im));
808 ovs_assert(ovs_scan("123", "%2jd %jd", &im, &im2));
809 ovs_assert(im == 12);
810 ovs_assert(im2 == 3);
811 ovs_assert(ovs_scan("+123", "%2jd %jd", &im, &im2));
813 ovs_assert(im2 == 23);
814 ovs_assert(ovs_scan("-123", "%2jd %jd", &im, &im2));
815 ovs_assert(im == -1);
816 ovs_assert(im2 == 23);
817 ovs_assert(ovs_scan("0123", "%2jd %jd", &im, &im2));
819 ovs_assert(im2 == 23);
821 ovs_assert(ovs_scan("123", "%td", &pd));
822 ovs_assert(pd == 123);
823 ovs_assert(ovs_scan("0", "%td", &pd));
825 ovs_assert(!ovs_scan("123", "%td%td", &pd, &pd2));
826 ovs_assert(ovs_scan("+123", "%td", &pd));
827 ovs_assert(pd == 123);
828 ovs_assert(ovs_scan("-123", "%td", &pd));
829 ovs_assert(pd == -123);
830 ovs_assert(ovs_scan("0123", "%td", &pd));
831 ovs_assert(pd == 123);
832 ovs_assert(ovs_scan(" 123", "%td", &pd));
833 ovs_assert(pd == 123);
834 ovs_assert(ovs_scan("0x123", "%td", &pd));
836 ovs_assert(ovs_scan("123", "%2td %td", &pd, &pd2));
837 ovs_assert(pd == 12);
838 ovs_assert(pd2 == 3);
839 ovs_assert(ovs_scan("+123", "%2td %td", &pd, &pd2));
841 ovs_assert(pd2 == 23);
842 ovs_assert(ovs_scan("-123", "%2td %td", &pd, &pd2));
843 ovs_assert(pd == -1);
844 ovs_assert(pd2 == 23);
845 ovs_assert(ovs_scan("0123", "%2td %td", &pd, &pd2));
847 ovs_assert(pd2 == 23);
849 ovs_assert(ovs_scan("123", "%zd", &sz));
850 ovs_assert(sz == 123);
851 ovs_assert(ovs_scan("0", "%zd", &sz));
853 ovs_assert(!ovs_scan("123", "%zd%zd", &sz, &sz2));
854 ovs_assert(ovs_scan("+123", "%zd", &sz));
855 ovs_assert(sz == 123);
856 ovs_assert(ovs_scan("-123", "%zd", &sz));
857 ovs_assert(sz == -123);
858 ovs_assert(ovs_scan("0123", "%zd", &sz));
859 ovs_assert(sz == 123);
860 ovs_assert(ovs_scan(" 123", "%zd", &sz));
861 ovs_assert(sz == 123);
862 ovs_assert(ovs_scan("0x123", "%zd", &sz));
864 ovs_assert(ovs_scan("123", "%2zd %zd", &sz, &sz2));
865 ovs_assert(sz == 12);
866 ovs_assert(sz2 == 3);
867 ovs_assert(ovs_scan("+123", "%2zd %zd", &sz, &sz2));
869 ovs_assert(sz2 == 23);
870 ovs_assert(ovs_scan("-123", "%2zd %zd", &sz, &sz2));
871 ovs_assert(sz == -1);
872 ovs_assert(sz2 == 23);
873 ovs_assert(ovs_scan("0123", "%2zd %zd", &sz, &sz2));
875 ovs_assert(sz2 == 23);
877 ovs_assert(ovs_scan("0.25", "%f", &f));
878 ovs_assert(f == 0.25);
879 ovs_assert(ovs_scan("1.0", "%f", &f));
880 ovs_assert(f == 1.0);
881 ovs_assert(ovs_scan("-5", "%f", &f));
882 ovs_assert(f == -5.0);
883 ovs_assert(ovs_scan("+6", "%f", &f));
884 ovs_assert(f == 6.0);
885 ovs_assert(ovs_scan("-1e5", "%f", &f));
886 ovs_assert(f == -1e5);
887 ovs_assert(ovs_scan("-.25", "%f", &f));
888 ovs_assert(f == -.25);
889 ovs_assert(ovs_scan("+123.e1", "%f", &f));
890 ovs_assert(f == 1230.0);
891 ovs_assert(ovs_scan("25e-2", "%f", &f));
892 ovs_assert(f == 0.25);
893 ovs_assert(ovs_scan("0.25", "%1f %f", &f, &f2));
895 ovs_assert(f2 == 0.25);
896 ovs_assert(ovs_scan("1.0", "%2f %f", &f, &f2));
897 ovs_assert(f == 1.0);
898 ovs_assert(f2 == 0.0);
899 ovs_assert(!ovs_scan("-5", "%1f", &f));
900 ovs_assert(!ovs_scan("+6", "%1f", &f));
901 ovs_assert(!ovs_scan("-1e5", "%2f %*f", &f));
903 ovs_assert(!ovs_scan("-.25", "%2f", &f));
904 ovs_assert(!ovs_scan("+123.e1", "%6f", &f));
905 ovs_assert(!ovs_scan("25e-2", "%4f", &f));
907 ovs_assert(ovs_scan("0.25", "%lf", &d));
908 ovs_assert(d == 0.25);
909 ovs_assert(ovs_scan("1.0", "%lf", &d));
910 ovs_assert(d == 1.0);
911 ovs_assert(ovs_scan("-5", "%lf", &d));
912 ovs_assert(d == -5.0);
913 ovs_assert(ovs_scan("+6", "%lf", &d));
914 ovs_assert(d == 6.0);
915 ovs_assert(ovs_scan("-1e5", "%lf", &d));
916 ovs_assert(d == -1e5);
917 ovs_assert(ovs_scan("-.25", "%lf", &d));
918 ovs_assert(d == -.25);
919 ovs_assert(ovs_scan("+123.e1", "%lf", &d));
920 ovs_assert(d == 1230.0);
921 ovs_assert(ovs_scan("25e-2", "%lf", &d));
922 ovs_assert(d == 0.25);
923 ovs_assert(ovs_scan("0.25", "%1lf %lf", &d, &d2));
925 ovs_assert(d2 == 0.25);
926 ovs_assert(ovs_scan("1.0", "%2lf %lf", &d, &d2));
927 ovs_assert(d == 1.0);
928 ovs_assert(d2 == 0.0);
929 ovs_assert(!ovs_scan("-5", "%1lf", &d));
930 ovs_assert(!ovs_scan("+6", "%1lf", &d));
931 ovs_assert(!ovs_scan("-1e5", "%2lf %*f", &d));
933 ovs_assert(!ovs_scan("-.25", "%2lf", &d));
934 ovs_assert(!ovs_scan("+123.e1", "%6lf", &d));
935 ovs_assert(!ovs_scan("25e-2", "%4lf", &d));
937 ovs_assert(ovs_scan("0.25", "%Lf", &ld));
938 ovs_assert(ld == 0.25);
939 ovs_assert(ovs_scan("1.0", "%Lf", &ld));
940 ovs_assert(ld == 1.0);
941 ovs_assert(ovs_scan("-5", "%Lf", &ld));
942 ovs_assert(ld == -5.0);
943 ovs_assert(ovs_scan("+6", "%Lf", &ld));
944 ovs_assert(ld == 6.0);
945 ovs_assert(ovs_scan("-1e5", "%Lf", &ld));
946 ovs_assert(ld == -1e5);
947 ovs_assert(ovs_scan("-.25", "%Lf", &ld));
948 ovs_assert(ld == -.25);
949 ovs_assert(ovs_scan("+123.e1", "%Lf", &ld));
950 ovs_assert(ld == 1230.0);
951 ovs_assert(ovs_scan("25e-2", "%Lf", &ld));
952 ovs_assert(ld == 0.25);
953 ovs_assert(ovs_scan("0.25", "%1Lf %Lf", &ld, &ld2));
955 ovs_assert(ld2 == 0.25);
956 ovs_assert(ovs_scan("1.0", "%2Lf %Lf", &ld, &ld2));
957 ovs_assert(ld == 1.0);
958 ovs_assert(ld2 == 0.0);
959 ovs_assert(!ovs_scan("-5", "%1Lf", &ld));
960 ovs_assert(!ovs_scan("+6", "%1Lf", &ld));
961 ovs_assert(!ovs_scan("-1e5", "%2Lf %*f", &ld));
962 ovs_assert(ld == -1);
963 ovs_assert(!ovs_scan("-.25", "%2Lf", &ld));
964 ovs_assert(!ovs_scan("+123.e1", "%6Lf", &ld));
965 ovs_assert(!ovs_scan("25e-2", "%4Lf", &ld));
967 ovs_assert(ovs_scan(" Hello,\tworld ", "%*s%n%*s%n", &n, &n2));
969 ovs_assert(n2 == 13);
970 ovs_assert(!ovs_scan(" Hello,\tworld ", "%*s%*s%*s"));
971 ovs_assert(ovs_scan(" Hello,\tworld ", "%6s%n%5s%n", str, &n, str2, &n2));
972 ovs_assert(!strcmp(str, "Hello,"));
974 ovs_assert(!strcmp(str2, "world"));
975 ovs_assert(n2 == 13);
976 ovs_assert(ovs_scan(" Hello,\tworld ", "%5s%5s%5s", str, str2, str3));
977 ovs_assert(!strcmp(str, "Hello"));
978 ovs_assert(!strcmp(str2, ","));
979 ovs_assert(!strcmp(str3, "world"));
980 ovs_assert(!ovs_scan(" ", "%*s"));
982 ovs_assert(ovs_scan(" Hello,\tworld ", "%*c%n%*c%n%c%n",
986 ovs_assert(c == 'e');
988 ovs_assert(ovs_scan(" Hello,\tworld ", "%*5c%5c", str));
989 ovs_assert(!memcmp(str, "o,\two", 5));
990 ovs_assert(!ovs_scan(" Hello,\tworld ", "%*15c"));
992 ovs_assert(ovs_scan("0x1234xyzzy", "%9[x0-9a-fA-F]%n", str, &n));
993 ovs_assert(!strcmp(str, "0x1234x"));
995 ovs_assert(ovs_scan("foo:bar=baz", "%5[^:=]%n:%5[^:=]%n=%5[^:=]%n",
996 str, &n, str2, &n2, str3, &n3));
997 ovs_assert(!strcmp(str, "foo"));
999 ovs_assert(!strcmp(str2, "bar"));
1000 ovs_assert(n2 == 7);
1001 ovs_assert(!strcmp(str3, "baz"));
1002 ovs_assert(n3 == 11);
1003 ovs_assert(!ovs_scan(" ", "%*[0-9]"));
1004 ovs_assert(ovs_scan("0x123a]4xyzzy-", "%[]x0-9a-fA-F]", str));
1005 ovs_assert(!strcmp(str, "0x123a]4x"));
1006 ovs_assert(ovs_scan("abc]xyz","%[^]xyz]", str));
1007 ovs_assert(!strcmp(str, "abc"));
1008 ovs_assert(!ovs_scan("0x123a]4xyzzy-", "%[x0-9]a-fA-F]", str));
1009 ovs_assert(ovs_scan("0x12-3]xyz", "%[x0-9a-f-]", str));
1010 ovs_assert(!strcmp(str, "0x12-3"));
1011 ovs_assert(ovs_scan("0x12-3]xyz", "%[^a-f-]", str));
1012 ovs_assert(!strcmp(str, "0x12"));
1013 ovs_assert(sscanf("0x12-3]xyz", "%[^-a-f]", str));
1014 ovs_assert(!strcmp(str, "0x12"));
1018 test_snprintf(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1022 ovs_assert(snprintf(s, 4, "abcde") == 5);
1023 ovs_assert(!strcmp(s, "abc"));
1025 ovs_assert(snprintf(s, 5, "abcde") == 5);
1026 ovs_assert(!strcmp(s, "abcd"));
1028 ovs_assert(snprintf(s, 6, "abcde") == 5);
1029 ovs_assert(!strcmp(s, "abcde"));
1031 ovs_assert(snprintf(NULL, 0, "abcde") == 5);
1034 static const struct command commands[] = {
1035 {"ctz", 0, 0, test_ctz},
1036 {"clz", 0, 0, test_clz},
1037 {"round_up_pow2", 0, 0, test_round_up_pow2},
1038 {"round_down_pow2", 0, 0, test_round_down_pow2},
1039 {"count_1bits", 0, 0, test_count_1bits},
1040 {"log_2_floor", 0, 0, test_log_2_floor},
1041 {"bitwise_copy", 0, 0, test_bitwise_copy},
1042 {"bitwise_zero", 0, 0, test_bitwise_zero},
1043 {"bitwise_one", 0, 0, test_bitwise_one},
1044 {"bitwise_is_all_zeros", 0, 0, test_bitwise_is_all_zeros},
1045 {"follow-symlinks", 1, INT_MAX, test_follow_symlinks},
1046 {"assert", 0, 0, test_assert},
1047 {"ovs_scan", 0, 0, test_ovs_scan},
1048 {"snprintf", 0, 0, test_snprintf},
1053 parse_options(int argc, char *argv[])
1058 static const struct option long_options[] = {
1062 char *short_options = long_options_to_short_options(long_options);
1065 int c = getopt_long(argc, argv, short_options, long_options, NULL);
1071 VLOG_OPTION_HANDLERS
1080 free(short_options);
1084 main(int argc, char *argv[])
1086 set_program_name(argv[0]);
1087 parse_options(argc, argv);
1088 run_command(argc - optind, argv + optind, commands);