]> git.proxmox.com Git - ovs.git/blame - tests/test-util.c
tests: Convert ovsdb-monitor-sort utility from Perl to Python.
[ovs.git] / tests / test-util.c
CommitLineData
711e0157 1/*
76adea87 2 * Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
711e0157
BP
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include <config.h>
3f636c7e
JR
18#undef NDEBUG
19#include "util.h"
20#include <assert.h>
4749f73d 21#include <getopt.h>
711e0157 22#include <inttypes.h>
aad29cd1 23#include <limits.h>
711e0157
BP
24#include <stdio.h>
25#include <stdlib.h>
ddc4f8e2 26#include "byte-order.h"
8c2296a6 27#include "command-line.h"
3f636c7e 28#include "ovstest.h"
711e0157 29#include "random.h"
e6211adc 30#include "openvswitch/vlog.h"
a656cb77 31
711e0157 32static void
aad29cd1 33check_log_2_floor(uint32_t x, int n)
711e0157
BP
34{
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);
38 abort();
39 }
40}
41
8c2296a6 42static void
1636c761 43test_log_2_floor(struct ovs_cmdl_context *ctx OVS_UNUSED)
8c2296a6
BP
44{
45 int n;
46
47 for (n = 0; n < 32; n++) {
48 /* Check minimum x such that f(x) == n. */
49 check_log_2_floor(1 << n, n);
50
51 /* Check maximum x such that f(x) == n. */
52 check_log_2_floor((1 << n) | ((1 << n) - 1), n);
53
54 /* Check a random value in the middle. */
55 check_log_2_floor((random_uint32() & ((1 << n) - 1)) | (1 << n), n);
56 }
57
58 /* log_2_floor(0) is undefined, so don't check it. */
59}
60
aad29cd1 61static void
d578065e 62check_ctz32(uint32_t x, int n)
aad29cd1 63{
d578065e
JR
64 if (ctz32(x) != n) {
65 fprintf(stderr, "ctz32(%"PRIu32") is %d but should be %d\n",
66 x, ctz32(x), n);
aad29cd1
BP
67 abort();
68 }
69}
70
cc4c738e
JR
71static void
72check_ctz64(uint64_t x, int n)
73{
74 if (ctz64(x) != n) {
75 fprintf(stderr, "ctz64(%"PRIu64") is %d but should be %d\n",
76 x, ctz64(x), n);
77 abort();
78 }
79}
80
8c2296a6 81static void
1636c761 82test_ctz(struct ovs_cmdl_context *ctx OVS_UNUSED)
8c2296a6
BP
83{
84 int n;
85
86 for (n = 0; n < 32; n++) {
87 /* Check minimum x such that f(x) == n. */
d578065e 88 check_ctz32(1 << n, n);
8c2296a6
BP
89
90 /* Check maximum x such that f(x) == n. */
d578065e 91 check_ctz32(UINT32_MAX << n, n);
8c2296a6
BP
92
93 /* Check a random value in the middle. */
d578065e 94 check_ctz32((random_uint32() | 1) << n, n);
8c2296a6
BP
95 }
96
cc4c738e
JR
97
98 for (n = 0; n < 64; n++) {
99 /* Check minimum x such that f(x) == n. */
100 check_ctz64(UINT64_C(1) << n, n);
101
102 /* Check maximum x such that f(x) == n. */
103 check_ctz64(UINT64_MAX << n, n);
104
105 /* Check a random value in the middle. */
106 check_ctz64((random_uint64() | UINT64_C(1)) << n, n);
107 }
108
8c2296a6 109 /* Check ctz(0). */
d578065e 110 check_ctz32(0, 32);
cc4c738e 111 check_ctz64(0, 64);
8c2296a6
BP
112}
113
8c947903
JR
114static void
115check_clz32(uint32_t x, int n)
116{
117 if (clz32(x) != n) {
118 fprintf(stderr, "clz32(%"PRIu32") is %d but should be %d\n",
119 x, clz32(x), n);
120 abort();
121 }
122}
123
124static void
125check_clz64(uint64_t x, int n)
126{
127 if (clz64(x) != n) {
128 fprintf(stderr, "clz64(%"PRIu64") is %d but should be %d\n",
129 x, clz64(x), n);
130 abort();
131 }
132}
133
134static void
1636c761 135test_clz(struct ovs_cmdl_context *ctx OVS_UNUSED)
8c947903
JR
136{
137 int n;
138
139 for (n = 0; n < 32; n++) {
140 /* Check minimum x such that f(x) == n. */
141 check_clz32((1u << 31) >> n, n);
142
143 /* Check maximum x such that f(x) == n. */
144 check_clz32(UINT32_MAX >> n, n);
145
146 /* Check a random value in the middle. */
147 check_clz32((random_uint32() | 1u << 31) >> n, n);
148 }
149
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);
153
154 /* Check maximum x such that f(x) == n. */
155 check_clz64(UINT64_MAX >> n, n);
156
157 /* Check a random value in the middle. */
158 check_clz64((random_uint64() | UINT64_C(1) << 63) >> n, n);
159 }
160
161 /* Check clz(0). */
162 check_clz32(0, 32);
163 check_clz64(0, 64);
164}
165
e93ab553
BP
166/* Returns a random number in the range 'min'...'max' inclusive. */
167static uint32_t
168random_in_range(uint32_t min, uint32_t max)
169{
170 return min == max ? min : min + random_range(max - min + 1);
171}
172
173static void
174check_rup2(uint32_t x, int n)
175{
176 uint32_t rup2 = ROUND_UP_POW2(x);
177 if (rup2 != n) {
178 fprintf(stderr, "ROUND_UP_POW2(%#"PRIx32") is %#"PRIx32" "
179 "but should be %#"PRIx32"\n", x, rup2, n);
180 abort();
181 }
182}
183
184static void
1636c761 185test_round_up_pow2(struct ovs_cmdl_context *ctx OVS_UNUSED)
e93ab553
BP
186{
187 int n;
188
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;
193
194 check_rup2(min, 1u << n);
195 check_rup2(max, 1u << n);
196 check_rup2(random_in_range(min, max), 1u << n);
197 }
198 check_rup2(0, 0);
199}
200
201static void
202check_rdp2(uint32_t x, int n)
203{
204 uint32_t rdp2 = ROUND_DOWN_POW2(x);
205 if (rdp2 != n) {
206 fprintf(stderr, "ROUND_DOWN_POW2(%#"PRIx32") is %#"PRIx32" "
207 "but should be %#"PRIx32"\n", x, rdp2, n);
208 abort();
209 }
210}
211
212static void
1636c761 213test_round_down_pow2(struct ovs_cmdl_context *ctx OVS_UNUSED)
e93ab553
BP
214{
215 int n;
216
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;
221
222 check_rdp2(min, 1u << n);
223 check_rdp2(max, 1u << n);
224 check_rdp2(random_in_range(min, max), 1u << n);
225 }
226 check_rdp2(0, 0);
227}
228
a656cb77 229static void
cc4c738e 230shuffle(uint64_t *p, size_t n)
a656cb77
BP
231{
232 for (; n > 1; n--, p++) {
cc4c738e
JR
233 uint64_t *q = &p[random_range(n)];
234 uint64_t tmp = *p;
a656cb77
BP
235 *p = *q;
236 *q = tmp;
237 }
238}
239
240static void
fb9aefa3 241check_count_1bits(uint64_t x, int n)
a656cb77 242{
fb9aefa3
BP
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);
a656cb77
BP
246 abort();
247 }
248}
249
250static void
1636c761 251test_count_1bits(struct ovs_cmdl_context *ctx OVS_UNUSED)
a656cb77 252{
cc4c738e 253 uint64_t bits[64];
a656cb77
BP
254 int i;
255
256 for (i = 0; i < ARRAY_SIZE(bits); i++) {
cc4c738e 257 bits[i] = UINT64_C(1) << i;
a656cb77
BP
258 }
259
fb9aefa3 260 check_count_1bits(0, 0);
cc4c738e
JR
261
262 for (i = 0; i < 1000; i++) {
263 uint64_t x = 0;
264 int j;
265
266 shuffle(bits, ARRAY_SIZE(bits));
267 for (j = 0; j < 64; j++) {
268 x |= bits[j];
fb9aefa3 269 check_count_1bits(x, j + 1);
cc4c738e
JR
270 }
271 assert(x == UINT64_MAX);
272
273 shuffle(bits, ARRAY_SIZE(bits));
274 for (j = 63; j >= 0; j--) {
275 x &= ~bits[j];
fb9aefa3 276 check_count_1bits(x, j);
cc4c738e
JR
277 }
278 assert(x == 0);
279 }
a656cb77
BP
280}
281
ddc4f8e2
BP
282/* Returns the sum of the squares of the first 'n' positive integers. */
283static unsigned int
284sum_of_squares(int n)
285{
286 return n * (n + 1) * (2 * n + 1) / 6;
287}
288
289static void
1636c761 290test_bitwise_copy(struct ovs_cmdl_context *ctx OVS_UNUSED)
ddc4f8e2
BP
291{
292 unsigned int n_loops;
293 int src_ofs;
294 int dst_ofs;
295 int n_bits;
296
297 n_loops = 0;
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;
304 ovs_be64 expect;
305
306 if (n_bits == 64) {
307 expect = dst;
308 } else {
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)
312 << dst_ofs);
313 }
314
315 bitwise_copy(&src, sizeof src, src_ofs,
316 &dst, sizeof dst, dst_ofs,
317 n_bits);
318 if (expect != dst) {
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,
324 n_bits,
325 ntohll(dst), ntohll(expect));
326 abort();
327 }
328
329 n_loops++;
330 }
331 }
332 }
333
334 if (n_loops != sum_of_squares(64)) {
335 abort();
336 }
337}
338
6cc7ea5e 339static void
1636c761 340test_bitwise_zero(struct ovs_cmdl_context *ctx OVS_UNUSED)
6cc7ea5e
BP
341{
342 unsigned int n_loops;
343 int dst_ofs;
344 int n_bits;
345
346 n_loops = 0;
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;
351 ovs_be64 expect;
352
353 if (n_bits == 64) {
354 expect = htonll(0);
355 } else {
356 uint64_t mask = (UINT64_C(1) << n_bits) - 1;
357 expect = orig_dst & ~htonll(mask << dst_ofs);
358 }
359
360 bitwise_zero(&dst, sizeof dst, dst_ofs, n_bits);
361 if (expect != dst) {
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,
366 n_bits,
367 ntohll(dst), ntohll(expect));
368 abort();
369 }
370
371 n_loops++;
372 }
373 }
374
375 if (n_loops != 64 * (64 + 1) / 2) {
376 abort();
377 }
378}
379
c2dd4932 380static void
1636c761 381test_bitwise_one(struct ovs_cmdl_context *ctx OVS_UNUSED)
c2dd4932
BP
382{
383 unsigned int n_loops;
384 int dst_ofs;
385 int n_bits;
386
387 n_loops = 0;
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;
392 ovs_be64 expect;
393
394 if (n_bits == 64) {
b8266395 395 expect = OVS_BE64_MAX;
c2dd4932
BP
396 } else {
397 uint64_t mask = (UINT64_C(1) << n_bits) - 1;
398 expect = orig_dst | htonll(mask << dst_ofs);
399 }
400
401 bitwise_one(&dst, sizeof dst, dst_ofs, n_bits);
402 if (expect != dst) {
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,
407 n_bits,
408 ntohll(dst), ntohll(expect));
409 abort();
410 }
411
412 n_loops++;
413 }
414 }
415
416 if (n_loops != 64 * (64 + 1) / 2) {
417 abort();
418 }
419}
420
79a010aa 421static void
1636c761 422test_bitwise_is_all_zeros(struct ovs_cmdl_context *ctx OVS_UNUSED)
79a010aa
BP
423{
424 int n_loops;
425
79a010aa
BP
426 for (n_loops = 0; n_loops < 100; n_loops++) {
427 ovs_be64 x = htonll(0);
428 int i;
429
430 for (i = 0; i < 64; i++) {
431 ovs_be64 bit;
432 int ofs, n;
433
434 /* Change a random 0-bit into a 1-bit. */
435 do {
b028db44 436 bit = htonll(UINT64_C(1) << (random_range(64)));
79a010aa
BP
437 } while (x & bit);
438 x |= bit;
439
440 for (ofs = 0; ofs < 64; ofs++) {
441 for (n = 0; n <= 64 - ofs; n++) {
442 bool expect;
443 bool answer;
444
445 expect = (n == 64
446 ? x == 0
447 : !(x & htonll(((UINT64_C(1) << n) - 1)
448 << ofs)));
449 answer = bitwise_is_all_zeros(&x, sizeof x, ofs, n);
450 if (expect != answer) {
451 fprintf(stderr,
452 "bitwise_is_all_zeros(0x%016"PRIx64",8,%d,%d "
453 "returned %s instead of %s\n",
454 ntohll(x), ofs, n,
455 answer ? "true" : "false",
456 expect ? "true" : "false");
457 abort();
458 }
459 }
460 }
461 }
462 }
463}
fee0c963 464
76adea87
HZ
465static int
466trivial_bitwise_rscan(const void *p, unsigned int len, bool target,
467 int start, int end)
468{
469 int ofs;
470
471 for (ofs = start; ofs > end; ofs--) {
472 if (bitwise_get_bit(p, len, ofs) == target) {
473 break;
474 }
475 }
476 return ofs;
477}
478
479static void
480test_bitwise_rscan(struct ovs_cmdl_context *ctx OVS_UNUSED)
481{
482 /* All 1s */
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));
490
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));
507
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));
518
519 /* All 0s */
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));
525
526 int n_loops;
527 for (n_loops = 0; n_loops < 100; n_loops++) {
528 ovs_be64 x = htonll(0);
529 int i;
530
531 for (i = 0; i < 64; i++) {
532 ovs_be64 bit;
533
534 /* Change a random 0-bit into a 1-bit. */
535 do {
536 bit = htonll(UINT64_C(1) << (random_range(64)));
537 } while (x & bit);
538 x |= bit;
539
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) {
548 fprintf(stderr,
549 "bitwise_rscan(0x%016"PRIx64",8,%s,%d,%d) "
550 "returned %s instead of %s\n",
551 ntohll(x),
552 target ? "true" : "false",
553 start, end,
554 answer ? "true" : "false",
555 expect ? "true" : "false");
556 abort();
557 }
558 }
559 }
560 }
561 }
562 }
563}
564
fee0c963 565static void
1636c761 566test_follow_symlinks(struct ovs_cmdl_context *ctx)
fee0c963
BP
567{
568 int i;
569
1636c761
RB
570 for (i = 1; i < ctx->argc; i++) {
571 char *target = follow_symlinks(ctx->argv[i]);
fee0c963
BP
572 puts(target);
573 free(target);
574 }
575}
4749f73d
BP
576
577static void
1636c761 578test_assert(struct ovs_cmdl_context *ctx OVS_UNUSED)
4749f73d
BP
579{
580 ovs_assert(false);
581}
ed2232fc
BP
582
583static void
1636c761 584test_ovs_scan(struct ovs_cmdl_context *ctx OVS_UNUSED)
ed2232fc
BP
585{
586 char str[16], str2[16], str3[16];
587 long double ld, ld2;
588 long long ll, ll2;
589 signed char c, c2;
590 ptrdiff_t pd, pd2;
591 intmax_t im, im2;
592 size_t sz, sz2;
593 int n, n2, n3;
594 double d, d2;
595 short s, s2;
596 float f, f2;
597 long l, l2;
598 int i, i2;
599
ed2232fc
BP
600 ovs_assert(ovs_scan("", " "));
601 ovs_assert(ovs_scan(" ", " "));
602 ovs_assert(ovs_scan(" ", " "));
603 ovs_assert(ovs_scan(" \t ", " "));
604
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"));
609
610 ovs_assert(ovs_scan("123", "%d", &i));
611 ovs_assert(i == 123);
612 ovs_assert(ovs_scan("0", "%d", &i));
613 ovs_assert(i == 0);
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));
624 ovs_assert(i == 0);
625 ovs_assert(ovs_scan("123", "%2d %d", &i, &i2));
626 ovs_assert(i == 12);
627 ovs_assert(i2 == 3);
628 ovs_assert(ovs_scan("+123", "%2d %d", &i, &i2));
629 ovs_assert(i == 1);
630 ovs_assert(i2 == 23);
631 ovs_assert(ovs_scan("-123", "%2d %d", &i, &i2));
632 ovs_assert(i == -1);
633 ovs_assert(i2 == 23);
634 ovs_assert(ovs_scan("0123", "%2d %d", &i, &i2));
635 ovs_assert(i == 1);
636 ovs_assert(i2 == 23);
637 ovs_assert(ovs_scan("123", "%*2d %d", &i));
638 ovs_assert(i == 3);
639 ovs_assert(ovs_scan("+123", "%2d %*d", &i));
640 ovs_assert(i == 1);
641 ovs_assert(i2 == 23);
642 ovs_assert(ovs_scan("-123", "%*2d %*d"));
643
644 ovs_assert(ovs_scan("123", "%u", &i));
645 ovs_assert(i == 123);
646 ovs_assert(ovs_scan("0", "%u", &i));
647 ovs_assert(i == 0);
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));
658 ovs_assert(i == 0);
659 ovs_assert(ovs_scan("123", "%2u %u", &i, &i2));
660 ovs_assert(i == 12);
661 ovs_assert(i2 == 3);
662 ovs_assert(ovs_scan("+123", "%2u %u", &i, &i2));
663 ovs_assert(i == 1);
664 ovs_assert(i2 == 23);
665 ovs_assert(ovs_scan("-123", "%2u %u", &i, &i2));
666 ovs_assert(i == -1);
667 ovs_assert(i2 == 23);
668 ovs_assert(ovs_scan("0123", "%2u %u", &i, &i2));
669 ovs_assert(i == 1);
670 ovs_assert(i2 == 23);
671 ovs_assert(ovs_scan("123", "%*2u %u", &i));
672 ovs_assert(i == 3);
673 ovs_assert(ovs_scan("+123", "%2u %*u", &i));
674 ovs_assert(i == 1);
675 ovs_assert(i2 == 23);
676 ovs_assert(ovs_scan("-123", "%*2u %*u"));
677
678 ovs_assert(ovs_scan("123", "%i", &i));
679 ovs_assert(i == 123);
680 ovs_assert(ovs_scan("0", "%i", &i));
681 ovs_assert(i == 0);
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));
694 ovs_assert(i == 12);
695 ovs_assert(i2 == 3);
696 ovs_assert(ovs_scan("+123", "%2i %i", &i, &i2));
697 ovs_assert(i == 1);
698 ovs_assert(i2 == 23);
699 ovs_assert(ovs_scan("-123", "%2i %i", &i, &i2));
700 ovs_assert(i == -1);
701 ovs_assert(i2 == 23);
702 ovs_assert(ovs_scan("0123", "%2i %i", &i, &i2));
703 ovs_assert(i == 1);
704 ovs_assert(i2 == 23);
705 ovs_assert(ovs_scan("123", "%*2i %i", &i));
706 ovs_assert(i == 3);
707 ovs_assert(ovs_scan("+123", "%2i %*i", &i));
708 ovs_assert(i == 1);
709 ovs_assert(i2 == 23);
710 ovs_assert(ovs_scan("-123", "%*2i %*i"));
711
712 ovs_assert(ovs_scan("123", "%o", &i));
713 ovs_assert(i == 0123);
714 ovs_assert(ovs_scan("0", "%o", &i));
715 ovs_assert(i == 0);
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));
726 ovs_assert(i == 0);
727 ovs_assert(ovs_scan("123", "%2o %o", &i, &i2));
728 ovs_assert(i == 012);
729 ovs_assert(i2 == 3);
730 ovs_assert(ovs_scan("+123", "%2o %o", &i, &i2));
731 ovs_assert(i == 1);
732 ovs_assert(i2 == 023);
733 ovs_assert(ovs_scan("-123", "%2o %o", &i, &i2));
734 ovs_assert(i == -1);
735 ovs_assert(i2 == 023);
736 ovs_assert(ovs_scan("0123", "%2o %o", &i, &i2));
737 ovs_assert(i == 1);
738 ovs_assert(i2 == 023);
739 ovs_assert(ovs_scan("123", "%*2o %o", &i));
740 ovs_assert(i == 3);
741 ovs_assert(ovs_scan("+123", "%2o %*o", &i));
742 ovs_assert(i == 1);
743 ovs_assert(i2 == 023);
744 ovs_assert(ovs_scan("-123", "%*2o %*o"));
745
746 ovs_assert(ovs_scan("123", "%x", &i));
747 ovs_assert(i == 0x123);
748 ovs_assert(ovs_scan("0", "%x", &i));
749 ovs_assert(i == 0);
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);
763 ovs_assert(i2 == 3);
764 ovs_assert(ovs_scan("+123", "%2x %x", &i, &i2));
765 ovs_assert(i == 1);
766 ovs_assert(i2 == 0x23);
767 ovs_assert(ovs_scan("-123", "%2x %x", &i, &i2));
768 ovs_assert(i == -1);
769 ovs_assert(i2 == 0x23);
770 ovs_assert(ovs_scan("0123", "%2x %x", &i, &i2));
771 ovs_assert(i == 1);
772 ovs_assert(i2 == 0x23);
773 ovs_assert(ovs_scan("123", "%*2x %x", &i));
774 ovs_assert(i == 3);
775 ovs_assert(ovs_scan("+123", "%2x %*x", &i));
776 ovs_assert(i == 1);
777 ovs_assert(i2 == 0x23);
778 ovs_assert(ovs_scan("-123", "%*2x %*x"));
779
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));
792 ovs_assert(s == 0);
793 ovs_assert(ovs_scan("123", "%2hd %hd", &s, &s2));
794 ovs_assert(s == 12);
795 ovs_assert(s2 == 3);
796 ovs_assert(ovs_scan("+123", "%2hd %hd", &s, &s2));
797 ovs_assert(s == 1);
798 ovs_assert(s2 == 23);
799 ovs_assert(ovs_scan("-123", "%2hd %hd", &s, &s2));
800 ovs_assert(s == -1);
801 ovs_assert(s2 == 23);
802 ovs_assert(ovs_scan("0123", "%2hd %hd", &s, &s2));
803 ovs_assert(s == 1);
804 ovs_assert(s2 == 23);
805
806 ovs_assert(ovs_scan("123", "%hhd", &c));
807 ovs_assert(c == 123);
808 ovs_assert(ovs_scan("0", "%hhd", &c));
809 ovs_assert(c == 0);
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));
820 ovs_assert(c == 0);
821 ovs_assert(ovs_scan("123", "%2hhd %hhd", &c, &c2));
822 ovs_assert(c == 12);
823 ovs_assert(c2 == 3);
824 ovs_assert(ovs_scan("+123", "%2hhd %hhd", &c, &c2));
825 ovs_assert(c == 1);
826 ovs_assert(c2 == 23);
827 ovs_assert(ovs_scan("-123", "%2hhd %hhd", &c, &c2));
828 ovs_assert(c == -1);
829 ovs_assert(c2 == 23);
830 ovs_assert(ovs_scan("0123", "%2hhd %hhd", &c, &c2));
831 ovs_assert(c == 1);
832 ovs_assert(c2 == 23);
833
834 ovs_assert(ovs_scan("123", "%ld", &l));
835 ovs_assert(l == 123);
836 ovs_assert(ovs_scan("0", "%ld", &l));
837 ovs_assert(l == 0);
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));
848 ovs_assert(l == 0);
849 ovs_assert(ovs_scan("123", "%2ld %ld", &l, &l2));
850 ovs_assert(l == 12);
851 ovs_assert(l2 == 3);
852 ovs_assert(ovs_scan("+123", "%2ld %ld", &l, &l2));
853 ovs_assert(l == 1);
854 ovs_assert(l2 == 23);
855 ovs_assert(ovs_scan("-123", "%2ld %ld", &l, &l2));
856 ovs_assert(l == -1);
857 ovs_assert(l2 == 23);
858 ovs_assert(ovs_scan("0123", "%2ld %ld", &l, &l2));
859 ovs_assert(l == 1);
860 ovs_assert(l2 == 23);
861
862 ovs_assert(ovs_scan("123", "%lld", &ll));
863 ovs_assert(ll == 123);
864 ovs_assert(ovs_scan("0", "%lld", &ll));
865 ovs_assert(ll == 0);
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));
876 ovs_assert(ll == 0);
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));
881 ovs_assert(ll == 1);
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));
887 ovs_assert(ll == 1);
888 ovs_assert(ll2 == 23);
889
890 ovs_assert(ovs_scan("123", "%jd", &im));
891 ovs_assert(im == 123);
892 ovs_assert(ovs_scan("0", "%jd", &im));
893 ovs_assert(im == 0);
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));
904 ovs_assert(im == 0);
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));
909 ovs_assert(im == 1);
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));
915 ovs_assert(im == 1);
916 ovs_assert(im2 == 23);
917
918 ovs_assert(ovs_scan("123", "%td", &pd));
919 ovs_assert(pd == 123);
920 ovs_assert(ovs_scan("0", "%td", &pd));
921 ovs_assert(pd == 0);
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));
932 ovs_assert(pd == 0);
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));
937 ovs_assert(pd == 1);
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));
943 ovs_assert(pd == 1);
944 ovs_assert(pd2 == 23);
945
946 ovs_assert(ovs_scan("123", "%zd", &sz));
947 ovs_assert(sz == 123);
948 ovs_assert(ovs_scan("0", "%zd", &sz));
949 ovs_assert(sz == 0);
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));
960 ovs_assert(sz == 0);
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));
965 ovs_assert(sz == 1);
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));
971 ovs_assert(sz == 1);
972 ovs_assert(sz2 == 23);
973
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));
991 ovs_assert(f == 0);
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));
999 ovs_assert(f == -1);
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));
1003
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));
1021 ovs_assert(d == 0);
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));
1033
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));
1063
1064 ovs_assert(ovs_scan(" Hello,\tworld ", "%*s%n%*s%n", &n, &n2));
1065 ovs_assert(n == 7);
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,"));
1070 ovs_assert(n == 7);
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"));
1078
1079 ovs_assert(ovs_scan(" Hello,\tworld ", "%*c%n%*c%n%c%n",
1080 &n, &n2, &c, &n3));
1081 ovs_assert(n == 1);
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"));
1088
1089 ovs_assert(ovs_scan("0x1234xyzzy", "%9[x0-9a-fA-F]%n", str, &n));
1090 ovs_assert(!strcmp(str, "0x1234x"));
1091 ovs_assert(n == 7);
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"));
1095 ovs_assert(n == 3);
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"));
1112}
cde1c287
BP
1113
1114static void
1636c761 1115test_snprintf(struct ovs_cmdl_context *ctx OVS_UNUSED)
cde1c287
BP
1116{
1117 char s[16];
1118
fa792e4f
BP
1119#if __GNUC__ >= 7
1120 /* GCC 7+ warns about the following calls that truncate a string using
1121 * snprintf(). We're testing that truncation works properly, so
1122 * temporarily disable the warning. */
1123#pragma GCC diagnostic push
1124#pragma GCC diagnostic ignored "-Wformat-truncation"
1125#endif
cde1c287
BP
1126 ovs_assert(snprintf(s, 4, "abcde") == 5);
1127 ovs_assert(!strcmp(s, "abc"));
1128
1129 ovs_assert(snprintf(s, 5, "abcde") == 5);
1130 ovs_assert(!strcmp(s, "abcd"));
fa792e4f
BP
1131#if __GNUC__ >= 7
1132#pragma GCC diagnostic pop
1133#endif
cde1c287
BP
1134
1135 ovs_assert(snprintf(s, 6, "abcde") == 5);
1136 ovs_assert(!strcmp(s, "abcde"));
1137
1138 ovs_assert(snprintf(NULL, 0, "abcde") == 5);
1139}
3c1150ce
GS
1140
1141#ifndef _WIN32
1142static void
1636c761 1143test_file_name(struct ovs_cmdl_context *ctx)
3c1150ce
GS
1144{
1145 int i;
1146
1636c761 1147 for (i = 1; i < ctx->argc; i++) {
3c1150ce
GS
1148 char *dir, *base;
1149
1636c761 1150 dir = dir_name(ctx->argv[i]);
3c1150ce
GS
1151 puts(dir);
1152 free(dir);
1153
1636c761 1154 base = base_name(ctx->argv[i]);
3c1150ce
GS
1155 puts(base);
1156 free(base);
1157 }
1158}
1159#endif /* _WIN32 */
8c2296a6 1160\f
5f383751 1161static const struct ovs_cmdl_command commands[] = {
1f4a7252
RM
1162 {"ctz", NULL, 0, 0, test_ctz, OVS_RO},
1163 {"clz", NULL, 0, 0, test_clz, OVS_RO},
1164 {"round_up_pow2", NULL, 0, 0, test_round_up_pow2, OVS_RO},
1165 {"round_down_pow2", NULL, 0, 0, test_round_down_pow2, OVS_RO},
1166 {"count_1bits", NULL, 0, 0, test_count_1bits, OVS_RO},
1167 {"log_2_floor", NULL, 0, 0, test_log_2_floor, OVS_RO},
1168 {"bitwise_copy", NULL, 0, 0, test_bitwise_copy, OVS_RO},
1169 {"bitwise_zero", NULL, 0, 0, test_bitwise_zero, OVS_RO},
1170 {"bitwise_one", NULL, 0, 0, test_bitwise_one, OVS_RO},
1171 {"bitwise_is_all_zeros", NULL, 0, 0, test_bitwise_is_all_zeros, OVS_RO},
1172 {"bitwise_rscan", NULL, 0, 0, test_bitwise_rscan, OVS_RO},
1173 {"follow-symlinks", NULL, 1, INT_MAX, test_follow_symlinks, OVS_RO},
1174 {"assert", NULL, 0, 0, test_assert, OVS_RO},
1175 {"ovs_scan", NULL, 0, 0, test_ovs_scan, OVS_RO},
1176 {"snprintf", NULL, 0, 0, test_snprintf, OVS_RO},
3c1150ce 1177#ifndef _WIN32
1f4a7252 1178 {"file_name", NULL, 1, INT_MAX, test_file_name, OVS_RO},
3c1150ce 1179#endif
1f4a7252 1180 {NULL, NULL, 0, 0, NULL, OVS_RO},
8c2296a6 1181};
79a010aa 1182
4749f73d
BP
1183static void
1184parse_options(int argc, char *argv[])
1185{
1186 enum {
1187 VLOG_OPTION_ENUMS
1188 };
07fc4ed3 1189 static const struct option long_options[] = {
4749f73d
BP
1190 VLOG_LONG_OPTIONS,
1191 {NULL, 0, NULL, 0},
1192 };
5f383751 1193 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
4749f73d
BP
1194
1195 for (;;) {
1196 int c = getopt_long(argc, argv, short_options, long_options, NULL);
1197 if (c == -1) {
1198 break;
1199 }
1200
1201 switch (c) {
1202 VLOG_OPTION_HANDLERS
1203
1204 case '?':
1205 exit(EXIT_FAILURE);
1206
1207 default:
1208 abort();
1209 }
1210 }
1211 free(short_options);
1212}
1213
eadd1644
AZ
1214static void
1215test_util_main(int argc, char *argv[])
711e0157 1216{
1636c761 1217 struct ovs_cmdl_context ctx = { .argc = 0, };
8c2296a6 1218 set_program_name(argv[0]);
4749f73d 1219 parse_options(argc, argv);
784acd82
GS
1220 /* On Windows, stderr is fully buffered if connected to a pipe.
1221 * Make it _IONBF so that an abort does not miss log contents.
1222 * POSIX doesn't define the circumstances in which stderr is
1223 * fully buffered either. */
1224 setvbuf(stderr, NULL, _IONBF, 0);
1636c761
RB
1225 ctx.argc = argc - optind;
1226 ctx.argv = argv + optind;
1227 ovs_cmdl_run_command(&ctx, commands);
711e0157 1228}
eadd1644
AZ
1229
1230OVSTEST_REGISTER("test-util", test_util_main);