aboutsummaryrefslogtreecommitdiff
path: root/testsuite/printf.tests
blob: 558ecb1941beb6d0b02636f848b8f793a4e8220a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/sh
# Copyright 2008 by Denys Vlasenko
# Licensed under GPL v2, see file LICENSE for details.

. testing.sh

# Need this in order to not execute shell builtin
bb="busybox "

# testing "test name" "command" "expected result" "file input" "stdin"

testing "printf produces no further output 1" \
	"${bb}printf '\c' foo" \
	"" \
	"" ""

testing "printf produces no further output 2" \
	"${bb}printf '%s\c' foo bar" \
	"foo" \
	"" ""

testing "printf repeatedly uses pattern for each argv" \
	"${bb}printf '%s\n' foo \$HOME" \
	"foo\n$HOME\n" \
	"" ""

testing "printf understands %b escaped_string" \
	"${bb}printf '%b' 'a\tb' 'c\\d\n' 2>&1; echo \$?" \
	"a\tbc\\d\n""0\n" \
	"" ""

testing "printf understands %d '\"x' \"'y\" \"'zTAIL\"" \
	"${bb}printf '%d\n' '\"x' \"'y\" \"'zTAIL\" 2>&1; echo \$?" \
	"120\n""121\n""122\n""0\n" \
	"" ""

testing "printf understands %s '\"x' \"'y\" \"'zTAIL\"" \
	"${bb}printf '%s\n' '\"x' \"'y\" \"'zTAIL\" 2>&1; echo \$?" \
	"\"x\n""'y\n""'zTAIL\n""0\n" \
	"" ""

testing "printf understands %23.12f" \
	"${bb}printf '|%23.12f|\n' 5.25 2>&1; echo \$?" \
	"|         5.250000000000|\n""0\n" \
	"" ""

testing "printf understands %*.*f" \
	"${bb}printf '|%*.*f|\n' 23 12 5.25 2>&1; echo \$?" \
	"|         5.250000000000|\n""0\n" \
	"" ""

testing "printf understands %*f with negative width" \
	"${bb}printf '|%*f|\n' -23 5.25 2>&1; echo \$?" \
	"|5.250000               |\n""0\n" \
	"" ""

testing "printf understands %.*f with negative precision" \
	"${bb}printf '|%.*f|\n' -12 5.25 2>&1; echo \$?" \
	"|5.250000|\n""0\n" \
	"" ""

testing "printf understands %*.*f with negative width/precision" \
	"${bb}printf '|%*.*f|\n' -23 -12 5.25 2>&1; echo \$?" \
	"|5.250000               |\n""0\n" \
	"" ""

testing "printf understands %zd" \
	"${bb}printf '%zd\n' -5 2>&1; echo \$?" \
	"-5\n""0\n" \
	"" ""

testing "printf understands %ld" \
	"${bb}printf '%ld\n' -5 2>&1; echo \$?" \
	"-5\n""0\n" \
	"" ""

testing "printf understands %Ld" \
	"${bb}printf '%Ld\n' -5 2>&1; echo \$?" \
	"-5\n""0\n" \
	"" ""

# "FIXED" now to act compatibly
## We are "more correct" here than bash/coreutils: they happily print -2
## as if it is a huge unsigned number
#testing "printf handles %u -N" \
#	"${bb}printf '%u\n' 1 -2 3 2>&1; echo \$?" \
#	"1\n""printf: -2: invalid number\n""0\n""3\n""0\n" \
#	"" ""

# Actually, we are wrong here: exit code should be 1
testing "printf handles %d bad_input" \
	"${bb}printf '%d\n' 1 - 2 bad 3 123bad 4 2>&1; echo \$?" \
"1\n""printf: -: invalid number\n""0\n"\
"2\n""printf: bad: invalid number\n""0\n"\
"3\n""printf: 123bad: invalid number\n""0\n"\
"4\n""0\n" \
	"" ""

testing "printf aborts on bare %" \
	"${bb}printf '%' a b c 2>&1; echo \$?" \
	"printf: %: invalid format\n""1\n" \
	"" ""

testing "printf aborts on %r" \
	"${bb}printf '%r' a b c 2>&1; echo \$?" \
	"printf: %r: invalid format\n""1\n" \
	"" ""

exit $FAILCOUNT