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