xref: /titanic_41/usr/src/test/util-tests/tests/printf/printf_test.ksh (revision f3312ec0e8acbd249df97358fb8c3ca92f4e089c)
1#! /usr/bin/ksh
2#
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# http://www.illumos.org/license/CDDL.
12#
13
14#
15# Copyright 2014 Garrett D'Amore <garrett@damore.org>
16#
17
18PRINTF=${PRINTF:=/usr/bin/printf}
19
20test_start() {
21	print "TEST STARTING ${1}: ${2}"
22}
23
24test_pass() {
25	print "TEST PASS: ${1}"
26}
27
28test_fail() {
29	print "TEST FAIL: ${1}: ${2}"
30	exit -1
31}
32
33checkrv() {
34	if [[ $? -ne 0 ]]; then
35		test_fail $1 "exit failure"
36	fi
37}
38
39compare() {
40	if [[ "$2" != "$3" ]]; then
41		test_fail $1 "compare mismatch, got [$2] expected [$3]"
42	fi
43}
44
45typeset -A tests=()
46
47
48typeset -A tests[01]=()
49tests[01][desc]="hexadecimal lowercase"
50tests[01][format]='%04x'
51tests[01][args]="255"
52tests[01][result]="00ff"
53
54typeset -A tests[02]=()
55tests[02][desc]="hexadecimal 32-bit"
56tests[02][format]='%08x'
57tests[02][args]='65537'
58tests[02][result]=00010001
59
60typeset -A tests[03]=()
61tests[03][desc]="multiple arguments"
62tests[03][format]='%d %s '
63tests[03][args]="1 one 2 two 3 three"
64tests[03][result]='1 one 2 two 3 three '
65
66typeset -A tests[04]=()
67tests[04][desc]="variable position parameters"
68tests[04][format]='%2$s %1$d '
69tests[04][args]="1 one 2 two 3 three"
70tests[04][result]='one 1 two 2 three 3 '
71
72typeset -A tests[05]=()
73tests[05][desc]="width"
74tests[05][format]='%10s'
75tests[05][args]="abcdef"
76tests[05][result]='    abcdef'
77
78typeset -A tests[06]=()
79tests[06][desc]="width and precision"
80tests[06][format]='%10.3s'
81tests[06][args]="abcdef"
82tests[06][result]='       abc'
83
84typeset -A tests[07]=()
85tests[07][desc]="variable width and precision"
86tests[07][format]='%*.*s'
87tests[07][args]="10 3 abcdef"
88tests[07][result]='       abc'
89
90typeset -A tests[08]=()
91tests[08][desc]="variable position width and precision"
92tests[08][format]='%2$*1$.*3$s'
93tests[08][args]="10 abcdef 3"
94tests[08][result]='       abc'
95
96typeset -A tests[09]=()
97tests[09][desc]="multi variable position width and precision"
98tests[09][format]='%2$*1$.*3$s'
99tests[09][args]="10 abcdef 3 5 xyz 1"
100tests[09][result]='       abc    x'
101
102typeset -A tests[10]=()
103tests[10][desc]="decimal from hex"
104tests[10][format]='%d '
105tests[10][args]="0x1000 0XA"
106tests[10][result]='4096 10 '
107
108typeset -A tests[11]=()
109tests[11][desc]="negative dec (64-bit)"
110tests[11][format]='%x'
111tests[11][args]="-1"
112tests[11][result]='ffffffffffffffff'
113
114typeset -A tests[12]=()
115tests[12][desc]="float (basic)"
116tests[12][format]='%f'
117tests[12][args]="3.14"
118tests[12][result]='3.140000'
119
120typeset -A tests[12]=()
121tests[12][desc]="float precision"
122tests[12][format]='%.2f'
123tests[12][args]="3.14159"
124tests[12][result]='3.14'
125
126typeset -A tests[13]=()
127tests[13][desc]="left justify"
128tests[13][format]='%-5d'
129tests[13][args]="45"
130tests[13][result]='45   '
131
132typeset -A tests[14]=()
133tests[14][desc]="newlines"
134tests[14][format]='%s\n%s\n%s'
135tests[14][args]="one two three"
136tests[14][result]='one
137two
138three'
139
140typeset -A tests[15]=()
141tests[15][desc]="embedded octal escape"
142tests[15][format]='%s\41%s'
143tests[15][args]="one two"
144tests[15][result]='one!two'
145
146typeset -A tests[16]=()
147tests[16][desc]="backslash string (%b)"
148tests[16][format]='%b'
149tests[16][args]='\0101\0102\0103'
150tests[16][result]='ABC'
151
152typeset -A tests[17]=()
153tests[17][desc]="backslash c in %b"
154tests[17][format]='%b%s'
155tests[17][args]='\0101\cone two'
156tests[17][result]='A'
157
158typeset -A tests[18]=()
159tests[18][desc]="backslash octal in format"
160tests[18][format]='HI\1120K\0112tabbed\11again'
161tests[18][args]=
162tests[18][result]='HIJ0K	2tabbed	again'
163
164typeset -A tests[19]=()
165tests[19][desc]="backslash octal in %b"
166tests[19][format]="%b"
167tests[19][args]='HI\0112K\011tabbed'
168tests[19][result]='HIJK	tabbed'
169
170typeset -A tests[20]=()
171tests[20][desc]="numeric %d and ASCII conversions"
172tests[20][format]='%d '
173tests[20][args]="3 +3 -3 \"3 \"+ '-"
174tests[20][result]='3 3 -3 51 43 45 '
175
176typeset -A tests[21]=()
177tests[21][desc]="verify second arg only"
178tests[21][format]='%2$s'
179tests[21][args]='abc xyz'
180tests[21][result]="xyz"
181
182typeset -A tests[22]=()
183tests[22][desc]="verify missing signed arg"
184tests[22][format]='%d %d'
185tests[22][args]='151'
186tests[22][result]="151 0"
187
188typeset -A tests[23]=()
189tests[23][desc]="verify missing unsigned arg"
190tests[23][format]='%u %u'
191tests[23][args]='151'
192tests[23][result]="151 0"
193
194#debug=yes
195
196for i in "${!tests[@]}"; do
197	t=test_$i
198	desc=${tests[$i][desc]}
199	format=${tests[$i][format]}
200	args="${tests[$i][args]}"
201	result=${tests[$i][result]}
202
203	test_start $t "${tests[$i][desc]}"
204	[[ -n "$debug" ]] && echo $PRINTF "$format" "${args[@]}"
205	comp=$($PRINTF "$format" ${args[@]})
206	checkrv $t
207	[[ -n "$debug" ]] && echo "got [$comp]"
208	good=$result
209	compare $t "$comp" "$good"
210	test_pass $t
211done
212