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