summaryrefslogtreecommitdiff
path: root/tests/dict.test
blob: c0528e82e56df0e42e8755a02ab2da010be8ea62 (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
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-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

testreport