about summary refs log tree commit diff
path: root/Test/B02typeset.ztst
blob: 51ebc6535b0efed1de9d5155cf86df800f3daf5f (plain) (blame)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# There are certain usages of typeset and its synonyms that it is not
# possible to test here, because they must appear at the top level and
# everything that follows is processed by an "eval" within a function.

# Equivalences:
#  declare	typeset
#  export	typeset -x		and typeset -x implies -g
#  float	typeset -E
#  functions	typeset -f
#  integer	typeset -i
#  local	typeset +g -m		approximately
#  readonly	typeset -r

# Tested elsewhere:
#  Equivalence of autoload and typeset -fu 	A05execution
#  Associative array creation & assignment	D04parameter, D06subscript
#  Effects of GLOBAL_EXPORT			E01options
#  Function tracing (typeset -ft)		E02xtrace

# Not yet tested:
#  Assorted illegal flag combinations

%prep

  setopt noglob

  scalar=scalar
  array=(a r r a y)

  scope00() {
    typeset scalar
    scalar=local
    typeset -a array
    array=(l o c a l)
    print $scalar $array
  }
  scope01() {
    local scalar
    scalar=local
    local -a array
    array=(l o c a l)
    print $scalar $array
  }
  scope02() {
    declare scalar
    scalar=local
    declare -a array
    array=(l o c a l)
    print $scalar $array
  }
  scope10() {
    export outer=outer
    /bin/sh -fc 'echo $outer'
  }
  scope11() {
    typeset -x outer=outer
    /bin/sh -fc 'echo $outer'
  }
  scope12() {
    local -x outer=inner
    /bin/sh -fc 'echo $outer'
  }
  scope13() {
    local -xT OUTER outer
    outer=(i n n e r)
    /bin/sh -fc 'echo $OUTER'
  }

  # Bug?  `typeset -h' complains that ! # $ * - ? @ are not identifiers.
  stress00() {
    typeset -h +g -m [[:alpha:]_]*
    unset -m [[:alpha:]_]*
    typeset +m [[:alpha:]_]*
  }

%test

 typeset +m scalar array
0:Report types of parameters with typeset +m
>scalar
>array array

 scope00
 print $scalar $array
0:Simple local declarations
>local l o c a l
>scalar a r r a y

 scope01
 print $scalar $array
0:Equivalence of local and typeset in functions
>local l o c a l
>scalar a r r a y

 scope02
 print $scalar $array
0:Basic equivalence of declare and typeset
>local l o c a l
>scalar a r r a y

 declare +m scalar
0:declare previously lacked -m/+m options
>scalar

 scope10
 print $outer
0:Global export
>outer
>outer

 scope11
 print $outer
0:Equivalence of export and typeset -x
>outer
>outer

 scope12
 print $outer
0:Local export
>inner
>outer

 float f=3.14159
 typeset +m f
 float -E3 f
 print $f
 float -F f
 print $f
0:Floating point, adding a precision, and fixed point
>float local f
>3.14e+00
>3.142

 integer i=3.141
 typeset +m i
 integer -i2 i
 print $i
0:Integer and changing the base
>integer local i
>2#11

 float -E3 f=3.141
 typeset +m f
 integer -i2 f
 typeset +m f
 print $f
0:Conversion of floating point to integer
>float local f
>integer 2 local f
>2#11

 typeset -f
0q:Equivalence of functions and typeset -f
>$(functions)

 readonly r=success
 print $r
 r=failure
1:Readonly declaration
>success
?(eval):3: read-only variable: r

 typeset r=success
 readonly r
 print $r
 r=failure
1:Convert to readonly
>success
?(eval):4: read-only variable: r

 typeset -gU array
 print $array
0:Uniquified arrays and non-local scope
>a r y

 typeset -T SCALAR=l:o:c:a:l array
 print $array
 typeset -U SCALAR
 print $SCALAR $array
