zlib: Don't use PASTE for INTMAX error messages
[jimtcl.git] / tests / dict.test
blobc0528e82e56df0e42e8755a02ab2da010be8ea62
1 source [file dirname [info script]]/testing.tcl
3 test dict-1.1 "Basic dict" {
4         set d [dict create]
5         dict set d fruit apple
6         dict set d car holden
7         #puts "d=$d"
8         #puts "d(fruit)=$d(fruit)"
9         dict get $d car
10 } {holden}
12 catch {unset d}
14 test dict-2.1 "Dict via reference" references {
15         set d [dict create]
16         dict set d fruit apple
17         dict set d car holden
19         # now create a dictionary reference
20         set dref [ref $d dict]
21         dict get [getref $dref] car
22 } {holden}
24 test dict-2.2 "Modify dict via reference" references {
25         # Get the value out of the refernence
26         set d [getref $dref]
27         # Modify it
28         dict set d car toyota
29         # And put the new value back
30         setref $dref $d
31         # Finally check it
32         dict get [getref $dref] car
33 } {toyota}
35 test dict-2.3 "Modify dict via reference - one line" references {
36         # Get the value out of the refernence
37         set d [getref $dref]
38         setref $dref [dict set d car toyota]
39         # Finally check it
40         dict get [getref $dref] car
41 } {toyota}
43 # Sort a dictionary in key order - return a list
44 proc dictsort {dict} {
45         set result {}
46         foreach k [lsort [dict keys $dict]] {
47                 lappend result $k [dict get $dict $k]
48         }
49         return $result
52 set a [dict create a 1 b 2]
53 set b [dict create b 3 c 4]
54 test dict-3.1 {Merge} {
55         dict merge
56 } {}
57 test dict-3.2 {Merge} {
58         dictsort [dict merge $a]
59 } {a 1 b 2}
60 test dict-3.3 {Merge} {
61         dictsort [dict merge $b]
62 } {b 3 c 4}
63 test dict-3.4 {Merge} {
64         dictsort [dict merge $a $b]
65 } {a 1 b 3 c 4}
66 test dict-3.5 {Merge} {
67         dictsort [dict merge $b $a]
68 } {a 1 b 2 c 4}
69 test dict-3.6 {Merge} {
70         dictsort [dict merge $b $a {a 5}]
71 } {a 5 b 2 c 4}
72 test dict-3.7 {Merge} {
73         dictsort [dict merge {a 5} $b $a]
74 } {a 1 b 2 c 4}
75 test dict-3.8 {Merge} {
76         catch {dict merge 1 $b $a}
77 } 1
78 test dict-3.9 {Merge} {
79         catch {dict merge $b 1 $a}
80 } 1
81 test dict-3.10 {Merge} {
82         catch {dict merge $b $a 1}
83 } 1
84 test dict-3.11 {Merge} {
85         catch {dict merge 1}
86 } 1
88 test dict-4.1 {Dict size} {
89         dict size {a b}
90 } 1
91 test dict-4.2 {Dict size} {
92         dict size {a b c d}
93 } 2
95 test dict-5.1 {Dict with} {
96         proc a {} {
97                 set x [dict create a b c d]
98                 dict with x {
99                         set a B
100                         unset c
101                 }
102                 set x
103         }
104         dictsort [a]
105 } {a B}
106 test dict-5.2 {Dict with} {
107         proc a {} {
108                 set x [dict create a b c d]
109                 dict with x {
110                         set a B
111                         unset c
112                 }
113                 set x
114         }
115         dictsort [a]
116 } {a B}
118 test dict-22.1 {dict with command} {
119     list [catch {dict with} msg] $msg
120 } {1 {wrong # args: should be "dict with dictVar ?key ...? script"}}
121 test dict-22.2 {dict with command} {
122     list [catch {dict with v} msg] $msg
123 } {1 {wrong # args: should be "dict with dictVar ?key ...? script"}}
124 test dict-22.3 {dict with command} {
125     unset -nocomplain v
126     list [catch {dict with v {error "in body"}} msg] $msg
127 } {1 {can't read "v": no such variable}}
128 test dict-22.4 {dict with command} {
129     set a {b c d e}
130     unset -nocomplain b d
131     set result [list [info exist b] [info exist d]]
132     dict with a {
133         lappend result [info exist b] [info exist d] $b $d
134     }
135     set result
136 } {0 0 1 1 c e}
137 test dict-22.5 {dict with command} {
138     set a {b c d e}
139     dict with a {
140         lassign "$b $d" d b
141     }
142     dictsort $a
143 } {b e d c}
144 test dict-22.6 {dict with command} {
145     set a {b c d e}
146     dict with a {
147         unset b
148         # This *won't* go into the dict...
149         set f g
150     }
151     set a
152 } {d e}
153 test dict-22.7 {dict with command} {
154     set a {b c d e}
155     dict with a {
156         dict unset a b
157     }
158     dictsort $a
159 } {b c d e}
160 test dict-22.8 {dict with command} {
161     set a [dict create b c]
162     dict with a {
163         set b $a
164     }
165     set a
166 } {b {b c}}
167 test dict-22.9 {dict with command} {
168     set a {b {c d}}
169     dict with a b {
170         set c $c$c
171     }
172     set a
173 } {b {c dd}}
174 test dict-22.10 {dict with command: result handling tricky case} {
175     set a {b {c d}}
176     foreach i {0 1} {
177         if {$i} break
178         dict with a b {
179             set a {}
180             # We're checking to see if we lose this break
181             break
182         }
183     }
184     list $i $a
185 } {0 {}}
186 test dict-22.11 {dict with command: no recursive structures [Bug 1786481]} {
187     set foo {t {t {t {inner 1}}}}
188     dict with foo {
189         dict with t {
190             dict with t {
191                 dict with t {
192                     incr inner
193                 }
194             }
195         }
196     }
197     string range [append foo OK] end-1 end
198 } OK
200 test dict-23.1 {dict unset missing last level} {
201     set a {b c d e}
202         dict unset a xyz
203         dict size $a
204 } 2
206 test dict-23.2 {dict unset command} -returnCodes error -body {
207     set dictVar a
208     dict unset dictVar a
209 } -cleanup {
210     unset dictVar
211 } -result {missing value to go with key}
213 test dict-23.3 {dict unset command} -setup {
214     unset -nocomplain dictVar
215 } -body {
216     list [info exists dictVar] [dict unset dictVar a] [info exists dictVar]
217 } -cleanup {
218     unset dictVar
219 } -result {0 {} 1}
221 test dict-23.4 {dict unset command: write failure} -setup {
222     unset -nocomplain dictVar
223 } -body {
224     set dictVar 1
225     dict unset dictVar a
226 } -returnCodes error -cleanup {
227     unset dictVar
228 } -result {missing value to go with key}
230 test dict-24.1 {dict/list shimmering - Bug 3004007} {set l [list p 1 p 2 q 3];dict get $l q;set l} {p 1 p 2 q 3}
231 test dict-24.2 {dict/list shimmering - Bug 3004007} {set l [list p 1 p 2 q 3];dict get $l q;llength $l} 6
233 test dict-24.3 {dict/list shimmering with embedded nulls} {
234         # Must be a string containing embedded nulls that would be double quoted in string form
235         set binary_value 1\000\\
236         set dictVar [dict create value $binary_value]
237         lassign $dictVar k v
238         string length $v
239 } {3}
240 test dict-24.4 {dict/list shimmering with lappend and foreach} {
241         set a [list 1 2 3 4]
243         foreach b $a {
244                 # convert to dict
245                 dict size $a
246                 # append to list
247                 lappend a x y
248         }
249         llength $a
250 } 12
252 testreport