toybox/tests/printf.test
<<
>>
Prefs
   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" "" ""
  34
  35testing "%s width precision" \
  36  "$PRINTF '%3s,%.3s,%10s,%10.3s' abcde fghij klmno pqrst" \
  37  "abcde,fgh,     klmno,       pqr" "" ""
  38
  39# posix: "The format operand shall be reused as often as necessary to satisfy
  40# the argument operands."
  41
  42testing "extra args" "$PRINTF 'abc%s!%ddef\n' X 42 ARG 36" \
  43        "abcX!42def\nabcARG!36def\n" "" ""
  44
  45testing "'%3c'" "$PRINTF '%3c' x" "  x" "" ""
  46testing "'%-3c'" "$PRINTF '%-3c' x" "x  " "" ""
  47testing "'%+d'" "$PRINTF '%+d' 5" "+5" "" ""
  48
  49
  50testing "'%5d%4d' 1 21 321 4321 54321" \
  51  "$PRINTF '%5d%4d' 1 21 321 4321 54321" "    1  21  321432154321   0" "" ""
  52testing "'%c %c' 78 79" "$PRINTF '%c %c' 78 79" "7 7" "" ""
  53testing "'%d %d' 78 79" "$PRINTF '%d %d' 78 79" "78 79" "" ""
  54testing "'%f %f' 78 79" "$PRINTF '%f %f' 78 79" \
  55  "78.000000 79.000000" "" ""
  56testing "'f f' 78 79" "$PRINTF 'f f' 78 79 2>/dev/null" "f f" "" ""
  57testing "'%i %i' 78 79" "$PRINTF '%i %i' 78 79" "78 79" "" ""
  58testing "'%o %o' 78 79" "$PRINTF '%o %o' 78 79" "116 117" "" ""
  59testing "'%u %u' 78 79" "$PRINTF '%u %u' 78 79" "78 79" "" ""
  60testing "'%u %u' -1 -2" "$PRINTF '%u %u' -1 -2" \
  61  "18446744073709551615 18446744073709551614" "" ""
  62testing "'%x %X' 78 79" "$PRINTF '%x %X' 78 79" "4e 4F" "" ""
  63testing "'%g %G' 78 79" "$PRINTF '%g %G' 78 79" "78 79" "" ""
  64testing "'%s %s' 78 79" "$PRINTF '%s %s' 78 79" "78 79" "" ""
  65
  66testing "%.s acts like %.0s" "$PRINTF %.s_ 1 2 3 4 5" "_____" "" ""
  67testing "corner case" "$PRINTF '\\8'" '\8' '' ''
  68
  69# The posix spec explicitly specifies inconsistent behavior,
  70# so treating the \0066 in %b like the \0066 not in %b is wrong because posix.
  71testing "printf posix inconsistency" "$PRINTF '\\0066-%b' '\\0066'" "\x066-6" \
  72  "" ""
  73
  74testing "printf \x" "$PRINTF 'A\x1b\x2B\x3Q\xa' | od -An -tx1" \
  75  " 41 1b 2b 03 51 0a\n" "" ""
  76
  77testing "printf \c" "$PRINTF 'one\ctwo'" "one" "" ""
  78
  79# An extra leading 0 is fine for %b, but not as a direct escape, for some
  80# reason...
  81testing "printf octal %b" "$PRINTF '\0007%b' '\0007' | xxd -p" "003707\n" "" ""
  82
  83# Unlike echo, printf errors out on bad hex.
  84testcmd "invalid hex 1" "'one\xvdtwo' 2>/dev/null || echo err" "oneerr\n" "" ""
  85testcmd "invalid hex 2" "'one\xavtwo'" "one\nvtwo" "" ""
  86# But bad octal is shown as text.
  87testcmd "invalid oct" "'one\079two'" "one\a9two" "" ""
  88
  89# extension for ESC
  90testcmd "%b \e" "'%b' '\\e' | xxd -p" "1b\n" "" ""
  91testcmd "\e" "'\\e' | xxd -p" "1b\n" "" ""
  92