aboutsummaryrefslogtreecommitdiff
path: root/tests/printf.test
blob: eb0e0bec526b58ba591e3a9f6715a6eaa13d58be (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
#!/bin/bash

# Copyright 2013 Robin Mittal <robinmittal.it@gmail.com>
# Copyright 2013 Divya Kothari <divya.s.kothari@gmail.com>

[ -f testing.sh ] && . testing.sh

#testing "name" "command" "result" "infile" "stdin"

# Disable shell builtin
PRINTF="$(which printf)"

testing "text" "$PRINTF TEXT" "TEXT" "" ""
testing "escapes" "$PRINTF 'one\ntwo\n\v\t\r\f\e\b\athree'" \
  "one\ntwo\n\v\t\r\f\e\b\athree" "" ""
testing "%b escapes" "$PRINTF %b 'one\ntwo\n\v\t\r\f\e\b\athree'" \
  "one\ntwo\n\v\t\r\f\e\b\athree" "" ""
testing "null" "$PRINTF 'x\0y' | od -An -tx1" ' 78 00 79\n' "" ""
testing "trailing slash" "$PRINTF 'abc\'" 'abc\' "" ""
testing "octal" "$PRINTF ' \1\002\429\045x'" ' \001\002"9%x' "" ""
testing "not octal" "$PRINTF '\9'" '\9' "" ""
testing "hex" "$PRINTF 'A\x1b\x2B\x3Q\xa' | od -An -tx1" \
  ' 41 1b 2b 03 51 0a\n' "" ""
testing "%x" "$PRINTF '%x\n' 0x2a" "2a\n" "" ""

testing "%d 42" "$PRINTF %d 42" "42" "" ""
testing "%d 0x2a" "$PRINTF %d 0x2a" "42" "" ""
testing "%d 052" "$PRINTF %d 052" "42" "" ""

testing "%s width precision" \
  "$PRINTF '%3s,%.3s,%10s,%10.3s' abcde fghij klmno pqrst" \
  "abcde,fgh,     klmno,       pqr" "" ""

# posix: "The format operand shall be reused as often as necessary to satisfy
# the argument operands."

testing "extra args" "$PRINTF 'abc%s!%ddef\n' X 42 ARG 36" \
	"abcX!42def\nabcARG!36def\n" "" ""

testing "'%3c'" "$PRINTF '%3c' x" "  x" "" ""
testing "'%-3c'" "$PRINTF '%-3c' x" "x  " "" ""
testing "'%+d'" "$PRINTF '%+d' 5" "+5" "" ""


testing "'%5d%4d' 1 21 321 4321 54321" \
  "$PRINTF '%5d%4d' 1 21 321 4321 54321" "    1  21  321432154321   0" "" ""
testing "'%c %c' 78 79" "$PRINTF '%c %c' 78 79" "7 7" "" ""
testing "'%d %d' 78 79" "$PRINTF '%d %d' 78 79" "78 79" "" ""
testing "'%f %f' 78 79" "$PRINTF '%f %f' 78 79" \
  "78.000000 79.000000" "" ""
testing "'f f' 78 79" "$PRINTF 'f f' 78 79 2>/dev/null" "f f" "" ""
testing "'%i %i' 78 79" "$PRINTF '%i %i' 78 79" "78 79" "" ""
testing "'%o %o' 78 79" "$PRINTF '%o %o' 78 79" "116 117" "" ""
testing "'%u %u' 78 79" "$PRINTF '%u %u' 78 79" "78 79" "" ""
testing "'%u %u' -1 -2" "$PRINTF '%u %u' -1 -2" \
  "18446744073709551615 18446744073709551614" "" ""
testing "'%x %X' 78 79" "$PRINTF '%x %X' 78 79" "4e 4F" "" ""
testing "'%g %G' 78 79" "$PRINTF '%g %G' 78 79" "78 79" "" ""
testing "'%s %s' 78 79" "$PRINTF '%s %s' 78 79" "78 79" "" ""

testing "%.s acts like %.0s" "$PRINTF %.s_ 1 2 3 4 5" "_____" "" ""
testing "corner case" "$PRINTF '\\8'" '\8' '' ''

# The posix spec explicitly specifies inconsistent behavior,
# so treating the \0066 in %b like the \0066 not in %b is wrong because posix.
testing "printf posix inconsistency" "$PRINTF '\\0066-%b' '\\0066'" "\x066-6" \
  "" ""

testing "printf \x" "$PRINTF 'A\x1b\x2B\x3Q\xa' | od -An -tx1" \
  " 41 1b 2b 03 51 0a\n" "" ""