0:Tied parameters and uniquified colon-arrays
>l o c a l
>l:o:c:a l o c a

 (setopt NO_multibyte cbases
 LC_ALL=C 2>/dev/null
 typeset -T SCALAR=$'l\x83o\x83c\x83a\x83l' array $'\x83'
 print $array
 typeset -U SCALAR
 for (( i = 1; i <= ${#SCALAR}; i++ )); do
   char=$SCALAR[i]
   print $(( [#16] #char ))
 done
 print $array)
0:Tied parameters and uniquified arrays with meta-character as separator
>l o c a l
>0x6C
>0x83
>0x6F
>0x83
>0x63
>0x83
>0x61
>l o c a

 typeset -T SCALAR=$'l\000o\000c\000a\000l' array $'\000'
 typeset -U SCALAR
 print $array
 [[ $SCALAR == $'l\000o\000c\000a' ]]
0:Tied parameters and uniquified arrays with NUL-character as separator
>l o c a

 typeset -T SCALAR array
 typeset +T SCALAR
1:Untying is prohibited
?(eval):typeset:2: use unset to remove tied variables

 OUTER=outer
 scope13
 print $OUTER
0:Export of tied parameters
>i:n:n:e:r
>outer

 typeset -TU MORESTUFF=here-we-go-go-again morestuff '-'
 print -l $morestuff
0:Tied arrays with separator specified
>here
>we
>go
>again

 typeset -T THIS will not work
1:Tied array syntax
?(eval):typeset:1: -T requires names of scalar and array

 local array[2]=x
1:Illegal local array element assignment
?(eval):local:1: array[2]: can't create local array elements

 local -a array
 typeset array[1]=a array[2]=b array[3]=c
 print $array
0:Legal local array element assignment
>a b c

 local -A assoc
 local b=1 ;: to stomp assoc[1] if assoc[b] is broken
 typeset assoc[1]=a assoc[b]=2 assoc[3]=c
 print $assoc[1] $assoc[b] $assoc[3]
0:Legal local associative array element assignment
>a 2 c

 local scalar scalar[1]=a scalar[2]=b scalar[3]=c
 print $scalar
0:Local scalar subscript assignment
>abc

 typeset -L 10 fools
 for fools in "   once" "twice"  "      thrice" "   oops too long here"; do
   print "'$fools'"
 done
0:Left justification of scalars
>'once      '
>'twice     '
>'thrice    '
>'oops too l'

 typeset -L 10 -F 3 foolf
 for foolf in 1.3 4.6 -2.987 -4.91031; do
   print "'$foolf'"
 done
0:Left justification of floating point
>'1.300     '
>'4.600     '
>'-2.987    '
>'-4.910    '

 typeset -L 10 -Z foolzs
 for foolzs in 001.3 04.6 -2.987 -04.91231; do
   print "'$foolzs'"
 done
0:Left justification of scalars with zero suppression
>'1.3       '
>'4.6       '
>'-2.987    '
>'-04.91231 '

 typeset -R 10 foors
 for foors in short longer even-longer; do
   print "'$foors'"
 done
0:Right justification of scalars
>'     short'
>'    longer'
>'ven-longer'

 typeset -Z 10 foozs
 for foozs in 42 -42 " 43" " -43"; do
   print "'$foozs'"
 done
0:Right justification of scalars with zeroes
>'0000000042'
>'       -42'
>' 000000043'
>'       -43'

 integer -Z 10 foozi
 for foozi in 42 -42 " 43" " -43"; do
   print "'$foozi'"
 done
0:Right justification of integers with zero, no initial base
>'0000000042'
>'-000000042'
>'0000000043'
>'-000000043'
# In case you hadn't twigged, the spaces are absorbed in the initial
# math evaluation, so don't get through.

 unsetopt cbases
 integer -Z 10 -i 16 foozi16
 for foozi16 in 42 -42 " 43" " -43"; do
   print "'$foozi16'"
 done
0:Right justification of integers with zero, base 16, C_BASES off
>'16#000002A'
>'-16#00002A'
>'16#000002B'
>'-16#00002B'

 setopt cbases
 integer -Z 10 -i 16 foozi16c
 for foozi16c in 42 -42 " 43" " -43"; do
   print "'$foozi16c'"
 done
0:Right justification of integers with zero, base 16, C_BASES on
>'0x0000002A'
>'-0x000002A'
>'0x0000002B'
>'-0x000002B'

 setopt cbases
 integer -Z 10 -i 16 foozi16c
 for foozi16c in 0x1234 -0x1234; do
   for (( i = 1; i <= 5; i++ )); do
       print "'${foozi16c[i,11-i]}'"
   done
   print "'${foozi16c[-2]}'"
 done
0:Extracting substrings from padded integers
>'0x00001234'
>'x0000123'
>'000012'
>'0001'
>'00'
>'3'
>'-0x0001234'
>'0x000123'
>'x00012'
>'0001'
>'00'
>'3'

 typeset -F 3 -Z 10 foozf
 for foozf in 3.14159 -3.14159 4 -4; do
   print "'$foozf'"
 done
0:Right justification of fixed point numbers with zero
>'000003.142'
>'-00003.142'
>'000004.000'
>'-00004.000'

 stress00
 print $scalar $array
0q:Stress test: all parameters are local and unset, using -m
>scalar a r y

 local parentenv=preserved
 fn() {
  typeset -h +g -m \*
  unset -m \*
  integer i=9
  float -H f=9
  declare -t scalar
  declare -H -a array
  typeset
  typeset +
 }
 fn
 echo $parentenv
0:Parameter hiding and tagging, printing types and values
>array local array
>float local f
>integer local i=9
>local tagged scalar=''
>array local array
>float local f
>integer local i
>local tagged scalar
>preserved

 export ENVFOO=bar
 print ENVFOO in environment
 env | grep '^ENVFOO'
 print Changing ENVFOO
 ENVFOO="not bar any more"
 env | grep '^ENVFOO'
 unset ENVFOO
 print ENVFOO no longer in environment
 env | grep '^ENVFOO'
1:Adding and removing values to and from the environment
>ENVFOO in environment
>ENVFOO=bar
>Changing ENVFOO
>ENVFOO=not bar any more
>ENVFOO no longer in environment

 (export FOOENV=BAR
 env | grep '^FOOENV'
 print Exec
 exec $ZTST_testdir/../Src/zsh -fc '
 print Unset
 unset FOOENV
 env | grep "^FOOENV"')
1:Can unset environment variables after exec
>FOOENV=BAR
>Exec
>Unset

 local case1=upper
 typeset -u case1
 print $case1
 upper="VALUE OF \$UPPER"
 print ${(P)case1}
0:Upper case conversion, does not apply to values used internally
>UPPER
>VALUE OF $UPPER

 local case2=LOWER
 typeset -l case2
 print $case2
 LOWER="value of \$lower"
 print ${(P)case2}
0:Lower case conversion, does not apply to values used internally
>lower
>value of $lower

 typeset -a array
 array=(foo bar)
 fn() { typeset -p array nonexistent; }
 fn
1:declare -p shouldn't create scoped values
>typeset -a array
>array=(foo bar)
?fn:typeset: no such variable: nonexistent

 unsetopt typesetsilent
 silent1(){ typeset -g silence; }
 silent2(){ local silence; silent1; }
 silent2
0:typeset -g should be silent even without TYPESET_SILENT

 typeset -T TIED_SCALAR tied_array
 TIED_SCALAR=foo:bar
 print $tied_array
 typeset -T TIED_SCALAR=goo:car tied_array
 print $tied_array
0:retying arrays to same array works
>foo bar
>goo car