-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_printf.sh
More file actions
executable file
·32 lines (28 loc) · 925 Bytes
/
test_printf.sh
File metadata and controls
executable file
·32 lines (28 loc) · 925 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env bash
set -euo pipefail
source "$(dirname "$0")/helpers.sh"
pass=0 fail=0
run_test() {
local name="$1"
local expected="$2"
shift 2
local actual
actual=$("$CFBOX" printf "$@")
if [[ "$expected" == "$actual" ]]; then
((++pass))
else
echo "FAIL [$name]: expected=$(printf '%q' "$expected") actual=$(printf '%q' "$actual")"
((++fail))
fi
}
run_test "string" "hello world" '%s %s' hello world
run_test "int" "42" '%d' 42
run_test "float" "3.14" '%.2f' 3.14159
run_test "char" "A" '%c' ABC
run_test "percent" "%" '%%'
# escape_newline: $() strips trailing newline, so add X marker
actual=$("$CFBOX" printf 'hello\n'; echo X)
if [[ "$actual" == $'hello\nX' ]]; then ((++pass)); else echo "FAIL [escape_newline] got $(printf '%q' "$actual")"; ((++fail)); fi
run_test "reuse_format" "abc" '%s' a b c
echo "printf: $pass passed, $fail failed"
[[ $fail -eq 0 ]]