1598f4ceeSGarrett D'Amore#! /usr/bin/ksh
2598f4ceeSGarrett D'Amore#
3598f4ceeSGarrett D'Amore#
4598f4ceeSGarrett D'Amore# This file and its contents are supplied under the terms of the
5598f4ceeSGarrett D'Amore# Common Development and Distribution License ("CDDL"), version 1.0.
6598f4ceeSGarrett D'Amore# You may only use this file in accordance with the terms of version
7598f4ceeSGarrett D'Amore# 1.0 of the CDDL.
8598f4ceeSGarrett D'Amore#
9598f4ceeSGarrett D'Amore# A full copy of the text of the CDDL should have accompanied this
10598f4ceeSGarrett D'Amore# source.  A copy of the CDDL is also available via the Internet at
11598f4ceeSGarrett D'Amore# http://www.illumos.org/license/CDDL.
12598f4ceeSGarrett D'Amore#
13598f4ceeSGarrett D'Amore
14598f4ceeSGarrett D'Amore#
15598f4ceeSGarrett D'Amore# Copyright 2014 Garrett D'Amore <garrett@damore.org>
16598f4ceeSGarrett D'Amore#
17598f4ceeSGarrett D'Amore
18598f4ceeSGarrett D'AmorePRINTF=${PRINTF:=/usr/bin/printf}
19598f4ceeSGarrett D'Amore
20598f4ceeSGarrett D'Amoretest_start() {
21598f4ceeSGarrett D'Amore	print "TEST STARTING ${1}: ${2}"
22598f4ceeSGarrett D'Amore}
23598f4ceeSGarrett D'Amore
24598f4ceeSGarrett D'Amoretest_pass() {
25598f4ceeSGarrett D'Amore	print "TEST PASS: ${1}"
26598f4ceeSGarrett D'Amore}
27598f4ceeSGarrett D'Amore
28598f4ceeSGarrett D'Amoretest_fail() {
29598f4ceeSGarrett D'Amore	print "TEST FAIL: ${1}: ${2}"
30598f4ceeSGarrett D'Amore	exit -1
31598f4ceeSGarrett D'Amore}
32598f4ceeSGarrett D'Amore
33598f4ceeSGarrett D'Amorecheckrv() {
34598f4ceeSGarrett D'Amore	if [[ $? -ne 0 ]]; then
35598f4ceeSGarrett D'Amore		test_fail $1 "exit failure"
36598f4ceeSGarrett D'Amore	fi
37598f4ceeSGarrett D'Amore}
38598f4ceeSGarrett D'Amore
39598f4ceeSGarrett D'Amorecompare() {
40598f4ceeSGarrett D'Amore	if [[ "$2" != "$3" ]]; then
41598f4ceeSGarrett D'Amore		test_fail $1 "compare mismatch, got [$2] expected [$3]"
42598f4ceeSGarrett D'Amore	fi
43598f4ceeSGarrett D'Amore}
44598f4ceeSGarrett D'Amore
45598f4ceeSGarrett D'Amoretypeset -A tests=()
46598f4ceeSGarrett D'Amore
47598f4ceeSGarrett D'Amore
48598f4ceeSGarrett D'Amoretypeset -A tests[01]=()
49598f4ceeSGarrett D'Amoretests[01][desc]="hexadecimal lowercase"
50598f4ceeSGarrett D'Amoretests[01][format]='%04x'
51598f4ceeSGarrett D'Amoretests[01][args]="255"
52598f4ceeSGarrett D'Amoretests[01][result]="00ff"
53598f4ceeSGarrett D'Amore
54598f4ceeSGarrett D'Amoretypeset -A tests[02]=()
55598f4ceeSGarrett D'Amoretests[02][desc]="hexadecimal 32-bit"
56598f4ceeSGarrett D'Amoretests[02][format]='%08x'
57598f4ceeSGarrett D'Amoretests[02][args]='65537'
58598f4ceeSGarrett D'Amoretests[02][result]=00010001
59598f4ceeSGarrett D'Amore
60598f4ceeSGarrett D'Amoretypeset -A tests[03]=()
61598f4ceeSGarrett D'Amoretests[03][desc]="multiple arguments"
62598f4ceeSGarrett D'Amoretests[03][format]='%d %s '
63598f4ceeSGarrett D'Amoretests[03][args]="1 one 2 two 3 three"
64598f4ceeSGarrett D'Amoretests[03][result]='1 one 2 two 3 three '
65598f4ceeSGarrett D'Amore
66598f4ceeSGarrett D'Amoretypeset -A tests[04]=()
67598f4ceeSGarrett D'Amoretests[04][desc]="variable position parameters"
68598f4ceeSGarrett D'Amoretests[04][format]='%2$s %1$d '
69598f4ceeSGarrett D'Amoretests[04][args]="1 one 2 two 3 three"
70598f4ceeSGarrett D'Amoretests[04][result]='one 1 two 2 three 3 '
71598f4ceeSGarrett D'Amore
72598f4ceeSGarrett D'Amoretypeset -A tests[05]=()
73598f4ceeSGarrett D'Amoretests[05][desc]="width"
74598f4ceeSGarrett D'Amoretests[05][format]='%10s'
75598f4ceeSGarrett D'Amoretests[05][args]="abcdef"
76598f4ceeSGarrett D'Amoretests[05][result]='    abcdef'
77598f4ceeSGarrett D'Amore
78598f4ceeSGarrett D'Amoretypeset -A tests[06]=()
79598f4ceeSGarrett D'Amoretests[06][desc]="width and precision"
80598f4ceeSGarrett D'Amoretests[06][format]='%10.3s'
81598f4ceeSGarrett D'Amoretests[06][args]="abcdef"
82598f4ceeSGarrett D'Amoretests[06][result]='       abc'
83598f4ceeSGarrett D'Amore
84598f4ceeSGarrett D'Amoretypeset -A tests[07]=()
85598f4ceeSGarrett D'Amoretests[07][desc]="variable width and precision"
86598f4ceeSGarrett D'Amoretests[07][format]='%*.*s'
87598f4ceeSGarrett D'Amoretests[07][args]="10 3 abcdef"
88598f4ceeSGarrett D'Amoretests[07][result]='       abc'
89598f4ceeSGarrett D'Amore
90598f4ceeSGarrett D'Amoretypeset -A tests[08]=()
91598f4ceeSGarrett D'Amoretests[08][desc]="variable position width and precision"
92598f4ceeSGarrett D'Amoretests[08][format]='%2$*1$.*3$s'
93598f4ceeSGarrett D'Amoretests[08][args]="10 abcdef 3"
94598f4ceeSGarrett D'Amoretests[08][result]='       abc'
95598f4ceeSGarrett D'Amore
96598f4ceeSGarrett D'Amoretypeset -A tests[09]=()
97598f4ceeSGarrett D'Amoretests[09][desc]="multi variable position width and precision"
98598f4ceeSGarrett D'Amoretests[09][format]='%2$*1$.*3$s'
99598f4ceeSGarrett D'Amoretests[09][args]="10 abcdef 3 5 xyz 1"
100598f4ceeSGarrett D'Amoretests[09][result]='       abc    x'
101598f4ceeSGarrett D'Amore
102598f4ceeSGarrett D'Amoretypeset -A tests[10]=()
103598f4ceeSGarrett D'Amoretests[10][desc]="decimal from hex"
104598f4ceeSGarrett D'Amoretests[10][format]='%d '
105598f4ceeSGarrett D'Amoretests[10][args]="0x1000 0XA"
106598f4ceeSGarrett D'Amoretests[10][result]='4096 10 '
107598f4ceeSGarrett D'Amore
108598f4ceeSGarrett D'Amoretypeset -A tests[11]=()
109598f4ceeSGarrett D'Amoretests[11][desc]="negative dec (64-bit)"
110598f4ceeSGarrett D'Amoretests[11][format]='%x'
111598f4ceeSGarrett D'Amoretests[11][args]="-1"
112598f4ceeSGarrett D'Amoretests[11][result]='ffffffffffffffff'
113598f4ceeSGarrett D'Amore
114598f4ceeSGarrett D'Amoretypeset -A tests[12]=()
115598f4ceeSGarrett D'Amoretests[12][desc]="float (basic)"
116598f4ceeSGarrett D'Amoretests[12][format]='%f'
117598f4ceeSGarrett D'Amoretests[12][args]="3.14"
118598f4ceeSGarrett D'Amoretests[12][result]='3.140000'
119598f4ceeSGarrett D'Amore
120598f4ceeSGarrett D'Amoretypeset -A tests[12]=()
121598f4ceeSGarrett D'Amoretests[12][desc]="float precision"
122598f4ceeSGarrett D'Amoretests[12][format]='%.2f'
123598f4ceeSGarrett D'Amoretests[12][args]="3.14159"
124598f4ceeSGarrett D'Amoretests[12][result]='3.14'
125598f4ceeSGarrett D'Amore
126598f4ceeSGarrett D'Amoretypeset -A tests[13]=()
127598f4ceeSGarrett D'Amoretests[13][desc]="left justify"
128598f4ceeSGarrett D'Amoretests[13][format]='%-5d'
129598f4ceeSGarrett D'Amoretests[13][args]="45"
130598f4ceeSGarrett D'Amoretests[13][result]='45   '
131598f4ceeSGarrett D'Amore
132598f4ceeSGarrett D'Amoretypeset -A tests[14]=()
133598f4ceeSGarrett D'Amoretests[14][desc]="newlines"
134598f4ceeSGarrett D'Amoretests[14][format]='%s\n%s\n%s'
135598f4ceeSGarrett D'Amoretests[14][args]="one two three"
136598f4ceeSGarrett D'Amoretests[14][result]='one
137598f4ceeSGarrett D'Amoretwo
138598f4ceeSGarrett D'Amorethree'
139598f4ceeSGarrett D'Amore
140598f4ceeSGarrett D'Amoretypeset -A tests[15]=()
141598f4ceeSGarrett D'Amoretests[15][desc]="embedded octal escape"
142598f4ceeSGarrett D'Amoretests[15][format]='%s\41%s'
143598f4ceeSGarrett D'Amoretests[15][args]="one two"
144598f4ceeSGarrett D'Amoretests[15][result]='one!two'
145598f4ceeSGarrett D'Amore
146598f4ceeSGarrett D'Amoretypeset -A tests[16]=()
147598f4ceeSGarrett D'Amoretests[16][desc]="backslash string (%b)"
148598f4ceeSGarrett D'Amoretests[16][format]='%b'
149598f4ceeSGarrett D'Amoretests[16][args]='\0101\0102\0103'
150598f4ceeSGarrett D'Amoretests[16][result]='ABC'
151598f4ceeSGarrett D'Amore
152598f4ceeSGarrett D'Amoretypeset -A tests[17]=()
153598f4ceeSGarrett D'Amoretests[17][desc]="backslash c in %b"
154598f4ceeSGarrett D'Amoretests[17][format]='%b%s'
155598f4ceeSGarrett D'Amoretests[17][args]='\0101\cone two'
156598f4ceeSGarrett D'Amoretests[17][result]='A'
157598f4ceeSGarrett D'Amore
158598f4ceeSGarrett D'Amoretypeset -A tests[18]=()
159598f4ceeSGarrett D'Amoretests[18][desc]="backslash octal in format"
160598f4ceeSGarrett D'Amoretests[18][format]='HI\1120K\0112tabbed\11again'
161598f4ceeSGarrett D'Amoretests[18][args]=
162598f4ceeSGarrett D'Amoretests[18][result]='HIJ0K	2tabbed	again'
163598f4ceeSGarrett D'Amore
164598f4ceeSGarrett D'Amoretypeset -A tests[19]=()
165598f4ceeSGarrett D'Amoretests[19][desc]="backslash octal in %b"
166598f4ceeSGarrett D'Amoretests[19][format]="%b"
167598f4ceeSGarrett D'Amoretests[19][args]='HI\0112K\011tabbed'
168598f4ceeSGarrett D'Amoretests[19][result]='HIJK	tabbed'
169598f4ceeSGarrett D'Amore
170598f4ceeSGarrett D'Amoretypeset -A tests[20]=()
171598f4ceeSGarrett D'Amoretests[20][desc]="numeric %d and ASCII conversions"
172598f4ceeSGarrett D'Amoretests[20][format]='%d '
173598f4ceeSGarrett D'Amoretests[20][args]="3 +3 -3 \"3 \"+ '-"
174598f4ceeSGarrett D'Amoretests[20][result]='3 3 -3 51 43 45 '
175598f4ceeSGarrett D'Amore
176598f4ceeSGarrett D'Amoretypeset -A tests[21]=()
177598f4ceeSGarrett D'Amoretests[21][desc]="verify second arg only"
178598f4ceeSGarrett D'Amoretests[21][format]='%2$s'
179598f4ceeSGarrett D'Amoretests[21][args]='abc xyz'
180598f4ceeSGarrett D'Amoretests[21][result]="xyz"
181598f4ceeSGarrett D'Amore
182*e56bd285SGarrett D'Amoretypeset -A tests[22]=()
183*e56bd285SGarrett D'Amoretests[22][desc]="verify missing signed arg"
184*e56bd285SGarrett D'Amoretests[22][format]='%d %d'
185*e56bd285SGarrett D'Amoretests[22][args]='151'
186*e56bd285SGarrett D'Amoretests[22][result]="151 0"
187*e56bd285SGarrett D'Amore
188*e56bd285SGarrett D'Amoretypeset -A tests[23]=()
189*e56bd285SGarrett D'Amoretests[23][desc]="verify missing unsigned arg"
190*e56bd285SGarrett D'Amoretests[23][format]='%u %u'
191*e56bd285SGarrett D'Amoretests[23][args]='151'
192*e56bd285SGarrett D'Amoretests[23][result]="151 0"
193*e56bd285SGarrett D'Amore
194598f4ceeSGarrett D'Amore#debug=yes
195598f4ceeSGarrett D'Amore
196598f4ceeSGarrett D'Amorefor i in "${!tests[@]}"; do
197598f4ceeSGarrett D'Amore	t=test_$i
198598f4ceeSGarrett D'Amore	desc=${tests[$i][desc]}
199598f4ceeSGarrett D'Amore	format=${tests[$i][format]}
200598f4ceeSGarrett D'Amore	args="${tests[$i][args]}"
201598f4ceeSGarrett D'Amore	result=${tests[$i][result]}
202598f4ceeSGarrett D'Amore
203598f4ceeSGarrett D'Amore	test_start $t "${tests[$i][desc]}"
204598f4ceeSGarrett D'Amore	[[ -n "$debug" ]] && echo $PRINTF "$format" "${args[@]}"
205598f4ceeSGarrett D'Amore	comp=$($PRINTF "$format" ${args[@]})
206598f4ceeSGarrett D'Amore	checkrv $t
207598f4ceeSGarrett D'Amore	[[ -n "$debug" ]] && echo "got [$comp]"
208598f4ceeSGarrett D'Amore	good=$result
209598f4ceeSGarrett D'Amore	compare $t "$comp" "$good"
210598f4ceeSGarrett D'Amore	test_pass $t
211598f4ceeSGarrett D'Amoredone
212