Divya Kothari | a0f56be | 2014-06-26 07:25:20 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Copyright 2013 Robin Mittal <robinmittal.it@gmail.com> |
| 4 | # Copyright 2013 Divya Kothari <divya.s.kothari@gmail.com> |
| 5 | |
| 6 | [ -f testing.sh ] && . testing.sh |
| 7 | |
| 8 | #testing "name" "command" "result" "infile" "stdin" |
Divya Kothari | a0f56be | 2014-06-26 07:25:20 -0500 | [diff] [blame] | 9 | |
Rob Landley | dd2cc65 | 2015-01-06 15:06:51 -0600 | [diff] [blame] | 10 | # Disable shell builtin |
| 11 | PRINTF="$(which printf)" |
Rob Landley | cc39d95 | 2015-01-06 12:07:20 -0600 | [diff] [blame] | 12 | |
Rob Landley | dd2cc65 | 2015-01-06 15:06:51 -0600 | [diff] [blame] | 13 | testing "printf text" "$PRINTF TEXT" "TEXT" "" "" |
| 14 | testing "printf escapes" "$PRINTF 'one\ntwo\n\v\t\r\f\e\b\athree'" \ |
| 15 | "one\ntwo\n\v\t\r\f\e\b\athree" "" "" |
| 16 | testing "printf %b escapes" "$PRINTF %b 'one\ntwo\n\v\t\r\f\e\b\athree'" \ |
| 17 | "one\ntwo\n\v\t\r\f\e\b\athree" "" "" |
| 18 | testing "printf null" "$PRINTF 'x\0y' | od -An -tx1" ' 78 00 79\n' "" "" |
| 19 | testing "printf trailing slash" "$PRINTF 'abc\'" 'abc\' "" "" |
| 20 | testing "printf octal" "$PRINTF ' \1\002\429\045x'" ' \001\002"9%x' "" "" |
| 21 | testing "printf not octal" "$PRINTF '\9'" '\9' "" "" |
| 22 | testing "printf hex" "$PRINTF 'A\x1b\x2B\x3Q\xa' | od -An -tx1" \ |
| 23 | ' 41 1b 2b 03 51 0a\n' "" "" |
Rob Landley | 70cbfe8 | 2015-01-10 20:02:21 -0600 | [diff] [blame] | 24 | testing "printf %x" "$PRINTF '%x\n' 0x2a" "2a\n" "" "" |
Rob Landley | dd2cc65 | 2015-01-06 15:06:51 -0600 | [diff] [blame] | 25 | |
| 26 | testing "printf %d 42" "$PRINTF %d 42" "42" "" "" |
| 27 | testing "printf %d 0x2a" "$PRINTF %d 0x2a" "42" "" "" |
| 28 | testing "printf %d 052" "$PRINTF %d 052" "42" "" "" |
| 29 | |
| 30 | testing "printf %s width precision" \ |
| 31 | "$PRINTF '%3s,%.3s,%10s,%10.3s' abcde fghij klmno pqrst" \ |
| 32 | "abcde,fgh, klmno, pqr" "" "" |
Rob Landley | cc39d95 | 2015-01-06 12:07:20 -0600 | [diff] [blame] | 33 | |
Rob Landley | 70cbfe8 | 2015-01-10 20:02:21 -0600 | [diff] [blame] | 34 | # posix: "The format operand shall be reused as often as necessary to satisfy |
| 35 | # the argument operands." |
| 36 | |
| 37 | testing "printf extra args" "$PRINTF 'abc%s!%ddef\n' X 42 ARG 36" \ |
| 38 | "abcX!42def\nabcARG!36def\n" "" "" |
| 39 | |
Rob Landley | d0dead3 | 2015-01-11 10:16:38 -0600 | [diff] [blame] | 40 | testing "printf '%3c'" "$PRINTF '%3c' x" " x" "" "" |
| 41 | testing "printf '%-3c'" "$PRINTF '%-3c' x" "x " "" "" |
| 42 | testing "printf '%+d'" "$PRINTF '%+d' 5" "+5" "" "" |
| 43 | |
Rob Landley | 70cbfe8 | 2015-01-10 20:02:21 -0600 | [diff] [blame] | 44 | |
Divya Kothari | a0f56be | 2014-06-26 07:25:20 -0500 | [diff] [blame] | 45 | testing "printf '%5d%4d' 1 21 321 4321 54321" \ |
Rob Landley | dd2cc65 | 2015-01-06 15:06:51 -0600 | [diff] [blame] | 46 | "$PRINTF '%5d%4d' 1 21 321 4321 54321" " 1 21 321432154321 0" "" "" |
| 47 | testing "printf '%c %c' 78 79" "$PRINTF '%c %c' 78 79" "7 7" "" "" |
| 48 | testing "printf '%d %d' 78 79" "$PRINTF '%d %d' 78 79" "78 79" "" "" |
| 49 | testing "printf '%f %f' 78 79" "$PRINTF '%f %f' 78 79" \ |
Divya Kothari | a0f56be | 2014-06-26 07:25:20 -0500 | [diff] [blame] | 50 | "78.000000 79.000000" "" "" |
Rob Landley | dd2cc65 | 2015-01-06 15:06:51 -0600 | [diff] [blame] | 51 | testing "printf 'f f' 78 79" "$PRINTF 'f f' 78 79" "f f" "" "" |
| 52 | testing "printf '%i %i' 78 79" "$PRINTF '%i %i' 78 79" "78 79" "" "" |
| 53 | testing "printf '%o %o' 78 79" "$PRINTF '%o %o' 78 79" "116 117" "" "" |
| 54 | testing "printf '%u %u' 78 79" "$PRINTF '%u %u' 78 79" "78 79" "" "" |
| 55 | testing "printf '%u %u' -1 -2" "$PRINTF '%u %u' -1 -2" \ |
Divya Kothari | a0f56be | 2014-06-26 07:25:20 -0500 | [diff] [blame] | 56 | "18446744073709551615 18446744073709551614" "" "" |
Rob Landley | dd2cc65 | 2015-01-06 15:06:51 -0600 | [diff] [blame] | 57 | testing "printf '%x %X' 78 79" "$PRINTF '%x %X' 78 79" "4e 4F" "" "" |
| 58 | testing "printf '%g %G' 78 79" "$PRINTF '%g %G' 78 79" "78 79" "" "" |
| 59 | testing "printf '%s %s' 78 79" "$PRINTF '%s %s' 78 79" "78 79" "" "" |
Rob Landley | 5d431d1 | 2015-03-28 13:22:27 -0500 | [diff] [blame] | 60 | |
| 61 | testing "printf %.s acts like %.0s" "$PRINTF %.s_ 1 2 3 4 5" "_____" "" "" |