1# 2# CDDL HEADER START 3# 4# The contents of this file are subject to the terms of the 5# Common Development and Distribution License (the "License"). 6# You may not use this file except in compliance with the License. 7# 8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9# or http://www.opensolaris.org/os/licensing. 10# See the License for the specific language governing permissions 11# and limitations under the License. 12# 13# When distributing Covered Code, include this CDDL HEADER in each 14# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15# If applicable, add the following below this CDDL HEADER, with the 16# fields enclosed by brackets "[]" replaced with your own identifying 17# information: Portions Copyright [yyyy] [name of copyright owner] 18# 19# CDDL HEADER END 20# 21 22# 23# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 24# 25 26function err_exit2 27{ 28 print -u2 -n "\t" 29 print -u2 -r ${Command}[$1]: "${@:2}" 30 (( Errors < 127 && Errors++ )) 31} 32 33function testfunc 34{ 35 integer line_number=$1 36 typeset cmd="$2" 37 typeset expected_output="$3" 38 typeset output 39 40 output="$($SHELL -c "${cmd}" 2>&1 )" 41 42 [[ "${output}" != "${expected_output}" ]] && err_exit2 ${line_number} "${output} != ${expected_output}" 43} 44alias testfunc='testfunc $LINENO' 45alias err_exit='err_exit2 $LINENO' 46 47set -o nounset 48Command=${0##*/} 49integer Errors=0 50 51 52# string 53testfunc '(function l { typeset -S x ; x+="#" ; $1 && print "$x" ; } ; l false ; l false ; l true)' "###" 54testfunc 'function l { typeset -S x=">" ; x+="#" ; $1 && print "$x" ; } ; l false ; l false ; l true' ">###" 55testfunc 'function l { typeset -S x=">" ; x+="#" ; $1 && print "$x" ; } ; l false ; (l false) ; l true' ">##" 56testfunc 'function l { typeset -S x=">" ; x+="#" ; $1 && print "$x" ; } ; l false; ( ulimit -c 0 ; l false) ; l true' ">##" 57 58# integer 59testfunc '(function l { typeset -S -i x ; x+=1 ; $1 && print "$x" ; } ; l false ; l false ; l true )' "3" 60testfunc '(function l { typeset -S -i x ; x+=1 ; $1 && print "$x" ; } ; l false ; (l false) ; l true )' "2" 61 62# float 63testfunc '(function l { float -S x=0.5 ; (( x+=.5 )) ; $1 && print "$x" ; } ; l false ; l false ; l true )' "2" 64testfunc '(function l { float -S x=0.5 ; (( x+=.5 )) ; $1 && print "$x" ; } ; l false ; (l false) ; l true )' "1.5" 65 66# compound variable 67[[ "${ 68 function l 69 { 70 typeset -S s=( a=0 b=0 ) 71 72 (( s.a++, s.b++ )) 73 74 $1 && printf 'a=%d, b=%d\n' s.a s.b 75 } 76 l false ; l false ; l true 77}" != "a=3, b=3" ]] && err_exit "static compound var failed" 78 79 80# array variable 81[[ "$( 82 function ar 83 { 84 typeset -a -S s=( "hello" ) 85 86 s+=( "an element" ) 87 88 $1 && { printf '%s' "${s[@]}" ; printf '\n' ; } 89 } 90 ar false ; ar false ; ar true 91)" != "helloan elementan elementan element" ]] && err_exit "static array var failed" 92 93 94# Test visibilty of "global" vs. "static" variables. if we have a "static" variable in a 95# function and "unset" it we should see a global variable with the same 96# name, right ? 97integer hx=5 98function test_hx_scope 99{ 100 integer -S hx=9 101 $2 && unset hx 102 $1 && printf "hx=%d\n" hx 103} 104test_hx_scope false false 105test_hx_scope false false 106# first test the "unset" call in a $(...) subshell... 107[[ "$( test_hx_scope true true )" != "hx=5" ]] && err_exit "can't see global variable hx after unsetting static variable hx" 108# ... end then test whether the value has changed. 109[[ "${ test_hx_scope true false }" != "hx=9" ]] && err_exit "hx variable somehow changed" 110 111 112# tests done 113exit $((Errors)) 114