1#!/usr/bin/perl
2# improve printf.c test coverage
3
4# Copyright (C) 2008-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19use strict;
20
21my $prog = 'printf';
22my $try = "Try '$prog --help' for more information.\n";
23my $pow_2_31 = 2**31;
24
25# Turn off localization of executable's output.
26@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
27
28my @Tests =
29(
30  ['no-args', {EXIT=>1}, {ERR=>"$prog: missing operand\n$try"}],
31  ['no-arg2', '--', {EXIT=>1}, {ERR=>"$prog: missing operand\n$try"}],
32  ['escape-1', q('\a\b\f\n\r\t\v\z\c'), {OUT=>"\a\b\f\n\r\t\x0b\\z"}],
33  ['hex-ucX',   '%X 999',    {OUT=>"3E7"}],
34  ['hex-ucXw',  '%4X 999',   {OUT=>" 3E7"}],
35  ['hex-ucXp',  '%.4X 999',  {OUT=>"03E7"}],
36  ['hex-ucXwp', '%5.4X 999', {OUT=>" 03E7"}],
37  ['hex-vw',   '%*X 4 42',  {OUT=>"  2A"}],
38  ['hex-vp',   '%.*X 4 42',  {OUT=>"002A"}],
39  ['hex-vwvp', '%*.*X 3 2 15',  {OUT=>" 0F"}],
40  ['b', q('nl\ntab\tx'), {OUT=>"nl\ntab\tx"}],
41  ['c1', '%c 123',     {OUT=>"1"}],
42  ['cw', '%\*c 3 123', {OUT=>"  1"}],
43  ['d-ucXwp', '%5.4d 999', {OUT=>" 0999"}],
44  ['d-vw',   '%*d 4 42',  {OUT=>"  42"}],
45  ['d-vp',   '%.*d 4 42',  {OUT=>"0042"}],
46  ['d-vwvp', '%*.*d 3 2 15',  {OUT=>" 15"}],
47  ['d-neg-prec', '%.*d -3 15',  {OUT=>"15"}],
48  ['d-big-prec', "%.*d $pow_2_31 15",  # INT_MAX
49   {EXIT=>1}, {ERR=>"$prog: invalid precision: '$pow_2_31'\n"}],
50  ['d-big-fwidth', "%*d $pow_2_31 15",  # INT_MAX
51   {EXIT=>1}, {ERR=>"$prog: invalid field width: '$pow_2_31'\n"}],
52  ['F',  '%F  1',  {OUT=>"1.000000"}],
53  ['LF', '%LF 1',  {OUT=>"1.000000"}],
54  ['E',  '%E  2',  {OUT=>"2.000000E+00"}],
55  ['LE', '%LE 2',  {OUT=>"2.000000E+00"}],
56  ['s', '%s x',      {OUT=>"x"}],
57  ['sw', '%\*s 2 x', {OUT=>" x"}],
58  ['sp',  '%.\*s 2 abcd',  {OUT=>"ab"}],
59  ['swp', '%\*.\*s 2 2 abcd',  {OUT=>"ab"}],
60  ['sw-no-args', '%\*s'],
61  ['sw-no-args2', '%.\*s'],
62  ['G-ucXwp', '%5.4G 3', {OUT=>"    3"}],
63  ['G-vw',   '%*G 4 42',  {OUT=>"  42"}],
64  ['G-vp',   '%.*G 4 42',  {OUT=>"42"}],
65  ['G-vwvp', '%*.*G 5 3 15',  {OUT=>"   15"}],
66  ['esc', q('\xaa\0377'),  {OUT=>"\xaa\0377"}],
67  ['esc-bad-hex', q('\x'), {EXIT=>1},
68    {ERR=>"$prog: missing hexadecimal number in escape\n"}],
69  ['u-bad-hex', q('\u00'), {EXIT=>1},
70    {ERR=>"$prog: missing hexadecimal number in escape\n"}],
71  ['U-bad-hex', q('\U0000'), {EXIT=>1},
72    {ERR=>"$prog: missing hexadecimal number in escape\n"}],
73  ['u4', q('\u0030'), {OUT=>"0"}],
74  ['U8', q('\U00000030'), {OUT=>"0"}],
75  ['u-invalid', q('\ud800'), {EXIT=>1},
76    {ERR=>"$prog: invalid universal character name \\ud800\n"}],
77  ['u-missing', q('\u'), {EXIT=>1},
78    {ERR=>"$prog: missing hexadecimal number in escape\n"}],
79  ['d-invalid', '%d no-num', {OUT=>'0'}, {EXIT=>1},
80    # Depending on the strtol implementation we expect one of these:
81    #   no-num: Invalid argument         (FreeBSD6)
82    #   no-num: expected a numeric value (glibc, Solaris 10)
83    {ERR_SUBST => 's/Invalid argument$/expected a numeric value/'},
84    {ERR=>"$prog: 'no-num': expected a numeric value\n"}],
85  ['d-bad-suffix', '%d 9z', {OUT=>'9'}, {EXIT=>1},
86    {ERR=>"$prog: '9z': value not completely converted\n"}],
87  ['d-out-of-range', '%d '.('9'x30), {EXIT=>1},
88    {OUT=>"inaccurate"}, {OUT_SUBST => 's/\d+/inaccurate/'},
89    {ERR=>"$prog: 9...9\n"}, {ERR_SUBST => "s/'9+.*/9...9/"}],
90  ['excess', 'B 1', {OUT=>'B'},
91    {ERR=>"$prog: warning: ignoring excess arguments, starting with '1'\n"}],
92  ['percent', '%%', {OUT=>'%'}],
93  ['d-sp',    q('% d' 33), {OUT=>' 33'}],
94  ['d-plus',  q('%+d' 33), {OUT=>'+33'}],
95  ['d-minus', q('%-d' 33), {OUT=> '33'}],
96  ['d-zero',  q('%02d' 1), {OUT=> '01'}],
97  ['d-quote', q("%'d" 3333), {OUT=> '3333'}, {OUT_SUBST => 'tr/3//c'}],
98  ['d-hash', q("%#d" 3333), {EXIT=>1},
99    {ERR=>"$prog: %#d: invalid conversion specification\n"}],
100);
101
102my $save_temps = $ENV{DEBUG};
103my $verbose = $ENV{VERBOSE};
104
105my $fail = run_tests ($prog, \$prog, \@Tests, $save_temps, $verbose);
106exit $fail;
107