aboutsummaryrefslogtreecommitdiff
path: root/tests/dict.test
blob: 0c2c395bdd202cd8a27de4320708222ff7bcaf4c (plain)
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
source [file dirname [info script]]/testing.tcl

test dict-1.1 "Basic dict" {
	set d [dict create]
	dict set d fruit apple
	dict set d car holden
	#puts "d=$d"
	#puts "d(fruit)=$d(fruit)"
	dict get $d car
} {holden}

catch {unset d}

test dict-2.1 "Dict via reference" references {
	set d [dict create]
	dict set d fruit apple
	dict set d car holden

	# now create a dictionary reference
	set dref [ref $d dict]
	dict get [getref $dref] car
} {holden}

test dict-2.2 "Modify dict via reference" references {
	# Get the value out of the refernence
	set d [getref $dref]
	# Modify it
	dict set d car toyota
	# And put the new value back
	setref $dref $d
	# Finally check it
	dict get [getref $dref] car
} {toyota}

test dict-2.3 "Modify dict via reference - one line" references {
	# Get the value out of the refernence
	set d [getref $dref]
	setref $dref [dict set d car toyota]
	# Finally check it
	dict get [getref $dref] car
} {toyota}

# Sort a dictionary in key order - return a list
proc dictsort {dict} {
	set result {}
	foreach k [lsort [dict keys $dict]] {
		lappend result $k [dict get $dict $k]
	}
	return $result
}

set a [dict create a 1 b 2]
set b [dict create b 3 c 4]
test dict-3.1 {Merge} {
	dict merge
} {}
test dict-3.2 {Merge} {
	dictsort [dict merge $a]
} {a 1 b 2}
test dict-3.3 {Merge} {
	dictsort [dict merge $b]
} {b 3 c 4}
test dict-3.4 {Merge} {
	dictsort [dict merge $a $b]
} {a 1 b 3 c 4}
test dict-3.5 {Merge} {
	dictsort [dict merge $b $a]
} {a 1 b 2 c 4}
test dict-3.6 {Merge} {
	dictsort [dict merge $b $a {a 5}]
} {a 5 b 2 c 4}
test dict-3.7 {Merge} {
	dictsort [dict merge {a 5} $b $a]
} {a 1 b 2 c 4}
test dict-3.8 {Merge} {
	catch {dict merge 1 $b $a}
} 1
test dict-3.9 {Merge} {
	catch {dict merge $b 1 $a}
} 1
test dict-3.10 {Merge} {
	catch {dict merge $b $a 1}
} 1
test dict-3.11 {Merge} {
	catch {dict merge 1}
} 1

test dict-4.1 {Dict size} {
	dict size {a b}
} 1
test dict-4.2 {Dict size} {
	dict size {a b c d}
} 2

test dict-5.1 {Dict with} {
	proc a {} {
		set x [dict create a b c d]
		dict with x {
			set a B
			unset c
		}
		set x
	}
	dictsort [a]
} {a B}
test dict-5.2 {Dict with} {
	proc a {} {
		set x [dict create a b c d]
		dict with x {
			set a B
			unset c
		}
		set x
	}
	dictsort [a]
} {a B}
test dict-5.3 {Dict with return value} {
	proc a {} {
		set x [dict create a b c d]
		dict with x {
			set a B
			unset c
			# dict with should return the script return value
			set retvalue ok
		}
	}
	a
} {ok}

