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" 9 10# Disable shell builtin 11PRINTF="$(which printf)" 12 13testing "text" "$PRINTF TEXT" "TEXT" "" "" 14 15# TODO: we have to use \x1b rather than \e in the expectations because 16# the Mac is stuck on bash 3.2 which doesn't support \e. This can go 17# away when we have a usable toysh. 18testing "escapes" "$PRINTF 'one\ntwo\n\v\t\r\f\e\b\athree'" \ 19 "one\ntwo\n\v\t\r\f\x1b\b\athree" "" "" 20testing "%b escapes" "$PRINTF %b 'one\ntwo\n\v\t\r\f\e\b\athree'" \ 21 "one\ntwo\n\v\t\r\f\x1b\b\athree" "" "" 22 23testing "null" "$PRINTF 'x\0y' | od -An -tx1" ' 78 00 79\n' "" "" 24testing "trailing slash" "$PRINTF 'abc\'" 'abc\' "" "" 25testing "octal" "$PRINTF ' \1\002\429\045x'" ' \001\002"9%x' "" "" 26testing "not octal" "$PRINTF '\9'" '\9' "" "" 27testing "hex" "$PRINTF 'A\x1b\x2B\x3Q\xa' | od -An -tx1" \ 28 ' 41 1b 2b 03 51 0a\n' "" "" 29testing "%x" "$PRINTF '%x\n' 0x2a" "2a\n" "" "" 30 31testing "%d 42" "$PRINTF %d 42" "42" "" "" 32testing "%d 0x2a" "$PRINTF %d 0x2a" "42" "" "" 33testing "%d 052" "$PRINTF %d 052" "42" "" "" 34testing "%d none" "$PRINTF %d" "0" "" "" 35testing "%d null" "$PRINTF %d ''" "0" "" "" 36 37testing "%s width precision" \ 38 "$PRINTF '%3s,%.3s,%10s,%10.3s' abcde fghij klmno pqrst" \ 39 "abcde,fgh, klmno, pqr" "" "" 40 41# posix: "The format operand shall be reused as often as necessary to satisfy 42# the argument operands." 43 44testing "extra args" "$PRINTF 'abc%s!%ddef\n' X 42 ARG 36" \ 45 "abcX!42def\nabcARG!36def\n" "" "" 46 47testing "'%3c'" "$PRINTF '%3c' x" " x" "" "" 48testing "'%-3c'" "$PRINTF '%-3c' x" "x " "" "" 49testing "'%+d'" "$PRINTF '%+d' 5" "+5" "" "" 50 51testing "'%5d%4d' 1 21 321 4321 54321" \ 52 "$PRINTF '%5d%4d' 1 21 321 4321 54321" " 1 21 321432154321 0" "" "" 53testing "'%c %c' 78 79" "$PRINTF '%c %c' 78 79" "7 7" "" "" 54testing "'%d %d' 78 79" "$PRINTF '%d %d' 78 79" "78 79" "" "" 55testing "'%f %f' 78 79" "$PRINTF '%f %f' 78 79" \ 56 "78.000000 79.000000" "" "" 57testing "'f f' 78 79" "$PRINTF 'f f' 78 79 2>/dev/null" "f f" "" "" 58testing "'%i %i' 78 79" "$PRINTF '%i %i' 78 79" "78 79" "" "" 59testing "'%o %o' 78 79" "$PRINTF '%o %o' 78 79" "116 117" "" "" 60testing "'%u %u' 78 79" "$PRINTF '%u %u' 78 79" "78 79" "" "" 61testing "'%u %u' -1 -2" "$PRINTF '%u %u' -1 -2" \ 62 "18446744073709551615 18446744073709551614" "" "" 63testing "'%x %X' 78 79" "$PRINTF '%x %X' 78 79" "4e 4F" "" "" 64testing "'%g %G' 78 79" "$PRINTF '%g %G' 78 79" "78 79" "" "" 65testing "'%s %s' 78 79" "$PRINTF '%s %s' 78 79" "78 79" "" "" 66 67testing "%.s acts like %.0s" "$PRINTF %.s_ 1 2 3 4 5" "_____" "" "" 68testing "corner case" "$PRINTF '\\8'" '\8' '' '' 69 70# The posix spec explicitly specifies inconsistent behavior, 71# so treating the \0066 in %b like the \0066 not in %b is wrong because posix. 72testing "printf posix inconsistency" "$PRINTF '\\0066-%b' '\\0066'" "\x066-6" \ 73 "" "" 74 75testing "printf \x" "$PRINTF 'A\x1b\x2B\x3Q\xa' | od -An -tx1" \ 76 " 41 1b 2b 03 51 0a\n" "" "" 77 78testing "printf \c" "$PRINTF 'one\ctwo'" "one" "" "" 79 80# An extra leading 0 is fine for %b, but not as a direct escape, for some 81# reason... 82testing "printf octal %b" "$PRINTF '\0007%b' '\0007' | xxd -p" "003707\n" "" "" 83 84# Unlike echo, printf errors out on bad hex. 85testcmd "invalid hex 1" "'one\xvdtwo' 2>/dev/null || echo err" "oneerr\n" "" "" 86testcmd "invalid hex 2" "'one\xavtwo'" "one\nvtwo" "" "" 87# But bad octal is shown as text. 88testcmd "invalid oct" "'one\079two'" "one\a9two" "" "" 89 90# extension for ESC 91testcmd "%b \e" "'%b' '\\e' | xxd -p" "1b\n" "" "" 92testcmd "\e" "'\\e' | xxd -p" "1b\n" "" "" 93