blob: eb0e0bec526b58ba591e3a9f6715a6eaa13d58be [file] [log] [blame]
Divya Kotharia0f56be2014-06-26 07:25:20 -05001#!/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 Kotharia0f56be2014-06-26 07:25:20 -05009
Rob Landleydd2cc652015-01-06 15:06:51 -060010# Disable shell builtin
11PRINTF="$(which printf)"
Rob Landleycc39d952015-01-06 12:07:20 -060012
Rob Landley336c44a2016-03-02 15:20:04 -060013testing "text" "$PRINTF TEXT" "TEXT" "" ""
14testing "escapes" "$PRINTF 'one\ntwo\n\v\t\r\f\e\b\athree'" \
Rob Landleydd2cc652015-01-06 15:06:51 -060015 "one\ntwo\n\v\t\r\f\e\b\athree" "" ""
Rob Landley336c44a2016-03-02 15:20:04 -060016testing "%b escapes" "$PRINTF %b 'one\ntwo\n\v\t\r\f\e\b\athree'" \
Rob Landleydd2cc652015-01-06 15:06:51 -060017 "one\ntwo\n\v\t\r\f\e\b\athree" "" ""
Rob Landley336c44a2016-03-02 15:20:04 -060018testing "null" "$PRINTF 'x\0y' | od -An -tx1" ' 78 00 79\n' "" ""
19testing "trailing slash" "$PRINTF 'abc\'" 'abc\' "" ""
20testing "octal" "$PRINTF ' \1\002\429\045x'" ' \001\002"9%x' "" ""
21testing "not octal" "$PRINTF '\9'" '\9' "" ""
22testing "hex" "$PRINTF 'A\x1b\x2B\x3Q\xa' | od -An -tx1" \
Rob Landleydd2cc652015-01-06 15:06:51 -060023 ' 41 1b 2b 03 51 0a\n' "" ""
Rob Landley336c44a2016-03-02 15:20:04 -060024testing "%x" "$PRINTF '%x\n' 0x2a" "2a\n" "" ""
Rob Landleydd2cc652015-01-06 15:06:51 -060025
Rob Landley336c44a2016-03-02 15:20:04 -060026testing "%d 42" "$PRINTF %d 42" "42" "" ""
27testing "%d 0x2a" "$PRINTF %d 0x2a" "42" "" ""
28testing "%d 052" "$PRINTF %d 052" "42" "" ""
Rob Landleydd2cc652015-01-06 15:06:51 -060029
Rob Landley336c44a2016-03-02 15:20:04 -060030testing "%s width precision" \
Rob Landleydd2cc652015-01-06 15:06:51 -060031 "$PRINTF '%3s,%.3s,%10s,%10.3s' abcde fghij klmno pqrst" \
32 "abcde,fgh, klmno, pqr" "" ""
Rob Landleycc39d952015-01-06 12:07:20 -060033
Rob Landley70cbfe82015-01-10 20:02:21 -060034# posix: "The format operand shall be reused as often as necessary to satisfy
35# the argument operands."
36
Rob Landley336c44a2016-03-02 15:20:04 -060037testing "extra args" "$PRINTF 'abc%s!%ddef\n' X 42 ARG 36" \
Rob Landley70cbfe82015-01-10 20:02:21 -060038 "abcX!42def\nabcARG!36def\n" "" ""
39
Rob Landley336c44a2016-03-02 15:20:04 -060040testing "'%3c'" "$PRINTF '%3c' x" " x" "" ""
41testing "'%-3c'" "$PRINTF '%-3c' x" "x " "" ""
42testing "'%+d'" "$PRINTF '%+d' 5" "+5" "" ""
Rob Landleyd0dead32015-01-11 10:16:38 -060043
Rob Landley70cbfe82015-01-10 20:02:21 -060044
Rob Landley336c44a2016-03-02 15:20:04 -060045testing "'%5d%4d' 1 21 321 4321 54321" \
Rob Landleydd2cc652015-01-06 15:06:51 -060046 "$PRINTF '%5d%4d' 1 21 321 4321 54321" " 1 21 321432154321 0" "" ""
Rob Landley336c44a2016-03-02 15:20:04 -060047testing "'%c %c' 78 79" "$PRINTF '%c %c' 78 79" "7 7" "" ""
48testing "'%d %d' 78 79" "$PRINTF '%d %d' 78 79" "78 79" "" ""
49testing "'%f %f' 78 79" "$PRINTF '%f %f' 78 79" \
Divya Kotharia0f56be2014-06-26 07:25:20 -050050 "78.000000 79.000000" "" ""
Rob Landley54e83132016-10-18 16:28:47 -050051testing "'f f' 78 79" "$PRINTF 'f f' 78 79 2>/dev/null" "f f" "" ""
Rob Landley336c44a2016-03-02 15:20:04 -060052testing "'%i %i' 78 79" "$PRINTF '%i %i' 78 79" "78 79" "" ""
53testing "'%o %o' 78 79" "$PRINTF '%o %o' 78 79" "116 117" "" ""
54testing "'%u %u' 78 79" "$PRINTF '%u %u' 78 79" "78 79" "" ""
55testing "'%u %u' -1 -2" "$PRINTF '%u %u' -1 -2" \
Divya Kotharia0f56be2014-06-26 07:25:20 -050056 "18446744073709551615 18446744073709551614" "" ""
Rob Landley336c44a2016-03-02 15:20:04 -060057testing "'%x %X' 78 79" "$PRINTF '%x %X' 78 79" "4e 4F" "" ""
58testing "'%g %G' 78 79" "$PRINTF '%g %G' 78 79" "78 79" "" ""
59testing "'%s %s' 78 79" "$PRINTF '%s %s' 78 79" "78 79" "" ""
Rob Landley5d431d12015-03-28 13:22:27 -050060
Rob Landley336c44a2016-03-02 15:20:04 -060061testing "%.s acts like %.0s" "$PRINTF %.s_ 1 2 3 4 5" "_____" "" ""
Rob Landleybf950cd2016-07-04 02:59:09 -050062testing "corner case" "$PRINTF '\\8'" '\8' '' ''
63
64# The posix spec explicitly specifies inconsistent behavior,
65# so treating the \0066 in %b like the \0066 not in %b is wrong because posix.
66testing "printf posix inconsistency" "$PRINTF '\\0066-%b' '\\0066'" "\x066-6" \
67 "" ""
Rob Landley54e83132016-10-18 16:28:47 -050068
69testing "printf \x" "$PRINTF 'A\x1b\x2B\x3Q\xa' | od -An -tx1" \
70 " 41 1b 2b 03 51 0a\n" "" ""