test dict-22.1 {dict with command} {
    list [catch {dict with} msg] $msg
} {1 {wrong # args: should be "dict with dictVar ?key ...? script"}}
test dict-22.2 {dict with command} {
    list [catch {dict with v} msg] $msg
} {1 {wrong # args: should be "dict with dictVar ?key ...? script"}}
test dict-22.3 {dict with command} {
    unset -nocomplain v
    list [catch {dict with v {error "in body"}} msg] $msg
} {1 {can't read "v": no such variable}}
test dict-22.4 {dict with command} {
    set a {b c d e}
    unset -nocomplain b d
    set result [list [info exist b] [info exist d]]
    dict with a {
	lappend result [info exist b] [info exist d] $b $d
    }
    set result
} {0 0 1 1 c e}
test dict-22.5 {dict with command} {
    set a {b c d e}
    dict with a {
	lassign "$b $d" d b
    }
    dictsort $a
} {b e d c}
test dict-22.6 {dict with command} {
    set a {b c d e}
    dict with a {
	unset b
	# This *won't* go into the dict...
	set f g
    }
    set a
} {d e}
test dict-22.7 {dict with command} {
    set a {b c d e}
    dict with a {
	dict unset a b
    }
    dictsort $a
} {b c d e}
test dict-22.8 {dict with command} {
    set a [dict create b c]
    dict with a {
	set b $a
    }
    set a
} {b {b c}}
test dict-22.9 {dict with command} {
    set a {b {c d}}
    dict with a b {
	set c $c$c
    }
    set a
} {b {c dd}}
test dict-22.10 {dict with command: result handling tricky case} {
    set a {b {c d}}
    foreach i {0 1} {
	if {$i} break
	dict with a b {
	    set a {}
	    # We're checking to see if we lose this break
	    break
	}
    }
    list $i $a
} {0 {}}
test dict-22.11 {dict with command: no recursive structures [Bug 1786481]} {
    set foo {t {t {t {inner 1}}}}
    dict with foo {
	dict with t {
	    dict with t {
		dict with t {
		    incr inner
		}
	    }
	}
    }
    string range [append foo OK] end-1 end
} OK

test dict-23.1 {dict unset missing last level} {
    set a {b c d e}
	dict unset a xyz
	dict size $a
} 2

test dict-23.2 {dict unset command} -returnCodes error -body {
    set dictVar a
    dict unset dictVar a
} -cleanup {
    unset dictVar
} -result {missing value to go with key}

test dict-23.3 {dict unset command} -setup {
    unset -nocomplain dictVar
} -body {
    list [info exists dictVar] [dict unset dictVar a] [info exists dictVar]
} -cleanup {
    unset dictVar
} -result {0 {} 1}

test dict-23.4 {dict unset command: write failure} -setup {
    unset -nocomplain dictVar
} -body {
    set dictVar 1
    dict unset dictVar a
} -returnCodes error -cleanup {
    unset dictVar
} -result {missing value to go with key}

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}
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

test dict-24.3 {dict/list shimmering with embedded nulls} {
	# Must be a string containing embedded nulls that would be double quoted in string form
	set binary_value 1\000\\
	set dictVar [dict create value $binary_value]
	lassign $dictVar k v
	string length $v
} {3}
test dict-24.4 {dict/list shimmering with lappend and foreach} {
	set a [list 1 2 3 4]

	foreach b $a {
		# convert to dict
		dict size $a
		# append to list
		lappend a x y
	}
	llength $a
} 12

# As of 0.79, dicts maintain insertion order
test dict-25.1 {dict ordering} {
	dict keys {a x 0 y}
} {a 0}

test dict-25.2 {dict ordering} {
	dict keys {0 x a y}
} {0 a}

test dict-25.3 {dict ordering} {
	set d [dict create a y 0 x 2 z]
	dict set d 1 w
	dict keys $d
} {a 0 2 1}

test dict-25.3 {dict ordering} {
	set d [dict create a y 0 x 2 z]
	dict set d 0 w
	dict keys $d
} {a 0 2}

test dict-25.4 {removal of keys that hash earlier} {
	set parsed {formPost {text {This is text.} {text file} Hello. {image file} abc}}

	dict unset parsed formPost text
	dict unset parsed formPost {image file}
	dict get $parsed formPost {text file}
} Hello.

test dict-25.5 {list to dict, duplicate keys} {
	set l [list a 1 a 2 a 3]
	# make sure there is no string rep
	lappend l b 4
	dict get $l a
} {3}

# Follow Tcl, to force interpretation, not compilation.
# No effect in Jim
set dict dict
test dict-26.1 {dict getdef command} -body {
    dict getdef {a b} a c
} -result b
test dict-26.2 {dict getdef command} -body {
    dict getdef {a b} b c
} -result c
test dict-26.3 {dict getdef command} -body {
    dict getdef {a {b c}} a b d
} -result c
test dict-26.4 {dict getdef command} -body {
    dict getdef {a {b c}} a c d
} -result d
test dict-26.5 {dict getdef command} -body {
    dict getdef {a {b c}} b c d
} -result d
test dict-26.6 {dict getdef command} -returnCodes error -body {
    dict getdef {a {b c d}} a b d
} -result {missing value to go with key}
test dict-26.7 {dict getdef command} -returnCodes error -body {
    dict getdef
} -result {wrong # args: should be "dict getdef dictionary ?key ...? key default"}
test dict-26.8 {dict getdef command} -returnCodes error -body {
    dict getdef {}
} -result {wrong # args: should be "dict getdef dictionary ?key ...? key default"}
test dict-26.9 {dict getdef command} -returnCodes error -body {
    dict getdef {} {}
} -result {wrong # args: should be "dict getdef dictionary ?key ...? key default"}
test dict-26.10 {dict getdef command} -returnCodes error -body {
    dict getdef {a b c} d e
} -result {missing value to go with key}
test dict-26.11 {dict getdef command} -body {
    $dict getdef {a b} a c
} -result b
test dict-26.12 {dict getdef command} -body {
    $dict getdef {a b} b c
} -result c
test dict-26.13 {dict getdef command} -body {
    $dict getdef {a {b c}} a b d
} -result c
test dict-26.14 {dict getdef command} -body {
    $dict getdef {a {b c}} a c d
} -result d
test dict-26.15 {dict getdef command} -body {
    $dict getdef {a {b c}} b c d
} -result d
test dict-26.16 {dict getdef command} -returnCodes error -body {
    $dict getdef {a {b c d}} a b d
} -result {missing value to go with key}
test dict-26.17 {dict getdef command} -returnCodes error -body {
    $dict getdef {a b c} d e
} -result {missing value to go with key}

test dict-27.1 {dict getwithdefault command} -body {
    dict getwithdefault {a b} a c
} -result b
test dict-27.2 {dict getwithdefault command} -body {
    dict getwithdefault {a b} b c
} -result c
test dict-27.3 {dict getwithdefault command} -body {
    dict getwithdefault {a {b c}} a b d
} -result c
test dict-27.4 {dict getwithdefault command} -body {
    dict getwithdefault {a {b c}} a c d
} -result d
test dict-27.5 {dict getwithdefault command} -body {
    dict getwithdefault {a {b c}} b c d
} -result d
test dict-27.6 {dict getwithdefault command} -returnCodes error -body {
    dict getwithdefault {a {b c d}} a b d
} -result {missing value to go with key}
test dict-27.7 {dict getwithdefault command} -returnCodes error -body {
    dict getwithdefault
} -result {wrong # args: should be "dict getwithdefault dictionary ?key ...? key default"}
test dict-27.8 {dict getwithdefault command} -returnCodes error -body {
    dict getwithdefault {}
} -result {wrong # args: should be "dict getwithdefault dictionary ?key ...? key default"}
test dict-27.9 {dict getwithdefault command} -returnCodes error -body {
    dict getwithdefault {} {}
} -result {wrong # args: should be "dict getwithdefault dictionary ?key ...? key default"}
test dict-27.10 {dict getdef command} -returnCodes error -body {
    dict getwithdefault {a b c} d e
} -result {missing value to go with key}
test dict-27.11 {dict getwithdefault command} -body {
    $dict getwithdefault {a b} a c
} -result b
test dict-27.12 {dict getwithdefault command} -body {
    $dict getwithdefault {a b} b c
} -result c
test dict-27.13 {dict getwithdefault command} -body {
    $dict getwithdefault {a {b c}} a b d
} -result c
test dict-27.14 {dict getwithdefault command} -body {
    $dict getwithdefault {a {b c}} a c d
} -result d
test dict-27.15 {dict getwithdefault command} -body {
    $dict getwithdefault {a {b c}} b c d
} -result d
test dict-27.16 {dict getwithdefault command} -returnCodes error -body {
    $dict getwithdefault {a {b c d}} a b d
} -result {missing value to go with key}
test dict-27.17 {dict getdef command} -returnCodes error -body {
    $dict getwithdefault {a b c} d e
} -result {missing value to go with key}

testreport