t_snprintb.c revision 1.30 1 /* $NetBSD: t_snprintb.c,v 1.30 2024/03/04 21:35:28 rillig Exp $ */
2
3 /*
4 * Copyright (c) 2002, 2004, 2008, 2010, 2024 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code was contributed to The NetBSD Foundation by Christos Zoulas and
8 * Roland Illig.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __COPYRIGHT("@(#) Copyright (c) 2008, 2010, 2024\
34 The NetBSD Foundation, inc. All rights reserved.");
35 __RCSID("$NetBSD: t_snprintb.c,v 1.30 2024/03/04 21:35:28 rillig Exp $");
36
37 #include <stdio.h>
38 #include <string.h>
39 #include <util.h>
40 #include <vis.h>
41
42 #include <atf-c.h>
43
44 static const char *
45 vis_arr(const char *arr, size_t arrsize)
46 {
47 static char buf[3][1024];
48 static size_t i;
49
50 i = (i + 1) % (sizeof(buf) / sizeof(buf[0]));
51 int rv = strnvisx(buf[i] + 1, sizeof(buf[i]) - 2, arr, arrsize,
52 VIS_WHITE | VIS_OCTAL);
53 ATF_REQUIRE_MSG(rv >= 0, "buffer too small for size %zu", arrsize);
54 buf[i][0] = '"';
55 buf[i][1 + rv] = '"';
56 buf[i][1 + rv + 1] = '\0';
57 return buf[i];
58 }
59
60 static void
61 check_snprintb_m(const char *file, size_t line,
62 size_t bufsize, const char *bitfmt, size_t bitfmtlen, uint64_t val,
63 size_t line_max,
64 int want_rv, const char *want_buf, size_t want_bufsize)
65 {
66 char buf[1024];
67
68 ATF_REQUIRE(bufsize <= sizeof(buf));
69 ATF_REQUIRE(want_bufsize <= sizeof(buf));
70 if (bitfmtlen > 2 && bitfmt[0] == '\177')
71 ATF_REQUIRE_MSG(
72 bitfmt[bitfmtlen - 1] == '\0',
73 "%s:%zu: missing trailing '\\0' in new-style bitfmt",
74 file, line);
75 if (bufsize == 0)
76 want_bufsize = 0;
77 memset(buf, 0x5a, sizeof(buf));
78
79 int rv = snprintb_m(buf, bufsize, bitfmt, val, line_max);
80
81 size_t have_bufsize = sizeof(buf);
82 while (have_bufsize > 0 && buf[have_bufsize - 1] == 0x5a)
83 have_bufsize--;
84 if (rv > 0 && (unsigned)rv < have_bufsize
85 && buf[rv - 1] == '\0' && buf[rv] == '\0')
86 have_bufsize = rv + 1;
87 if (rv < 0)
88 for (size_t i = have_bufsize; i >= 2; i--)
89 if (buf[i - 2] == '\0' && buf[i - 1] == '\0')
90 have_bufsize = i;
91
92 ATF_CHECK_MSG(
93 rv == want_rv
94 && memcmp(buf, want_buf, want_bufsize) == 0
95 && (line_max == 0 || have_bufsize < 2
96 || buf[have_bufsize - 2] == '\0')
97 && (have_bufsize < 1 || buf[have_bufsize - 1] == '\0'),
98 "failed:\n"
99 "\ttest case: %s:%zu\n"
100 "\tformat: %s\n"
101 "\tvalue: %#jx\n"
102 "\tline_max: %zu\n"
103 "\twant: %d bytes %s\n"
104 "\thave: %d bytes %s\n",
105 file, line,
106 vis_arr(bitfmt, bitfmtlen),
107 (uintmax_t)val,
108 line_max,
109 want_rv, vis_arr(want_buf, want_bufsize),
110 rv, vis_arr(buf, have_bufsize));
111 }
112
113 #define h_snprintb_m_len(bufsize, bitfmt, val, line_max, \
114 want_rv, want_buf) \
115 check_snprintb_m(__FILE__, __LINE__, \
116 bufsize, bitfmt, sizeof(bitfmt) - 1, val, line_max, \
117 want_rv, want_buf, sizeof(want_buf))
118
119 #define h_snprintb(bitfmt, val, want_buf) \
120 h_snprintb_m_len(1024, bitfmt, val, 0, sizeof(want_buf) - 1, want_buf)
121
122 #define h_snprintb_len(bufsize, bitfmt, val, want_rv, want_buf) \
123 h_snprintb_m_len(bufsize, bitfmt, val, 0, want_rv, want_buf)
124
125 #define h_snprintb_error(bitfmt, want_buf) \
126 h_snprintb_m_len(1024, bitfmt, 0x00, 0, -1, want_buf)
127
128 #define h_snprintb_m(bitfmt, val, line_max, want_buf) \
129 h_snprintb_m_len(1024, bitfmt, val, line_max, \
130 sizeof(want_buf) - 1, want_buf)
131
132 ATF_TC(snprintb);
133 ATF_TC_HEAD(snprintb, tc)
134 {
135 atf_tc_set_md_var(tc, "descr", "Checks snprintb(3)");
136 }
137 ATF_TC_BODY(snprintb, tc)
138 {
139
140 // style and number base, old style, octal, zero value
141 //
142 // The value 0 does not get a leading '0'.
143 h_snprintb(
144 "\010",
145 0x00,
146 "0");
147
148 // style and number base, old style, octal, nonzero value
149 //
150 // Nonzero octal values get a leading '0'.
151 h_snprintb(
152 "\010",
153 0xff,
154 "0377");
155
156 // style and number base, old style, decimal, zero value
157 h_snprintb(
158 "\012",
159 0x00,
160 "0");
161
162 // style and number base, old style, decimal, nonzero value
163 h_snprintb(
164 "\012",
165 0xff,
166 "255");
167
168 // style and number base, old style, hexadecimal, zero value
169 //
170 // The value 0 does not get a leading '0x'.
171 h_snprintb(
172 "\020",
173 0x00,
174 "0");
175
176 // style and number base, old style, hexadecimal, nonzero value
177 //
178 // Nonzero hexadecimal values get a leading '0x'.
179 h_snprintb(
180 "\177\020",
181 0xff,
182 "0xff");
183
184 // style and number base, old style, invalid base 0
185 h_snprintb_error(
186 "",
187 "#");
188
189 // style and number base, old style, invalid base 2
190 h_snprintb_error(
191 "\002",
192 "#");
193
194 // style and number base, old style, invalid base 255 or -1
195 h_snprintb_error(
196 "\377",
197 "#");
198
199 // style and number base, new style, octal, zero value
200 //
201 // The value 0 does not get a leading '0'.
202 h_snprintb(
203 "\177\010",
204 0x00,
205 "0");
206
207 // style and number base, new style, octal, nonzero value
208 //
209 // Nonzero octal values get a leading '0'.
210 h_snprintb(
211 "\177\010",
212 0xff,
213 "0377");
214
215 // style and number base, new style, decimal, zero value
216 h_snprintb(
217 "\177\012",
218 0x00,
219 "0");
220
221 // style and number base, new style, decimal, nonzero value
222 h_snprintb(
223 "\177\012",
224 0xff,
225 "255");
226
227 // style and number base, new style, hexadecimal, zero value
228 //
229 // The value 0 does not get a leading '0x'.
230 h_snprintb(
231 "\177\020",
232 0x00,
233 "0");
234
235 // style and number base, new style, hexadecimal, nonzero value
236 //
237 // Nonzero hexadecimal values get a leading '0x'.
238 h_snprintb(
239 "\177\020",
240 0xff,
241 "0xff");
242
243 // style and number base, new style, invalid number base 0
244 h_snprintb_error(
245 "\177",
246 "#");
247
248 // style and number base, new style, invalid number base 2
249 h_snprintb_error(
250 "\177\002",
251 "#");
252
253 // style and number base, new style, invalid number base 255 or -1
254 h_snprintb_error(
255 "\177\377",
256 "#");
257
258 // old style, from lsb to msb
259 h_snprintb(
260 "\020"
261 "\001bit1"
262 "\002bit2"
263 "\037bit31"
264 "\040bit32",
265 0xffffffff80000001,
266 "0xffffffff80000001<bit1,bit32>");
267
268 // old style, invalid bit number, at the beginning
269 h_snprintb_error(
270 "\020"
271 "\041invalid",
272 "0#");
273
274 // old style, invalid bit number, in the middle
275 //
276 // The old-style format supports only 32 bits, interpreting the
277 // \041 as part of the text belonging to bit 1.
278 h_snprintb(
279 "\020"
280 "\001bit1"
281 "\041bit33",
282 0x01,
283 "0x1<bit1!bit33>");
284
285 // old style, repeated bit numbers
286 //
287 // When a bit number is mentioned more than once,
288 // this is most likely a typo.
289 h_snprintb(
290 "\020"
291 "\001once"
292 "\001again",
293 0x01,
294 "0x1<once,again>");
295
296 // old style, non-printable description
297 //
298 // The characters ' ' and '\t' are interpreted as bit numbers,
299 // not as part of the description; the visual arrangement in this
300 // example is intentionally misleading.
301 h_snprintb(
302 "\020"
303 "\001least significant"
304 "\002horizontal\ttab"
305 "\003\xC3\xA4",
306 0xff,
307 "0xff<least,horizontal,\xC3\xA4>");
308
309 // old style, empty description
310 //
311 // Empty descriptions result in multiple commas in a row, which is a
312 // mistake.
313 h_snprintb(
314 "\020"
315 "\001lsb"
316 "\004"
317 "\005"
318 "\010msb",
319 0xff,
320 "0xff<lsb,,,msb>");
321
322 // old style, buffer size 0, null buffer
323 //
324 // If the buffer size is 0, the buffer is not accessed at all and
325 // may be a null pointer.
326 int null_rv_old = snprintb(NULL, 0, "\020\001lsb", 0x01);
327 ATF_CHECK_MSG(
328 null_rv_old == 8,
329 "want length 8, have %d", null_rv_old);
330
331 // old style, buffer too small for value
332 h_snprintb_len(
333 1, "\020", 0x00,
334 1, "");
335
336 // old style, buffer large enough for zero value
337 h_snprintb_len(
338 2, "\020", 0x00,
339 1, "0");
340
341 // old style, buffer too small for nonzero value
342 h_snprintb_len(
343 3, "\020", 0x07,
344 3, "0x");
345
346 // old style, buffer large enough for nonzero value
347 h_snprintb_len(
348 4, "\020", 0x07,
349 3, "0x7");
350
351 // old style, buffer too small for '<'
352 h_snprintb_len(
353 4, "\020\001lsb", 0x07,
354 8, "0x7");
355
356 // old style, buffer too small for description
357 h_snprintb_len(
358 7, "\020\001lsb", 0x07,
359 8, "0x7<ls");
360
361 // old style, buffer too small for '>'
362 h_snprintb_len(
363 8, "\020\001lsb", 0x07,
364 8, "0x7<lsb");
365
366 // old style, buffer large enough for '>'
367 h_snprintb_len(
368 9, "\020\001lsb", 0x07,
369 8, "0x7<lsb>");
370
371 // old style, buffer too small for second description
372 h_snprintb_len(
373 9, "\020\001one\002two", 0x07,
374 12, "0x7<one,");
375
376 // old style, buffer too small for second description
377 h_snprintb_len(
378 10, "\020\001one\002two", 0x07,
379 12, "0x7<one,t");
380
381 // old style, buffer too small for '>' after second description
382 h_snprintb_len(
383 12, "\020\001one\002two", 0x07,
384 12, "0x7<one,two");
385
386 // old style, buffer large enough for '>' after second description
387 h_snprintb_len(
388 13, "\020\001one\002two", 0x07,
389 12, "0x7<one,two>");
390
391 // new style, buffer size 0, null buffer
392 //
393 // If the buffer size is 0, the buffer may be NULL.
394 int null_rv_new = snprintb(NULL, 0, "\177\020b\000lsb\0", 0x01);
395 ATF_CHECK_MSG(
396 null_rv_new == 8,
397 "want length 8, have %d", null_rv_new);
398
399 // new style single bits
400 h_snprintb(
401 "\177\020"
402 "b\000lsb\0"
403 "b\001above-lsb\0"
404 "b\037bit31\0"
405 "b\040bit32\0"
406 "b\076below-msb\0"
407 "b\077msb\0",
408 0x8000000180000001,
409 "0x8000000180000001<lsb,bit31,bit32,msb>");
410
411 // new style single bits, duplicate bits
412 h_snprintb(
413 "\177\020"
414 "b\000lsb\0"
415 "b\000lsb\0"
416 "b\000lsb\0",
417 0xff,
418 "0xff<lsb,lsb,lsb>");
419
420 // new style single bits, empty description
421 h_snprintb(
422 "\177\020"
423 "b\000lsb\0"
424 "b\001\0"
425 "b\002\0"
426 "b\007msb\0"
427 ,
428 0xff,
429 "0xff<lsb,,,msb>");
430
431 // new style single bits, bit number too large
432 h_snprintb_error(
433 "\177\020"
434 "b\100too-high\0",
435 "0#");
436 h_snprintb_error(
437 "\177\020"
438 "b\377too-high\0",
439 "0#");
440
441 // new style single bits, non-printable description
442 //
443 // Contrary to the old-style format, the new-style format allows
444 // arbitrary characters in the description, even control characters
445 // and non-ASCII characters.
446 h_snprintb(
447 "\177\020"
448 "b\000space \t \xC3\xA4\0",
449 0x1,
450 "0x1<space \t \xC3\xA4>");
451
452 // new style named bit-field, octal
453 //
454 // The bit-field value gets a leading '0' iff it is nonzero.
455 h_snprintb(
456 "\177\010"
457 "f\000\010byte0\0"
458 "f\010\010byte1\0",
459 0x0100,
460 "0400<byte0=0,byte1=01>");
461
462 // new style named bit-field, decimal
463 h_snprintb(
464 "\177\012"
465 "f\000\010byte0\0"
466 "f\010\010byte1\0",
467 0x0100,
468 "256<byte0=0,byte1=1>");
469
470 // new style named bit-field, hexadecimal
471 //
472 // The bit-field value gets a leading '0x' iff it is nonzero.
473 h_snprintb(
474 "\177\020"
475 "f\000\010byte0\0"
476 "f\010\010byte1\0",
477 0x0100,
478 "0x100<byte0=0,byte1=0x1>");
479
480 // new style bit-field, from 0 width 0
481 h_snprintb(
482 "\177\020"
483 "f\000\000zero-width\0"
484 "=\000zero\0",
485 0xffff,
486 "0xffff<zero-width=0=zero>");
487
488 // new style bit-field, from 0 width 1
489 h_snprintb(
490 "\177\020"
491 "f\000\001lsb\0"
492 "=\000zero\0"
493 "=\001one\0",
494 0x0,
495 "0<lsb=0=zero>");
496 h_snprintb(
497 "\177\020"
498 "f\000\001lsb\0"
499 "=\000zero\0"
500 "=\001one\0",
501 0x1,
502 "0x1<lsb=0x1=one>");
503
504 // new style bit-field, from 0 width 63
505 h_snprintb(
506 "\177\020"
507 "f\000\077uint63\0"
508 "=\125match\0",
509 0xaaaa5555aaaa5555,
510 "0xaaaa5555aaaa5555<uint63=0x2aaa5555aaaa5555>");
511
512 // new style bit-field, from 0 width 64
513 h_snprintb(
514 "\177\020"
515 "f\000\100uint64\0"
516 "=\125match\0",
517 0xaaaa5555aaaa5555,
518 "0xaaaa5555aaaa5555<uint64=0xaaaa5555aaaa5555>");
519
520 // new style bit-field, from 0 width 65
521 h_snprintb_error(
522 "\177\020"
523 "f\000\101uint65\0",
524 "0#");
525
526 // new style bit-field, from 1 width 8
527 h_snprintb(
528 "\177\020"
529 "f\001\010uint8\0"
530 "=\203match\0",
531 0x0106,
532 "0x106<uint8=0x83=match>");
533
534 // new style bit-field, from 1 width 9
535 //
536 // The '=' and ':' directives can match a bit-field value between
537 // 0 and 255, independent of the bit-field's width.
538 h_snprintb(
539 "\177\020"
540 "f\001\011uint9\0"
541 "=\203match\0"
542 "*=default-f\0"
543 "F\001\011\0"
544 ":\203match\0"
545 "*default-F\0",
546 0x0306,
547 "0x306<uint9=0x183=default-f,default-F>");
548
549 // new style bit-field, from 24 width 32
550 h_snprintb(
551 "\177\020"
552 "f\030\040uint32\0",
553 0xaaaa555500000000,
554 "0xaaaa555500000000<uint32=0xaa555500>");
555
556 // new style bit-field, from 60 width 4
557 h_snprintb(
558 "\177\020"
559 "f\074\004uint4\0",
560 0xf555555555555555,
561 "0xf555555555555555<uint4=0xf>");
562
563 // new style bit-field, from 60 width 5
564 //
565 // The end of the bit-field is out of bounds.
566 h_snprintb(
567 "\177\020"
568 "f\074\005uint5\0",
569 0xf555555555555555,
570 "0xf555555555555555<uint5=0xf>");
571
572 // new style bit-field, from 64 width 0
573 //
574 // The beginning of the bit-field is out of bounds, the end is fine.
575 h_snprintb_error(
576 "\177\020"
577 "f\100\000uint0\0",
578 "0#");
579
580 // new style bit-field, from 65 width 0
581 //
582 // The beginning and end of the bit-field are out of bounds.
583 h_snprintb_error(
584 "\177\020"
585 "f\101\000uint0\0",
586 "0#");
587
588 // new style bit-field, empty field description
589 //
590 // An empty field description results in an isolated '=', which is a
591 // mistake.
592 h_snprintb(
593 "\177\020"
594 "f\000\004\0"
595 "=\001one\0",
596 0x1,
597 "0x1<=0x1=one>");
598
599 // new style bit-field, non-printable description
600 //
601 // Contrary to the old-style format, the new-style format allows
602 // arbitrary characters in the description, even control characters
603 // and non-ASCII characters.
604 h_snprintb(
605 "\177\020"
606 "f\000\010\t \xC3\xA4\0"
607 "=\001\t \xC3\xA4\0"
608 "F\000\010\0"
609 ":\001\t \xC3\xA4\0"
610 "F\000\010\0"
611 "*\t \xC3\xA4\0",
612 0x1,
613 "0x1<\t \xC3\xA4=0x1=\t \xC3\xA4,\t \xC3\xA4,\t \xC3\xA4>");
614
615 // new style bit-field, '=' with empty description
616 //
617 // The description of a '=' directive should not be empty, as the
618 // outputs contains several '=' in a row.
619 h_snprintb(
620 "\177\020"
621 "f\000\004f\0"
622 "=\001one\0"
623 "=\001\0"
624 "=\001\0",
625 0x1,
626 "0x1<f=0x1=one==>");
627
628 // new style bit-field, 'F' followed by ':' with empty description
629 //
630 // An empty description of a ':' directive that doesn't match results
631 // in empty angle brackets, which is a mistake.
632 h_snprintb(
633 "\177\020"
634 "F\000\004\0"
635 ":\001\0"
636 "*default\0",
637 0x1,
638 "0x1<>");
639
640 // new style bit-field, 'F', ':' with empty description, '*'
641 //
642 // An empty description of a ':' directive that matches results in
643 // normal-looking output, but if it didn't match, the output would
644 // contain empty angle brackets, which is a mistake.
645 h_snprintb(
646 "\177\020"
647 "F\000\004\0"
648 ":\001\0"
649 "*default\0",
650 0x2,
651 "0x2<default>");
652
653 // new style bit-field, 'f' with non-exhaustive '='
654 h_snprintb(
655 "\177\020"
656 "f\000\004Field\0"
657 "=\1one\0"
658 "=\2two\0",
659 0x3,
660 "0x3<Field=0x3>");
661
662 // new style bit-field, 'F' with non-exhaustive ':'
663 //
664 // An unnamed bit-field that does not match any values generates empty
665 // angle brackets, which looks confusing. The ':' directives should
666 // either be exhaustive, or there should be a '*' catch-all directive.
667 h_snprintb(
668 "\177\020"
669 "F\000\004\0"
670 ":\1one\0"
671 ":\2two\0",
672 0x3,
673 "0x3<>");
674
675 // new style bit-field, 'F' with non-exhaustive ':'
676 //
677 // A bit-field that does not match any values generates multiple commas
678 // in a row, which looks confusing. The ':' directives should either be
679 // exhaustive, or there should be a '*' catch-all directive.
680 h_snprintb(
681 "\177\020"
682 "b\000bit0\0"
683 "F\000\004\0"
684 ":\1one\0"
685 ":\2two\0"
686 "b\001bit1\0",
687 0x3,
688 "0x3<bit0,,bit1>");
689
690 // new style bit-field, '=', can never match
691 //
692 // The extracted value from the bit-field has 7 bits and is thus less
693 // than 128, therefore it can neither match 128 nor 255.
694 h_snprintb(
695 "\177\020"
696 "f\000\007f\0"
697 "=\200never\0"
698 "=\377never\0",
699 0xff,
700 "0xff<f=0x7f>");
701
702 // new style, two separate bit-fields
703 h_snprintb(
704 "\177\020"
705 "f\000\004f1\0"
706 "=\001one\0"
707 "=\002two\0"
708 "f\004\004f2\0"
709 "=\001one\0"
710 "=\002two\0",
711 0x12,
712 "0x12<f1=0x2=two,f2=0x1=one>");
713
714 // new style, mixed named and unnamed bit-fields
715 h_snprintb(
716 "\177\020"
717 "f\000\004f1\0"
718 "=\001one\0"
719 "=\002two\0"
720 "F\010\004\0"
721 ":\015thirteen\0"
722 "f\004\004f2\0"
723 "=\001one\0"
724 "=\002two\0",
725 0x0d12,
726 "0xd12<f1=0x2=two,thirteen,f2=0x1=one>");
727
728 // new style bit-field, overlapping
729 h_snprintb(
730 "\177\020"
731 "f\000\004lo\0"
732 "f\002\004mid\0"
733 "f\004\004hi\0"
734 "f\000\010all\0",
735 0x18,
736 "0x18<lo=0x8,mid=0x6,hi=0x1,all=0x18>");
737
738 // new style bit-field, difference between '=' and ':'
739 //
740 // The ':' directive can almost emulate the '=' directive, without the
741 // numeric output and with a different separator. It's best to use
742 // either 'f' with '=', or 'F' with ':', but not mix them.
743 h_snprintb(
744 "\177\020"
745 "f\000\004field\0"
746 "=\010f-value\0"
747 "F\000\000\0" // Use an empty bit-field
748 ":\000separator\0" // to generate a separator.
749 "F\000\004\0"
750 ":\010F-value\0",
751 0x18,
752 "0x18<field=0x8=f-value,separator,F-value>");
753
754 // new style bit-field default, fixed string
755 //
756 // The 'f' directive pairs up with the '=' directive,
757 // the 'F' directive pairs up with the ':' directive,
758 // but there's only one 'default' directive for both variants,
759 // so its description should include the '=' when used with 'f' but
760 // not with 'F'.
761 h_snprintb(
762 "\177\020"
763 "f\030\010f1\0"
764 "*default\0"
765 "f\020\010f2\0"
766 "*=default\0"
767 "F\010\010\0"
768 "*default\0"
769 "F\010\010\0"
770 "*=default\0",
771 0x11223344,
772 "0x11223344<f1=0x11default,f2=0x22=default,default,=default>");
773
774 // new style bit-field default, numeric conversion specifier
775 h_snprintb(
776 "\177\020"
777 "f\010\010f\0"
778 "*=f(%ju)\0"
779 "F\000\010F\0"
780 "*F(%ju)\0",
781 0x1122,
782 "0x1122<f=0x11=f(17),F(34)>");
783
784 // new style bit-field default, can never match
785 //
786 // The '=' directive are exhaustive, making the '*' redundant.
787 h_snprintb(
788 "\177\020"
789 "f\010\002f\0"
790 "=\000zero\0"
791 "=\001one\0"
792 "=\002two\0"
793 "=\003three\0"
794 "*default\0",
795 0xff00,
796 "0xff00<f=0x3=three>");
797
798 // new style bit-field default, invalid conversion specifier
799 //
800 // There is no reliable way to make snprintf return an error, as such
801 // errors are defined as undefined behavior in the C standard.
802 // Instead, here's a conversion specifier that produces a literal '%'.
803 h_snprintb(
804 "\177\020"
805 "f\000\010f\0"
806 "*=%030ju%%\0",
807 0xff,
808 "0xff<f=0xff=000000000000000000000000000255%>");
809
810 // new style unknown directive, at the beginning
811 h_snprintb_len(
812 128,
813 "\177\020"
814 "unknown\0",
815 0xff,
816 -1,
817 "0xff#");
818
819 // new style unknown directive, after a known directive
820 h_snprintb_len(
821 128,
822 "\177\020"
823 "b\007msb\0"
824 "unknown\0",
825 0xff,
826 -1,
827 "0xff<msb#");
828
829 // new style combinations, 'b' '='
830 //
831 // A '=' directive without a preceding 'f' or 'F' directive generates
832 // misleading output outside the angle brackets, which is a mistake.
833 h_snprintb(
834 "\177\020"
835 "b\004bit4\0"
836 "=\000clear\0"
837 "=\001set\0"
838 "=\245complete\0"
839 "b\000bit0\0"
840 "=\000clear\0"
841 "=\001set\0"
842 "=\245complete\0",
843 0xa5,
844 "0xa5=complete<bit0=complete>");
845
846 // new style combinations, 'b' ':'
847 //
848 // A ':' directive without a preceding 'f' or 'F' directive generates
849 // misleading output outside or inside the angle brackets, which is a
850 // mistake.
851 h_snprintb(
852 "\177\020"
853 "b\004bit4\0"
854 ":\000clear\0"
855 ":\001set\0"
856 ":\245complete\0"
857 "b\000bit0\0"
858 ":\000clear\0"
859 ":\001set\0"
860 ":\245complete\0",
861 0xa5,
862 "0xa5complete<bit0complete>");
863
864 // new style combinations, 'b' '*'
865 //
866 // A '*' directive without a preceding 'f' or 'F' directive is ignored.
867 h_snprintb(
868 "\177\020"
869 "b\004bit4\0"
870 "*default(%ju)\0"
871 "b\000bit0\0"
872 "*default(%ju)\0",
873 0xa5,
874 "0xa5<bit0>");
875
876 // new style combinations, 'f' 'b' '='
877 //
878 // A 'b' directive that occurs between an 'f' and an '=' directive
879 // generates misleading output, which is a mistake.
880 h_snprintb(
881 "\177\020"
882 "f\000\010f\0"
883 "b\005bit5\0"
884 "=\245match\0",
885 0xa5,
886 "0xa5<f=0xa5,bit5=match>");
887
888 // new style combinations, 'f' 'b' ':'
889 //
890 // A 'b' directive that occurs between an 'f' and a ':' directive
891 // generates misleading output, which is a mistake.
892 h_snprintb(
893 "\177\020"
894 "f\000\010f\0"
895 "b\005bit5\0"
896 ":\245match\0",
897 0xa5,
898 "0xa5<f=0xa5,bit5match>");
899
900 // new style combinations, 'f' ':'
901 //
902 // Combining the 'f' directive with the ':' directive produces the
903 // misleading output '0x1one', which is a mistake.
904 h_snprintb(
905 "\177\20"
906 "f\000\004nibble\0"
907 ":\001one\0",
908 0x01,
909 "0x1<nibble=0x1one>");
910
911 // new style combinations, 'F' '='
912 //
913 // Combining the 'F' and '=' directives outputs an isolated '=', which
914 // is a mistake.
915 h_snprintb(
916 "\177\20"
917 "F\000\004\0"
918 "=\001one\0",
919 0x01,
920 "0x1<=one>");
921
922 // new style combinations, '='
923 //
924 // A '=' directive without a preceding 'f' or 'F' directive generates
925 // output that doesn't match the standard '0xaa<description>' form,
926 // which is a mistake.
927 h_snprintb(
928 "\177\020"
929 "=\245match\0",
930 0xa5,
931 "0xa5=match");
932
933 // new style combinations, ':'
934 //
935 // A ':' directive without a preceding 'f' or 'F' directive generates
936 // misleading output, which is a mistake.
937 h_snprintb(
938 "\177\020"
939 ":\245match\0",
940 0xa5,
941 "0xa5match");
942
943 // new style combinations, '*'
944 //
945 // A '*' directive without a preceding 'f' or 'F' is useless, which is
946 // a mistake.
947 h_snprintb(
948 "\177\020"
949 "*match\0",
950 0xa5,
951 "0xa5");
952
953 // new style combinations, 'f' '*' '='
954 //
955 // After a catch-all '*' directive, any following '=' directive
956 // generates misleading output, which is a mistake.
957 h_snprintb(
958 "\177\020"
959 "f\000\010f\0"
960 "*=default\0"
961 "=\245match\0",
962 0xa5,
963 "0xa5<f=0xa5=default=match>");
964
965 // new style combinations, 'F' '*' ':'
966 //
967 // After a catch-all '*' directive, any following ':' directive
968 // generates misleading output, which is a mistake.
969 h_snprintb(
970 "\177\020"
971 "F\000\010F\0"
972 "*default\0"
973 ":\245-match\0",
974 0xa5,
975 "0xa5<default-match>");
976
977 // new style combinations, '*' '*'
978 //
979 // After a catch-all '*' directive, any further '*' directive is
980 // ignored and thus redundant, which is a mistake.
981 h_snprintb(
982 "\177\020"
983 "f\000\010f\0"
984 "*=default-f\0"
985 "*ignored\0"
986 "F\000\010\0"
987 "*default-F\0"
988 "*ignored\0",
989 0xa5,
990 "0xa5<f=0xa5=default-f,default-F>");
991
992 // example from the manual page, old style octal
993 h_snprintb(
994 "\10\2BITTWO\1BITONE",
995 0x03,
996 "03<BITTWO,BITONE>");
997
998 // example from the manual page, old style hexadecimal
999 //
1000 // When using a hexadecimal escape sequence to encode a bit number,
1001 // the description must not start with a hexadecimal digit, or that
1002 // digit is interpreted as part of the bit number. To prevent this,
1003 // the bit number and the description need to be written as separate
1004 // string literals.
1005 h_snprintb(
1006 "\20"
1007 "\x10NOTBOOT" "\x0f""FPP" "\x0eSDVMA"
1008 "\x0cVIDEO" "\x0bLORES" "\x0a""FPA" "\x09""DIAG"
1009 "\x07""CACHE" "\x06IOCACHE" "\x05LOOPBACK"
1010 "\x04""DBGCACHE",
1011 0xe860,
1012 "0xe860<NOTBOOT,FPP,SDVMA,VIDEO,CACHE,IOCACHE>");
1013
1014 // example from the manual page, new style bits and fields
1015 h_snprintb(
1016 "\177\020"
1017 "b\0LSB\0" "b\1BITONE\0"
1018 "f\4\4NIBBLE2\0"
1019 "f\x10\4BURST\0" "=\4FOUR\0" "=\xf""FIFTEEN\0"
1020 "b\x1fMSB\0",
1021 0x800f0701,
1022 "0x800f0701<LSB,NIBBLE2=0,BURST=0xf=FIFTEEN,MSB>");
1023
1024 // example from the manual page, new style mmap
1025 #define MAP_FMT \
1026 "\177\020" \
1027 "b\0" "SHARED\0" \
1028 "b\1" "PRIVATE\0" \
1029 "b\2" "COPY\0" \
1030 "b\4" "FIXED\0" \
1031 "b\5" "RENAME\0" \
1032 "b\6" "NORESERVE\0" \
1033 "b\7" "INHERIT\0" \
1034 "b\11" "HASSEMAPHORE\0" \
1035 "b\12" "TRYFIXED\0" \
1036 "b\13" "WIRED\0" \
1037 "F\14\1\0" \
1038 ":\0" "FILE\0" \
1039 ":\1" "ANONYMOUS\0" \
1040 "b\15" "STACK\0" \
1041 "F\30\010\0" \
1042 ":\000" "ALIGN=NONE\0" \
1043 ":\015" "ALIGN=8KB\0" \
1044 "*" "ALIGN=2^%ju\0"
1045 h_snprintb(
1046 MAP_FMT,
1047 0x0d001234,
1048 "0xd001234<COPY,FIXED,RENAME,HASSEMAPHORE,ANONYMOUS,ALIGN=8KB>");
1049 h_snprintb(
1050 MAP_FMT,
1051 0x2e000000,
1052 "0x2e000000<FILE,ALIGN=2^46>");
1053
1054 // It is possible but cumbersome to implement a reduced variant of
1055 // rot13 using snprintb, shown here for lowercase letters only.
1056 for (char ch = 'A'; ch <= '~'; ch++) {
1057 char rot13 = ch >= 'a' && ch <= 'm' ? ch + 13
1058 : ch >= 'n' && ch <= 'z' ? ch - 13
1059 : '?';
1060 char expected[8];
1061 ATF_REQUIRE_EQ(7,
1062 snprintf(expected, sizeof(expected), "%#x<%c>", ch, rot13));
1063 h_snprintb(
1064 "\177\020"
1065 "F\000\010\0"
1066 ":an\0:bo\0:cp\0:dq\0:er\0:fs\0:gt\0:hu\0"
1067 ":iv\0:jw\0:kx\0:ly\0:mz\0"
1068 ":na\0:ob\0:pc\0:qd\0:re\0:sf\0:tg\0:uh\0"
1069 ":vi\0:wj\0:xk\0:yl\0:zm\0"
1070 // If snprintf accepted "%jc", it would be possible to
1071 // echo the non-alphabetic characters instead of a
1072 // catchall question mark.
1073 "*?\0",
1074 ch,
1075 expected);
1076 }
1077
1078 // new style, small buffers
1079 h_snprintb_len(
1080 0, "\177\020", 0x00,
1081 1, "");
1082 h_snprintb_len(
1083 1, "\177\020", 0x00,
1084 1, "");
1085 h_snprintb_len(
1086 2, "\177\020", 0x00,
1087 1, "0");
1088 h_snprintb_len(
1089 3, "\177\020", 0x00,
1090 1, "0");
1091 h_snprintb_len(
1092 3, "\177\020", 0x07,
1093 3, "0x");
1094 h_snprintb_len(
1095 4, "\177\020", 0x07,
1096 3, "0x7");
1097 h_snprintb_len(
1098 7, "\177\020b\000lsb\0", 0x07,
1099 8, "0x7<ls");
1100 h_snprintb_len(
1101 8, "\177\020b\000lsb\0", 0x07,
1102 8, "0x7<lsb");
1103 h_snprintb_len(
1104 9, "\177\020b\000lsb\0", 0x07,
1105 8, "0x7<lsb>");
1106 h_snprintb_len(
1107 9, "\177\020b\000one\0b\001two\0", 0x07,
1108 12, "0x7<one,");
1109 h_snprintb_len(
1110 10, "\177\020b\000one\0b\001two\0", 0x07,
1111 12, "0x7<one,t");
1112 h_snprintb_len(
1113 12, "\177\020b\000one\0b\001two\0", 0x07,
1114 12, "0x7<one,two");
1115 h_snprintb_len(
1116 13, "\177\020b\000one\0b\001two\0", 0x07,
1117 12, "0x7<one,two>");
1118 }
1119
1120 ATF_TC(snprintb_m);
1121 ATF_TC_HEAD(snprintb_m, tc)
1122 {
1123 atf_tc_set_md_var(tc, "descr", "Checks snprintb_m(3)");
1124 }
1125 ATF_TC_BODY(snprintb_m, tc)
1126 {
1127
1128 // old style, line_max exceeded by number in line 1
1129 h_snprintb_m(
1130 "\020",
1131 0xff,
1132 1,
1133 "#\0");
1134
1135 // old style, line_max exceeded by '<' in line 1
1136 h_snprintb_m(
1137 "\020"
1138 "\001lsb",
1139 0xff,
1140 4,
1141 "0xf#\0");
1142
1143 // old style, line_max exceeded by description
1144 h_snprintb_m(
1145 "\020"
1146 "\001bit1"
1147 "\002bit2",
1148 0xff,
1149 7,
1150 "0xff<b#\0"
1151 "0xff<b#\0");
1152
1153 // old style, line_max exceeded by '>' in line 1
1154 h_snprintb_m(
1155 "\020"
1156 "\001bit1"
1157 "\0022",
1158 0xff,
1159 9,
1160 "0xff<bit#\0"
1161 "0xff<2>\0");
1162
1163 // old style, line_max exceeded by description in line 2
1164 h_snprintb_m(
1165 "\020"
1166 "\0011"
1167 "\002bit2",
1168 0xff,
1169 8,
1170 "0xff<1>\0"
1171 "0xff<bi#\0");
1172
1173 // old style, line_max exceeded by '>' in line 2
1174 h_snprintb_m(
1175 "\020"
1176 "\0011"
1177 "\002bit2",
1178 0xff,
1179 9,
1180 "0xff<1>\0"
1181 "0xff<bit#\0");
1182
1183 // old style, complete
1184 h_snprintb_m(
1185 "\020"
1186 "\0011"
1187 "\002bit2",
1188 0xff,
1189 10,
1190 "0xff<1>\0"
1191 "0xff<bit2>\0");
1192
1193 // new style, line_max exceeded by value in line 1
1194 h_snprintb_m(
1195 "\177\020",
1196 0xff,
1197 3,
1198 "0x#\0");
1199
1200 // new style, line_max exceeded by single-bit '<' in line 1
1201 h_snprintb_m(
1202 "\177\020"
1203 "b\000bit\0",
1204 0xff,
1205 4,
1206 "0xf#\0");
1207
1208 // new style, line_max exceeded by single-bit description in line 1
1209 h_snprintb_m(
1210 "\177\020"
1211 "b\000bit0\0"
1212 "b\001two\0",
1213 0xff,
1214 8,
1215 "0xff<bi#\0"
1216 "0xff<tw#\0");
1217
1218 // new style, line_max exceeded by single-bit '>' in line 1
1219 h_snprintb_m(
1220 "\177\020"
1221 "b\000bit0\0"
1222 "b\001two\0",
1223 0xff,
1224 9,
1225 "0xff<bit#\0"
1226 "0xff<two>\0");
1227
1228 // new style, line_max exceeded by single-bit description in line 2
1229 h_snprintb_m(
1230 "\177\020"
1231 "b\000one\0"
1232 "b\001three\0",
1233 0xff,
1234 9,
1235 "0xff<one>\0"
1236 "0xff<thr#\0");
1237
1238 // new style, line_max exceeded by single-bit '>' in line 2
1239 h_snprintb_m(
1240 "\177\020"
1241 "b\000one\0"
1242 "b\001three\0",
1243 0xff,
1244 10,
1245 "0xff<one>\0"
1246 "0xff<thre#\0");
1247
1248 // new style, single-bit complete
1249 h_snprintb_m(
1250 "\177\020"
1251 "b\000one\0"
1252 "b\001three\0",
1253 0xff,
1254 11,
1255 "0xff<one>\0"
1256 "0xff<three>\0");
1257
1258 // new style, line_max exceeded by named bit-field number in line 1
1259 h_snprintb_m(
1260 "\177\020"
1261 "f\000\004lo\0",
1262 0xff,
1263 3,
1264 "0x#\0");
1265
1266 // new style, line_max exceeded by named bit-field '<' in line 1
1267 h_snprintb_m(
1268 "\177\020"
1269 "f\000\004lo\0",
1270 0xff,
1271 4,
1272 "0xf#\0");
1273
1274 // new style, line_max exceeded by bit-field description in line 1
1275 h_snprintb_m(
1276 "\177\020"
1277 "f\000\004lo\0",
1278 0xff,
1279 6,
1280 "0xff<#\0");
1281
1282 // new style, line_max exceeded by named bit-field '=' in line 1
1283 h_snprintb_m(
1284 "\177\020"
1285 "f\000\004lo\0",
1286 0xff,
1287 7,
1288 "0xff<l#\0");
1289
1290 // new style, line_max exceeded by named bit-field value in line 1
1291 h_snprintb_m(
1292 "\177\020"
1293 "f\000\004lo\0",
1294 0xff,
1295 10,
1296 "0xff<lo=0#\0");
1297
1298 // new style, line_max exceeded by named bit-field '=' in line 1
1299 h_snprintb_m(
1300 "\177\020"
1301 "f\000\004lo\0"
1302 "=\017match\0",
1303 0xff,
1304 12,
1305 "0xff<lo=0xf#\0");
1306
1307 // new style, line_max exceeded by named bit-field value description in
1308 // line 1
1309 h_snprintb_m(
1310 "\177\020"
1311 "f\000\004lo\0"
1312 "=\017match\0",
1313 0xff,
1314 16,
1315 "0xff<lo=0xf=mat#\0");
1316
1317 // new style, line_max exceeded by named bit-field '>' in line 1
1318 h_snprintb_m(
1319 "\177\020"
1320 "f\000\004lo\0"
1321 "=\017match\0",
1322 0xff,
1323 17,
1324 "0xff<lo=0xf=matc#\0");
1325
1326 // new style, line_max exceeded by named bit-field description in
1327 // line 2
1328 h_snprintb_m(
1329 "\177\020"
1330 "f\000\004lo\0"
1331 "f\000\004low-bits\0"
1332 "=\017match\0",
1333 0xff,
1334 12,
1335 "0xff<lo=0xf>\0"
1336 "0xff<low-bi#\0");
1337
1338 // new style, line_max exceeded by named bit-field '=' in line 2
1339 h_snprintb_m(
1340 "\177\020"
1341 "f\000\004lo\0"
1342 "f\000\004low-bits\0"
1343 "=\017match\0",
1344 0xff,
1345 13,
1346 "0xff<lo=0xf>\0"
1347 "0xff<low-bit#\0");
1348
1349 // new style, line_max exceeded by named bit-field value in line 2
1350 h_snprintb_m(
1351 "\177\020"
1352 "f\000\004lo\0"
1353 "f\000\004low-bits\0"
1354 "=\017match\0",
1355 0xff,
1356 16,
1357 "0xff<lo=0xf>\0"
1358 "0xff<low-bits=0#\0");
1359
1360 // new style, line_max exceeded by named bit-field '=' in line 2
1361 h_snprintb_m(
1362 "\177\020"
1363 "f\000\004lo\0"
1364 "f\000\004low-bits\0"
1365 "=\017match\0",
1366 0xff,
1367 18,
1368 "0xff<lo=0xf>\0"
1369 "0xff<low-bits=0xf#\0");
1370
1371 // new style, line_max exceeded by named bit-field value description
1372 // in line 2
1373 h_snprintb_m(
1374 "\177\020"
1375 "f\000\004lo\0"
1376 "f\000\004low-bits\0"
1377 "=\017match\0",
1378 0xff,
1379 22,
1380 "0xff<lo=0xf>\0"
1381 "0xff<low-bits=0xf=mat#\0");
1382
1383 // new style, line_max exceeded by named bit-field '>' in line 2
1384 h_snprintb_m(
1385 "\177\020"
1386 "f\000\004lo\0"
1387 "f\000\004low-bits\0"
1388 "=\017match\0",
1389 0xff,
1390 23,
1391 "0xff<lo=0xf>\0"
1392 "0xff<low-bits=0xf=matc#\0");
1393
1394 // new style, named bit-field complete
1395 h_snprintb_m(
1396 "\177\020"
1397 "f\000\004lo\0"
1398 "f\000\004low-bits\0"
1399 "=\017match\0",
1400 0xff,
1401 24,
1402 "0xff<lo=0xf>\0"
1403 "0xff<low-bits=0xf=match>\0");
1404
1405 // new style, line_max exceeded by unnamed bit-field number in line 1
1406 h_snprintb_m(
1407 "\177\020"
1408 "F\000\004\0",
1409 0xff,
1410 3,
1411 "0x#\0");
1412
1413 // new style, line_max exceeded by unnamed bit-field '<' in line 1
1414 h_snprintb_m(
1415 "\177\020"
1416 "F\000\004\0",
1417 0xff,
1418 4,
1419 "0xf#\0");
1420
1421 // new style, line_max exceeded by unnamed bit-field value description
1422 // in line 1
1423 h_snprintb_m(
1424 "\177\020"
1425 "F\000\004\0"
1426 ":\017match\0",
1427 0xff,
1428 9,
1429 "0xff<mat#\0");
1430
1431 // new style, line_max exceeded by unnamed bit-field '>' in line 1
1432 h_snprintb_m(
1433 "\177\020"
1434 "F\000\004\0"
1435 ":\017match\0",
1436 0xff,
1437 10,
1438 "0xff<matc#\0");
1439
1440 // new style, line_max exceeded by unnamed bit-field value description
1441 // in line 2
1442 h_snprintb_m(
1443 "\177\020"
1444 "F\000\004\0"
1445 ":\017m1\0"
1446 ":\017match\0",
1447 0xff,
1448 10,
1449 "0xff<m1ma#\0");
1450
1451 // new style, line_max exceeded by unnamed bit-field '>' in line 2
1452 h_snprintb_m(
1453 "\177\020"
1454 "F\000\004\0"
1455 ":\017m1\0"
1456 ":\017match\0",
1457 0xff,
1458 10,
1459 "0xff<m1ma#\0");
1460
1461 // new style unnamed bit-field complete
1462 h_snprintb_m(
1463 "\177\020"
1464 "F\000\004\0"
1465 ":\017m1\0"
1466 ":\017match\0",
1467 0xff,
1468 13,
1469 "0xff<m1match>\0");
1470
1471 // new style, line_max exceeded by bit-field default
1472 h_snprintb_m(
1473 "\177\020"
1474 "f\000\004f\0"
1475 "*=default\0",
1476 0xff,
1477 17,
1478 "0xff<f=0xf=defau#\0");
1479
1480 // new style, line_max exceeded by unmatched field value
1481 h_snprintb_m(
1482 "\177\020"
1483 "f\000\004bits\0"
1484 ":\000zero\0",
1485 0xff,
1486 11,
1487 "0xff<bits=#\0");
1488
1489 // example from the manual page, new style bits and fields
1490 h_snprintb_m(
1491 "\177\020"
1492 "b\0LSB\0"
1493 "b\1BITONE\0"
1494 "f\4\4NIBBLE2\0"
1495 "f\x10\4BURST\0"
1496 "=\4FOUR\0"
1497 "=\xf""FIFTEEN\0"
1498 "b\x1fMSB\0",
1499 0x800f0701,
1500 34,
1501 "0x800f0701<LSB,NIBBLE2=0>\0"
1502 "0x800f0701<BURST=0xf=FIFTEEN,MSB>\0");
1503
1504 // new style, buffer too small for complete number in line 2
1505 h_snprintb_m_len(
1506 15,
1507 "\177\020"
1508 "b\000lsb\0"
1509 "b\001two\0",
1510 0xff,
1511 11,
1512 20,
1513 "0xff<lsb>\0"
1514 "0xf\0"); // XXX: incomplete number may be misleading
1515
1516 // new-style format, buffer too small for '<' in line 2
1517 h_snprintb_m_len(
1518 16,
1519 "\177\020"
1520 "b\000lsb\0"
1521 "b\001two\0",
1522 0xff,
1523 11,
1524 20,
1525 "0xff<lsb>\0"
1526 "0xff\0");
1527
1528 // new-style format, buffer too small for fallback
1529 h_snprintb_m(
1530 "\177\020"
1531 "f\000\004bits\0"
1532 "*=fallback\0"
1533 "b\0024\0",
1534 0xff,
1535 64,
1536 "0xff<bits=0xf=fallback,4>\0");
1537
1538 // new-style format, buffer too small for numeric fallback
1539 h_snprintb_m_len(
1540 20,
1541 "\177\020"
1542 "F\000\004\0"
1543 "*fallback(%040jd)\0",
1544 0xff,
1545 64,
1546 57,
1547 "0xff<fallback(0000\0");
1548
1549 // new-style format, buffer too small for numeric fallback past buffer
1550 h_snprintb_m_len(
1551 15,
1552 "\177\020"
1553 "F\000\004\0"
1554 "*fallback(%010jd)\0"
1555 "F\004\004\0"
1556 "*fallback(%010jd)\0",
1557 0xff,
1558 64,
1559 48,
1560 "0xff<fallback\0");
1561
1562 // new style, bits and fields, line break between fields
1563 h_snprintb_m(
1564 "\177\020"
1565 "b\0LSB\0"
1566 "b\1_BITONE\0"
1567 "f\4\4NIBBLE2\0"
1568 "f\x10\4BURST\0"
1569 "=\04FOUR\0"
1570 "=\17FIFTEEN\0"
1571 "b\x1fMSB\0",
1572 0x800f0701,
1573 33,
1574 "0x800f0701<LSB,NIBBLE2=0>\0"
1575 "0x800f0701<BURST=0xf=FIFTEEN,MSB>\0");
1576
1577 // new style, bits and fields, line break after field description
1578 h_snprintb_m(
1579 "\177\020"
1580 "b\0LSB\0"
1581 "b\1_BITONE\0"
1582 "f\4\4NIBBLE2\0"
1583 "f\020\4BURST\0"
1584 "=\04FOUR\0"
1585 "=\17FIFTEEN\0"
1586 "b\037MSB\0",
1587 0x800f0701,
1588 32,
1589 "0x800f0701<LSB,NIBBLE2=0>\0"
1590 "0x800f0701<BURST=0xf=FIFTEEN>\0"
1591 "0x800f0701<MSB>\0");
1592 }
1593
1594 ATF_TP_ADD_TCS(tp)
1595 {
1596
1597 ATF_TP_ADD_TC(tp, snprintb);
1598 ATF_TP_ADD_TC(tp, snprintb_m);
1599
1600 return atf_no_error();
1601 }
1602