2 * Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016 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.
26 #include "byte-order.h"
27 #include "command-line.h"
30 #include "openvswitch/vlog.h"
33 check_log_2_floor(uint32_t x, int n)
35 if (log_2_floor(x) != n) {
36 fprintf(stderr, "log_2_floor(%"PRIu32") is %d but should be %d\n",
37 x, log_2_floor(x), n);
43 test_log_2_floor(struct ovs_cmdl_context *ctx OVS_UNUSED)
47 for (n = 0; n < 32; n++) {
48 /* Check minimum x such that f(x) == n. */
49 check_log_2_floor(1 << n, n);
51 /* Check maximum x such that f(x) == n. */
52 check_log_2_floor((1 << n) | ((1 << n) - 1), n);
54 /* Check a random value in the middle. */
55 check_log_2_floor((random_uint32() & ((1 << n) - 1)) | (1 << n), n);
58 /* log_2_floor(0) is undefined, so don't check it. */
62 check_ctz32(uint32_t x, int n)
65 fprintf(stderr, "ctz32(%"PRIu32") is %d but should be %d\n",
72 check_ctz64(uint64_t x, int n)
75 fprintf(stderr, "ctz64(%"PRIu64") is %d but should be %d\n",
82 test_ctz(struct ovs_cmdl_context *ctx OVS_UNUSED)
86 for (n = 0; n < 32; n++) {
87 /* Check minimum x such that f(x) == n. */
88 check_ctz32(1 << n, n);
90 /* Check maximum x such that f(x) == n. */
91 check_ctz32(UINT32_MAX << n, n);
93 /* Check a random value in the middle. */
94 check_ctz32((random_uint32() | 1) << n, n);
98 for (n = 0; n < 64; n++) {
99 /* Check minimum x such that f(x) == n. */
100 check_ctz64(UINT64_C(1) << n, n);
102 /* Check maximum x such that f(x) == n. */
103 check_ctz64(UINT64_MAX << n, n);
105 /* Check a random value in the middle. */
106 check_ctz64((random_uint64() | UINT64_C(1)) << n, n);
115 check_clz32(uint32_t x, int n)
118 fprintf(stderr, "clz32(%"PRIu32") is %d but should be %d\n",
125 check_clz64(uint64_t x, int n)
128 fprintf(stderr, "clz64(%"PRIu64") is %d but should be %d\n",
135 test_clz(struct ovs_cmdl_context *ctx OVS_UNUSED)
139 for (n = 0; n < 32; n++) {
140 /* Check minimum x such that f(x) == n. */
141 check_clz32((1u << 31) >> n, n);
143 /* Check maximum x such that f(x) == n. */
144 check_clz32(UINT32_MAX >> n, n);
146 /* Check a random value in the middle. */
147 check_clz32((random_uint32() | 1u << 31) >> n, n);
150 for (n = 0; n < 64; n++) {
151 /* Check minimum x such that f(x) == n. */
152 check_clz64((UINT64_C(1) << 63) >> n, n);
154 /* Check maximum x such that f(x) == n. */
155 check_clz64(UINT64_MAX >> n, n);
157 /* Check a random value in the middle. */
158 check_clz64((random_uint64() | UINT64_C(1) << 63) >> n, n);
166 /* Returns a random number in the range 'min'...'max' inclusive. */
168 random_in_range(uint32_t min, uint32_t max)
170 return min == max ? min : min + random_range(max - min + 1);
174 check_rup2(uint32_t x, int n)
176 uint32_t rup2 = ROUND_UP_POW2(x);
178 fprintf(stderr, "ROUND_UP_POW2(%#"PRIx32") is %#"PRIx32" "
179 "but should be %#"PRIx32"\n", x, rup2, n);
185 test_round_up_pow2(struct ovs_cmdl_context *ctx OVS_UNUSED)
189 for (n = 0; n < 32; n++) {
190 /* Min, max value for which ROUND_UP_POW2 should yield (1 << n). */
191 uint32_t min = ((1u << n) >> 1) + 1;
192 uint32_t max = 1u << n;
194 check_rup2(min, 1u << n);
195 check_rup2(max, 1u << n);
196 check_rup2(random_in_range(min, max), 1u << n);
202 check_rdp2(uint32_t x, int n)
204 uint32_t rdp2 = ROUND_DOWN_POW2(x);
206 fprintf(stderr, "ROUND_DOWN_POW2(%#"PRIx32") is %#"PRIx32" "
207 "but should be %#"PRIx32"\n", x, rdp2, n);
213 test_round_down_pow2(struct ovs_cmdl_context *ctx OVS_UNUSED)
217 for (n = 0; n < 32; n++) {
218 /* Min, max value for which ROUND_DOWN_POW2 should yield (1 << n). */
219 uint32_t min = 1u << n;
220 uint32_t max = ((1u << n) << 1) - 1;
222 check_rdp2(min, 1u << n);
223 check_rdp2(max, 1u << n);
224 check_rdp2(random_in_range(min, max), 1u << n);
230 shuffle(uint64_t *p, size_t n)
232 for (; n > 1; n--, p++) {
233 uint64_t *q = &p[random_range(n)];
241 check_count_1bits(uint64_t x, int n)
243 if (count_1bits(x) != n) {
244 fprintf(stderr, "count_1bits(%#"PRIx64") is %d but should be %d\n",
245 x, count_1bits(x), n);
251 test_count_1bits(struct ovs_cmdl_context *ctx OVS_UNUSED)
256 for (i = 0; i < ARRAY_SIZE(bits); i++) {
257 bits[i] = UINT64_C(1) << i;
260 check_count_1bits(0, 0);
262 for (i = 0; i < 1000; i++) {
266 shuffle(bits, ARRAY_SIZE(bits));
267 for (j = 0; j < 64; j++) {
269 check_count_1bits(x, j + 1);
271 assert(x == UINT64_MAX);
273 shuffle(bits, ARRAY_SIZE(bits));
274 for (j = 63; j >= 0; j--) {
276 check_count_1bits(x, j);
282 /* Returns the sum of the squares of the first 'n' positive integers. */
284 sum_of_squares(int n)
286 return n * (n + 1) * (2 * n + 1) / 6;
290 test_bitwise_copy(struct ovs_cmdl_context *ctx OVS_UNUSED)
292 unsigned int n_loops;
298 for (n_bits = 0; n_bits <= 64; n_bits++) {
299 for (src_ofs = 0; src_ofs < 64 - n_bits; src_ofs++) {
300 for (dst_ofs = 0; dst_ofs < 64 - n_bits; dst_ofs++) {
301 ovs_be64 src = htonll(random_uint64());
302 ovs_be64 dst = htonll(random_uint64());
303 ovs_be64 orig_dst = dst;
309 uint64_t mask = (UINT64_C(1) << n_bits) - 1;
310 expect = orig_dst & ~htonll(mask << dst_ofs);
311 expect |= htonll(((ntohll(src) >> src_ofs) & mask)
315 bitwise_copy(&src, sizeof src, src_ofs,
316 &dst, sizeof dst, dst_ofs,
319 fprintf(stderr,"copy_bits(0x%016"PRIx64",8,%d, "
320 "0x%016"PRIx64",8,%d, %d) yielded 0x%016"PRIx64" "
321 "instead of the expected 0x%016"PRIx64"\n",
322 ntohll(src), src_ofs,
323 ntohll(orig_dst), dst_ofs,
325 ntohll(dst), ntohll(expect));
334 if (n_loops != sum_of_squares(64)) {
340 test_bitwise_zero(struct ovs_cmdl_context *ctx OVS_UNUSED)
342 unsigned int n_loops;
347 for (n_bits = 0; n_bits <= 64; n_bits++) {
348 for (dst_ofs = 0; dst_ofs < 64 - n_bits; dst_ofs++) {
349 ovs_be64 dst = htonll(random_uint64());
350 ovs_be64 orig_dst = dst;
356 uint64_t mask = (UINT64_C(1) << n_bits) - 1;
357 expect = orig_dst & ~htonll(mask << dst_ofs);
360 bitwise_zero(&dst, sizeof dst, dst_ofs, n_bits);
362 fprintf(stderr,"bitwise_zero(0x%016"PRIx64",8,%d, %d) "
363 "yielded 0x%016"PRIx64" "
364 "instead of the expected 0x%016"PRIx64"\n",
365 ntohll(orig_dst), dst_ofs,
367 ntohll(dst), ntohll(expect));
375 if (n_loops != 64 * (64 + 1) / 2) {
381 test_bitwise_one(struct ovs_cmdl_context *ctx OVS_UNUSED)
383 unsigned int n_loops;
388 for (n_bits = 0; n_bits <= 64; n_bits++) {
389 for (dst_ofs = 0; dst_ofs < 64 - n_bits; dst_ofs++) {
390 ovs_be64 dst = htonll(random_uint64());
391 ovs_be64 orig_dst = dst;
395 expect = OVS_BE64_MAX;
397 uint64_t mask = (UINT64_C(1) << n_bits) - 1;
398 expect = orig_dst | htonll(mask << dst_ofs);
401 bitwise_one(&dst, sizeof dst, dst_ofs, n_bits);
403 fprintf(stderr,"bitwise_one(0x%016"PRIx64",8,%d, %d) "
404 "yielded 0x%016"PRIx64" "
405 "instead of the expected 0x%016"PRIx64"\n",
406 ntohll(orig_dst), dst_ofs,
408 ntohll(dst), ntohll(expect));
416 if (n_loops != 64 * (64 + 1) / 2) {
422 test_bitwise_is_all_zeros(struct ovs_cmdl_context *ctx OVS_UNUSED)
426 for (n_loops = 0; n_loops < 100; n_loops++) {
427 ovs_be64 x = htonll(0);
430 for (i = 0; i < 64; i++) {
434 /* Change a random 0-bit into a 1-bit. */
436 bit = htonll(UINT64_C(1) << (random_range(64)));
440 for (ofs = 0; ofs < 64; ofs++) {
441 for (n = 0; n <= 64 - ofs; n++) {
447 : !(x & htonll(((UINT64_C(1) << n) - 1)
449 answer = bitwise_is_all_zeros(&x, sizeof x, ofs, n);
450 if (expect != answer) {
452 "bitwise_is_all_zeros(0x%016"PRIx64",8,%d,%d "
453 "returned %s instead of %s\n",
455 answer ? "true" : "false",
456 expect ? "true" : "false");
466 trivial_bitwise_rscan(const void *p, unsigned int len, bool target,
471 for (ofs = start; ofs > end; ofs--) {
472 if (bitwise_get_bit(p, len, ofs) == target) {
480 test_bitwise_rscan(struct ovs_cmdl_context *ctx OVS_UNUSED)
483 uint8_t s1[3] = {0xff, 0xff, 0xff};
484 /* Target is the first bit */
485 ovs_assert(23 == bitwise_rscan(s1, 3, 1, 23, -1));
486 /* Target is not found, return -1 */
487 ovs_assert(-1 == bitwise_rscan(s1, 3, 0, 23, -1));
488 /* Target is not found and end != -1, return end */
489 ovs_assert(20 == bitwise_rscan(s1, 3, 0, 23, 20));
491 /* bit 20 - 23 are 0s */
492 uint8_t s2[3] = {0x0f, 0xff, 0xff};
493 /* Target is in the first byte but not the first bit */
494 ovs_assert(19 == bitwise_rscan(s2, 3, 1, 23, -1));
495 /* Target exists before the start postion */
496 ovs_assert(18 == bitwise_rscan(s2, 3, 1, 18, -1));
497 /* Target exists after the end postion, return end */
498 ovs_assert(20 == bitwise_rscan(s2, 3, 1, 23, 20));
499 /* Target is at the end postion, return end */
500 ovs_assert(19 == bitwise_rscan(s2, 3, 1, 23, 19));
501 /* start == end, target at start */
502 ovs_assert(19 == bitwise_rscan(s2, 3, 1, 19, 19));
503 /* start == end, target not at start */
504 ovs_assert(20 == bitwise_rscan(s2, 3, 1, 20, 20));
505 /* Target is 0 ... */
506 ovs_assert(22 == bitwise_rscan(s2, 3, 0, 22, -1));
508 /* bit 4 - 23 are 0s */
509 uint8_t s3[3] = {0x00, 0x00, 0x0f};
510 /* Target is in the end byte */
511 ovs_assert(3 == bitwise_rscan(s3, 3, 1, 16, -1));
512 /* Target exists after the end byte, return end */
513 ovs_assert(15 == bitwise_rscan(s3, 3, 1, 23, 15));
514 /* Target exists in end byte but after the end bit, return end */
515 ovs_assert(4 == bitwise_rscan(s3, 3, 1, 23, 4));
516 /* Target is 0 ... */
517 ovs_assert(12 == bitwise_rscan(s3, 3, 0, 12, -1));
520 uint8_t s4[3] = {0x00, 0x00, 0x00};
521 /* Target not found */
522 ovs_assert(-1 == bitwise_rscan(s4, 3, 1, 23, -1));
523 /* Target is 0 ..., start is 0 */
524 ovs_assert(0 == bitwise_rscan(s4, 3, 0, 0, -1));
527 for (n_loops = 0; n_loops < 100; n_loops++) {
528 ovs_be64 x = htonll(0);
531 for (i = 0; i < 64; i++) {
534 /* Change a random 0-bit into a 1-bit. */
536 bit = htonll(UINT64_C(1) << (random_range(64)));
540 for (int end = -1; end <= 63; end++) {
541 for (int start = end; start <= 63; start++) {
542 for (int target = 0; target < 2; target++) {
543 bool expect = trivial_bitwise_rscan(
544 &x, sizeof x, target, start, end);
545 bool answer = bitwise_rscan(
546 &x, sizeof x, target, start, end);
547 if (expect != answer) {
549 "bitwise_rscan(0x%016"PRIx64",8,%s,%d,%d) "
550 "returned %s instead of %s\n",
552 target ? "true" : "false",
554 answer ? "true" : "false",
555 expect ? "true" : "false");
566 test_follow_symlinks(struct ovs_cmdl_context *ctx)
570 for (i = 1; i < ctx->argc; i++) {
571 char *target = follow_symlinks(ctx->argv[i]);
578 test_assert(struct ovs_cmdl_context *ctx OVS_UNUSED)
584 test_ovs_scan(struct ovs_cmdl_context *ctx OVS_UNUSED)
586 char str[16], str2[16], str3[16];
600 ovs_assert(ovs_scan("", " "));
601 ovs_assert(ovs_scan(" ", " "));
602 ovs_assert(ovs_scan(" ", " "));
603 ovs_assert(ovs_scan(" \t ", " "));
605 ovs_assert(ovs_scan("xyzzy", "xyzzy"));
606 ovs_assert(ovs_scan("xy%zzy", "xy%%zzy"));
607 ovs_assert(!ovs_scan(" xy%zzy", "xy%%zzy"));
608 ovs_assert(ovs_scan(" xy%\tzzy", " xy%% zzy"));
610 ovs_assert(ovs_scan("123", "%d", &i));
611 ovs_assert(i == 123);
612 ovs_assert(ovs_scan("0", "%d", &i));
614 ovs_assert(!ovs_scan("123", "%d%d", &i, &i2));
615 ovs_assert(ovs_scan("+123", "%d", &i));
616 ovs_assert(i == 123);
617 ovs_assert(ovs_scan("-123", "%d", &i));
618 ovs_assert(i == -123);
619 ovs_assert(ovs_scan("0123", "%d", &i));
620 ovs_assert(i == 123);
621 ovs_assert(ovs_scan(" 123", "%d", &i));
622 ovs_assert(i == 123);
623 ovs_assert(ovs_scan("0x123", "%d", &i));
625 ovs_assert(ovs_scan("123", "%2d %d", &i, &i2));
628 ovs_assert(ovs_scan("+123", "%2d %d", &i, &i2));
630 ovs_assert(i2 == 23);
631 ovs_assert(ovs_scan("-123", "%2d %d", &i, &i2));
633 ovs_assert(i2 == 23);
634 ovs_assert(ovs_scan("0123", "%2d %d", &i, &i2));
636 ovs_assert(i2 == 23);
637 ovs_assert(ovs_scan("123", "%*2d %d", &i));
639 ovs_assert(ovs_scan("+123", "%2d %*d", &i));
641 ovs_assert(i2 == 23);
642 ovs_assert(ovs_scan("-123", "%*2d %*d"));
644 ovs_assert(ovs_scan("123", "%u", &i));
645 ovs_assert(i == 123);
646 ovs_assert(ovs_scan("0", "%u", &i));
648 ovs_assert(!ovs_scan("123", "%u%u", &i, &i2));
649 ovs_assert(ovs_scan("+123", "%u", &i));
650 ovs_assert(i == 123);
651 ovs_assert(ovs_scan("-123", "%u", &i));
652 ovs_assert(i == -123);
653 ovs_assert(ovs_scan("0123", "%u", &i));
654 ovs_assert(i == 123);
655 ovs_assert(ovs_scan(" 123", "%u", &i));
656 ovs_assert(i == 123);
657 ovs_assert(ovs_scan("0x123", "%u", &i));
659 ovs_assert(ovs_scan("123", "%2u %u", &i, &i2));
662 ovs_assert(ovs_scan("+123", "%2u %u", &i, &i2));
664 ovs_assert(i2 == 23);
665 ovs_assert(ovs_scan("-123", "%2u %u", &i, &i2));
667 ovs_assert(i2 == 23);
668 ovs_assert(ovs_scan("0123", "%2u %u", &i, &i2));
670 ovs_assert(i2 == 23);
671 ovs_assert(ovs_scan("123", "%*2u %u", &i));
673 ovs_assert(ovs_scan("+123", "%2u %*u", &i));
675 ovs_assert(i2 == 23);
676 ovs_assert(ovs_scan("-123", "%*2u %*u"));
678 ovs_assert(ovs_scan("123", "%i", &i));
679 ovs_assert(i == 123);
680 ovs_assert(ovs_scan("0", "%i", &i));
682 ovs_assert(!ovs_scan("123", "%i%i", &i, &i2));
683 ovs_assert(ovs_scan("+123", "%i", &i));
684 ovs_assert(i == 123);
685 ovs_assert(ovs_scan("-123", "%i", &i));
686 ovs_assert(i == -123);
687 ovs_assert(ovs_scan("0123", "%i", &i));
688 ovs_assert(i == 0123);
689 ovs_assert(ovs_scan(" 123", "%i", &i));
690 ovs_assert(i == 123);
691 ovs_assert(ovs_scan("0x123", "%i", &i));
692 ovs_assert(i == 0x123);
693 ovs_assert(ovs_scan("123", "%2i %i", &i, &i2));
696 ovs_assert(ovs_scan("+123", "%2i %i", &i, &i2));
698 ovs_assert(i2 == 23);
699 ovs_assert(ovs_scan("-123", "%2i %i", &i, &i2));
701 ovs_assert(i2 == 23);
702 ovs_assert(ovs_scan("0123", "%2i %i", &i, &i2));
704 ovs_assert(i2 == 23);
705 ovs_assert(ovs_scan("123", "%*2i %i", &i));
707 ovs_assert(ovs_scan("+123", "%2i %*i", &i));
709 ovs_assert(i2 == 23);
710 ovs_assert(ovs_scan("-123", "%*2i %*i"));
712 ovs_assert(ovs_scan("123", "%o", &i));
713 ovs_assert(i == 0123);
714 ovs_assert(ovs_scan("0", "%o", &i));
716 ovs_assert(!ovs_scan("123", "%o%o", &i, &i2));
717 ovs_assert(ovs_scan("+123", "%o", &i));
718 ovs_assert(i == 0123);
719 ovs_assert(ovs_scan("-123", "%o", &i));
720 ovs_assert(i == -0123);
721 ovs_assert(ovs_scan("0123", "%o", &i));
722 ovs_assert(i == 0123);
723 ovs_assert(ovs_scan(" 123", "%o", &i));
724 ovs_assert(i == 0123);
725 ovs_assert(ovs_scan("0x123", "%o", &i));
727 ovs_assert(ovs_scan("123", "%2o %o", &i, &i2));
728 ovs_assert(i == 012);
730 ovs_assert(ovs_scan("+123", "%2o %o", &i, &i2));
732 ovs_assert(i2 == 023);
733 ovs_assert(ovs_scan("-123", "%2o %o", &i, &i2));
735 ovs_assert(i2 == 023);
736 ovs_assert(ovs_scan("0123", "%2o %o", &i, &i2));
738 ovs_assert(i2 == 023);
739 ovs_assert(ovs_scan("123", "%*2o %o", &i));
741 ovs_assert(ovs_scan("+123", "%2o %*o", &i));
743 ovs_assert(i2 == 023);
744 ovs_assert(ovs_scan("-123", "%*2o %*o"));
746 ovs_assert(ovs_scan("123", "%x", &i));
747 ovs_assert(i == 0x123);
748 ovs_assert(ovs_scan("0", "%x", &i));
750 ovs_assert(!ovs_scan("123", "%x%x", &i, &i2));
751 ovs_assert(ovs_scan("+123", "%x", &i));
752 ovs_assert(i == 0x123);
753 ovs_assert(ovs_scan("-123", "%x", &i));
754 ovs_assert(i == -0x123);
755 ovs_assert(ovs_scan("0123", "%x", &i));
756 ovs_assert(i == 0x123);
757 ovs_assert(ovs_scan(" 123", "%x", &i));
758 ovs_assert(i == 0x123);
759 ovs_assert(ovs_scan("0x123", "%x", &i));
760 ovs_assert(i == 0x123);
761 ovs_assert(ovs_scan("123", "%2x %x", &i, &i2));
762 ovs_assert(i == 0x12);
764 ovs_assert(ovs_scan("+123", "%2x %x", &i, &i2));
766 ovs_assert(i2 == 0x23);
767 ovs_assert(ovs_scan("-123", "%2x %x", &i, &i2));
769 ovs_assert(i2 == 0x23);
770 ovs_assert(ovs_scan("0123", "%2x %x", &i, &i2));
772 ovs_assert(i2 == 0x23);
773 ovs_assert(ovs_scan("123", "%*2x %x", &i));
775 ovs_assert(ovs_scan("+123", "%2x %*x", &i));
777 ovs_assert(i2 == 0x23);
778 ovs_assert(ovs_scan("-123", "%*2x %*x"));
780 ovs_assert(ovs_scan("123", "%hd", &s));
781 ovs_assert(s == 123);
782 ovs_assert(!ovs_scan("123", "%hd%hd", &s, &s2));
783 ovs_assert(ovs_scan("+123", "%hd", &s));
784 ovs_assert(s == 123);
785 ovs_assert(ovs_scan("-123", "%hd", &s));
786 ovs_assert(s == -123);
787 ovs_assert(ovs_scan("0123", "%hd", &s));
788 ovs_assert(s == 123);
789 ovs_assert(ovs_scan(" 123", "%hd", &s));
790 ovs_assert(s == 123);
791 ovs_assert(ovs_scan("0x123", "%hd", &s));
793 ovs_assert(ovs_scan("123", "%2hd %hd", &s, &s2));
796 ovs_assert(ovs_scan("+123", "%2hd %hd", &s, &s2));
798 ovs_assert(s2 == 23);
799 ovs_assert(ovs_scan("-123", "%2hd %hd", &s, &s2));
801 ovs_assert(s2 == 23);
802 ovs_assert(ovs_scan("0123", "%2hd %hd", &s, &s2));
804 ovs_assert(s2 == 23);
806 ovs_assert(ovs_scan("123", "%hhd", &c));
807 ovs_assert(c == 123);
808 ovs_assert(ovs_scan("0", "%hhd", &c));
810 ovs_assert(!ovs_scan("123", "%hhd%hhd", &c, &c2));
811 ovs_assert(ovs_scan("+123", "%hhd", &c));
812 ovs_assert(c == 123);
813 ovs_assert(ovs_scan("-123", "%hhd", &c));
814 ovs_assert(c == -123);
815 ovs_assert(ovs_scan("0123", "%hhd", &c));
816 ovs_assert(c == 123);
817 ovs_assert(ovs_scan(" 123", "%hhd", &c));
818 ovs_assert(c == 123);
819 ovs_assert(ovs_scan("0x123", "%hhd", &c));
821 ovs_assert(ovs_scan("123", "%2hhd %hhd", &c, &c2));
824 ovs_assert(ovs_scan("+123", "%2hhd %hhd", &c, &c2));
826 ovs_assert(c2 == 23);
827 ovs_assert(ovs_scan("-123", "%2hhd %hhd", &c, &c2));
829 ovs_assert(c2 == 23);
830 ovs_assert(ovs_scan("0123", "%2hhd %hhd", &c, &c2));
832 ovs_assert(c2 == 23);
834 ovs_assert(ovs_scan("123", "%ld", &l));
835 ovs_assert(l == 123);
836 ovs_assert(ovs_scan("0", "%ld", &l));
838 ovs_assert(!ovs_scan("123", "%ld%ld", &l, &l2));
839 ovs_assert(ovs_scan("+123", "%ld", &l));
840 ovs_assert(l == 123);
841 ovs_assert(ovs_scan("-123", "%ld", &l));
842 ovs_assert(l == -123);
843 ovs_assert(ovs_scan("0123", "%ld", &l));
844 ovs_assert(l == 123);
845 ovs_assert(ovs_scan(" 123", "%ld", &l));
846 ovs_assert(l == 123);
847 ovs_assert(ovs_scan("0x123", "%ld", &l));
849 ovs_assert(ovs_scan("123", "%2ld %ld", &l, &l2));
852 ovs_assert(ovs_scan("+123", "%2ld %ld", &l, &l2));
854 ovs_assert(l2 == 23);
855 ovs_assert(ovs_scan("-123", "%2ld %ld", &l, &l2));
857 ovs_assert(l2 == 23);
858 ovs_assert(ovs_scan("0123", "%2ld %ld", &l, &l2));
860 ovs_assert(l2 == 23);
862 ovs_assert(ovs_scan("123", "%lld", &ll));
863 ovs_assert(ll == 123);
864 ovs_assert(ovs_scan("0", "%lld", &ll));
866 ovs_assert(!ovs_scan("123", "%lld%lld", &ll, &ll2));
867 ovs_assert(ovs_scan("+123", "%lld", &ll));
868 ovs_assert(ll == 123);
869 ovs_assert(ovs_scan("-123", "%lld", &ll));
870 ovs_assert(ll == -123);
871 ovs_assert(ovs_scan("0123", "%lld", &ll));
872 ovs_assert(ll == 123);
873 ovs_assert(ovs_scan(" 123", "%lld", &ll));
874 ovs_assert(ll == 123);
875 ovs_assert(ovs_scan("0x123", "%lld", &ll));
877 ovs_assert(ovs_scan("123", "%2lld %lld", &ll, &ll2));
878 ovs_assert(ll == 12);
879 ovs_assert(ll2 == 3);
880 ovs_assert(ovs_scan("+123", "%2lld %lld", &ll, &ll2));
882 ovs_assert(ll2 == 23);
883 ovs_assert(ovs_scan("-123", "%2lld %lld", &ll, &ll2));
884 ovs_assert(ll == -1);
885 ovs_assert(ll2 == 23);
886 ovs_assert(ovs_scan("0123", "%2lld %lld", &ll, &ll2));
888 ovs_assert(ll2 == 23);
890 ovs_assert(ovs_scan("123", "%jd", &im));
891 ovs_assert(im == 123);
892 ovs_assert(ovs_scan("0", "%jd", &im));
894 ovs_assert(!ovs_scan("123", "%jd%jd", &im, &im2));
895 ovs_assert(ovs_scan("+123", "%jd", &im));
896 ovs_assert(im == 123);
897 ovs_assert(ovs_scan("-123", "%jd", &im));
898 ovs_assert(im == -123);
899 ovs_assert(ovs_scan("0123", "%jd", &im));
900 ovs_assert(im == 123);
901 ovs_assert(ovs_scan(" 123", "%jd", &im));
902 ovs_assert(im == 123);
903 ovs_assert(ovs_scan("0x123", "%jd", &im));
905 ovs_assert(ovs_scan("123", "%2jd %jd", &im, &im2));
906 ovs_assert(im == 12);
907 ovs_assert(im2 == 3);
908 ovs_assert(ovs_scan("+123", "%2jd %jd", &im, &im2));
910 ovs_assert(im2 == 23);
911 ovs_assert(ovs_scan("-123", "%2jd %jd", &im, &im2));
912 ovs_assert(im == -1);
913 ovs_assert(im2 == 23);
914 ovs_assert(ovs_scan("0123", "%2jd %jd", &im, &im2));
916 ovs_assert(im2 == 23);
918 ovs_assert(ovs_scan("123", "%td", &pd));
919 ovs_assert(pd == 123);
920 ovs_assert(ovs_scan("0", "%td", &pd));
922 ovs_assert(!ovs_scan("123", "%td%td", &pd, &pd2));
923 ovs_assert(ovs_scan("+123", "%td", &pd));
924 ovs_assert(pd == 123);
925 ovs_assert(ovs_scan("-123", "%td", &pd));
926 ovs_assert(pd == -123);
927 ovs_assert(ovs_scan("0123", "%td", &pd));
928 ovs_assert(pd == 123);
929 ovs_assert(ovs_scan(" 123", "%td", &pd));
930 ovs_assert(pd == 123);
931 ovs_assert(ovs_scan("0x123", "%td", &pd));
933 ovs_assert(ovs_scan("123", "%2td %td", &pd, &pd2));
934 ovs_assert(pd == 12);
935 ovs_assert(pd2 == 3);
936 ovs_assert(ovs_scan("+123", "%2td %td", &pd, &pd2));
938 ovs_assert(pd2 == 23);
939 ovs_assert(ovs_scan("-123", "%2td %td", &pd, &pd2));
940 ovs_assert(pd == -1);
941 ovs_assert(pd2 == 23);
942 ovs_assert(ovs_scan("0123", "%2td %td", &pd, &pd2));
944 ovs_assert(pd2 == 23);
946 ovs_assert(ovs_scan("123", "%zd", &sz));
947 ovs_assert(sz == 123);
948 ovs_assert(ovs_scan("0", "%zd", &sz));
950 ovs_assert(!ovs_scan("123", "%zd%zd", &sz, &sz2));
951 ovs_assert(ovs_scan("+123", "%zd", &sz));
952 ovs_assert(sz == 123);
953 ovs_assert(ovs_scan("-123", "%zd", &sz));
954 ovs_assert(sz == -123);
955 ovs_assert(ovs_scan("0123", "%zd", &sz));
956 ovs_assert(sz == 123);
957 ovs_assert(ovs_scan(" 123", "%zd", &sz));
958 ovs_assert(sz == 123);
959 ovs_assert(ovs_scan("0x123", "%zd", &sz));
961 ovs_assert(ovs_scan("123", "%2zd %zd", &sz, &sz2));
962 ovs_assert(sz == 12);
963 ovs_assert(sz2 == 3);
964 ovs_assert(ovs_scan("+123", "%2zd %zd", &sz, &sz2));
966 ovs_assert(sz2 == 23);
967 ovs_assert(ovs_scan("-123", "%2zd %zd", &sz, &sz2));
968 ovs_assert(sz == -1);
969 ovs_assert(sz2 == 23);
970 ovs_assert(ovs_scan("0123", "%2zd %zd", &sz, &sz2));
972 ovs_assert(sz2 == 23);
974 ovs_assert(ovs_scan("0.25", "%f", &f));
975 ovs_assert(f == 0.25);
976 ovs_assert(ovs_scan("1.0", "%f", &f));
977 ovs_assert(f == 1.0);
978 ovs_assert(ovs_scan("-5", "%f", &f));
979 ovs_assert(f == -5.0);
980 ovs_assert(ovs_scan("+6", "%f", &f));
981 ovs_assert(f == 6.0);
982 ovs_assert(ovs_scan("-1e5", "%f", &f));
983 ovs_assert(f == -1e5);
984 ovs_assert(ovs_scan("-.25", "%f", &f));
985 ovs_assert(f == -.25);
986 ovs_assert(ovs_scan("+123.e1", "%f", &f));
987 ovs_assert(f == 1230.0);
988 ovs_assert(ovs_scan("25e-2", "%f", &f));
989 ovs_assert(f == 0.25);
990 ovs_assert(ovs_scan("0.25", "%1f %f", &f, &f2));
992 ovs_assert(f2 == 0.25);
993 ovs_assert(ovs_scan("1.0", "%2f %f", &f, &f2));
994 ovs_assert(f == 1.0);
995 ovs_assert(f2 == 0.0);
996 ovs_assert(!ovs_scan("-5", "%1f", &f));
997 ovs_assert(!ovs_scan("+6", "%1f", &f));
998 ovs_assert(!ovs_scan("-1e5", "%2f %*f", &f));
1000 ovs_assert(!ovs_scan("-.25", "%2f", &f));
1001 ovs_assert(!ovs_scan("+123.e1", "%6f", &f));
1002 ovs_assert(!ovs_scan("25e-2", "%4f", &f));
1004 ovs_assert(ovs_scan("0.25", "%lf", &d));
1005 ovs_assert(d == 0.25);
1006 ovs_assert(ovs_scan("1.0", "%lf", &d));
1007 ovs_assert(d == 1.0);
1008 ovs_assert(ovs_scan("-5", "%lf", &d));
1009 ovs_assert(d == -5.0);
1010 ovs_assert(ovs_scan("+6", "%lf", &d));
1011 ovs_assert(d == 6.0);
1012 ovs_assert(ovs_scan("-1e5", "%lf", &d));
1013 ovs_assert(d == -1e5);
1014 ovs_assert(ovs_scan("-.25", "%lf", &d));
1015 ovs_assert(d == -.25);
1016 ovs_assert(ovs_scan("+123.e1", "%lf", &d));
1017 ovs_assert(d == 1230.0);
1018 ovs_assert(ovs_scan("25e-2", "%lf", &d));
1019 ovs_assert(d == 0.25);
1020 ovs_assert(ovs_scan("0.25", "%1lf %lf", &d, &d2));
1022 ovs_assert(d2 == 0.25);
1023 ovs_assert(ovs_scan("1.0", "%2lf %lf", &d, &d2));
1024 ovs_assert(d == 1.0);
1025 ovs_assert(d2 == 0.0);
1026 ovs_assert(!ovs_scan("-5", "%1lf", &d));
1027 ovs_assert(!ovs_scan("+6", "%1lf", &d));
1028 ovs_assert(!ovs_scan("-1e5", "%2lf %*f", &d));
1029 ovs_assert(d == -1);
1030 ovs_assert(!ovs_scan("-.25", "%2lf", &d));
1031 ovs_assert(!ovs_scan("+123.e1", "%6lf", &d));
1032 ovs_assert(!ovs_scan("25e-2", "%4lf", &d));
1034 ovs_assert(ovs_scan("0.25", "%Lf", &ld));
1035 ovs_assert(ld == 0.25);
1036 ovs_assert(ovs_scan("1.0", "%Lf", &ld));
1037 ovs_assert(ld == 1.0);
1038 ovs_assert(ovs_scan("-5", "%Lf", &ld));
1039 ovs_assert(ld == -5.0);
1040 ovs_assert(ovs_scan("+6", "%Lf", &ld));
1041 ovs_assert(ld == 6.0);
1042 ovs_assert(ovs_scan("-1e5", "%Lf", &ld));
1043 ovs_assert(ld == -1e5);
1044 ovs_assert(ovs_scan("-.25", "%Lf", &ld));
1045 ovs_assert(ld == -.25);
1046 ovs_assert(ovs_scan("+123.e1", "%Lf", &ld));
1047 ovs_assert(ld == 1230.0);
1048 ovs_assert(ovs_scan("25e-2", "%Lf", &ld));
1049 ovs_assert(ld == 0.25);
1050 ovs_assert(ovs_scan("0.25", "%1Lf %Lf", &ld, &ld2));
1051 ovs_assert(ld == 0);
1052 ovs_assert(ld2 == 0.25);
1053 ovs_assert(ovs_scan("1.0", "%2Lf %Lf", &ld, &ld2));
1054 ovs_assert(ld == 1.0);
1055 ovs_assert(ld2 == 0.0);
1056 ovs_assert(!ovs_scan("-5", "%1Lf", &ld));
1057 ovs_assert(!ovs_scan("+6", "%1Lf", &ld));
1058 ovs_assert(!ovs_scan("-1e5", "%2Lf %*f", &ld));
1059 ovs_assert(ld == -1);
1060 ovs_assert(!ovs_scan("-.25", "%2Lf", &ld));
1061 ovs_assert(!ovs_scan("+123.e1", "%6Lf", &ld));
1062 ovs_assert(!ovs_scan("25e-2", "%4Lf", &ld));
1064 ovs_assert(ovs_scan(" Hello,\tworld ", "%*s%n%*s%n", &n, &n2));
1066 ovs_assert(n2 == 13);
1067 ovs_assert(!ovs_scan(" Hello,\tworld ", "%*s%*s%*s"));
1068 ovs_assert(ovs_scan(" Hello,\tworld ", "%6s%n%5s%n", str, &n, str2, &n2));
1069 ovs_assert(!strcmp(str, "Hello,"));
1071 ovs_assert(!strcmp(str2, "world"));
1072 ovs_assert(n2 == 13);
1073 ovs_assert(ovs_scan(" Hello,\tworld ", "%5s%5s%5s", str, str2, str3));
1074 ovs_assert(!strcmp(str, "Hello"));
1075 ovs_assert(!strcmp(str2, ","));
1076 ovs_assert(!strcmp(str3, "world"));
1077 ovs_assert(!ovs_scan(" ", "%*s"));
1079 ovs_assert(ovs_scan(" Hello,\tworld ", "%*c%n%*c%n%c%n",
1082 ovs_assert(n2 == 2);
1083 ovs_assert(c == 'e');
1084 ovs_assert(n3 == 3);
1085 ovs_assert(ovs_scan(" Hello,\tworld ", "%*5c%5c", str));
1086 ovs_assert(!memcmp(str, "o,\two", 5));
1087 ovs_assert(!ovs_scan(" Hello,\tworld ", "%*15c"));
1089 ovs_assert(ovs_scan("0x1234xyzzy", "%9[x0-9a-fA-F]%n", str, &n));
1090 ovs_assert(!strcmp(str, "0x1234x"));
1092 ovs_assert(ovs_scan("foo:bar=baz", "%5[^:=]%n:%5[^:=]%n=%5[^:=]%n",
1093 str, &n, str2, &n2, str3, &n3));
1094 ovs_assert(!strcmp(str, "foo"));
1096 ovs_assert(!strcmp(str2, "bar"));
1097 ovs_assert(n2 == 7);
1098 ovs_assert(!strcmp(str3, "baz"));
1099 ovs_assert(n3 == 11);
1100 ovs_assert(!ovs_scan(" ", "%*[0-9]"));
1101 ovs_assert(ovs_scan("0x123a]4xyzzy-", "%[]x0-9a-fA-F]", str));
1102 ovs_assert(!strcmp(str, "0x123a]4x"));
1103 ovs_assert(ovs_scan("abc]xyz","%[^]xyz]", str));
1104 ovs_assert(!strcmp(str, "abc"));
1105 ovs_assert(!ovs_scan("0x123a]4xyzzy-", "%[x0-9]a-fA-F]", str));
1106 ovs_assert(ovs_scan("0x12-3]xyz", "%[x0-9a-f-]", str));
1107 ovs_assert(!strcmp(str, "0x12-3"));
1108 ovs_assert(ovs_scan("0x12-3]xyz", "%[^a-f-]", str));
1109 ovs_assert(!strcmp(str, "0x12"));
1110 ovs_assert(sscanf("0x12-3]xyz", "%[^-a-f]", str));
1111 ovs_assert(!strcmp(str, "0x12"));
1115 test_snprintf(struct ovs_cmdl_context *ctx OVS_UNUSED)
1119 ovs_assert(snprintf(s, 4, "abcde") == 5);
1120 ovs_assert(!strcmp(s, "abc"));
1122 ovs_assert(snprintf(s, 5, "abcde") == 5);
1123 ovs_assert(!strcmp(s, "abcd"));
1125 ovs_assert(snprintf(s, 6, "abcde") == 5);
1126 ovs_assert(!strcmp(s, "abcde"));
1128 ovs_assert(snprintf(NULL, 0, "abcde") == 5);
1133 test_file_name(struct ovs_cmdl_context *ctx)
1137 for (i = 1; i < ctx->argc; i++) {
1140 dir = dir_name(ctx->argv[i]);
1144 base = base_name(ctx->argv[i]);
1151 static const struct ovs_cmdl_command commands[] = {
1152 {"ctz", NULL, 0, 0, test_ctz},
1153 {"clz", NULL, 0, 0, test_clz},
1154 {"round_up_pow2", NULL, 0, 0, test_round_up_pow2},
1155 {"round_down_pow2", NULL, 0, 0, test_round_down_pow2},
1156 {"count_1bits", NULL, 0, 0, test_count_1bits},
1157 {"log_2_floor", NULL, 0, 0, test_log_2_floor},
1158 {"bitwise_copy", NULL, 0, 0, test_bitwise_copy},
1159 {"bitwise_zero", NULL, 0, 0, test_bitwise_zero},
1160 {"bitwise_one", NULL, 0, 0, test_bitwise_one},
1161 {"bitwise_is_all_zeros", NULL, 0, 0, test_bitwise_is_all_zeros},
1162 {"bitwise_rscan", NULL, 0, 0, test_bitwise_rscan},
1163 {"follow-symlinks", NULL, 1, INT_MAX, test_follow_symlinks},
1164 {"assert", NULL, 0, 0, test_assert},
1165 {"ovs_scan", NULL, 0, 0, test_ovs_scan},
1166 {"snprintf", NULL, 0, 0, test_snprintf},
1168 {"file_name", NULL, 1, INT_MAX, test_file_name},
1170 {NULL, NULL, 0, 0, NULL},
1174 parse_options(int argc, char *argv[])
1179 static const struct option long_options[] = {
1183 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
1186 int c = getopt_long(argc, argv, short_options, long_options, NULL);
1192 VLOG_OPTION_HANDLERS
1201 free(short_options);
1205 test_util_main(int argc, char *argv[])
1207 struct ovs_cmdl_context ctx = { .argc = 0, };
1208 set_program_name(argv[0]);
1209 parse_options(argc, argv);
1210 /* On Windows, stderr is fully buffered if connected to a pipe.
1211 * Make it _IONBF so that an abort does not miss log contents.
1212 * POSIX doesn't define the circumstances in which stderr is
1213 * fully buffered either. */
1214 setvbuf(stderr, NULL, _IONBF, 0);
1215 ctx.argc = argc - optind;
1216 ctx.argv = argv + optind;
1217 ovs_cmdl_run_command(&ctx, commands);
1220 OVSTEST_REGISTER("test-util", test_util_main);