tcltest: do a better job of cleanup up after tests
[jimtcl.git] / tests / array.test
blobba88147bb5b21a9987786942b5baa11b794f06fa
1 source [file dirname [info script]]/testing.tcl
3 needs cmd array
5 unset -nocomplain a
6 array set a {
7         1 one
8         2 two
9         22 "twenty two"
10         3 three
13 test array-1.1 "array exists - true" {
14         array exists a
15 } {1}
17 test array-1.2 "array exists - false" {
18         array exists b
19 } {0}
21 test array-1.3 "array size" {
22         array size a
23 } {4}
25 test array-1.4 "array size - nonexistant" {
26         array size b
27 } {0}
29 test array-1.5 "array get" {
30         set result {}
31         foreach {name value} [array get a] {
32                 lappend result $name $value
33         }
34         lsort $result
35 } {1 2 22 3 one three {twenty two} two}
37 test array-1.6 "array get - pattern" {
38         set result {}
39         foreach {name value} [array get a 2*] {
40                 lappend result $name $value
41         }
42         lsort $result
43 } {2 22 {twenty two} two}
45 test array-1.7 "array names" {
46         lsort [array names a]
47 } {1 2 22 3}
49 test array-1.8 "array get - pattern" {
50         lsort [array names a 2*]
51 } {2 22}
53 #set b $a
54 array set b [array get a]
56 test array-1.9 "array set - replace" {
57         array set b {22 twenty-two}
58         set b(22)
59 } {twenty-two}
61 test array-1.10 "array unset - pattern" {
62         array unset b 2*
63         lsort [array names b]
64 } {1 3}
66 test array-1.11 "array unset - all" {
67         array unset b
68         list [array size b] [array exists b]
69 } {0 0}
71 test array-1.12 "array set to invalid variable" -body {
72         unset -nocomplain a b
73         set a 1
74         array set a(1) {b c}
75 } -returnCodes error -result {can't set "a(1)": variable isn't array}
77 test array-1.13 "unset missing array element" -body {
78         unset -nocomplain a
79         set a(1) one
80         unset a(2)
81 } -returnCodes error -result {can't unset "a(2)": no such element in array}
83 test array-1.14 "access array via unset var" -body {
84         unset -nocomplain b
85         expr {$a($b) + 4}
86 } -returnCodes error -result {can't read "b": no such variable}
88 testreport