3 * Types for the test-vector framework
5 * (c) 2023 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU Library General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * mLib is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 * License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib. If not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
28 /*----- Header files ------------------------------------------------------*/
48 /*----- Preliminary utilities ---------------------------------------------*/
50 /* --- @trivial_release@ --- *
52 * Arguments: @union tvec_regval *rv@ = a register value
53 * @const struct tvec_regdef@ = the register definition
57 * Use: Does nothing. Used for register values which don't retain
61 static void trivial_release(union tvec_regval
*rv
,
62 const struct tvec_regdef
*rd
)
65 /*----- Integer utilities -------------------------------------------------*/
67 /* --- @unsigned_to_buf@, @signed_to_buf@ --- *
69 * Arguments: @buf *b@ = buffer to write on
70 * @unsigned long u@ or @long i@ = integer to write
72 * Returns: Zero on success, @-1@ on failure.
74 * Use: Write @i@ to the buffer, in big-endian (two's-complement, it
78 static int unsigned_to_buf(buf
*b
, unsigned long u
)
79 { kludge64 k
; ASSIGN64(k
, u
); return (buf_putk64l(b
, k
)); }
81 static int signed_to_buf(buf
*b
, long i
)
87 if (i
>= 0) ASSIGN64(k
, u
);
88 else { ASSIGN64(k
, ~u
); CPL64(k
, k
); }
89 return (buf_putk64l(b
, k
));
92 /* --- @unsigned_from_buf@, @signed_from_buf@ --- *
94 * Arguments: @buf *b@ = buffer to write on
95 * @unsigned long *u_out@ or @long *i_out@ = where to put the
98 * Returns: Zero on success, @-1@ on failure.
100 * Use: Read an integer, in big-endian (two's-complement, if signed)
101 * format, from the buffer.
104 static int unsigned_from_buf(buf
*b
, unsigned long *u_out
)
108 ASSIGN64(ulmax
, ULONG_MAX
);
109 if (buf_getk64l(b
, &k
)) return (-1);
110 if (CMP64(k
, >, ulmax
)) return (-1);
111 *u_out
= GET64(unsigned long, k
); return (0);
114 /* --- @hex_width@ --- *
116 * Arguments: @unsigned long u@ = an integer
118 * Returns: A suitable number of digits to use in order to display @u@ in
119 * hex. Currently, we select a power of two sufficient to show
120 * the value, but at least 2.
123 static int hex_width(unsigned long u
)
128 for (t
= u
>> 4, wd
= 4; t
>>= wd
, wd
*= 2, t
; );
132 /* --- @format_unsigned_hex@, @format_signed_hex@ --- *
134 * Arguments: @const struct gprintf_ops *gops@ = print operations
135 * @void *go@ = print destination
136 * @unsigned long u@ or @long i@ = integer to print
140 * Use: Print an unsigned or signed integer in hexadecimal.
143 static void format_unsigned_hex(const struct gprintf_ops
*gops
, void *go
,
145 { gprintf(gops
, go
, "0x%0*lx", hex_width(u
), u
); }
147 static void format_signed_hex(const struct gprintf_ops
*gops
, void *go
,
150 unsigned long u
= i
>= 0 ? i
: -(unsigned long)i
;
151 gprintf(gops
, go
, "%s0x%0*lx", i
< 0 ?
"-" : "", hex_width(u
), u
);
154 static int signed_from_buf(buf
*b
, long *i_out
)
156 kludge64 k
, lmax
, not_lmin
;
158 ASSIGN64(lmax
, LONG_MAX
); ASSIGN64(not_lmin
, ~(unsigned long)LONG_MIN
);
159 if (buf_getk64l(b
, &k
)) return (-1);
160 if (CMP64(k
, <=, lmax
)) *i_out
= (long)GET64(unsigned long, k
);
163 if (CMP64(k
, <=, not_lmin
)) *i_out
= -(long)GET64(unsigned long, k
) - 1;
169 /* --- @check_unsigned_range@, @check_signed_range@ --- *
171 * Arguments: @unsigned long u@ or @long i@ = an integer
172 * @const struct tvec_urange *ur@ or
173 * @const struct tvec_irange *ir@ = range specification,
175 * @struct tvec_state *tv@ = test vector state
177 * Returns: Zero on success, or @-1@ on error.
179 * Use: Check that the integer is within bounds. If not, report a
180 * suitable error and return a failure indication.
183 static int check_signed_range(long i
,
184 const struct tvec_irange
*ir
,
185 struct tvec_state
*tv
)
187 if (ir
&& (ir
->min
> i
|| i
> ir
->max
)) {
188 tvec_error(tv
, "integer %ld out of range (must be in [%ld .. %ld])",
189 i
, ir
->min
, ir
->max
);
195 static int check_unsigned_range(unsigned long u
,
196 const struct tvec_urange
*ur
,
197 struct tvec_state
*tv
)
199 if (ur
&& (ur
->min
> u
|| u
> ur
->max
)) {
200 tvec_error(tv
, "integer %lu out of range (must be in [%lu .. %lu])",
201 u
, ur
->min
, ur
->max
);
207 /* --- @chtodig@ --- *
209 * Arguments: @int ch@ = a character
211 * Returns: The numeric value of the character as a digit, or @-1@ if
212 * it's not a digit. Letters count as extended digits starting
213 * with value 10; case is not significant.
216 static int chtodig(int ch
)
218 if ('0' <= ch
&& ch
<= '9') return (ch
- '0');
219 else if ('a' <= ch
&& ch
<= 'z') return (ch
- 'a' + 10);
220 else if ('A' <= ch
&& ch
<= 'Z') return (ch
- 'A' + 10);
224 /* --- @parse_unsigned_integer@, @parse_signed_integer@ --- *
226 * Arguments: @unsigned long *u_out@, @long *i_out@ = where to put the
228 * @const char **q_out@ = where to put the end position
229 * @const char *p@ = pointer to the string to parse
231 * Returns: Zero on success, @-1@ on error.
233 * Use: Parse an integer from a string in the test-vector format.
234 * This is mostly extension of the traditional C @strtoul@
235 * format: supported inputs include:
237 * * NNN -- a decimal number (even if it starts with `0');
238 * * 0xNNN -- hexadecimal;
241 * * NNrNNN -- base NN.
243 * Furthermore, single underscores are permitted internally as
244 * an insignificant digit separator.
247 static int parse_unsigned_integer(unsigned long *u_out
, const char **q_out
,
254 #define f_implicit 1u /* implicitly reading base 10 */
255 #define f_digit 2u /* read a real digit */
256 #define f_uscore 4u /* found an underscore */
260 * This will deal with the traditional `0[box]...' prefixes. We'll leave
261 * our new `NNr...' syntax for later.
263 if (p
[0] != '0' || !p
[1]) {
264 d
= chtodig(*p
); if (0 > d
|| d
>= 10) return (-1);
265 r
= 10; u
= d
; p
++; f
|= f_implicit
| f_digit
;
267 u
= 0; d
= chtodig(p
[2]);
268 if (d
< 0) { r
= 10; f
|= f_implicit
| f_digit
; p
++; }
269 else if ((p
[1] == 'x' || p
[1] == 'X') && d
< 16) { r
= 16; p
+= 2; }
270 else if ((p
[1] == 'o' || p
[1] == 'O') && d
< 8) { r
= 8; p
+= 2; }
271 else if ((p
[1] == 'b' || p
[1] == 'B') && d
< 2) { r
= 2; p
+= 2; }
272 else { r
= 10; f
|= f_digit
; p
++; }
277 /* Work through the string a character at a time. */
279 ch
= *p
; switch (ch
) {
282 /* An underscore is OK if we haven't just seen one. */
284 if (f
&f_uscore
) goto done
;
285 p
++; f
= (f
&~f_implicit
) | f_uscore
;
289 /* An `r' is OK if the number so far is small enough to be a sensible
290 * base, and we're scanning decimal implicitly.
293 if (!(f
&f_implicit
) || !u
|| u
>= 36) goto done
;
294 d
= chtodig(p
[1]); if (0 > d
|| d
>= u
) goto done
;
295 r
= u
; u
= d
; f
= (f
&~f_implicit
) | f_digit
; p
+= 2; q
= p
;
299 /* Otherwise we expect a valid digit and accumulate it. */
300 d
= chtodig(ch
); if (d
< 0 || d
>= r
) goto done
;
301 if (u
> ULONG_MAX
/r
) return (-1);
302 u
*= r
; if (u
> ULONG_MAX
- d
) return (-1);
303 u
+= d
; f
= (f
&~f_uscore
) | f_digit
; p
++; q
= p
;
309 if (!(f
&f_digit
)) return (-1);
310 *u_out
= u
; *q_out
= q
; return (0);
317 static int parse_signed_integer(long *i_out
, const char **q_out
,
324 /* Read an initial sign. */
326 else if (*p
== '-') { f
|= f_neg
; p
++; }
328 /* Scan an unsigned number. */
329 if (parse_unsigned_integer(&u
, q_out
, p
)) return (-1);
331 /* Check for signed overflow and apply the sign. */
333 if (u
> LONG_MAX
) return (-1);
336 if (u
&& u
- 1 > -(LONG_MIN
+ 1)) return (-1);
337 *i_out
= u ?
-(long)(u
- 1) - 1 : 0;
345 /* --- @parse_unsigned@, @parse_signed@ --- *
347 * Arguments: @unsigned long *u_out@ or @long *i_out@ = where to put the
349 * @const char *p@ = string to parse
350 * @const struct tvec_urange *ur@ or
351 * @const struct tvec_irange *ir@ = range specification,
353 * @struct tvec_state *tv@ = test vector state
355 * Returns: Zero on success, @-1@ on error.
357 * Use: Parse and range-check an integer. Unlike @parse_(un)signed_
358 * integer@, these functions check that there's no cruft
359 * following the final digit, and report errors as they find
360 * them rather than leaving that to the caller.
363 static int parse_unsigned(unsigned long *u_out
, const char *p
,
364 const struct tvec_urange
*ur
,
365 struct tvec_state
*tv
)
370 if (parse_unsigned_integer(&u
, &q
, p
))
371 return (tvec_error(tv
, "invalid unsigned integer `%s'", p
));
372 if (*q
) return (tvec_syntax(tv
, *q
, "end-of-line"));
373 if (check_unsigned_range(u
, ur
, tv
)) return (-1);
374 *u_out
= u
; return (0);
377 static int parse_signed(long *i_out
, const char *p
,
378 const struct tvec_irange
*ir
,
379 struct tvec_state
*tv
)
384 if (parse_signed_integer(&i
, &q
, p
))
385 return (tvec_error(tv
, "invalid signed integer `%s'", p
));
386 if (*q
) return (tvec_syntax(tv
, *q
, "end-of-line"));
387 if (check_signed_range(i
, ir
, tv
)) return (-1);
388 *i_out
= i
; return (0);
391 /*----- Floating-point utilities ------------------------------------------*/
393 /* --- @eqish_floating_p@ --- *
395 * Arguments: @double x, y@ = two numbers to compare
396 * @const struct tvec_floatinfo *fi@ = floating-point info
398 * Returns: Nonzero if the comparand @y@ is sufficiently close to the
399 * reference @x@, or zero if it's definitely different.
402 static int eqish_floating_p(double x
, double y
,
403 const struct tvec_floatinfo
*fi
)
407 if (NANP(x
)) return (NANP(y
)); else if (NANP(y
)) return (0);
408 if (INFP(x
)) return (x
== y
); else if (INFP(y
)) return (0);
410 switch (fi ? fi
->f
&TVFF_EQMASK
: TVFF_EXACT
) {
412 return (x
== y
&& NEGP(x
) == NEGP(y
));
414 t
= x
- y
; if (t
< 0) t
= -t
; return (t
< fi
->delta
);
416 t
= 1.0 - y
/x
; if (t
< 0) t
= -t
; return (t
< fi
->delta
);
422 /* --- @format_floating@ --- *
424 * Arguments: @const struct gprintf_ops *gops@ = print operations
425 * @void *go@ = print destination
426 * @double x@ = number to print
430 * Use: Print a floating-point number, accurately.
433 static void format_floating(const struct gprintf_ops
*gops
, void *go
,
439 gprintf(gops
, go
, "#nan");
441 gprintf(gops
, go
, x
> 0 ?
"#+inf" : "#-inf");
443 /* Ugh. C doesn't provide any function for just printing a
444 * floating-point number /correctly/, i.e., so that you can read the
445 * result back and recover the number you first thought of. There are
446 * complicated algorithms published for doing this, but I really don't
447 * want to get into that here. So we have this.
449 * The sign doesn't cause significant difficulty so we're going to ignore
450 * it for now. So suppose we're given a number %$x = f b^e$%, in
451 * base-%$b$% format, so %$f b^n$% and %$e$% are integers, with
452 * %$0 \le f < 1$%. We're going to convert it into the nearest integer
453 * of the form %$X = F B^E$%, with similar conditions, only with the
454 * additional requirement that %$X$% is normalized, i.e., that %$X = 0$%
455 * or %$F \ge B^{-N}$%.
457 * We're rounding to the nearest such %$X$%. If there is to be ambiguity
458 * in the conversion, then some %$x = f b^e$% and the next smallest
459 * representable number %$x' = x + b^{e-n}$% must both map to the same
460 * %$X$%, which means both %$x$% and %$x'$% must be nearer to %$X$% than
461 * any other number representable in the target system. The nest larger
462 * number is %$X' = X + B^{E-N}$%; the next smaller number will normally
463 * be %$W = X - B^{E-N}$%, but if %$F = 1/B$ then the next smaller number
464 * is actually %$X - B^{E-N-1}$%. We ignore this latter possibility in
465 * the pursuit of a conservative estimate (though actually it doesn't
468 * If both %$x$% and %$x'$% map to %$X$% then we must have
469 * %$L = X - B^{E-N}/2 \le x$% and %$x + b^{e-n} \le R = X + B^{E-N}/2$%;
470 * so firstly %$f b^e = x \ge L = W + B^{E-N}/2 > W = (F - B^{-N}) B^E$%,
471 * and secondly %$b^{e-n} \le B^{E-N}$%. Since these inequalities are in
472 * opposite senses, we can divide, giving
474 * %$f b^e/b^{e-n} > (F - B^{-N}) B^E/B^{E-N}$% ,
478 * %$f b^n > (F - B^{-N}) B^N = F B^N - 1$% .
480 * Now %$f \le 1 - b^{-n}$%, and %$F \ge B^{-1}$%, so, for this to be
481 * possible, it must be the case that
483 * %$(1 - b^{-n}) b^n = b^n - 1 > B^{N-1} - 1$% .
485 * Then rearrange and take logarithms, obtaining
487 * %$(N - 1) \log B < n \log b$% ,
491 * %$N < n \log b/\log B + 1$% .
493 * Recall that this is a necessary condition for a collision to occur; we
494 * are therefore safe whenever
496 * %$N \ge n \log b/\log B + 1$% ;
498 * so, taking ceilings,
500 * %$N \ge \lceil n \log b/\log B \rceil + 1$% .
502 * So that's why we have this.
504 * I'm going to assume that @n = DBL_MANT_DIG@ is sufficiently small that
505 * we can calculate this without ending up on the wrong side of an
508 * In C11, we have @DBL_DECIMAL_DIG@, which should be the same value only
509 * as a constant. Except that modern compilers are more than clever
510 * enough to work out that this is a constant anyway.
512 * This is sometimes an overestimate: we'll print out meaningless digits
513 * that don't represent anything we actually know about the number in
514 * question. To fix that, we'd need a complicated algorithm like Steele
515 * and White's Dragon4, Gay's @dtoa@, or Burger and Dybvig's algorithm
516 * (note that Loitsch's Grisu2 is conservative, and Grisu3 hands off to
517 * something else in difficult situations).
520 prec
= ceil(DBL_MANT_DIG
*log(FLT_RADIX
)/log(10)) + 1;
521 gprintf(gops
, go
, "%.*g", prec
, x
);
525 /* --- @parse_floating@ --- *
527 * Arguments: @double *x_out@ = where to put the result
528 * @const char *p@ = string to parse
529 * @const struct tvec_floatinfo *fi@ = floating-point info
530 * @struct tvec_state *tv@ = test vector state
532 * Returns: Zero on success, @-1@ on error.
534 * Use: Parse a floating-point number from a string. Reports any
538 static int parse_floating(double *x_out
, const char *p
,
539 const struct tvec_floatinfo
*fi
,
540 struct tvec_state
*tv
)
542 const char *pp
; char *q
;
547 /* Check for special tokens. */
548 if (STRCMP(p
, ==, "#nan")) {
552 tvec_error(tv
, "NaN not supported on this system");
557 else if (STRCMP(p
, ==, "#inf") ||
558 STRCMP(p
, ==, "#+inf") || STRCMP(p
, ==, "+#inf")) {
560 x
= INFINITY
; rc
= 0;
562 tvec_error(tv
, "infinity not supported on this system");
567 else if (STRCMP(p
, ==, "#-inf") || STRCMP(p
, ==, "-#inf")) {
569 x
= -INFINITY
; rc
= 0;
571 tvec_error(tv
, "infinity not supported on this system");
576 /* Check that this looks like a number, so we can exclude `strtod'
577 * recognizing its own non-finite number tokens.
581 if (*pp
== '+' || *pp
== '-') pp
++;
582 if (*pp
== '.') pp
++;
584 tvec_syntax(tv
, *p ?
*p
: fgetc(tv
->fp
), "floating-point number");
588 /* Parse the number using the system parser. */
589 olderr
= errno
; errno
= 0;
592 tvec_syntax(tv
, *q
, "end-of-line");
595 if (errno
&& (errno
!= ERANGE
|| (x
> 0 ?
-x
: x
) == HUGE_VAL
)) {
596 tvec_error(tv
, "invalid floating-point number `%s': %s",
603 /* Check that the number is acceptable. */
604 if (NANP(x
) && fi
&& !(fi
->f
&TVFF_NANOK
)) {
605 tvec_error(tv
, "#nan not allowed here");
609 if (fi
&& ((!(fi
->f
&TVFF_NOMIN
) && x
< fi
->min
) ||
610 (!(fi
->f
&TVFF_NOMAX
) && x
> fi
->max
))) {
611 dstr_puts(&d
, "floating-point number ");
612 format_floating(&dstr_printops
, &d
, x
);
613 dstr_puts(&d
, " out of range (must be in ");
614 if (fi
->f
&TVFF_NOMIN
)
615 dstr_puts(&d
, "(#-inf");
617 { dstr_putc(&d
, '['); format_floating(&dstr_printops
, &d
, fi
->min
); }
618 dstr_puts(&d
, " .. ");
619 if (fi
->f
&TVFF_NOMAX
)
620 dstr_puts(&d
, "#+inf)");
622 { format_floating(&dstr_printops
, &d
, fi
->max
); dstr_putc(&d
, ']'); }
623 dstr_putc(&d
, ')'); dstr_putz(&d
);
624 tvec_error(tv
, "%s", d
.buf
); rc
= -1; goto end
;
634 /*----- String utilities --------------------------------------------------*/
636 /* Special character name table. */
637 static const struct chartab
{
638 const char *name
; /* character name */
639 int ch
; /* character value */
640 unsigned f
; /* flags: */
641 #define CTF_PREFER 1u /* preferred name */
642 #define CTF_SHORT 2u /* short name (compact style) */
644 { "#eof", EOF
, CTF_PREFER
| CTF_SHORT
},
645 { "#nul", '\0', CTF_PREFER
},
646 { "#bell", '\a', CTF_PREFER
},
647 { "#ding", '\a', 0 },
648 { "#bel", '\a', CTF_SHORT
},
649 { "#backspace", '\b', CTF_PREFER
},
650 { "#bs", '\b', CTF_SHORT
},
651 { "#escape", '\x1b', CTF_PREFER
},
652 { "#esc", '\x1b', CTF_SHORT
},
653 { "#formfeed", '\f', CTF_PREFER
},
654 { "#ff", '\f', CTF_SHORT
},
655 { "#newline", '\n', CTF_PREFER
},
656 { "#linefeed", '\n', 0 },
657 { "#lf", '\n', CTF_SHORT
},
659 { "#return", '\r', CTF_PREFER
},
660 { "#carriage-return", '\r', 0 },
661 { "#cr", '\r', CTF_SHORT
},
662 { "#tab", '\t', CTF_PREFER
| CTF_SHORT
},
663 { "#horizontal-tab", '\t', 0 },
665 { "#vertical-tab", '\v', CTF_PREFER
},
666 { "#vt", '\v', CTF_SHORT
},
667 { "#space", ' ', 0 },
668 { "#spc", ' ', CTF_SHORT
},
669 { "#delete", '\x7f', CTF_PREFER
},
670 { "#del", '\x7f', CTF_SHORT
},
674 /* --- @find_charname@ --- *
676 * Arguments: @int ch@ = character to match
677 * @unsigned f@ = flags (@CTF_...@) to match
679 * Returns: The name of the character, or null if no match is found.
681 * Use: Looks up a name for a character. Specifically, it returns
682 * the first entry in the @chartab@ table which matches @ch@ and
683 * which has one of the flags @f@ set.
686 static const char *find_charname(int ch
, unsigned f
)
688 const struct chartab
*ct
;
690 for (ct
= chartab
; ct
->name
; ct
++)
691 if (ct
->ch
== ch
&& (ct
->f
&f
)) return (ct
->name
);
695 /* --- @read_charname@ --- *
697 * Arguments: @int *ch_out@ = where to put the character
698 * @const char *p@ = character name
699 * @unsigned f@ = flags (@TCF_...@)
701 * Returns: Zero if a match was found, @-1@ if not.
703 * Use: Looks up a character by name. If @RCF_EOFOK@ is set in @f@,
704 * then the @EOF@ marker can be matched; otherwise it can't.
708 static int read_charname(int *ch_out
, const char *p
, unsigned f
)
710 const struct chartab
*ct
;
712 for (ct
= chartab
; ct
->name
; ct
++)
713 if (STRCMP(p
, ==, ct
->name
) && ((f
&RCF_EOFOK
) || ct
->ch
>= 0))
714 { *ch_out
= ct
->ch
; return (0); }
718 /* --- @format_charesc@ --- *
720 * Arguments: @const struct gprintf_ops *gops@ = print operations
721 * @void *go@ = print destination
722 * @int ch@ = character to format
723 * @unsigned f@ = flags (@FCF_...@)
727 * Use: Format a character as an escape sequence, possibly as part of
728 * a larger string. If @FCF_BRACE@ is set in @f@, then put
729 * braces around a `\x...' code, so that it's suitable for use
730 * in a longer string.
734 static void format_charesc(const struct gprintf_ops
*gops
, void *go
,
738 case '\a': gprintf(gops
, go
, "\\a"); break;
739 case '\b': gprintf(gops
, go
, "\\b"); break;
740 case '\x1b': gprintf(gops
, go
, "\\e"); break;
741 case '\f': gprintf(gops
, go
, "\\f"); break;
742 case '\r': gprintf(gops
, go
, "\\r"); break;
743 case '\n': gprintf(gops
, go
, "\\n"); break;
744 case '\t': gprintf(gops
, go
, "\\t"); break;
745 case '\v': gprintf(gops
, go
, "\\v"); break;
746 case '\\': gprintf(gops
, go
, "\\\\"); break;
747 case '\'': gprintf(gops
, go
, "\\'"); break;
749 if (f
&FCF_BRACE
) gprintf(gops
, go
, "\\{0}");
750 else gprintf(gops
, go
, "\\0");
754 gprintf(gops
, go
, "\\x{%0*x}", hex_width(UCHAR_MAX
), ch
);
756 gprintf(gops
, go
, "\\x%0*x", hex_width(UCHAR_MAX
), ch
);
761 /* --- @format_char@ --- *
763 * Arguments: @const struct gprintf_ops *gops@ = print operations
764 * @void *go@ = print destination
765 * @int ch@ = character to format
769 * Use: Format a single character.
772 static void format_char(const struct gprintf_ops
*gops
, void *go
, int ch
)
775 case '\\': case '\'': escape
:
776 gprintf(gops
, go
, "'");
777 format_charesc(gops
, go
, ch
, 0);
778 gprintf(gops
, go
, "'");
781 if (!isprint(ch
)) goto escape
;
782 gprintf(gops
, go
, "'%c'", ch
);
787 /* --- @maybe_format_unsigned_char@, @maybe_format_signed_char@ --- *
789 * Arguments: @const struct gprintf_ops *gops@ = print operations
790 * @void *go@ = print destination
791 * @unsigned long u@ or @long i@ = an integer
795 * Use: Format a (signed or unsigned) integer as a character, if it's
796 * in range, printing something like `= 'q''. It's assumed that
797 * a comment marker has already been output.
800 static void maybe_format_unsigned_char
801 (const struct gprintf_ops
*gops
, void *go
, unsigned long u
)
805 p
= find_charname(u
, CTF_PREFER
);
806 if (p
) gprintf(gops
, go
, " = %s", p
);
808 { gprintf(gops
, go
, " = "); format_char(gops
, go
, u
); }
811 static void maybe_format_signed_char
812 (const struct gprintf_ops
*gops
, void *go
, long i
)
816 p
= find_charname(i
, CTF_PREFER
);
817 if (p
) gprintf(gops
, go
, " = %s", p
);
818 if (0 <= i
&& i
< UCHAR_MAX
)
819 { gprintf(gops
, go
, " = "); format_char(gops
, go
, i
); }
822 /* --- @read_charesc@ --- *
824 * Arguments: @int *ch_out@ = where to put the result
825 * @struct tvec_state *tv@ = test vector state
827 * Returns: Zero on success, @-1@ on error.
829 * Use: Parse and convert an escape sequence from @tv@'s input
830 * stream, assuming that the initial `\' has already been read.
831 * Reports errors as appropriate.
834 static int read_charesc(int *ch_out
, struct tvec_state
*tv
)
843 /* Things we shouldn't find. */
844 case EOF
: case '\n': return (tvec_syntax(tv
, ch
, "string escape"));
846 /* Single-character escapes. */
847 case '\'': *ch_out
= '\''; break;
848 case '\\': *ch_out
= '\\'; break;
849 case '"': *ch_out
= '"'; break;
850 case 'a': *ch_out
= '\a'; break;
851 case 'b': *ch_out
= '\b'; break;
852 case 'e': *ch_out
= '\x1b'; break;
853 case 'f': *ch_out
= '\f'; break;
854 case 'n': *ch_out
= '\n'; break;
855 case 'r': *ch_out
= '\r'; break;
856 case 't': *ch_out
= '\t'; break;
857 case 'v': *ch_out
= '\v'; break;
859 /* Hex escapes, with and without braces. */
862 if (ch
== '{') { f
|= f_brace
; ch
= getc(tv
->fp
); }
865 if (esc
< 0 || esc
>= 16) return (tvec_syntax(tv
, ch
, "hex digit"));
867 ch
= getc(tv
->fp
); i
= chtodig(ch
); if (i
< 0 || i
>= 16) break;
870 return (tvec_error(tv
,
871 "character code %d out of range", esc
));
873 if (!(f
&f_brace
)) ungetc(ch
, tv
->fp
);
874 else if (ch
!= '}') return (tvec_syntax(tv
, ch
, "`}'"));
878 /* Other things, primarily octal escapes. */
880 f
|= f_brace
; ch
= getc(tv
->fp
);
883 if ('0' <= ch
&& ch
< '8') {
884 i
= 1; esc
= ch
- '0';
887 if ('0' > ch
|| ch
>= '8') { ungetc(ch
, tv
->fp
); break; }
888 esc
= 8*esc
+ ch
- '0';
889 i
++; if (i
>= 3) break;
893 if (ch
!= '}') return (tvec_syntax(tv
, ch
, "`}'"));
896 return (tvec_error(tv
,
897 "character code %d out of range", esc
));
898 *ch_out
= esc
; break;
900 return (tvec_syntax(tv
, ch
, "string escape"));
909 /* --- @read_quoted_string@ --- *
911 * Arguments: @dstr *d@ = string to write to
912 * @int quote@ = initial quote, `'' or `"'
913 * @struct tvec_state *tv@ = test vector state
915 * Returns: Zero on success, @-1@ on error.
917 * Use: Read the rest of a quoted string into @d@, reporting errors
920 * A single-quoted string is entirely literal. A double-quoted
921 * string may contain C-like escapes.
924 static int read_quoted_string(dstr
*d
, int quote
, struct tvec_state
*tv
)
932 return (tvec_syntax(tv
, ch
, "`%c'", quote
));
934 if (quote
== '\'') goto ordinary
;
935 ch
= getc(tv
->fp
); if (ch
== '\n') { tv
->lno
++; break; }
936 ungetc(ch
, tv
->fp
); if (read_charesc(&ch
, tv
)) return (-1);
939 if (ch
== quote
) goto end
;
951 /* --- @collect_bare@ --- *
953 * Arguments: @dstr *d@ = string to write to
954 * @struct tvec_state *tv@ = test vector state
956 * Returns: Zero on success, @-1@ on error.
958 * Use: Read barewords and the whitespace between them. Stop when we
959 * encounter something which can't start a bareword.
962 static int collect_bare(dstr
*d
, struct tvec_state
*tv
)
965 enum { WORD
, SPACE
, ESCAPE
}; unsigned s
= WORD
;
972 tvec_syntax(tv
, ch
, "bareword");
975 if (s
== ESCAPE
) { tv
->lno
++; goto addch
; }
976 if (s
== WORD
) pos
= d
->len
;
977 ungetc(ch
, tv
->fp
); if (tvec_nexttoken(tv
)) { rc
= -1; goto end
; }
978 DPUTC(d
, ' '); s
= SPACE
;
980 case '"': case '\'': case '!': case '#': case ')': case '}': case ']':
981 if (s
== SPACE
) { ungetc(ch
, tv
->fp
); goto done
; }
987 if (s
!= ESCAPE
&& isspace(ch
)) {
988 if (s
== WORD
) pos
= d
->len
;
989 DPUTC(d
, ch
); s
= SPACE
;
993 DPUTC(d
, ch
); s
= WORD
;
998 if (s
== SPACE
) d
->len
= pos
;
1004 /* --- @set_up_encoding@ --- *
1006 * Arguments: @const codec_class **ccl_out@ = where to put the class
1007 * @unsigned *f_out@ = where to put the flags
1008 * @unsigned code@ = the coding scheme to use (@TVEC_...@)
1012 * Use: Helper for @read_compound_string@ below.
1014 * Return the appropriate codec class and flags for @code@.
1015 * Leaves @*ccl_out@ null if the coding scheme doesn't have a
1016 * backing codec class (e.g., @TVCODE_BARE@).
1019 enum { TVCODE_BARE
, TVCODE_HEX
, TVCODE_BASE64
, TVCODE_BASE32
};
1020 static void set_up_encoding(const codec_class
**ccl_out
, unsigned *f_out
,
1025 *ccl_out
= 0; *f_out
= 0;
1028 *ccl_out
= &hex_class
; *f_out
= CDCF_IGNCASE
;
1031 *ccl_out
= &base32_class
; *f_out
= CDCF_IGNCASE
| CDCF_IGNEQPAD
;
1034 *ccl_out
= &base64_class
; *f_out
= CDCF_IGNEQPAD
;
1041 /* --- @flush_codec@ --- *
1043 * Arguments: @codec *cdc@ = a codec, or null
1044 * @dstr *d@ = output string
1045 * @struct tvec_state *tv@ = test vector state
1047 * Returns: Zero on success, @-1@ on error.
1049 * Use: Helper for @read_compound_string@ below.
1051 * Flush out any final buffered material from @cdc@, and check
1052 * that it's in a good state. Frees the codec on success. Does
1053 * nothing if @cdc@ is null.
1056 static int flush_codec(codec
*cdc
, dstr
*d
, struct tvec_state
*tv
)
1061 err
= cdc
->ops
->code(cdc
, 0, 0, d
);
1063 return (tvec_error(tv
, "invalid %s sequence end: %s",
1064 cdc
->ops
->c
->name
, codec_strerror(err
)));
1065 cdc
->ops
->destroy(cdc
);
1070 /* --- @read_compound_string@ --- *
1072 * Arguments: @void **p_inout@ = address of output buffer pointer
1073 * @size_t *sz_inout@ = address of buffer size
1074 * @unsigned code@ = initial interpretation of barewords
1075 * @unsigned f@ = other flags (@RCSF_...@)
1076 * @struct tvec_state *tv@ = test vector state
1078 * Returns: Zero on success, @-1@ on error.
1080 * Use: Parse a compound string, i.e., a sequence of stringish pieces
1081 * which might be quoted strings, character names, or barewords
1082 * to be decoded accoding to @code@, interspersed with
1083 * additional directives.
1085 * If the initial buffer pointer is non-null and sufficiently
1086 * large, then it will be reused; otherwise, it is freed and a
1087 * fresh, sufficiently large buffer is allocated and returned.
1090 #define RCSF_NESTED 1u
1091 static int read_compound_string(void **p_inout
, size_t *sz_inout
,
1092 unsigned code
, unsigned f
,
1093 struct tvec_state
*tv
)
1095 const codec_class
*ccl
; unsigned cdf
;
1097 dstr d
= DSTR_INIT
, w
= DSTR_INIT
;
1100 void *pp
= 0; size_t sz
;
1104 set_up_encoding(&ccl
, &cdf
, code
); cdc
= 0;
1106 if (tvec_nexttoken(tv
)) return (tvec_syntax(tv
, fgetc(tv
->fp
), "string"));
1111 case ')': case ']': case '}':
1112 /* Close brackets. Leave these for recursive caller if there is one,
1116 if (!(f
&RCSF_NESTED
))
1117 { rc
= tvec_syntax(tv
, ch
, "string"); goto end
; }
1118 ungetc(ch
, tv
->fp
); goto done
;
1120 case '"': case '\'':
1121 /* Quotes. Read a quoted string. */
1123 if (cdc
&& flush_codec(cdc
, &d
, tv
)) { rc
= -1; goto end
; }
1125 if (read_quoted_string(&d
, ch
, tv
)) { rc
= -1; goto end
; }
1129 /* A named character. */
1132 if (cdc
&& flush_codec(cdc
, &d
, tv
)) { rc
= -1; goto end
; }
1134 DRESET(&w
); tvec_readword(tv
, &w
, ";", "character name");
1135 if (read_charname(&ch
, w
.buf
, RCF_EOFOK
)) {
1136 rc
= tvec_error(tv
, "unknown character name `%s'", d
.buf
);
1139 DPUTC(&d
, ch
); break;
1142 /* A magic keyword. */
1144 if (cdc
&& flush_codec(cdc
, &d
, tv
)) { rc
= -1; goto end
; }
1147 DRESET(&w
); tvec_readword(tv
, &w
, ";", "`!'-keyword");
1149 /* Change bareword coding system. */
1150 if (STRCMP(w
.buf
, ==, "!bare"))
1151 { code
= TVCODE_BARE
; set_up_encoding(&ccl
, &cdf
, code
); }
1152 else if (STRCMP(w
.buf
, ==, "!hex"))
1153 { code
= TVCODE_HEX
; set_up_encoding(&ccl
, &cdf
, code
); }
1154 else if (STRCMP(w
.buf
, ==, "!base32"))
1155 { code
= TVCODE_BASE32
; set_up_encoding(&ccl
, &cdf
, code
); }
1156 else if (STRCMP(w
.buf
, ==, "!base64"))
1157 { code
= TVCODE_BASE64
; set_up_encoding(&ccl
, &cdf
, code
); }
1159 /* Repeated substrings. */
1160 else if (STRCMP(w
.buf
, ==, "!repeat")) {
1161 if (tvec_nexttoken(tv
)) {
1162 rc
= tvec_syntax(tv
, fgetc(tv
->fp
), "repeat count");
1166 if (tvec_readword(tv
, &w
, ";{", "repeat count"))
1167 { rc
= -1; goto end
; }
1168 if (parse_unsigned_integer(&n
, &q
, w
.buf
)) {
1169 rc
= tvec_error(tv
, "invalid repeat count `%s'", w
.buf
);
1172 if (*q
) { rc
= tvec_syntax(tv
, *q
, "`{'"); goto end
; }
1173 if (tvec_nexttoken(tv
))
1174 { rc
= tvec_syntax(tv
, fgetc(tv
->fp
), "`{'"); goto end
; }
1175 ch
= getc(tv
->fp
); if (ch
!= '{')
1176 { rc
= tvec_syntax(tv
, ch
, "`{'"); goto end
; }
1178 if (read_compound_string(&pp
, &sz
, code
, f
| RCSF_NESTED
, tv
))
1179 { rc
= -1; goto end
; }
1180 ch
= getc(tv
->fp
); if (ch
!= '}')
1181 { rc
= tvec_syntax(tv
, ch
, "`}'"); goto end
; }
1183 if (n
> (size_t)-1/sz
)
1184 { rc
= tvec_error(tv
, "repeat size out of range"); goto end
; }
1185 dstr_ensure(&d
, n
*sz
);
1187 { memset(d
.buf
+ d
.len
, *(unsigned char *)pp
, n
); d
.len
+= n
; }
1189 for (; n
--; d
.len
+= sz
) memcpy(d
.buf
+ d
.len
, pp
, sz
);
1194 /* Anything else is an error. */
1196 tvec_error(tv
, "unknown string keyword `%s'", w
.buf
);
1202 /* A bareword. Process it according to the current coding system. */
1207 if (collect_bare(&d
, tv
)) goto done
;
1211 ungetc(ch
, tv
->fp
); DRESET(&w
);
1212 if (tvec_readword(tv
, &w
, ";", "%s-encoded fragment", ccl
->name
))
1213 { rc
= -1; goto end
; }
1214 if (!cdc
) cdc
= ccl
->decoder(cdf
);
1215 err
= cdc
->ops
->code(cdc
, w
.buf
, w
.len
, &d
);
1217 tvec_error(tv
, "invalid %s fragment `%s': %s",
1218 ccl
->name
, w
.buf
, codec_strerror(err
));
1225 } while (!tvec_nexttoken(tv
));
1228 /* Wrap things up. */
1229 if (cdc
&& flush_codec(cdc
, &d
, tv
)) { rc
= -1; goto end
; }
1231 if (*sz_inout
<= d
.len
)
1232 { xfree(*p_inout
); *p_inout
= xmalloc(d
.len
+ 1); }
1233 p
= *p_inout
; memcpy(p
, d
.buf
, d
.len
); p
[d
.len
] = 0; *sz_inout
= d
.len
;
1237 /* Clean up any debris. */
1238 if (cdc
) cdc
->ops
->destroy(cdc
);
1240 dstr_destroy(&d
); dstr_destroy(&w
);
1244 /*----- Signed and unsigned integer types ---------------------------------*/
1246 /* --- @init_int@, @init_uint@ --- *
1248 * Arguments: @union tvec_regval *rv@ = register value
1249 * @const struct tvec_regdef *rd@ = register definition
1253 * Use: Initialize a register value.
1255 * Integer values are initialized to zero.
1258 static void init_int(union tvec_regval
*rv
, const struct tvec_regdef
*rd
)
1261 static void init_uint(union tvec_regval
*rv
, const struct tvec_regdef
*rd
)
1264 /* --- @eq_int@, @eq_uint@ --- *
1266 * Arguments: @const union tvec_regval *rv0, *rv1@ = register values
1267 * @const struct tvec_regdef *rd@ = register definition
1269 * Returns: Nonzero if the values are equal, zero if unequal
1271 * Use: Compare register values for equality.
1274 static int eq_int(const union tvec_regval
*rv0
, const union tvec_regval
*rv1
,
1275 const struct tvec_regdef
*rd
)
1276 { return (rv0
->i
== rv1
->i
); }
1278 static int eq_uint(const union tvec_regval
*rv0
,
1279 const union tvec_regval
*rv1
,
1280 const struct tvec_regdef
*rd
)
1281 { return (rv0
->u
== rv1
->u
); }
1283 /* --- @tobuf_int@, @tobuf_uint@ --- *
1285 * Arguments: @buf *b@ = buffer
1286 * @const union tvec_regval *rv@ = register value
1287 * @const struct tvec_regdef *rd@ = register definition
1289 * Returns: Zero on success, %$-1$% on failure.
1291 * Use: Serialize a register value to a buffer.
1293 * Integer values are serialized as little-endian 64-bit signed
1294 * or unsigned integers.
1297 static int tobuf_int(buf
*b
, const union tvec_regval
*rv
,
1298 const struct tvec_regdef
*rd
)
1299 { return (signed_to_buf(b
, rv
->i
)); }
1301 static int tobuf_uint(buf
*b
, const union tvec_regval
*rv
,
1302 const struct tvec_regdef
*rd
)
1303 { return (unsigned_to_buf(b
, rv
->u
)); }
1305 /* --- @frombuf_int@, @frombuf_uint@ --- *
1307 * Arguments: @buf *b@ = buffer
1308 * @union tvec_regval *rv@ = register value
1309 * @const struct tvec_regdef *rd@ = register definition
1311 * Returns: Zero on success, %$-1$% on failure.
1313 * Use: Deserialize a register value from a buffer.
1315 * Integer values are serialized as 64-bit signed or unsigned
1319 static int frombuf_int(buf
*b
, union tvec_regval
*rv
,
1320 const struct tvec_regdef
*rd
)
1321 { return (signed_from_buf(b
, &rv
->i
)); }
1323 static int frombuf_uint(buf
*b
, union tvec_regval
*rv
,
1324 const struct tvec_regdef
*rd
)
1325 { return (unsigned_from_buf(b
, &rv
->u
)); }
1327 /* --- @parse_int@, @parse_uint@ --- *
1329 * Arguments: @union tvec_regval *rv@ = register value
1330 * @const struct tvec_regdef *rd@ = register definition
1331 * @struct tvec_state *tv@ = test-vector state
1333 * Returns: Zero on success, %$-1$% on error.
1335 * Use: Parse a register value from an input file.
1337 * Integers may be input in decimal, hex, binary, or octal,
1338 * following approximately usual conventions.
1340 * * Signed integers may be preceded with a `+' or `-' sign.
1342 * * Decimal integers are just a sequence of decimal digits
1345 * * Octal integers are a sequence of digits `0' ... `7',
1346 * preceded by `0o' or `0O'.
1348 * * Hexadecimal integers are a sequence of digits `0'
1349 * ... `9', `a' ... `f', or `A' ... `F', preceded by `0x' or
1352 * * Radix-B integers are a sequence of digits `0' ... `9',
1353 * `a' ... `f', or `A' ... `F', each with value less than B,
1354 * preceded by `Br' or `BR', where 0 < B < 36 is expressed
1355 * in decimal without any leading `0' or internal
1358 * * A digit sequence may contain internal underscore `_'
1359 * separators, but not before or after all of the digits;
1360 * and two consecutive `_' characters are not permitted.
1363 static int parse_int(union tvec_regval
*rv
, const struct tvec_regdef
*rd
,
1364 struct tvec_state
*tv
)
1369 if (tvec_readword(tv
, &d
, ";", "signed integer")) { rc
= -1; goto end
; }
1370 if (parse_signed(&rv
->i
, d
.buf
, rd
->arg
.p
, tv
)) { rc
= -1; goto end
; }
1371 if (tvec_flushtoeol(tv
, 0)) { rc
= -1; goto end
; }
1378 static int parse_uint(union tvec_regval
*rv
, const struct tvec_regdef
*rd
,
1379 struct tvec_state
*tv
)
1384 if (tvec_readword(tv
, &d
, ";", "unsigned integer")) { rc
= -1; goto end
; }
1385 if (parse_unsigned(&rv
->u
, d
.buf
, rd
->arg
.p
, tv
)) { rc
= -1; goto end
; }
1386 if (tvec_flushtoeol(tv
, 0)) { rc
= -1; goto end
; }
1393 /* --- @dump_int@, @dump_uint@ --- *
1395 * Arguments: @const union tvec_regval *rv@ = register value
1396 * @const struct tvec_regdef *rd@ = register definition
1397 * @unsigned style@ = output style (@TVSF_...@)
1398 * @const struct gprintf_ops *gops@, @void *gp@ = format output
1402 * Use: Dump a register value to the format output.
1404 * Integer values are dumped in decimal and, unless compact
1405 * output is requested, hex, and maybe a character, as a
1409 static void dump_int(const union tvec_regval
*rv
,
1410 const struct tvec_regdef
*rd
,
1412 const struct gprintf_ops
*gops
, void *go
)
1415 gprintf(gops
, go
, "%ld", rv
->i
);
1416 if (!(style
&TVSF_COMPACT
)) {
1417 gprintf(gops
, go
, " ; = ");
1418 format_signed_hex(gops
, go
, rv
->i
);
1419 maybe_format_signed_char(gops
, go
, rv
->i
);
1423 static void dump_uint(const union tvec_regval
*rv
,
1424 const struct tvec_regdef
*rd
,
1426 const struct gprintf_ops
*gops
, void *go
)
1428 gprintf(gops
, go
, "%lu", rv
->u
);
1429 if (!(style
&TVSF_COMPACT
)) {
1430 gprintf(gops
, go
, " ; = ");
1431 format_unsigned_hex(gops
, go
, rv
->u
);
1432 maybe_format_unsigned_char(gops
, go
, rv
->u
);
1436 /* Integer type definitions. */
1437 const struct tvec_regty tvty_int
= {
1438 init_int
, trivial_release
, eq_int
,
1439 tobuf_int
, frombuf_int
,
1442 const struct tvec_regty tvty_uint
= {
1443 init_uint
, trivial_release
, eq_uint
,
1444 tobuf_uint
, frombuf_uint
,
1445 parse_uint
, dump_uint
1448 /* Predefined integer ranges. */
1449 const struct tvec_irange
1450 tvrange_schar
= { SCHAR_MIN
, SCHAR_MAX
},
1451 tvrange_short
= { SHRT_MIN
, SHRT_MAX
},
1452 tvrange_int
= { INT_MIN
, INT_MAX
},
1453 tvrange_long
= { LONG_MIN
, LONG_MAX
},
1454 tvrange_sbyte
= { -128, 127 },
1455 tvrange_i16
= { -32768, +32767 },
1456 tvrange_i32
= { -2147483648, 2147483647 };
1457 const struct tvec_urange
1458 tvrange_uchar
= { 0, UCHAR_MAX
},
1459 tvrange_ushort
= { 0, USHRT_MAX
},
1460 tvrange_uint
= { 0, UINT_MAX
},
1461 tvrange_ulong
= { 0, ULONG_MAX
},
1462 tvrange_size
= { 0, (size_t)-1 },
1463 tvrange_byte
= { 0, 255 },
1464 tvrange_u16
= { 0, 65535 },
1465 tvrange_u32
= { 0, 4294967296 };
1467 /* --- @tvec_claimeq_int@ --- *
1469 * Arguments: @struct tvec_state *tv@ = test-vector state
1470 * @long i0, i1@ = two signed integers
1471 * @const char *file@, @unsigned @lno@ = calling file and line
1472 * @const char *expr@ = the expression to quote on failure
1474 * Returns: Nonzero if @i0@ and @i1@ are equal, otherwise zero.
1476 * Use: Check that values of @i0@ and @i1@ are equal. As for
1477 * @tvec_claim@ above, a test case is automatically begun and
1478 * ended if none is already underway. If the values are
1479 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
1480 * mismatched values are dumped: @i0@ is printed as the output
1481 * value and @i1@ is printed as the input reference.
1484 int tvec_claimeq_int(struct tvec_state
*tv
, long i0
, long i1
,
1485 const char *file
, unsigned lno
, const char *expr
)
1487 tv
->out
[0].v
.i
= i0
; tv
->in
[0].v
.i
= i1
;
1488 return (tvec_claimeq(tv
, &tvty_int
, 0, file
, lno
, expr
));
1491 /* --- @tvec_claimeq_uint@ --- *
1493 * Arguments: @struct tvec_state *tv@ = test-vector state
1494 * @unsigned long u0, u1@ = two unsigned integers
1495 * @const char *file@, @unsigned @lno@ = calling file and line
1496 * @const char *expr@ = the expression to quote on failure
1498 * Returns: Nonzero if @u0@ and @u1@ are equal, otherwise zero.
1500 * Use: Check that values of @u0@ and @u1@ are equal. As for
1501 * @tvec_claim@ above, a test case is automatically begun and
1502 * ended if none is already underway. If the values are
1503 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
1504 * mismatched values are dumped: @u0@ is printed as the output
1505 * value and @u1@ is printed as the input reference.
1508 int tvec_claimeq_uint(struct tvec_state
*tv
,
1509 unsigned long u0
, unsigned long u1
,
1510 const char *file
, unsigned lno
, const char *expr
)
1512 tv
->out
[0].v
.u
= u0
; tv
->in
[0].v
.u
= u1
;
1513 return (tvec_claimeq(tv
, &tvty_uint
, 0, file
, lno
, expr
));
1516 /*----- Floating-point type -----------------------------------------------*/
1518 /* --- @float_int@ --- *
1520 * Arguments: @union tvec_regval *rv@ = register value
1521 * @const struct tvec_regdef *rd@ = register definition
1525 * Use: Initialize a register value.
1527 * Floating-point values are initialized to zero.
1530 static void init_float(union tvec_regval
*rv
, const struct tvec_regdef
*rd
)
1533 /* --- @eq_float@ --- *
1535 * Arguments: @const union tvec_regval *rv0, *rv1@ = register values
1536 * @const struct tvec_regdef *rd@ = register definition
1538 * Returns: Nonzero if the values are equal, zero if unequal
1540 * Use: Compare register values for equality.
1542 * Floating-point values may be considered equal if their
1543 * absolute or relative difference is sufficiently small, as
1544 * described in the register definition.
1547 static int eq_float(const union tvec_regval
*rv0
,
1548 const union tvec_regval
*rv1
,
1549 const struct tvec_regdef
*rd
)
1550 { return (eqish_floating_p(rv0
->f
, rv1
->f
, rd
->arg
.p
)); }
1552 /* --- @tobuf_float@ --- *
1554 * Arguments: @buf *b@ = buffer
1555 * @const union tvec_regval *rv@ = register value
1556 * @const struct tvec_regdef *rd@ = register definition
1558 * Returns: Zero on success, %$-1$% on failure.
1560 * Use: Serialize a register value to a buffer.
1562 * Floating-point values are serialized as little-endian
1563 * IEEE 754 Binary64.
1566 static int tobuf_float(buf
*b
, const union tvec_regval
*rv
,
1567 const struct tvec_regdef
*rd
)
1568 { return (buf_putf64l(b
, rv
->f
)); }
1570 /* --- @frombuf_float@ --- *
1572 * Arguments: @buf *b@ = buffer
1573 * @union tvec_regval *rv@ = register value
1574 * @const struct tvec_regdef *rd@ = register definition
1576 * Returns: Zero on success, %$-1$% on failure.
1578 * Use: Deserialize a register value from a buffer.
1580 * Floating-point values are serialized as little-endian
1581 * IEEE 754 Binary64.
1584 static int frombuf_float(buf
*b
, union tvec_regval
*rv
,
1585 const struct tvec_regdef
*rd
)
1586 { return (buf_getf64l(b
, &rv
->f
)); }
1588 /* --- @parse_float@ --- *
1590 * Arguments: @union tvec_regval *rv@ = register value
1591 * @const struct tvec_regdef *rd@ = register definition
1592 * @struct tvec_state *tv@ = test-vector state
1594 * Returns: Zero on success, %$-1$% on error.
1596 * Use: Parse a register value from an input file.
1598 * Floating-point values are either NaN (%|#nan|%, if supported
1599 * by the platform); positive or negative infinity (%|#inf|%,
1600 * %|+#inf|%, or %|#+inf|% (preferring the last), and %|-#inf|%
1601 * or %|#-inf|% (preferring the latter), if supported by the
1602 * platform); or a number in strtod(3) syntax.
1605 static int parse_float(union tvec_regval
*rv
, const struct tvec_regdef
*rd
,
1606 struct tvec_state
*tv
)
1611 if (tvec_readword(tv
, &d
, ";", "floating-point number"))
1612 { rc
= -1; goto end
; }
1613 if (parse_floating(&rv
->f
, d
.buf
, rd
->arg
.p
, tv
)) { rc
= -1; goto end
; }
1614 if (tvec_flushtoeol(tv
, 0)) { rc
= -1; goto end
; }
1621 /* --- @dump_float@ --- *
1623 * Arguments: @const union tvec_regval *rv@ = register value
1624 * @const struct tvec_regdef *rd@ = register definition
1625 * @unsigned style@ = output style (@TVSF_...@)
1626 * @const struct gprintf_ops *gops@, @void *gp@ = format output
1630 * Use: Dump a register value to the format output.
1632 * Floating-point values are dumped in decimal or as a special
1633 * token beginning with `%|#|%'. Some effort is taken to ensure
1634 * that the output is sufficient to uniquely identify the
1635 * original value, but, honestly, C makes this really hard.
1638 static void dump_float(const union tvec_regval
*rv
,
1639 const struct tvec_regdef
*rd
,
1641 const struct gprintf_ops
*gops
, void *go
)
1642 { format_floating(gops
, go
, rv
->f
); }
1644 /* Floating-point type definition. */
1645 const struct tvec_regty tvty_float
= {
1646 init_float
, trivial_release
, eq_float
,
1647 tobuf_float
, frombuf_float
,
1648 parse_float
, dump_float
1651 /* Predefined floating-point ranges. */
1652 const struct tvec_floatinfo
1653 tvflt_finite
= { TVFF_EXACT
, -DBL_MAX
, DBL_MAX
, 0.0 },
1654 tvflt_nonneg
= { TVFF_EXACT
, 0, DBL_MAX
, 0.0 };
1656 /* --- @tvec_claimeqish_float@ --- *
1658 * Arguments: @struct tvec_state *tv@ = test-vector state
1659 * @double f0, f1@ = two floating-point numbers
1660 * @unsigned f@ = flags (@TVFF_...@)
1661 * @double delta@ = maximum tolerable difference
1662 * @const char *file@, @unsigned @lno@ = calling file and line
1663 * @const char *expr@ = the expression to quote on failure
1665 * Returns: Nonzero if @f0@ and @u1@ are sufficiently close, otherwise
1668 * Use: Check that values of @f0@ and @f1@ are sufficiently close.
1669 * As for @tvec_claim@ above, a test case is automatically begun
1670 * and ended if none is already underway. If the values are
1671 * too far apart, then @tvec_fail@ is called, quoting @expr@,
1672 * and the mismatched values are dumped: @f0@ is printed as the
1673 * output value and @f1@ is printed as the input reference.
1675 * The details for the comparison are as follows.
1677 * * A NaN value matches any other NaN, and nothing else.
1679 * * An infinity matches another infinity of the same sign,
1682 * * If @f&TVFF_EQMASK@ is @TVFF_EXACT@, then any
1683 * representable number matches only itself: in particular,
1684 * positive and negative zero are considered distinct.
1685 * (This allows tests to check that they land on the correct
1686 * side of branch cuts, for example.)
1688 * * If @f&TVFF_EQMASK@ is @TVFF_ABSDELTA@, then %$x$% matches
1689 * %$y$% when %$|x - y| < \delta$%.
1691 * * If @f&TVFF_EQMASK@ is @TVFF_RELDELTA@, then %$x$% matches
1692 * %$y$% when %$|1 - y/x| < \delta$%. (Note that this
1693 * criterion is asymmetric FIXME
1696 int tvec_claimeqish_float(struct tvec_state
*tv
,
1697 double f0
, double f1
, unsigned f
, double delta
,
1698 const char *file
, unsigned lno
,
1701 struct tvec_floatinfo fi
;
1702 union tvec_misc arg
;
1704 fi
.f
= f
; fi
.min
= fi
.max
= 0.0; fi
.delta
= delta
; arg
.p
= &fi
;
1705 tv
->out
[0].v
.f
= f0
; tv
->in
[0].v
.f
= f1
;
1706 return (tvec_claimeq(tv
, &tvty_float
, &arg
, file
, lno
, expr
));
1709 /* --- @tvec_claimeq_float@ --- *
1711 * Arguments: @struct tvec_state *tv@ = test-vector state
1712 * @double f0, f1@ = two floating-point numbers
1713 * @const char *file@, @unsigned @lno@ = calling file and line
1714 * @const char *expr@ = the expression to quote on failure
1716 * Returns: Nonzero if @f0@ and @u1@ are identical, otherwise zero.
1718 * Use: Check that values of @f0@ and @f1@ are identical. The
1719 * function is exactly equivalent to @tvec_claimeqish_float@
1720 * with @f == TVFF_EXACT@.
1723 int tvec_claimeq_float(struct tvec_state
*tv
,
1724 double f0
, double f1
,
1725 const char *file
, unsigned lno
,
1728 return (tvec_claimeqish_float(tv
, f0
, f1
, TVFF_EXACT
, 0.0,
1732 /*----- Enumerations ------------------------------------------------------*/
1734 /* --- @init_tenum@ --- *
1736 * Arguments: @union tvec_regval *rv@ = register value
1737 * @const struct tvec_regdef *rd@ = register definition
1741 * Use: Initialize a register value.
1743 * Integer and floating-point enumeration values are initialized
1744 * as their underlying representations. Pointer enumerations
1745 * are initialized to %|#nil|%.
1748 #define init_ienum init_int
1749 #define init_uenum init_uint
1750 #define init_fenum init_float
1752 static void init_penum(union tvec_regval
*rv
, const struct tvec_regdef
*rd
)
1755 /* --- @eq_tenum@ --- *
1757 * Arguments: @const union tvec_regval *rv0, *rv1@ = register values
1758 * @const struct tvec_regdef *rd@ = register definition
1760 * Returns: Nonzero if the values are equal, zero if unequal
1762 * Use: Compare register values for equality.
1764 * Integer and floating-point enumeration values are compared as
1765 * their underlying representations; in particular, floating-
1766 * point enumerations may compare equal if their absolute or
1767 * relative difference is sufficiently small. Pointer
1768 * enumerations are compared as pointers.
1771 #define eq_ienum eq_int
1772 #define eq_uenum eq_uint
1774 static int eq_fenum(const union tvec_regval
*rv0
,
1775 const union tvec_regval
*rv1
,
1776 const struct tvec_regdef
*rd
)
1778 const struct tvec_fenuminfo
*ei
= rd
->arg
.p
;
1779 return (eqish_floating_p(rv0
->f
, rv1
->f
, ei
->fi
));
1782 static int eq_penum(const union tvec_regval
*rv0
,
1783 const union tvec_regval
*rv1
,
1784 const struct tvec_regdef
*rd
)
1785 { return (rv0
->p
== rv1
->p
); }
1787 /* --- @tobuf_tenum@ --- *
1789 * Arguments: @buf *b@ = buffer
1790 * @const union tvec_regval *rv@ = register value
1791 * @const struct tvec_regdef *rd@ = register definition
1793 * Returns: Zero on success, %$-1$% on failure.
1795 * Use: Serialize a register value to a buffer.
1797 * Integer and floating-point enumeration values are serialized
1798 * as their underlying representations. Pointer enumerations
1799 * are serialized as the signed integer index into the
1800 * association table; %|#nil|% serializes as %$-1$%, and
1801 * unrecognized pointers cause failure.
1804 #define tobuf_ienum tobuf_int
1805 #define tobuf_uenum tobuf_uint
1806 #define tobuf_fenum tobuf_float
1808 static int tobuf_penum(buf
*b
, const union tvec_regval
*rv
,
1809 const struct tvec_regdef
*rd
)
1811 const struct tvec_penuminfo
*pei
= rd
->arg
.p
;
1812 const struct tvec_passoc
*pa
;
1815 for (pa
= pei
->av
, i
= 0; pa
->tag
; pa
++, i
++)
1816 if (pa
->p
== rv
->p
) goto found
;
1820 return (signed_to_buf(b
, i
));
1823 /* --- @frombuf_tenum@ --- *
1825 * Arguments: @buf *b@ = buffer
1826 * @union tvec_regval *rv@ = register value
1827 * @const struct tvec_regdef *rd@ = register definition
1829 * Returns: Zero on success, %$-1$% on failure.
1831 * Use: Deserialize a register value from a buffer.
1833 * Integer and floating-point enumeration values are serialized
1834 * as their underlying representations. Pointer enumerations
1835 * are serialized as the signed integer index into the
1836 * association table; %|#nil|% serializes as %$-1$%; out-of-
1837 * range indices cause failure.
1840 #define frombuf_ienum frombuf_int
1841 #define frombuf_uenum frombuf_uint
1842 #define frombuf_fenum frombuf_float
1843 static int frombuf_penum(buf
*b
, union tvec_regval
*rv
,
1844 const struct tvec_regdef
*rd
)
1846 const struct tvec_penuminfo
*pei
= rd
->arg
.p
;
1847 const struct tvec_passoc
*pa
;
1850 for (pa
= pei
->av
, n
= 0; pa
->tag
; pa
++, n
++);
1851 if (signed_from_buf(b
, &i
)) return (-1);
1852 if (0 <= i
&& i
< n
) rv
->p
= (/*unconst*/ void *)pei
->av
[i
].p
;
1853 else if (i
== -1) rv
->p
= 0;
1858 /* --- @parse_tenum@ --- *
1860 * Arguments: @union tvec_regval *rv@ = register value
1861 * @const struct tvec_regdef *rd@ = register definition
1862 * @struct tvec_state *tv@ = test-vector state
1864 * Returns: Zero on success, %$-1$% on error.
1866 * Use: Parse a register value from an input file.
1868 * An enumerated value may be given by name or as a literal
1869 * value. For enumerations based on numeric types, the literal
1870 * values can be written in the same syntax as the underlying
1871 * values. For enumerations based on pointers, the only
1872 * permitted literal is %|#nil|%, which denotes a null pointer.
1875 #define DEFPARSE_ENUM(tag_, ty, slot) \
1876 static int parse_##slot##enum(union tvec_regval *rv, \
1877 const struct tvec_regdef *rd, \
1878 struct tvec_state *tv) \
1880 const struct tvec_##slot##enuminfo *ei = rd->arg.p; \
1881 const struct tvec_##slot##assoc *a; \
1882 dstr d = DSTR_INIT; \
1885 if (tvec_readword(tv, &d, ";", "enumeration tag or " LITSTR_##tag_)) \
1886 { rc = -1; goto end; } \
1887 for (a = ei->av; a->tag; a++) \
1888 if (STRCMP(a->tag, ==, d.buf)) { FOUND_##tag_ goto done; } \
1891 if (tvec_flushtoeol(tv, 0)) { rc = -1; goto end; } \
1898 #define LITSTR_INT "literal signed integer"
1899 #define FOUND_INT rv->i = a->i;
1900 #define MISSING_INT if (parse_signed(&rv->i, d.buf, ei->ir, tv)) \
1901 { rc = -1; goto end; }
1903 #define LITSTR_UINT "literal unsigned integer"
1904 #define FOUND_UINT rv->u = a->u;
1905 #define MISSING_UINT if (parse_unsigned(&rv->u, d.buf, ei->ur, tv)) \
1906 { rc = -1; goto end; }
1908 #define LITSTR_FLT "literal floating-point number, " \
1909 "`#-inf', `#+inf', or `#nan'"
1910 #define FOUND_FLT rv->f = a->f;
1911 #define MISSING_FLT if (parse_floating(&rv->f, d.buf, ei->fi, tv)) \
1912 { rc = -1; goto end; }
1914 #define LITSTR_PTR "`#nil'"
1915 #define FOUND_PTR rv->p = (/*unconst*/ void *)a->p;
1916 #define MISSING_PTR if (STRCMP(d.buf, ==, "#nil")) \
1919 tvec_error(tv, "unknown `%s' value `%s'", \
1921 rc = -1; goto end; \
1924 TVEC_MISCSLOTS(DEFPARSE_ENUM
)
1942 #undef DEFPARSE_ENUM
1944 /* --- @dump_tenum@ --- *
1946 * Arguments: @const union tvec_regval *rv@ = register value
1947 * @const struct tvec_regdef *rd@ = register definition
1948 * @unsigned style@ = output style (@TVSF_...@)
1949 * @const struct gprintf_ops *gops@, @void *gp@ = format output
1953 * Use: Dump a register value to the format output.
1955 * Enumeration values are dumped as their symbolic names, if
1956 * possible, with the underlying values provided as a comment
1957 * unless compact output is requested, as for the underlying
1958 * representation. A null pointer is printed as %|#nil|%;
1959 * non-null pointers are printed as %|#<TYPE PTR>|%, with the
1960 * enumeration TYPE and the raw pointer PTR printed with the
1961 * system's %|%p|% format specifier.
1965 #define DEFDUMP_ENUM(tag_, ty, slot) \
1966 static void dump_##slot##enum(const union tvec_regval *rv, \
1967 const struct tvec_regdef *rd, \
1969 const struct gprintf_ops *gops, void *go) \
1971 const struct tvec_##slot##enuminfo *ei = rd->arg.p; \
1972 const struct tvec_##slot##assoc *a; \
1974 for (a = ei->av; a->tag; a++) \
1975 if (rv->slot == a->slot) { \
1976 gprintf(gops, go, "%s", a->tag); \
1977 if (style&TVSF_COMPACT) return; \
1978 gprintf(gops, go, " ; = "); break; \
1984 #define MAYBE_PRINT_EXTRA \
1985 if (style&TVSF_COMPACT) /* nothing to do */; \
1986 else if (!a->tag) { gprintf(gops, go, " ; = "); goto _extra; } \
1987 else if (1) { gprintf(gops, go, " = "); goto _extra; } \
1990 #define PRINTRAW_INT gprintf(gops, go, "%ld", rv->i); \
1991 MAYBE_PRINT_EXTRA { \
1992 format_signed_hex(gops, go, rv->i); \
1993 maybe_format_signed_char(gops, go, rv->i); \
1996 #define PRINTRAW_UINT gprintf(gops, go, "%lu", rv->u); \
1997 MAYBE_PRINT_EXTRA { \
1998 format_unsigned_hex(gops, go, rv->u); \
1999 maybe_format_unsigned_char(gops, go, rv->u); \
2002 #define PRINTRAW_FLT format_floating(gops, go, rv->f);
2004 #define PRINTRAW_PTR if (!rv->p) gprintf(gops, go, "#nil"); \
2005 else gprintf(gops, go, "#<%s %p>", ei->name, rv->p);
2007 TVEC_MISCSLOTS(DEFDUMP_ENUM
)
2010 #undef PRINTRAW_UINT
2014 #undef MAYBE_PRINT_EXTRA
2017 /* Enumeration type definitions. */
2018 #define DEFTY_ENUM(tag, ty, slot) \
2019 const struct tvec_regty tvty_##slot##enum = { \
2020 init_##slot##enum, trivial_release, eq_##slot##enum, \
2021 tobuf_##slot##enum, frombuf_##slot##enum, \
2022 parse_##slot##enum, dump_##slot##enum \
2024 TVEC_MISCSLOTS(DEFTY_ENUM
)
2027 /* Predefined enumeration types. */
2028 static const struct tvec_iassoc bool_assoc
[] = {
2045 const struct tvec_ienuminfo tvenum_bool
=
2046 { "bool", bool_assoc
, &tvrange_int
};
2048 static const struct tvec_iassoc cmp_assoc
[] = {
2064 const struct tvec_ienuminfo tvenum_cmp
=
2065 { "cmp", cmp_assoc
, &tvrange_int
};
2067 /* --- @tvec_claimeq_tenum@ --- *
2069 * Arguments: @struct tvec_state *tv@ = test-vector state
2070 * @const struct tvec_typeenuminfo *ei@ = enumeration type info
2071 * @ty t0, t1@ = two values
2072 * @const char *file@, @unsigned @lno@ = calling file and line
2073 * @const char *expr@ = the expression to quote on failure
2075 * Returns: Nonzero if @t0@ and @t1@ are equal, otherwise zero.
2077 * Use: Check that values of @t0@ and @t1@ are equal. As for
2078 * @tvec_claim@ above, a test case is automatically begun and
2079 * ended if none is already underway. If the values are
2080 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
2081 * mismatched values are dumped: @t0@ is printed as the output
2082 * value and @t1@ is printed as the input reference.
2085 #define DEFCLAIM(tag, ty, slot) \
2086 int tvec_claimeq_##slot##enum \
2087 (struct tvec_state *tv, \
2088 const struct tvec_##slot##enuminfo *ei, ty e0, ty e1, \
2089 const char *file, unsigned lno, const char *expr) \
2091 union tvec_misc arg; \
2094 tv->out[0].v.slot = GET_##tag(e0); \
2095 tv->in[0].v.slot = GET_##tag(e1); \
2096 return (tvec_claimeq(tv, &tvty_##slot##enum, &arg, \
2097 file, lno, expr)); \
2099 #define GET_INT(e) (e)
2100 #define GET_UINT(e) (e)
2101 #define GET_FLT(e) (e)
2102 #define GET_PTR(e) ((/*unconst*/ void *)(e))
2103 TVEC_MISCSLOTS(DEFCLAIM
)
2110 /*----- Flag types --------------------------------------------------------*/
2112 /* Flag types are initialized, compared, and serialized as unsigned
2116 /* --- @parse_flags@ --- *
2118 * Arguments: @union tvec_regval *rv@ = register value
2119 * @const struct tvec_regdef *rd@ = register definition
2120 * @struct tvec_state *tv@ = test-vector state
2122 * Returns: Zero on success, %$-1$% on error.
2124 * Use: Parse a register value from an input file.
2126 * The input syntax is a sequence of items separated by `|'
2127 * signs. Each item may be the symbolic name of a field value,
2128 * or a literal unsigned integer. The masks associated with the
2129 * given symbolic names must be disjoint. The resulting
2130 * numerical value is simply the bitwise OR of the given values.
2133 static int parse_flags(union tvec_regval
*rv
, const struct tvec_regdef
*rd
,
2134 struct tvec_state
*tv
)
2136 const struct tvec_flaginfo
*fi
= rd
->arg
.p
;
2137 const struct tvec_flag
*f
;
2138 unsigned long m
= 0, v
= 0, t
;
2144 /* Read the next item. */
2146 if (tvec_readword(tv
, &d
, "|;", "flag name or integer"))
2147 { rc
= -1; goto end
; }
2149 /* Try to find a matching entry in the table. */
2150 for (f
= fi
->fv
; f
->tag
; f
++)
2151 if (STRCMP(f
->tag
, ==, d
.buf
)) {
2153 { tvec_error(tv
, "colliding flag setting"); rc
= -1; goto end
; }
2155 { m
|= f
->m
; v
|= f
->v
; goto next
; }
2158 /* Otherwise, try to parse it as a raw integer. */
2159 if (parse_unsigned(&t
, d
.buf
, fi
->range
, tv
))
2160 { rc
= -1; goto end
; }
2164 /* Advance to the next token. If it's a separator then consume it, and
2165 * go round again. Otherwise we stop here.
2167 if (tvec_nexttoken(tv
)) break;
2169 if (ch
!= '|') { tvec_syntax(tv
, ch
, "`|'"); rc
= -1; goto end
; }
2170 if (tvec_nexttoken(tv
))
2171 { tvec_syntax(tv
, '\n', "flag name or integer"); rc
= -1; goto end
; }
2181 /* --- @dump_flags@ --- *
2183 * Arguments: @const union tvec_regval *rv@ = register value
2184 * @const struct tvec_regdef *rd@ = register definition
2185 * @unsigned style@ = output style (@TVSF_...@)
2186 * @const struct gprintf_ops *gops@, @void *gp@ = format output
2190 * Use: Dump a register value to the format output.
2192 * The table of symbolic names and their associated values and
2193 * masks is repeatedly scanned, in order, to find disjoint
2194 * matches -- i.e., entries whose value matches the target value
2195 * in the bit positions indicated by the mask, and whose mask
2196 * doesn't overlap with any previously found matches; the names
2197 * are then output, separated by `|'. Any remaining nonzero
2198 * bits not covered by any of the matching masks are output as a
2199 * single literal integer, in hex.
2201 * Unless compact output is requested, or no symbolic names were
2202 * found, the raw numeric value is also printed in hex, as a
2206 static void dump_flags(const union tvec_regval
*rv
,
2207 const struct tvec_regdef
*rd
,
2209 const struct gprintf_ops
*gops
, void *go
)
2211 const struct tvec_flaginfo
*fi
= rd
->arg
.p
;
2212 const struct tvec_flag
*f
;
2213 unsigned long m
= ~0ul, v
= rv
->u
;
2216 for (f
= fi
->fv
, sep
= ""; f
->tag
; f
++)
2217 if ((m
&f
->m
) && (v
&f
->m
) == f
->v
) {
2218 gprintf(gops
, go
, "%s%s", sep
, f
->tag
); m
&= ~f
->m
;
2219 sep
= style
&TVSF_COMPACT ?
"|" : " | ";
2222 if (v
&m
) gprintf(gops
, go
, "%s0x%0*lx", sep
, hex_width(v
), v
&m
);
2224 if (m
!= ~0ul && !(style
&TVSF_COMPACT
))
2225 gprintf(gops
, go
, " ; = 0x%0*lx", hex_width(rv
->u
), rv
->u
);
2228 /* Flags type definition. */
2229 const struct tvec_regty tvty_flags
= {
2230 init_uint
, trivial_release
, eq_uint
,
2231 tobuf_uint
, frombuf_uint
,
2232 parse_flags
, dump_flags
2235 /* --- @tvec_claimeq_flags@ --- *
2237 * Arguments: @struct tvec_state *tv@ = test-vector state
2238 * @const struct tvec_flaginfo *fi@ = flags type info
2239 * @unsigned long f0, f1@ = two values
2240 * @const char *file@, @unsigned @lno@ = calling file and line
2241 * @const char *expr@ = the expression to quote on failure
2243 * Returns: Nonzero if @f0@ and @f1@ are equal, otherwise zero.
2245 * Use: Check that values of @f0@ and @f1@ are equal. As for
2246 * @tvec_claim@ above, a test case is automatically begun and
2247 * ended if none is already underway. If the values are
2248 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
2249 * mismatched values are dumped: @f0@ is printed as the output
2250 * value and @f1@ is printed as the input reference.
2253 int tvec_claimeq_flags(struct tvec_state
*tv
,
2254 const struct tvec_flaginfo
*fi
,
2255 unsigned long f0
, unsigned long f1
,
2256 const char *file
, unsigned lno
, const char *expr
)
2258 union tvec_misc arg
;
2260 arg
.p
= fi
; tv
->out
[0].v
.u
= f0
; tv
->in
[0].v
.u
= f1
;
2261 return (tvec_claimeq(tv
, &tvty_flags
, &arg
, file
, lno
, expr
));
2264 /*----- Characters --------------------------------------------------------*/
2266 /* Character values are initialized and compared as signed integers. */
2268 /* --- @tobuf_char@ --- *
2270 * Arguments: @buf *b@ = buffer
2271 * @const union tvec_regval *rv@ = register value
2272 * @const struct tvec_regdef *rd@ = register definition
2274 * Returns: Zero on success, %$-1$% on failure.
2276 * Use: Serialize a register value to a buffer.
2278 * Character values are serialized as little-endian 32-bit
2279 * unsigned integers, with %|EOF|% serialized as all-bits-set.
2282 static int tobuf_char(buf
*b
, const union tvec_regval
*rv
,
2283 const struct tvec_regdef
*rd
)
2287 if (0 <= rv
->i
&& rv
->i
<= UCHAR_MAX
) u
= rv
->i
;
2288 else if (rv
->i
== EOF
) u
= MASK32
;
2290 return (buf_putu32l(b
, u
));
2293 /* --- @frombuf_char@ --- *
2295 * Arguments: @buf *b@ = buffer
2296 * @union tvec_regval *rv@ = register value
2297 * @const struct tvec_regdef *rd@ = register definition
2299 * Returns: Zero on success, %$-1$% on failure.
2301 * Use: Deserialize a register value from a buffer.
2303 * Character values are serialized as little-endian 32-bit
2304 * unsigned integers, with %|EOF|% serialized as all-bits-set.
2307 static int frombuf_char(buf
*b
, union tvec_regval
*rv
,
2308 const struct tvec_regdef
*rd
)
2312 if (buf_getu32l(b
, &u
)) return (-1);
2313 if (0 <= u
&& u
<= UCHAR_MAX
) rv
->i
= u
;
2314 else if (u
== MASK32
) rv
->i
= EOF
;
2319 /* --- @parse_char@ --- *
2321 * Arguments: @union tvec_regval *rv@ = register value
2322 * @const struct tvec_regdef *rd@ = register definition
2323 * @struct tvec_state *tv@ = test-vector state
2325 * Returns: Zero on success, %$-1$% on error.
2327 * Use: Parse a register value from an input file.
2329 * A character value can be given by symbolic name, with a
2330 * leading `%|#|%'; or a character or `%|\|%'-escape sequence,
2331 * optionally in single quotes.
2333 * The following escape sequences and character names are
2336 * * `%|#eof|%' is the special end-of-file marker.
2338 * * `%|#nul|%' is the NUL character, sometimes used to
2339 * terminate strings.
2341 * * `%|bell|%', `%|bel|%', `%|ding|%', or `%|\a|%' is the BEL
2342 * character used to ring the terminal bell (or do some other
2343 * thing to attract the user's attention).
2345 * * %|#backspace|%, %|#bs|%, or %|\b|% is the backspace
2346 * character, used to move the cursor backwords by one cell.
2348 * * %|#escape|% %|#esc|%, or%|\e|% is the escape character,
2349 * used to introduce special terminal commands.
2351 * * %|#formfeed|%, %|#ff|%, or %|\f|% is the formfeed
2352 * character, used to separate pages of text.
2354 * * %|#newline|%, %|#linefeed|%, %|#lf|%, %|#nl|%, or %|\n|% is
2355 * the newline character, used to terminate lines of text or
2356 * advance the cursor to the next line (perhaps without
2357 * returning it to the start of the line).
2359 * * %|#return|%, %|#carriage-return|%, %|#cr|%, or %|\r|% is
2360 * the carriage-return character, used to return the cursor to
2361 * the start of the line.
2363 * * %|#tab|%, %|#horizontal-tab|%, %|#ht|%, or %|\t|% is the
2364 * tab character, used to advance the cursor to the next tab
2365 * stop on the current line.
2367 * * %|#vertical-tab|%, %|#vt|%, %|\v|% is the vertical tab
2370 * * %|#space|%, %|#spc|% is the space character.
2372 * * %|#delete|%, %|#del|% is the delete character, used to
2373 * erase the most recent character.
2375 * * %|\'|% is the single-quote character.
2377 * * %|\\|% is the backslash character.
2379 * * %|\"|% is the double-quote character.
2381 * * %|\NNN|% or %|\{NNN}|% is the character with code NNN in
2382 * octal. The NNN may be up to three digits long.
2384 * * %|\xNN|% or %|\x{NN}|% is the character with code NNN in
2388 static int parse_char(union tvec_regval
*rv
, const struct tvec_regdef
*rd
,
2389 struct tvec_state
*tv
)
2396 /* Inspect the character to see what we're up against. */
2400 /* It looks like a special token. Push the `%|#|%' back and fetch the
2401 * whole word. If there's just the `%|#|%' after all, then treat it as
2406 if (tvec_readword(tv
, &d
, ";", "character name")) { rc
= -1; goto end
; }
2407 if (STRCMP(d
.buf
, !=, "#")) {
2408 if (read_charname(&ch
, d
.buf
, RCF_EOFOK
)) {
2409 rc
= tvec_error(tv
, "unknown character name `%s'", d
.buf
);
2412 if (tvec_flushtoeol(tv
, 0)) { rc
= -1; goto end
; }
2413 rv
->i
= ch
; rc
= 0; goto end
;
2417 /* If this is a single quote then we expect to see a matching one later,
2418 * and we should process backslash escapes. Get the next character and see
2421 if (ch
== '\'') { f
|= f_quote
; ch
= getc(tv
->fp
); }
2423 /* Main character dispatch. */
2427 /* Unquoted, semicolon begins a comment. */
2428 if (!(f
&f_quote
)) { rc
= tvec_syntax(tv
, ch
, "character"); goto end
; }
2432 /* A newline. If we saw a single quote, then treat that as literal.
2433 * Otherwise this is an error.
2435 if (!(f
&f_quote
)) goto nochar
;
2436 else { f
&= ~f_quote
; ungetc(ch
, tv
->fp
); ch
= '\''; goto plain
; }
2439 /* End-of-file. Similar to newline, but with slightly different
2440 * effects on the parse state.
2442 if (!(f
&f_quote
)) goto nochar
;
2443 else { f
&= ~f_quote
; ch
= '\''; goto plain
; }
2446 /* A single quote. This must be the second of a pair, and there should
2447 * have been a character or escape sequence between them.
2449 rc
= tvec_syntax(tv
, ch
, "character"); goto end
;
2452 /* A backslash. Read a character escape. */
2453 if (read_charesc(&ch
, tv
)) return (-1);
2456 /* Anything else. Treat as literal. */
2460 /* If we saw an opening quote, then expect the closing quote. */
2463 if (ch
!= '\'') { rc
= tvec_syntax(tv
, ch
, "`''"); goto end
; }
2467 if (tvec_flushtoeol(tv
, 0)) { rc
= -1; goto end
; }
2476 /* --- @dump_char@ --- *
2478 * Arguments: @const union tvec_regval *rv@ = register value
2479 * @const struct tvec_regdef *rd@ = register definition
2480 * @unsigned style@ = output style (@TVSF_...@)
2481 * @const struct gprintf_ops *gops@, @void *gp@ = format output
2485 * Use: Dump a register value to the format output.
2487 * Character values are dumped as their symbolic names, if any,
2488 * or as a character or escape sequence within single quotes
2489 * (which may be omitted in compact style). If compact output
2490 * is not requested, then the single-quoted representation (for
2491 * characters dumped as symbolic names) and integer code in
2492 * decimal and hex are printed as a comment.
2495 static void dump_char(const union tvec_regval
*rv
,
2496 const struct tvec_regdef
*rd
,
2498 const struct gprintf_ops
*gops
, void *go
)
2504 /* Print a character name if we can find one. */
2505 p
= find_charname(rv
->i
, (style
&TVSF_COMPACT
) ? CTF_SHORT
: CTF_PREFER
);
2507 gprintf(gops
, go
, "%s", p
);
2508 if (style
&TVSF_COMPACT
) return;
2509 else { gprintf(gops
, go
, " ;"); f
|= f_semi
; }
2512 /* If the character isn't @EOF@ then print it as a single-quoted thing.
2513 * In compact style, see if we can omit the quotes.
2516 if (f
&f_semi
) gprintf(gops
, go
, " = ");
2518 case ' ': case '\\': case '\'': quote
:
2519 format_char(gops
, go
, rv
->i
);
2522 if (!(style
&TVSF_COMPACT
) || !isprint(rv
->i
)) goto quote
;
2523 gprintf(gops
, go
, "%c", (int)rv
->i
);
2528 /* And the character code as an integer. */
2529 if (!(style
&TVSF_COMPACT
)) {
2530 if (!(f
&f_semi
)) gprintf(gops
, go
, " ;");
2531 gprintf(gops
, go
, " = %ld = ", rv
->i
);
2532 format_signed_hex(gops
, go
, rv
->i
);
2538 /* Character type definition. */
2539 const struct tvec_regty tvty_char
= {
2540 init_int
, trivial_release
, eq_int
,
2541 tobuf_char
, frombuf_char
,
2542 parse_char
, dump_char
2545 /* --- @tvec_claimeq_char@ --- *
2547 * Arguments: @struct tvec_state *tv@ = test-vector state
2548 * @int ch0, ch1@ = two character codes
2549 * @const char *file@, @unsigned @lno@ = calling file and line
2550 * @const char *expr@ = the expression to quote on failure
2552 * Returns: Nonzero if @ch0@ and @ch1@ are equal, otherwise zero.
2554 * Use: Check that values of @ch0@ and @ch1@ are equal. As for
2555 * @tvec_claim@ above, a test case is automatically begun and
2556 * ended if none is already underway. If the values are
2557 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
2558 * mismatched values are dumped: @ch0@ is printed as the output
2559 * value and @ch1@ is printed as the input reference.
2562 int tvec_claimeq_char(struct tvec_state
*tv
, int c0
, int c1
,
2563 const char *file
, unsigned lno
, const char *expr
)
2565 tv
->out
[0].v
.i
= c0
; tv
->in
[0].v
.i
= c1
;
2566 return (tvec_claimeq(tv
, &tvty_char
, 0, file
, lno
, expr
));
2569 /*----- Text and byte strings ---------------------------------------------*/
2571 /* --- @init_text@, @init_bytes@ --- *
2573 * Arguments: @union tvec_regval *rv@ = register value
2574 * @const struct tvec_regdef *rd@ = register definition
2578 * Use: Initialize a register value.
2580 * Text and binary string values are initialized with a null
2581 * pointer and zero length.
2584 static void init_text(union tvec_regval
*rv
, const struct tvec_regdef
*rd
)
2585 { rv
->text
.p
= 0; rv
->text
.sz
= 0; }
2587 static void init_bytes(union tvec_regval
*rv
, const struct tvec_regdef
*rd
)
2588 { rv
->bytes
.p
= 0; rv
->bytes
.sz
= 0; }
2590 /* --- @release_string@, @release_bytes@ --- *
2592 * Arguments: @const union tvec_regval *rv@ = register value
2593 * @const struct tvec_regdef *rd@ = register definition
2597 * Use: Release resources held by a register value.
2599 * Text and binary string buffers are freed.
2602 static void release_text(union tvec_regval
*rv
,
2603 const struct tvec_regdef
*rd
)
2604 { xfree(rv
->text
.p
); }
2606 static void release_bytes(union tvec_regval
*rv
,
2607 const struct tvec_regdef
*rd
)
2608 { xfree(rv
->bytes
.p
); }
2610 /* --- @eq_text@, @eq_bytes@ --- *
2612 * Arguments: @const union tvec_regval *rv0, *rv1@ = register values
2613 * @const struct tvec_regdef *rd@ = register definition
2615 * Returns: Nonzero if the values are equal, zero if unequal
2617 * Use: Compare register values for equality.
2620 static int eq_text(const union tvec_regval
*rv0
,
2621 const union tvec_regval
*rv1
,
2622 const struct tvec_regdef
*rd
)
2624 return (rv0
->text
.sz
== rv1
->text
.sz
&&
2626 MEMCMP(rv0
->text
.p
, ==, rv1
->text
.p
, rv1
->text
.sz
)));
2629 static int eq_bytes(const union tvec_regval
*rv0
,
2630 const union tvec_regval
*rv1
,
2631 const struct tvec_regdef
*rd
)
2633 return (rv0
->bytes
.sz
== rv1
->bytes
.sz
&&
2635 MEMCMP(rv0
->bytes
.p
, ==, rv1
->bytes
.p
, rv1
->bytes
.sz
)));
2638 /* --- @tobuf_text@, @tobuf_bytes@ --- *
2640 * Arguments: @buf *b@ = buffer
2641 * @const union tvec_regval *rv@ = register value
2642 * @const struct tvec_regdef *rd@ = register definition
2644 * Returns: Zero on success, %$-1$% on failure.
2646 * Use: Serialize a register value to a buffer.
2648 * Text and binary string values are serialized as a little-
2649 * endian 64-bit length %$n$% in bytes followed by %$n$% bytes
2653 static int tobuf_text(buf
*b
, const union tvec_regval
*rv
,
2654 const struct tvec_regdef
*rd
)
2655 { return (buf_putmem64l(b
, rv
->text
.p
, rv
->text
.sz
)); }
2657 static int tobuf_bytes(buf
*b
, const union tvec_regval
*rv
,
2658 const struct tvec_regdef
*rd
)
2659 { return (buf_putmem64l(b
, rv
->bytes
.p
, rv
->bytes
.sz
)); }
2661 /* --- @frombuf_text@, @frombuf_bytes@ --- *
2663 * Arguments: @buf *b@ = buffer
2664 * @union tvec_regval *rv@ = register value
2665 * @const struct tvec_regdef *rd@ = register definition
2667 * Returns: Zero on success, %$-1$% on failure.
2669 * Use: Deserialize a register value from a buffer.
2671 * Text and binary string values are serialized as a little-
2672 * endian 64-bit length %$n$% in bytes followed by %$n$% bytes
2676 static int frombuf_text(buf
*b
, union tvec_regval
*rv
,
2677 const struct tvec_regdef
*rd
)
2682 p
= buf_getmem64l(b
, &sz
); if (!p
) return (-1);
2683 tvec_alloctext(rv
, sz
); memcpy(rv
->text
.p
, p
, sz
); rv
->text
.p
[sz
] = 0;
2687 static int frombuf_bytes(buf
*b
, union tvec_regval
*rv
,
2688 const struct tvec_regdef
*rd
)
2693 p
= buf_getmem64l(b
, &sz
); if (!p
) return (-1);
2694 tvec_allocbytes(rv
, sz
); memcpy(rv
->bytes
.p
, p
, sz
);
2698 /* --- @check_string_length@ --- *
2700 * Arguments: @size_t sz@ = found string length
2701 * @const struct tvec_urange *ur@ = acceptable range
2702 * @struct tvec_state *tv@ = test-vector state
2704 * Returns: Zero on success, %$-1$% on error.
2706 * Use: Checks that @sz@ is within the bounds described by @ur@,
2707 * reporting an error if not.
2710 static int check_string_length(size_t sz
, const struct tvec_urange
*ur
,
2711 struct tvec_state
*tv
)
2713 if (ur
&& (ur
->min
> sz
|| sz
> ur
->max
))
2714 return (tvec_error(tv
,
2715 "invalid string length %lu; must be in [%lu .. %lu]",
2716 (unsigned long)sz
, ur
->min
, ur
->max
));
2720 /* --- @parse_text@, @parse_bytes@ --- *
2722 * Arguments: @union tvec_regval *rv@ = register value
2723 * @const struct tvec_regdef *rd@ = register definition
2724 * @struct tvec_state *tv@ = test-vector state
2726 * Returns: Zero on success, %$-1$% on error.
2728 * Use: Parse a register value from an input file.
2730 * The input format for both kinds of strings is basically the
2731 * same: a `compound string', consisting of
2733 * * single-quoted strings, which are interpreted entirely
2734 * literally, but can't contain single quotes or newlines;
2736 * * double-quoted strings, in which `%|\|%'-escapes are
2737 * interpreted as for characters;
2739 * * character names, marked by an initial `%|#|%' sign;
2741 * * special tokens marked by an initial `%|!|%' sign; or
2743 * * barewords interpreted according to the current coding
2746 * The special tokens are
2748 * * `%|!bare|%', which causes subsequent sequences of
2749 * barewords to be treated as plain text;
2751 * * `%|!hex|%', `%|!base32|%', `%|!base64|%', which cause
2752 * subsequent barewords to be decoded in the requested
2755 * * `%|!repeat|% %$n$% %|{|% %%\textit{string}%% %|}|%',
2756 * which includes %$n$% copies of the (compound) string.
2758 * The only difference between text and binary strings is that
2759 * the initial coding scheme is %|bare|% for text strings and
2760 * %|hex|% for binary strings.
2763 static int parse_text(union tvec_regval
*rv
, const struct tvec_regdef
*rd
,
2764 struct tvec_state
*tv
)
2766 void *p
= rv
->text
.p
;
2768 if (read_compound_string(&p
, &rv
->text
.sz
, TVCODE_BARE
, 0, tv
))
2771 if (check_string_length(rv
->text
.sz
, rd
->arg
.p
, tv
)) return (-1);
2775 static int parse_bytes(union tvec_regval
*rv
, const struct tvec_regdef
*rd
,
2776 struct tvec_state
*tv
)
2778 void *p
= rv
->bytes
.p
;
2780 if (read_compound_string(&p
, &rv
->bytes
.sz
, TVCODE_HEX
, 0, tv
))
2783 if (check_string_length(rv
->bytes
.sz
, rd
->arg
.p
, tv
)) return (-1);
2787 /* --- @dump_text@, @dump_bytes@ --- *
2789 * Arguments: @const union tvec_regval *rv@ = register value
2790 * @const struct tvec_regdef *rd@ = register definition
2791 * @unsigned style@ = output style (@TVSF_...@)
2792 * @const struct gprintf_ops *gops@, @void *gp@ = format output
2796 * Use: Dump a register value to the format output.
2798 * Text string values are dumped as plain text, in double quotes
2799 * if necessary, and using backslash escape sequences for
2800 * nonprintable characters. Unless compact output is requested,
2801 * strings consisting of multiple lines are dumped with each
2802 * line of the string on a separate output line.
2804 * Binary string values are dumped in hexadecimal. In compact
2805 * style, the output simply consists of a single block of hex
2806 * digits. Otherwise, the dump is a display consisting of
2807 * groups of hex digits, with comments showing the offset (if
2808 * the string is long enough) and the corresponding plain text.
2810 * Empty strings are dumped as %|""|%.
2813 static void dump_text(const union tvec_regval
*rv
,
2814 const struct tvec_regdef
*rd
,
2816 const struct gprintf_ops
*gops
, void *go
)
2818 const unsigned char *p
, *q
, *l
;
2820 #define f_nonword 1u
2821 #define f_newline 2u
2823 if (!rv
->text
.sz
) { gprintf(gops
, go
, "\"\""); return; }
2825 p
= (const unsigned char *)rv
->text
.p
; l
= p
+ rv
->text
.sz
;
2827 case '!': case '#': case ';': case '"': case '\'':
2828 case '(': case '{': case '[': case ']': case '}': case ')':
2829 f
|= f_nonword
; break;
2831 for (q
= p
; q
< l
; q
++)
2832 if (*q
== '\n' && q
!= l
- 1) f
|= f_newline
;
2833 else if (!*q
|| !isgraph(*q
) || *q
== '\\') f
|= f_nonword
;
2834 if (f
&f_newline
) { gprintf(gops
, go
, "\n\t"); goto quote
; }
2835 else if (f
&f_nonword
) goto quote
;
2837 gops
->putm(go
, (const char *)p
, rv
->text
.sz
);
2841 gprintf(gops
, go
, "\"");
2842 for (q
= p
; q
< l
; q
++)
2843 if (!isprint(*q
) || *q
== '"') {
2844 if (p
< q
) gops
->putm(go
, (const char *)p
, q
- p
);
2845 if (*q
!= '\n' || (style
&TVSF_COMPACT
))
2846 format_charesc(gops
, go
, *q
, FCF_BRACE
);
2848 if (q
+ 1 == l
) { gprintf(gops
, go
, "\\n\""); return; }
2849 else gprintf(gops
, go
, "\\n\"\n\t\"");
2853 if (p
< q
) gops
->putm(go
, (const char *)p
, q
- p
);
2854 gprintf(gops
, go
, "\"");
2860 static void dump_bytes(const union tvec_regval
*rv
,
2861 const struct tvec_regdef
*rd
,
2863 const struct gprintf_ops
*gops
, void *go
)
2865 const unsigned char *p
= rv
->bytes
.p
, *l
= p
+ rv
->bytes
.sz
;
2866 size_t off
, sz
= rv
->bytes
.sz
;
2871 gprintf(gops
, go
, style
&TVSF_COMPACT ?
"\"\"" : "\"\" ; empty");
2875 if (style
&TVSF_COMPACT
) {
2876 while (p
< l
) gprintf(gops
, go
, "%02x", *p
++);
2880 if (sz
> 16) gprintf(gops
, go
, "\n\t");
2882 off
= 0; wd
= hex_width(sz
);
2884 if (l
- p
< 16) n
= l
- p
;
2887 for (i
= 0; i
< n
; i
++) {
2888 if (i
< n
) gprintf(gops
, go
, "%02x", p
[i
]);
2889 else gprintf(gops
, go
, " ");
2890 if (i
< n
- 1 && i
%4 == 3) gprintf(gops
, go
, " ");
2892 gprintf(gops
, go
, " ; ");
2893 if (sz
> 16) gprintf(gops
, go
, "[%0*lx] ", wd
, (unsigned long)off
);
2894 for (i
= 0; i
< n
; i
++)
2895 gprintf(gops
, go
, "%c", isprint(p
[i
]) ? p
[i
] : '.');
2897 if (p
< l
) gprintf(gops
, go
, "\n\t");
2901 /* Text and byte string type definitions. */
2902 const struct tvec_regty tvty_text
= {
2903 init_text
, release_text
, eq_text
,
2904 tobuf_text
, frombuf_text
,
2905 parse_text
, dump_text
2907 const struct tvec_regty tvty_bytes
= {
2908 init_bytes
, release_bytes
, eq_bytes
,
2909 tobuf_bytes
, frombuf_bytes
,
2910 parse_bytes
, dump_bytes
2913 /* --- @tvec_claimeq_text@ --- *
2915 * Arguments: @struct tvec_state *tv@ = test-vector state
2916 * @const char *p0@, @size_t sz0@ = first string with length
2917 * @const char *p1@, @size_t sz1@ = second string with length
2918 * @const char *file@, @unsigned @lno@ = calling file and line
2919 * @const char *expr@ = the expression to quote on failure
2921 * Returns: Nonzero if the strings at @p0@ and @p1@ are equal, otherwise
2924 * Use: Check that strings at @p0@ and @p1@ are equal. As for
2925 * @tvec_claim@ above, a test case is automatically begun and
2926 * ended if none is already underway. If the values are
2927 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
2928 * mismatched values are dumped: @p0@ is printed as the output
2929 * value and @p1@ is printed as the input reference.
2932 int tvec_claimeq_text(struct tvec_state
*tv
,
2933 const char *p0
, size_t sz0
,
2934 const char *p1
, size_t sz1
,
2935 const char *file
, unsigned lno
, const char *expr
)
2937 tv
->out
[0].v
.text
.p
= (/*unconst*/ char *)p0
; tv
->out
[0].v
.text
.sz
= sz0
;
2938 tv
->in
[0].v
.text
.p
=(/*unconst*/ char *) p1
; tv
->in
[0].v
.text
.sz
= sz1
;
2939 return (tvec_claimeq(tv
, &tvty_text
, 0, file
, lno
, expr
));
2942 /* --- @tvec_claimeq_textz@ --- *
2944 * Arguments: @struct tvec_state *tv@ = test-vector state
2945 * @const char *p0, *p1@ = two strings to compare
2946 * @const char *file@, @unsigned @lno@ = calling file and line
2947 * @const char *expr@ = the expression to quote on failure
2949 * Returns: Nonzero if the strings at @p0@ and @p1@ are equal, otherwise
2952 * Use: Check that strings at @p0@ and @p1@ are equal, as for
2953 * @tvec_claimeq_string@, except that the strings are assumed
2954 * null-terminated, so their lengths don't need to be supplied
2958 int tvec_claimeq_textz(struct tvec_state
*tv
,
2959 const char *p0
, const char *p1
,
2960 const char *file
, unsigned lno
, const char *expr
)
2962 tv
->out
[0].v
.text
.p
= (/*unconst*/ char *)p0
;
2963 tv
->out
[0].v
.text
.sz
= strlen(p0
);
2964 tv
->in
[0].v
.text
.p
= (/*unconst*/ char *)p1
;
2965 tv
->in
[0].v
.text
.sz
= strlen(p1
);
2966 return (tvec_claimeq(tv
, &tvty_text
, 0, file
, lno
, expr
));
2969 /* --- @tvec_claimeq_bytes@ --- *
2971 * Arguments: @struct tvec_state *tv@ = test-vector state
2972 * @const void *p0@, @size_t sz0@ = first string with length
2973 * @const void *p1@, @size_t sz1@ = second string with length
2974 * @const char *file@, @unsigned @lno@ = calling file and line
2975 * @const char *expr@ = the expression to quote on failure
2977 * Returns: Nonzero if the strings at @p0@ and @p1@ are equal, otherwise
2980 * Use: Check that binary strings at @p0@ and @p1@ are equal. As for
2981 * @tvec_claim@ above, a test case is automatically begun and
2982 * ended if none is already underway. If the values are
2983 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
2984 * mismatched values are dumped: @p0@ is printed as the output
2985 * value and @p1@ is printed as the input reference.
2988 int tvec_claimeq_bytes(struct tvec_state
*tv
,
2989 const void *p0
, size_t sz0
,
2990 const void *p1
, size_t sz1
,
2991 const char *file
, unsigned lno
, const char *expr
)
2993 tv
->out
[0].v
.bytes
.p
= (/*unconst*/ void *)p0
;
2994 tv
->out
[0].v
.bytes
.sz
= sz0
;
2995 tv
->in
[0].v
.bytes
.p
= (/*unconst*/ void *)p1
;
2996 tv
->in
[0].v
.bytes
.sz
= sz1
;
2997 return (tvec_claimeq(tv
, &tvty_bytes
, 0, file
, lno
, expr
));
3000 /* --- @tvec_alloctext@, @tvec_allocbytes@ --- *
3002 * Arguments: @union tvec_regval *rv@ = register value
3003 * @size_t sz@ = required size
3007 * Use: Allocated space in a text or binary string register. If the
3008 * current register size is sufficient, its buffer is left
3009 * alone; otherwise, the old buffer, if any, is freed and a
3010 * fresh buffer allocated. These functions are not intended to
3011 * be used to adjust a buffer repeatedly, e.g., while building
3012 * output incrementally: (a) they will perform badly, and (b)
3013 * the old buffer contents are simply discarded if reallocation
3014 * is necessary. Instead, use a @dbuf@ or @dstr@.
3016 * The @tvec_alloctext@ function sneakily allocates an extra
3017 * byte for a terminating zero. The @tvec_allocbytes@ function
3021 void tvec_alloctext(union tvec_regval
*rv
, size_t sz
)
3023 if (rv
->text
.sz
<= sz
) { xfree(rv
->text
.p
); rv
->text
.p
= xmalloc(sz
+ 1); }
3027 void tvec_allocbytes(union tvec_regval
*rv
, size_t sz
)
3029 if (rv
->bytes
.sz
< sz
) { xfree(rv
->bytes
.p
); rv
->bytes
.p
= xmalloc(sz
); }
3033 /*----- Buffer type -------------------------------------------------------*/
3035 /* Buffers are initialized and released as binary strings. */
3037 /* --- @eq_buffer@ --- *
3039 * Arguments: @const union tvec_regval *rv0, *rv1@ = register values
3040 * @const struct tvec_regdef *rd@ = register definition
3042 * Returns: Nonzero if the values are equal, zero if unequal
3044 * Use: Compare register values for equality.
3046 * Buffer values are equal if and only if their sizes are equal;
3047 * their contents are %%\emph{not}%% compared.
3050 static int eq_buffer(const union tvec_regval
*rv0
,
3051 const union tvec_regval
*rv1
,
3052 const struct tvec_regdef
*rd
)
3053 { return (rv0
->bytes
.sz
== rv1
->bytes
.sz
); }
3055 /* --- @tobuf_buffer@ --- *
3057 * Arguments: @buf *b@ = buffer
3058 * @const union tvec_regval *rv@ = register value
3059 * @const struct tvec_regdef *rd@ = register definition
3061 * Returns: Zero on success, %$-1$% on failure.
3063 * Use: Serialize a register value to a buffer.
3065 * Buffer values are serialized as just their lengths, as
3066 * unsigned integers.
3069 static int tobuf_buffer(buf
*b
, const union tvec_regval
*rv
,
3070 const struct tvec_regdef
*rd
)
3071 { return (unsigned_to_buf(b
, rv
->bytes
.sz
)); }
3073 /* --- @allocate_buffer@ --- *
3075 * Arguments: @union tvec_regval *rv@ = register value
3076 * @size_t sz@ = size to allocate
3080 * Use: Allocate @sz@ bytes to the buffer and fill the space with a
3081 * distinctive pattern.
3084 static void allocate_buffer(union tvec_regval
*rv
, size_t sz
)
3085 { tvec_allocbytes(rv
, sz
); memset(rv
->bytes
.p
, '?', sz
); }
3087 /* --- @frombuf_buffer@ --- *
3089 * Arguments: @buf *b@ = buffer
3090 * @union tvec_regval *rv@ = register value
3091 * @const struct tvec_regdef *rd@ = register definition
3093 * Returns: Zero on success, %$-1$% on failure.
3095 * Use: Deserialize a register value from a buffer.
3097 * Buffer values are serialized as just their lengths, as
3098 * unsigned integers. The buffer is allocated on
3099 * deserialization and filled with a distinctive pattern.
3102 static int frombuf_buffer(buf
*b
, union tvec_regval
*rv
,
3103 const struct tvec_regdef
*rd
)
3107 if (unsigned_from_buf(b
, &u
)) return (-1);
3108 if (u
> (size_t)-1) return (-1);
3109 allocate_buffer(rv
, u
);
3113 /* --- @parse_buffer@ --- *
3115 * Arguments: @union tvec_regval *rv@ = register value
3116 * @const struct tvec_regdef *rd@ = register definition
3117 * @struct tvec_state *tv@ = test-vector state
3119 * Returns: Zero on success, %$-1$% on error.
3121 * Use: Parse a register value from an input file.
3123 * The input format for a buffer value consists of an unsigned
3124 * integer followed by an optional unit specifier consisting of
3125 * an SI unit prefix and (optionally) the letter `B'. Unit
3126 * prefixes denote %%\emph{binary}%% multipliers, not decimal.
3128 * The buffer is allocated and filled with a distinctive
3132 static const char units
[] = "kMGTPEZY";
3134 static int parse_buffer(union tvec_regval
*rv
,
3135 const struct tvec_regdef
*rd
,
3136 struct tvec_state
*tv
)
3139 const char *q
, *unit
;
3146 if (tvec_readword(tv
, &d
, ";", "buffer length")) { rc
= -1; goto end
; }
3147 if (parse_unsigned_integer(&u
, &q
, d
.buf
)) goto bad
;
3149 tvec_skipspc(tv
); pos
= d
.len
;
3150 if (!tvec_readword(tv
, &d
, ";", 0)) pos
++;
3154 if (u
> (size_t)-1) goto rangerr
;
3155 for (t
= u
, unit
= units
; *unit
; unit
++) {
3156 if (t
> (size_t)-1/1024) f
|= f_range
;
3159 if (f
&f_range
) goto rangerr
;
3165 if (check_string_length(u
, rd
->arg
.p
, tv
)) { rc
= -1; goto end
; }
3167 if (tvec_flushtoeol(tv
, 0)) { rc
= -1; goto end
; }
3168 allocate_buffer(rv
, u
);
3171 DDESTROY(&d
); return (rc
);
3174 tvec_error(tv
, "invalid buffer length `%s'", d
.buf
);
3178 tvec_error(tv
, "buffer length `%s' out of range", d
.buf
);
3184 /* --- @dump_buffer@ --- *
3186 * Arguments: @const union tvec_regval *rv@ = register value
3187 * @const struct tvec_regdef *rd@ = register definition
3188 * @unsigned style@ = output style (@TVSF_...@)
3189 * @const struct gprintf_ops *gops@, @void *gp@ = format output
3193 * Use: Dump a register value to the format output.
3195 * Buffer values are dumped as their size with an appropriate
3196 * unit specifier. A unit prefix is only used if the size is an
3197 * exact multiple of the relevant power of two.
3200 static void dump_buffer(const union tvec_regval
*rv
,
3201 const struct tvec_regdef
*rd
,
3203 const struct gprintf_ops
*gops
, void *go
)
3206 unsigned long u
= rv
->bytes
.sz
;
3209 gprintf(gops
, go
, "%lu B", u
);
3211 for (unit
= units
, u
/= 1024; !(u
%1024) && unit
[1]; u
/= 1024, unit
++);
3212 gprintf(gops
, go
, "%lu %cB", u
, *unit
);
3216 /* Buffer type definition. */
3217 const struct tvec_regty tvty_buffer
= {
3218 init_bytes
, release_bytes
, eq_buffer
,
3219 tobuf_buffer
, frombuf_buffer
,
3220 parse_buffer
, dump_buffer
3223 /*----- That's all, folks -------------------------------------------------*/