summaryrefslogtreecommitdiffstats
path: root/tests/dict.test
blob: 398493a628526d6fb10f870b4ce768a902aadc03 (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
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
# This test file covers the dictionary object type and the dict command used
# to work with values of that type.
#
# This file contains a collection of tests for one or more of the Tcl built-in
# commands. Sourcing this file into Tcl runs the tests and generates output
# for errors.  No output means no errors were found.
#
# Copyright (c) 2003-2009 Donal K. Fellows
# See the file "license.terms" for information on usage and redistribution of
# this file, and for a DISCLAIMER OF ALL WARRANTIES.

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest 2
    namespace import -force ::tcltest::*
}

# Used for constraining memory leak tests
testConstraint memory [llength [info commands memory]]
if {[testConstraint memory]} {
    proc memtest script {
	set end [lindex [split [memory info] \n] 3 3]
	for {set i 0} {$i < 5} {incr i} {
	    uplevel 1 $script
	    set tmp $end
	    set end [lindex [split [memory info] \n] 3 3]
	}
	expr {$end - $tmp}
    }
}

test dict-1.1 {dict command basic syntax} -returnCodes error -body {
    dict
} -result {wrong # args: should be "dict subcommand ?arg ...?"}
test dict-1.2 {dict command basic syntax} -returnCodes error -body {
    dict ?
} -match glob -result {unknown or ambiguous subcommand "?": must be *}

test dict-2.1 {dict create command} {
    dict create
} {}
test dict-2.2 {dict create command} {
    dict create a b
} {a b}
test dict-2.3 {dict create command} -body {
    set result {}
    set dict [dict create a b c d]
    # Can't compare directly as ordering of values is undefined
    foreach key {a c} {
	set idx [lsearch -exact $dict $key]
	if {$idx & 1} {
	    error "found $key at odd index $idx in $dict"
	}
	lappend result [lindex $dict [expr {$idx+1}]]
    }
    return $result
} -cleanup {
    unset result dict key idx
} -result {b d}
test dict-2.4 {dict create command} -returnCodes error -body {
    dict create a
} -result {wrong # args: should be "dict create ?key value ...?"}
test dict-2.5 {dict create command} -returnCodes error -body {
    dict create a b c
} -result {wrong # args: should be "dict create ?key value ...?"}
test dict-2.6 {dict create command - initialse refcount field!} -body {
    # Bug 715751 will show up in memory debuggers like purify
    for {set i 0} {$i<10} {incr i} {
	set dictv [dict create a 0]
	set share [dict values $dictv]
	list [dict incr dictv a]
    }
} -cleanup {
    unset i dictv share
} -result {}
test dict-2.7 {dict create command - #-quoting in string rep} {
    dict create # #comment
} {{#} #comment}
test dict-2.8 {dict create command - #-quoting in string rep} -body {
    dict create #a x #b x
} -match glob -result {{#?} x #? x}

test dict-3.1 {dict get command} {dict get {a b} a} b
test dict-3.2 {dict get command} {dict get {a b c d} a} b
test dict-3.3 {dict get command} {dict get {a b c d} c} d
test dict-3.4 {dict get command} -returnCodes error -body {
    dict get {a b c d} b
} -result {key "b" not known in dictionary}
test dict-3.5 {dict get command} {dict get {a {p q r s} b {u v x y}} a p} q
test dict-3.6 {dict get command} {dict get {a {p q r s} b {u v x y}} a r} s
test dict-3.7 {dict get command} {dict get {a {p q r s} b {u v x y}} b u} v
test dict-3.8 {dict get command} {dict get {a {p q r s} b {u v x y}} b x} y
test dict-3.9 {dict get command} -returnCodes error -body {
    dict get {a {p q r s} b {u v x y}} a z
} -result {key "z" not known in dictionary}
test dict-3.10 {dict get command} -returnCodes error -body {
    dict get {a {p q r s} b {u v x y}} c z
} -result {key "c" not known in dictionary}
test dict-3.11 {dict get command} {dict get [dict create a b c d] a} b
test dict-3.12 {dict get command} -returnCodes error -body {
    dict get
} -result {wrong # args: should be "dict get dictionary ?key ...?"}
test dict-3.13 {dict get command} -body {
    set dict [dict get {a b c d}]
    if {$dict eq "a b c d"} {
	return OK
    } elseif {$dict eq "c d a b"} {
	return reordered
    } else {
	return $dict
    }
} -cleanup {
    unset dict
} -result OK
test dict-3.14 {dict get command} -returnCodes error -body {
    dict get {a b c d} a c
} -result {missing value to go with key}
test dict-3.15 {compiled dict get error cleanliness - Bug 2431847} -body {
    apply {{} {
	dict set a(z) b c
	dict get $a(z) d
    }}
} -returnCodes error -result {key "d" not known in dictionary}
test dict-3.16 {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-3.17 {dict/list shimmering - Bug 3004007} {set l [list p 1 p 2 q 3];dict get $l q;llength $l} 6

test dict-4.1 {dict replace command} {
    dict replace {a b c d}
} {a b c d}
test dict-4.2 {dict replace command} {
    dict replace {a b c d} e f
} {a b c d e f}
test dict-4.3 {dict replace command} {
    dict replace {a b c d} c f
} {a b c f}
test dict-4.4 {dict replace command} {
    dict replace {a b c d} c x a y
} {a y c x}
test dict-4.5 {dict replace command} -returnCodes error -body {
    dict replace
} -result {wrong # args: should be "dict replace dictionary ?key value ...?"}
test dict-4.6 {dict replace command} -returnCodes error -body {
    dict replace {a a} a
} -result {wrong # args: should be "dict replace dictionary ?key value ...?"}
test dict-4.7 {dict replace command} -returnCodes error -body {
    dict replace {a a a} a b
} -result {missing value to go with key}
test dict-4.8 {dict replace command} -returnCodes error -body {
    dict replace [list a a a] a b
} -result {missing value to go with key}
test dict-4.9 {dict replace command} {dict replace [list a a] a b} {a b}
test dict-4.10 {dict replace command} {dict replace [list a a] a b a c} {a c}

test dict-5.1 {dict remove command} {dict remove {a b c d} a} {c d}
test dict-5.2 {dict remove command} {dict remove {a b c d} c} {a b}
test dict-5.3 {dict remove command} {dict remove {a b c d} a c} {}
test dict-5.4 {dict remove command} {dict remove {a b c d} c a} {}
test dict-5.5 {dict remove command} {
    dict remove {a b c d}
} {a b c d}
test dict-5.6 {dict remove command} {dict remove {a b} c} {a b}
test dict-5.7 {dict remove command} -returnCodes error -body {
    dict remove
} -result {wrong # args: should be "dict remove dictionary ?key ...?"}

test dict-6.1 {dict keys command} {dict keys {a b}} a
test dict-6.2 {dict keys command} {dict keys {c d}} c
test dict-6.3 {dict keys command} {lsort [dict keys {a b c d}]} {a c}
test dict-6.4 {dict keys command} {dict keys {a b c d} a} a
test dict-6.5 {dict keys command} {dict keys {a b c d} c} c
test dict-6.6 {dict keys command} {dict keys {a b c d} e} {}
test dict-6.7 {dict keys command} {lsort [dict keys {a b c d ca da} c*]} {c ca}
test dict-6.8 {dict keys command} -returnCodes error -body {
    dict keys
} -result {wrong # args: should be "dict keys dictionary ?pattern?"}
test dict-6.9 {dict keys command} -returnCodes error -body {
    dict keys {} a b
} -result {wrong # args: should be "dict keys dictionary ?pattern?"}
test dict-6.10 {dict keys command} -returnCodes error -body {
    dict keys a
} -result {missing value to go with key}

test dict-7.1 {dict values command} {dict values {a b}} b
test dict-7.2 {dict values command} {dict values {c d}} d
test dict-7.3 {dict values command} {lsort [dict values {a b c d}]} {b d}
test dict-7.4 {dict values command} {dict values {a b c d} b} b
test dict-7.5 {dict values command} {dict values {a b c d} d} d
test dict-7.6 {dict values command} {dict values {a b c d} e} {}
test dict-7.7 {dict values command} {lsort [dict values {a b c d ca da} d*]} {d da}
test dict-7.8 {dict values command} -returnCodes error -body {
    dict values
} -result {wrong # args: should be "dict values dictionary ?pattern?"}
test dict-7.9 {dict values command} -returnCodes error -body {
    dict values {} a b
} -result {wrong # args: should be "dict values dictionary ?pattern?"}
test dict-7.10 {dict values command} -returnCodes error -body {
    dict values a
} -result {missing value to go with key}

test dict-8.1 {dict size command} {dict size {}} 0
test dict-8.2 {dict size command} {dict size {a b}} 1
test dict-8.3 {dict size command} {dict size {a b c d}} 2
test dict-8.4 {dict size command} -returnCodes error -body {
    dict size
} -result {wrong # args: should be "dict size dictionary"}
test dict-8.5 {dict size command} -returnCodes error -body {
    dict size a b
} -result {wrong # args: should be "dict size dictionary"}
test dict-8.6 {dict size command} -returnCodes error -body {
    dict size a
} -result {missing value to go with key}

test dict-9.1 {dict exists command} {dict exists {a b} a} 1
test dict-9.2 {dict exists command} {dict exists {a b} b} 0
test dict-9.3 {dict exists command} {dict exists {a {b c}} a b} 1
test dict-9.4 {dict exists command} {dict exists {a {b c}} a c} 0
test dict-9.5 {dict exists command} {dict exists {a {b c}} b c} 0
test dict-9.6 {dict exists command} {dict exists {a {b c d}} a c} 0
test dict-9.7 {dict exists command} -returnCodes error -body {
    dict exists
} -result {wrong # args: should be "dict exists dictionary key ?key ...?"}
test dict-9.8 {dict exists command} -returnCodes error -body {
    dict exists {}
} -result {wrong # args: should be "dict exists dictionary key ?key ...?"}

test dict-10.1 {dict info command} -body {
    # Actual string returned by this command is undefined; it is
    # intended for human consumption and not for use by scripts.
    dict info {}
} -match glob -result *
test dict-10.2 {dict info command} -returnCodes error -body {
    dict info
} -result {wrong # args: should be "dict info dictionary"}
test dict-10.3 {dict info command} -returnCodes error -body {
    dict info {} x
} -result {wrong # args: should be "dict info dictionary"}
test dict-10.4 {dict info command} -returnCodes error -body {
    dict info x
} -result {missing value to go with key}

test dict-11.1 {dict incr command: unshared value} -body {
    set dictv [dict create \
	    a [string index "=0=" 1] \
	    b [expr {1+2}] \
	    c [expr {wide(0x80000000)+1}]]
    dict incr dictv a
} -cleanup {
    unset dictv
} -result {a 1 b 3 c 2147483649}
test dict-11.2 {dict incr command: unshared value} -body {
    set dictv [dict create \
	    a [string index "=0=" 1] \
	    b [expr {1+2}] \
	    c [expr {wide(0x80000000)+1}]]
    dict incr dictv b
} -cleanup {
    unset dictv
} -result {a 0 b 4 c 2147483649}
test dict-11.3 {dict incr command: unshared value} -body {
    set dictv [dict create \
	    a [string index "=0=" 1] \
	    b [expr {1+2}] \
	    c [expr {wide(0x80000000)+1}]]
    dict incr dictv c
} -cleanup {
    unset dictv
} -result {a 0 b 3 c 2147483650}
test dict-11.4 {dict incr command: shared value} -body {
    set dictv [dict create a 0 b [expr {1+2}] c [expr {wide(0x80000000)+1}]]
    set sharing [dict values $dictv]
    dict incr dictv a
} -cleanup {
    unset dictv sharing
} -result {a 1 b 3 c 2147483649}
test dict-11.5 {dict incr command: shared value} -body {
    set dictv [dict create a 0 b [expr {1+2}] c [expr {wide(0x80000000)+1}]]
    set sharing [dict values $dictv]
    dict incr dictv b
} -cleanup {
    unset dictv sharing
} -result {a 0 b 4 c 2147483649}
test dict-11.6 {dict incr command: shared value} -body {
    set dictv [dict create a 0 b [expr {1+2}] c [expr {wide(0x80000000)+1}]]
    set sharing [dict values $dictv]
    dict incr dictv c
} -cleanup {
    unset dictv sharing
} -result {a 0 b 3 c 2147483650}
test dict-11.7 {dict incr command: unknown values} -body {
    set dictv [dict create a 0 b [expr {1+2}] c [expr {wide(0x80000000)+1}]]
    dict incr dictv d
} -cleanup {
    unset dictv
} -result {a 0 b 3 c 2147483649 d 1}
test dict-11.8 {dict incr command} -body {
    set dictv {a 1}
    dict incr dictv a 2
} -cleanup {
    unset dictv
} -result {a 3}
test dict-11.9 {dict incr command} -returnCodes error -body {
    set dictv {a dummy}
    dict incr dictv a
} -cleanup {
    unset dictv
} -result {expected integer but got "dummy"}
test dict-11.10 {dict incr command} -returnCodes error -body {
    set dictv {a 1}
    dict incr dictv a dummy
} -cleanup {
    unset dictv
} -result {expected integer but got "dummy"}
test dict-11.11 {dict incr command} -setup {
    unset -nocomplain dictv
} -body {
    dict incr dictv a
} -cleanup {
    unset dictv
} -result {a 1}
test dict-11.12 {dict incr command} -returnCodes error -body {
    set dictv a
    dict incr dictv a
} -cleanup {
    unset dictv
} -result {missing value to go with key}
test dict-11.13 {dict incr command} -returnCodes error -body {
    set dictv a
    dict incr dictv a a a
} -cleanup {
    unset dictv
} -result {wrong # args: should be "dict incr varName key ?increment?"}
test dict-11.14 {dict incr command} -returnCodes error -body {
    set dictv a
    dict incr dictv
} -cleanup {
    unset dictv
} -result {wrong # args: should be "dict incr varName key ?increment?"}
test dict-11.15 {dict incr command: write failure} -setup {
    unset -nocomplain dictVar
} -body {
    set dictVar(block) {}
    dict incr dictVar a
} -returnCodes error -cleanup {
    unset dictVar
} -result {can't set "dictVar": variable is array}
test dict-11.16 {dict incr command: compilation} {
    apply {{} {
	set v {a 0 b 0 c 0}
	dict incr v a
	dict incr v b 1
	dict incr v c 2
	dict incr v d 3
	list [dict get $v a] [dict get $v b] [dict get $v c] [dict get $v d]
    }}
} {1 1 2 3}
test dict-11.17 {dict incr command: compilation} {
    apply {{} {
	set dictv {a 1}
	dict incr dictv a 2
    }}
} {a 3}

test dict-12.1 {dict lappend command} -body {
    set dictv {a a}
    dict lappend dictv a
} -cleanup {
    unset dictv
} -result {a a}
test dict-12.2 {dict lappend command} -body {
    set dictv {a a}
    set sharing [dict values $dictv]
    dict lappend dictv a b
} -cleanup {
    unset dictv sharing
} -result {a {a b}}
test dict-12.3 {dict lappend command} -body {
    set dictv {a a}
    dict lappend dictv a b c
} -cleanup {
    unset dictv
} -result {a {a b c}}
test dict-12.2.1 {dict lappend command} -body {
    set dictv [dict create a [string index =a= 1]]
    dict lappend dictv a b
} -cleanup {
    unset dictv
} -result {a {a b}}
test dict-12.4 {dict lappend command} -body {
    set dictv {}
    dict lappend dictv a x y z
} -cleanup {
    unset dictv
} -result {a {x y z}}
test dict-12.5 {dict lappend command} -body {
    unset -nocomplain dictv
    dict lappend dictv a b
} -cleanup {
    unset dictv
} -result {a b}
test dict-12.6 {dict lappend command} -returnCodes error -body {
    set dictv a
    dict lappend dictv a a
} -cleanup {
    unset dictv
} -result {missing value to go with key}
test dict-12.7 {dict lappend command} -returnCodes error -body {
    dict lappend
} -result {wrong # args: should be "dict lappend varName key ?value ...?"}
test dict-12.8 {dict lappend command} -returnCodes error -body {
    dict lappend dictv
} -result {wrong # args: should be "dict lappend varName key ?value ...?"}
test dict-12.9 {dict lappend command} -returnCodes error -body {
    set dictv [dict create a "\{"]
    dict lappend dictv a a
} -cleanup {
    unset dictv
} -result {unmatched open brace in list}
test dict-12.10 {dict lappend command: write failure} -setup {
    unset -nocomplain dictVar
} -body {
    set dictVar(block) {}
    dict lappend dictVar a x
} -returnCodes error -cleanup {
    unset dictVar
} -result {can't set "dictVar": variable is array}
test dict-12.11 {compiled dict append: invalidate string rep - Bug 3079830} {
    apply {{} {set d {a 1 b 2 c 3}; dict lappend d b 22}}
} {a 1 b {2 22} c 3}

test dict-13.1 {dict append command} -body {
    set dictv {a a}
    dict append dictv a
} -cleanup {
    unset dictv
} -result {a a}
test dict-13.2 {dict append command} -body {
    set dictv {a a}
    set sharing [dict values $dictv]
    dict append dictv a b
} -cleanup {
    unset dictv sharing
} -result {a ab}
test dict-13.3 {dict append command} -body {
    set dictv {a a}
    dict append dictv a b c
} -cleanup {
    unset dictv
} -result {a abc}
test dict-13.2.1 {dict append command} -body {
    set dictv [dict create a [string index =a= 1]]
    dict append dictv a b
} -cleanup {
    unset dictv
} -result {a ab}
test dict-13.4 {dict append command} -body {
    set dictv {}
    dict append dictv a x y z
} -cleanup {
    unset dictv
} -result {a xyz}
test dict-13.5 {dict append command} -body {
    unset -nocomplain dictv
    dict append dictv a b
} -cleanup {
    unset dictv
} -result {a b}
test dict-13.6 {dict append command} -returnCodes error -body {
    set dictv a
    dict append dictv a a
} -cleanup {
    unset dictv
} -result {missing value to go with key}
test dict-13.7 {dict append command} -returnCodes error -body {
    dict append
} -result {wrong # args: should be "dict append varName key ?value ...?"}
test dict-13.8 {dict append command} -returnCodes error -body {
    dict append dictv
} -result {wrong # args: should be "dict append varName key ?value ...?"}
test dict-13.9 {dict append command: write failure} -setup {
    unset -nocomplain dictVar
} -body {
    set dictVar(block) {}
    dict append dictVar a x
} -returnCodes error -cleanup {
    unset dictVar
} -result {can't set "dictVar": variable is array}
test dict-13.10 {compiled dict append: crash case} {
    apply {{} {dict append dictVar a o k}}
} {a ok}
test dict-13.11 {compiled dict append: invalidate string rep - Bug 3079830} {
    apply {{} {set d {a 1 b 2 c 3}; dict append d b 22}}
} {a 1 b 222 c 3}

test dict-14.1 {dict for command: syntax} -returnCodes error -body {
    dict for
} -result {wrong # args: should be "dict for {keyVar valueVar} dictionary script"}
test dict-14.2 {dict for command: syntax} -returnCodes error -body {
    dict for x
} -result {wrong # args: should be "dict for {keyVar valueVar} dictionary script"}
test dict-14.3 {dict for command: syntax} -returnCodes error -body {
    dict for x x
} -result {wrong # args: should be "dict for {keyVar valueVar} dictionary script"}
test dict-14.4 {dict for command: syntax} -returnCodes error -body {
    dict for x x x x
} -result {wrong # args: should be "dict for {keyVar valueVar} dictionary script"}
test dict-14.5 {dict for command: syntax} -returnCodes error -body {
    dict for x x x
} -result {must have exactly two variable names}
test dict-14.6 {dict for command: syntax} -returnCodes error -body {
    dict for {x x x} x x
} -result {must have exactly two variable names}
test dict-14.7 {dict for command: syntax} -returnCodes error -body {
    dict for "\{x" x x
} -result {unmatched open brace in list}
test dict-14.8 {dict for command} -body {
    # This test confirms that [dict keys], [dict values] and [dict for]
    # all traverse a dictionary in the same order.
    set dictv {a A b B c C}
    set keys {}
    set values {}
    dict for {k v} $dictv {
	lappend keys $k
	lappend values $v
    }
    set result [expr {
	$keys eq [dict keys $dictv] && $values eq [dict values $dictv]
    }]
    expr {$result ? "YES" : [list "NO" $dictv $keys $values]}
} -cleanup {
    unset result keys values k v dictv
} -result YES
test dict-14.9 {dict for command} {
    dict for {k v} {} {
	error "unexpected execution of 'dict for' body"
    }
} {}
test dict-14.10 {dict for command: script results} -body {
    set times 0
    dict for {k v} {a a b b} {
	incr times
	continue
	error "shouldn't get here"
    }
    return $times
} -cleanup {
    unset times k v
} -result 2
test dict-14.11 {dict for command: script results} -body {
    set times 0
    dict for {k v} {a a b b} {
	incr times
	break
	error "shouldn't get here"
    }
    return $times
} -cleanup {
    unset times k v
} -result 1
test dict-14.12 {dict for command: script results} -body {
    set times 0
    list [catch {
	dict for {k v} {a a b b} {
	    incr times
	    error test
	}
    } msg] $msg $times $::errorInfo
} -cleanup {
    unset times k v msg
} -result {1 test 1 {test
    while executing
"error test"
    ("dict for" body line 3)
    invoked from within
"dict for {k v} {a a b b} {
	    incr times
	    error test
	}"}}
test dict-14.13 {dict for command: script results} {
    apply {{} {
	dict for {k v} {a b} {
	    return ok,$k,$v
	    error "skipped return completely"
	}
	error "return didn't go far enough"
    }}
} ok,a,b
test dict-14.14 {dict for command: handle representation loss} -body {
    set dictVar {a b c d e f g h}
    set keys {}
    set values {}
    dict for {k v} $dictVar {
	if {[llength $dictVar]} {
	    lappend keys $k
	    lappend values $v
	}
    }
    list [lsort $keys] [lsort $values]
} -cleanup {
    unset dictVar keys values k v
} -result {{a c e g} {b d f h}}
test dict-14.15 {dict for command: keys are unique and iterated over once only} -setup {
    unset -nocomplain accum
    array set accum {}
} -body {
    set dictVar {a1 a a2 b b1 c b2 d foo bar bar foo}
    dict for {k v} $dictVar {
	append accum($k) $v,
    }
    set result [lsort [array names accum]]
    lappend result :
    foreach k $result {
	catch {lappend result $accum($k)}
    }
    return $result
} -cleanup {
    unset dictVar k v result accum
} -result {a1 a2 b1 b2 bar foo : a, b, c, d, foo, bar,}
test dict-14.16 {dict for command in compilation context} {
    apply {{} {
	set res {x x x x x x}
	dict for {k v} {a 0 b 1 c 2 d 3 e 4 f 5} {
	    lset res $v $k
	    continue
	}
	return $res
    }}
} {a b c d e f}
test dict-14.17 {dict for command in compilation context} {
    # Bug 1379349
    apply {{} {
	set d [dict create a 1]		;# Dict must be unshared!
	dict for {k v} $d {
	    dict set d $k 0		;# Any modification will do
	}
	return $d
    }}
} {a 0}
test dict-14.18 {dict for command in compilation context} {
    # Bug 1382528
    apply {{} {
	dict for {k v} {} {}		;# Note empty dict
	catch { error foo }		;# Note compiled [catch]
    }}
} 1
test dict-14.19 {dict for and invalid dicts: bug 1531184} -body {
    di[list]ct for {k v} x {}
} -returnCodes 1 -result {missing value to go with key}
test dict-14.20 {dict for stack space compilation: bug 1903325} {
    apply {{x y args} {
	dict for {a b} $x {}
	concat "c=$y,$args"
    }} {} 1 2 3
} {c=1,2 3}
# There's probably a lot more tests to add here. Really ought to use a
# coverage tool for this job...

test dict-15.1 {dict set command} -body {
    set dictVar {}
    dict set dictVar a x
} -cleanup {
    unset dictVar
} -result {a x}
test dict-15.2 {dict set command} -body {
    set dictvar {a {}}
    dict set dictvar a b x
} -cleanup {
    unset dictvar
} -result {a {b x}}
test dict-15.3 {dict set command} -body {
    set dictvar {a {b {}}}
    dict set dictvar a b c x
} -cleanup {
    unset dictvar
} -result {a {b {c x}}}
test dict-15.4 {dict set command} -body {
    set dictVar {a y}
    dict set dictVar a x
} -cleanup {
    unset dictVar
} -result {a x}
test dict-15.5 {dict set command} -body {
    set dictVar {a {b y}}
    dict set dictVar a b x
} -cleanup {
    unset dictVar
} -result {a {b x}}
test dict-15.6 {dict set command} -body {
    set dictVar {a {b {c y}}}
    dict set dictVar a b c x
} -cleanup {
    unset dictVar
} -result {a {b {c x}}}
test dict-15.7 {dict set command: path creation} -body {
    set dictVar {}
    dict set dictVar a b x
} -cleanup {
    unset dictVar
} -result {a {b x}}
test dict-15.8 {dict set command: creates variables} -setup {
    unset -nocomplain dictVar
} -body {
    dict set dictVar a x
    return $dictVar
} -cleanup {
    unset dictVar
} -result {a x}
test dict-15.9 {dict set command: write failure} -setup {
    unset -nocomplain dictVar
} -body {
    set dictVar(block) {}
    dict set dictVar a x
} -returnCodes error -cleanup {
    unset dictVar
} -result {can't set "dictVar": variable is array}
test dict-15.10 {dict set command: syntax} -returnCodes error -body {
    dict set
} -result {wrong # args: should be "dict set varName key ?key ...? value"}
test dict-15.11 {dict set command: syntax} -returnCodes error -body {
    dict set a
} -result {wrong # args: should be "dict set varName key ?key ...? value"}
test dict-15.12 {dict set command: syntax} -returnCodes error -body {
    dict set a a
} -result {wrong # args: should be "dict set varName key ?key ...? value"}
test dict-15.13 {dict set command} -returnCodes error -body {
    set dictVar a
    dict set dictVar b c
} -cleanup {
    unset dictVar
} -result {missing value to go with key}

test dict-16.1 {dict unset command} -body {
    set dictVar {a b c d}
    dict unset dictVar a
} -cleanup {
    unset dictVar
} -result {c d}
test dict-16.2 {dict unset command} -body {
    set dictVar {a b c d}
    dict unset dictVar c
} -cleanup {
    unset dictVar
} -result {a b}
test dict-16.3 {dict unset command} -body {
    set dictVar {a b}
    dict unset dictVar c
} -cleanup {
    unset dictVar
} -result {a b}
test dict-16.4 {dict unset command} -body {
    set dictVar {a {b c d e}}
    dict unset dictVar a b
} -cleanup {
    unset dictVar
} -result {a {d e}}
test dict-16.5 {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-16.6 {dict unset command} -returnCodes error -body {
    set dictVar {a b}
    dict unset dictVar c d
} -cleanup {
    unset dictVar
} -result {key "c" not known in dictionary}
test dict-16.7 {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-16.8 {dict unset command} -returnCodes error -body {
    dict unset dictVar
} -result {wrong # args: should be "dict unset varName key ?key ...?"}
test dict-16.9 {dict unset command: write failure} -setup {
    unset -nocomplain dictVar
} -body {
    set dictVar(block) {}
    dict unset dictVar a
} -returnCodes error -cleanup {
    unset dictVar
} -result {can't set "dictVar": variable is array}

test dict-17.1 {dict filter command: key} -body {
    set dictVar {a1 a a2 b b1 c b2 d foo bar bar foo}
    dict filter $dictVar key a2
} -cleanup {
    unset dictVar
} -result {a2 b}
test dict-17.2 {dict filter command: key} -body {
    set dictVar {a1 a a2 b b1 c b2 d foo bar bar foo}
    dict size [dict filter $dictVar key *]
} -cleanup {
    unset dictVar
} -result 6
test dict-17.3 {dict filter command: key} -body {
    set dictVar {a1 a a2 b b1 c b2 d foo bar bar foo}
    dict filter $dictVar key ???
} -cleanup {
    unset dictVar
} -result {foo bar bar foo}
test dict-17.4 {dict filter command: key - no patterns} {
    dict filter {a b c d} key
} {}
test dict-17.4.1 {dict filter command: key - many patterns} {
    dict filter {a1 a a2 b b1 c b2 d foo bar bar foo} key a? b?
} {a1 a a2 b b1 c b2 d}
test dict-17.5 {dict filter command: key - bad dict} -returnCodes error -body {
    dict filter {a b c} key
} -result {missing value to go with key}
test dict-17.6 {dict filter command: value} -body {
    set dictVar {a1 a a2 b b1 c b2 d foo bar bar foo}
    dict filter $dictVar value c
} -cleanup {
    unset dictVar
} -result {b1 c}
test dict-17.7 {dict filter command: value} -body {
    set dictVar {a1 a a2 b b1 c b2 d foo bar bar foo}
    dict size [dict filter $dictVar value *]
} -cleanup {
    unset dictVar
} -result 6
test dict-17.8 {dict filter command: value} -body {
    set dictVar {a1 a a2 b b1 c b2 d foo bar bar foo}
    dict filter $dictVar value ???
} -cleanup {
    unset dictVar
} -result {foo bar bar foo}
test dict-17.9 {dict filter command: value - no patterns} {
    dict filter {a b c d} value
} {}
test dict-17.9.1 {dict filter command: value - many patterns} {
    dict filter {a a1 b a2 c b1 foo bar bar foo d b2} value a? b?
} {a a1 b a2 c b1 d b2}
test dict-17.10 {dict filter command: value - bad dict} -body {
    dict filter {a b c} value a
} -returnCodes error -result {missing value to go with key}
test dict-17.11 {dict filter command: script} -body {
    set dictVar {a1 a a2 b b1 c b2 d foo bar bar foo}
    set n 0
    list [dict filter $dictVar script {k v} {
	incr n
	expr {[string length $k] == [string length $v]}
    }] $n
} -cleanup {
    unset dictVar n k v
} -result {{foo bar bar foo} 6}
test dict-17.12 {dict filter command: script} -returnCodes error -body {
    dict filter {a b} script {k v} {
	concat $k $v
    }
} -cleanup {
    unset k v
} -result {expected boolean value but got "a b"}
test dict-17.13 {dict filter command: script} -body {
    list [catch {dict filter {a b} script {k v} {error x}} msg] $msg \
	    $::errorInfo
} -cleanup {
    unset k v msg
} -result {1 x {x
    while executing
"error x"
    ("dict filter" script line 1)
    invoked from within
"dict filter {a b} script {k v} {error x}"}}
test dict-17.14 {dict filter command: script} -setup {
    set n 0
} -body {
    list [dict filter {a b c d} script {k v} {
	incr n
	break
	error boom!
    }] $n
} -cleanup {
    unset n k v
} -result {{} 1}
test dict-17.15 {dict filter command: script} -setup {
    set n 0
} -body {
    list [dict filter {a b c d} script {k v} {
	incr n
	continue
	error boom!
    }] $n
} -cleanup {
    unset n k v
} -result {{} 2}
test dict-17.16 {dict filter command: script} {
    apply {{} {
	dict filter {a b} script {k v} {
	    return ok,$k,$v
	    error "skipped return completely"
	}
	error "return didn't go far enough"
    }}
} ok,a,b
test dict-17.17 {dict filter command: script} -body {
    dict filter {a b} script {k k} {continue}
    return $k
} -cleanup {
    unset k
} -result b
test dict-17.18 {dict filter command: script} -returnCodes error -body {
    dict filter {a b} script {k k}
} -result {wrong # args: should be "dict filter dictionary script {keyVar valueVar} filterScript"}
test dict-17.19 {dict filter command: script} -returnCodes error -body {
    dict filter {a b} script k {continue}
} -result {must have exactly two variable names}
test dict-17.20 {dict filter command: script} -returnCodes error -body {
    dict filter {a b} script "\{k v" {continue}
} -result {unmatched open brace in list}
test dict-17.21 {dict filter command} -returnCodes error -body {
    dict filter {a b}
} -result {wrong # args: should be "dict filter dictionary filterType ?arg ...?"}
test dict-17.22 {dict filter command} -returnCodes error -body {
    dict filter {a b} JUNK
} -result {bad filterType "JUNK": must be key, script, or value}
test dict-17.23 {dict filter command} -returnCodes error -body {
    dict filter a key *
} -result {missing value to go with key}

test dict-18.1 {dict-list relationship} -body {
    # Test that any internal conversion between list and dict does not change
    # the object
    set l [list 1 2 3 4 5 6 7 8 9 0 q w e r t y]
    dict values $l
    return $l
} -cleanup {
    unset l
} -result {1 2 3 4 5 6 7 8 9 0 q w e r t y}
test dict-18.2 {dict-list relationship} -body {
    # Test that the dictionary is a valid list
    set d [dict create "abc def" 0 "a\{b" 1 "c\}d" 2]
    for {set t 0} {$t < 5} {incr t} {
	llength $d
	dict lappend d "abc def" "\}\{"
	dict append  d "a\{b" "\}"
	dict incr    d "c\}d" 1
    }
    llength $d
} -cleanup {
    unset d t
} -result 6
test dict-18.3 {dict-list relationship} -body {
    set ld [list a b c d c e f g]
    list [string length $ld] [dict size $ld] [llength $ld]
} -cleanup {
    unset ld
} -result {15 3 8}
test dict-18.4 {dict-list relationship} -body {
    set ld [list a b c d c e f g]
    list [llength $ld] [dict size $ld] [llength $ld]
} -cleanup {
    unset ld
} -result {8 3 8}

# This is a test for a specific bug.
# It shows a bad ref counter when running with memdebug on.
test dict-19.1 {memory bug} {
    apply {{} {
        set successors [dict create x {c d}]
        dict set successors x a b
        dict get $successors x
    }}
} [dict create c d a b]
test dict-19.2 {dict: testing for leaks} -constraints memory -body {
    # This test is made to stress object reference management
    memtest {
	apply {{} {
	    # A shared invalid dictinary
	    set apa {a {}b c d}
	    set bepa $apa
	    catch {dict replace $apa e f}
	    catch {dict remove  $apa c d}
	    catch {dict incr    apa  a 5}
	    catch {dict lappend apa  a 5}
	    catch {dict append  apa  a 5}
	    catch {dict set     apa  a 5}
	    catch {dict unset   apa  a}

	    # A shared valid dictionary, invalid incr
	    set apa {a b c d}
	    set bepa $apa
	    catch {dict incr bepa a 5}

	    # An error during write to an unshared object, incr
	    set apa {a 1 b 2}
	    set bepa [lrange $apa 0 end]
	    trace add variable bepa write {error hej}
	    catch {dict incr bepa a 5}
	    trace remove variable bepa write {error hej}
	    unset bepa

	    # An error during write to a shared object, incr
	    set apa {a 1 b 2}
	    set bepa $apa
	    trace add variable bepa write {error hej}
	    catch {dict incr bepa a 5}
	    trace remove variable bepa write {error hej}
	    unset bepa

	    # A shared valid dictionary, invalid lappend
	    set apa [list a {{}b} c d]
	    set bepa $apa
	    catch {dict lappend bepa a 5}

	    # An error during write to an unshared object, lappend
	    set apa {a 1 b 2}
	    set bepa [lrange $apa 0 end]
	    trace add variable bepa write {error hej}
	    catch {dict lappend bepa a 5}
	    trace remove variable bepa write {error hej}
	    unset bepa

	    # An error during write to a shared object, lappend
	    set apa {a 1 b 2}
	    set bepa $apa
	    trace add variable bepa write {error hej}
	    catch {dict lappend bepa a 5}
	    trace remove variable bepa write {error hej}
	    unset bepa

	    # An error during write to an unshared object, append
	    set apa {a 1 b 2}
	    set bepa [lrange $apa 0 end]
	    trace add variable bepa write {error hej}
	    catch {dict append bepa a 5}
	    trace remove variable bepa write {error hej}
	    unset bepa

	    # An error during write to a shared object, append
	    set apa {a 1 b 2}
	    set bepa $apa
	    trace add variable bepa write {error hej}
	    catch {dict append bepa a 5}
	    trace remove variable bepa write {error hej}
	    unset bepa

	    # An error during write to an unshared object, set
	    set apa {a 1 b 2}
	    set bepa [lrange $apa 0 end]
	    trace add variable bepa write {error hej}
	    catch {dict set bepa a 5}
	    trace remove variable bepa write {error hej}
	    unset bepa

	    # An error during write to a shared object, set
	    set apa {a 1 b 2}
	    set bepa $apa
	    trace add variable bepa write {error hej}
	    catch {dict set bepa a 5}
	    trace remove variable bepa write {error hej}
	    unset bepa

	    # An error during write to an unshared object, unset
	    set apa {a 1 b 2}
	    set bepa [lrange $apa 0 end]
	    trace add variable bepa write {error hej}
	    catch {dict unset bepa a}
	    trace remove variable bepa write {error hej}
	    unset bepa

	    # An error during write to a shared object, unset
	    set apa {a 1 b 2}
	    set bepa $apa
	    trace add variable bepa write {error hej}
	    catch {dict unset bepa a}
	    trace remove variable bepa write {error hej}
	    unset bepa
	}}
    }
} -result 0
test dict-19.3 {testing for leaks - Bug 2874678} -constraints memory -body {
    set d aDictVar; # Force interpreted [dict incr]
    memtest {
	dict incr $d aKey 0
	unset $d
    }
} -cleanup {
    unset d
} -result 0

test dict-20.1 {dict merge command} {
    dict merge
} {}
test dict-20.2 {dict merge command} {
    dict merge {a b c d e f}
} {a b c d e f}
test dict-20.3 {dict merge command} -body {
    dict merge {a b c d e}
} -result {missing value to go with key} -returnCodes error
test dict-20.4 {dict merge command} {
    dict merge {a b c d} {e f g h}
} {a b c d e f g h}
test dict-20.5 {dict merge command} -body {
    dict merge {a b c d e} {e f g h}
} -result {missing value to go with key} -returnCodes error
test dict-20.6 {dict merge command} -body {
    dict merge {a b c d} {e f g h i}
} -result {missing value to go with key} -returnCodes error
test dict-20.7 {dict merge command} {
    dict merge {a b c d e f} {e x g h}
} {a b c d e x g h}
test dict-20.8 {dict merge command} {
    dict merge {a b c d} {a x c y}
} {a x c y}
test dict-20.9 {dict merge command} {
    dict merge {a b c d} {c y a x}
} {a x c y}
test dict-20.10 {dict merge command} {
    dict merge {a b c d e f} {a x 1 2 3 4} {a - 1 -}
} {a - c d e f 1 - 3 4}

test dict-21.1 {dict update command} -returnCodes 1 -body {
    dict update
} -result {wrong # args: should be "dict update varName key varName ?key varName ...? script"}
test dict-21.2 {dict update command} -returnCodes 1 -body {
    dict update v
} -result {wrong # args: should be "dict update varName key varName ?key varName ...? script"}
test dict-21.3 {dict update command} -returnCodes 1 -body {
    dict update v k
} -result {wrong # args: should be "dict update varName key varName ?key varName ...? script"}
test dict-21.4 {dict update command} -returnCodes 1 -body {
    dict update v k v
} -result {wrong # args: should be "dict update varName key varName ?key varName ...? script"}
test dict-21.5 {dict update command} -body {
    set a {b c}
    set result {}
    set bb {}
    dict update a b bb {
	lappend result $a $bb
    }
    lappend result $a
} -cleanup {
    unset a result bb
} -result {{b c} c {b c}}
test dict-21.6 {dict update command} -body {
    set a {b c}
    set result {}
    set bb {}
    dict update a b bb {
	lappend result $a $bb [set bb d]
    }
    lappend result $a
} -cleanup {
    unset a result bb
} -result {{b c} c d {b d}}
test dict-21.7 {dict update command} -body {
    set a {b c}
    set result {}
    set bb {}
    dict update a b bb {
	lappend result $a $bb [unset bb]
    }
    lappend result $a
} -cleanup {
    unset a result
} -result {{b c} c {} {}}
test dict-21.8 {dict update command} -body {
    set a {b c d e}
    dict update a b v1 d v2 {
	lassign "$v1 $v2" v2 v1
    }
    return $a
} -cleanup {
    unset a v1 v2
} -result {b e d c}
test dict-21.9 {dict update command} -body {
    set a {b c d e}
    dict update a b v1 d v2 {unset a}
    info exist a
} -cleanup {
    unset v1 v2
} -result 0
test dict-21.10 {dict update command} -body {
    set a {b {c d}}
    dict update a b v1 {
	dict update v1 c v2 {
	    set v2 foo
	}
    }
    return $a
} -cleanup {
    unset a v1 v2
} -result {b {c foo}}
test dict-21.11 {dict update command} -body {
    set a {b c d e}
    dict update a b v1 d v2 {
	dict set a f g
    }
    return $a
} -cleanup {
    unset a v1 v2
} -result {b c d e f g}
test dict-21.12 {dict update command} -body {
    set a {b c d e}
    dict update a b v1 d v2 f v3 {
	set v3 g
    }
    return $a
} -cleanup {
    unset a v1 v2 v3
} -result {b c d e f g}
test dict-21.13 {dict update command: compilation} {
    apply {d {
	while 1 {
	    dict update d a alpha b beta {
		set beta $alpha
		unset alpha
		break
	    }
	}
	return $d
    }} {a 1 c 2}
} {c 2 b 1}
test dict-21.14 {dict update command: compilation} {
    apply {x {
	set indices {2 3}
	trace add variable aa write "string length \$indices ;#"
	dict update x k aa l bb {}
    }} {k 1 l 2}
} {}
test dict-21.15 {dict update command: compilation} {
    apply {x {
	set indices {2 3}
	trace add variable aa read "string length \$indices ;#"
	dict update x k aa l bb {}
    }} {k 1 l 2}
} {}
test dict-21.16 {dict update command: no recursive structures [Bug 1786481]} -body {
    set foo {a {b {c {d {e 1}}}}}
    dict update foo a t {
	dict update t b t {
	    dict update t c t {
		dict update t d t {
		    dict incr t e
		}
	    }
	}
    }
    string range [append foo OK] end-1 end
} -cleanup {
    unset foo t
} -result OK
test dict-21.17 {dict update command: no recursive structures [Bug 1786481]} {
    apply {{} {
	set foo {a {b {c {d {e 1}}}}}
	dict update foo a t {
	    dict update t b t {
		dict update t c t {
		    dict update t d t {
			dict incr t e
		    }
		}
	    }
	}
	string range [append foo OK] end-1 end
    }}
} OK

test dict-22.1 {dict with command} -body {
    dict with
} -returnCodes 1 -result {wrong # args: should be "dict with dictVar ?key ...? script"}
test dict-22.2 {dict with command} -body {
    dict with v
} -returnCodes 1 -result {wrong # args: should be "dict with dictVar ?key ...? script"}
test dict-22.3 {dict with command} -body {
    unset -nocomplain v
    dict with v {error "in body"}
} -returnCodes 1 -result {can't read "v": no such variable}
test dict-22.4 {dict with command} -body {
    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
    }
    return $result
} -cleanup {
    unset a b d result
} -result {0 0 1 1 c e}
test dict-22.5 {dict with command} -body {
    set a {b c d e}
    dict with a {
	lassign "$b $d" d b
    }
    return $a
} -cleanup {
    unset a b d
} -result {b e d c}
test dict-22.6 {dict with command} -body {
    set a {b c d e}
    dict with a {
	unset b
	# This *won't* go into the dict...
	set f g
    }
    return $a
} -cleanup {
    unset a d f
} -result {d e}
test dict-22.7 {dict with command} -body {
    set a {b c d e}
    dict with a {
	dict unset a b
    }
    return $a
} -cleanup {
    unset a
} -result {d e b c}
test dict-22.8 {dict with command} -body {
    set a [dict create b c]
    dict with a {
	set b $a
    }
    return $a
} -cleanup {
    unset a b
} -result {b {b c}}
test dict-22.9 {dict with command} -body {
    set a {b {c d}}
    dict with a b {
	set c $c$c
    }
    return $a
} -cleanup {
    unset a c
} -result {b {c dd}}
test dict-22.10 {dict with command: result handling tricky case} -body {
    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
} -cleanup {
    unset a i c
} -result {0 {}}
test dict-22.11 {dict with command: no recursive structures [Bug 1786481]} -body {
    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
} -cleanup {
    unset foo t inner
} -result OK
test dict-22.12 {dict with: compiled} {
    apply {{} {
	set d {a 1 b 2}
	list [dict with d {
	    set a $b
	    unset b
	    dict set d c 3
	    list ok
	}] $d
    }}
} {ok {a 2 c 3}}
test dict-22.13 {dict with: compiled} {
    apply {i {
	set d($i) {a 1 b 2}
	list [dict with d($i) {
	    set a $b
	    unset b
	    dict set d($i) c 3
	    list ok
	}] [array get d]
    }} e
} {ok {e {a 2 c 3}}}
test dict-22.14 {dict with: compiled} {
    apply {{} {
	set d {a 1 b 2}
	foreach x {1 2 3} {
	    dict with d {
		incr a $b
		if {$x == 2} break
	    }
	    unset a b
	}
	list $a $b $x $d
    }}
} {5 2 2 {a 5 b 2}}
test dict-22.15 {dict with: compiled} {
    apply {i {
	set d($i) {a 1 b 2}
	foreach x {1 2 3} {
	    dict with d($i) {
		incr a $b
		if {$x == 2} break
	    }
	    unset a b
	}
	list $a $b $x [array get d]
    }} e
} {5 2 2 {e {a 5 b 2}}}
test dict-22.16 {dict with: compiled} {
    apply {{} {
	set d {p {q {a 1 b 2}}}
	dict with d p q {
	    set a $b.$a
	}
	return $d
    }}
} {p {q {a 2.1 b 2}}}
test dict-22.17 {dict with: compiled} {
    apply {i {
	set d($i) {p {q {a 1 b 2}}}
	dict with d($i) p q {
	    set a $b.$a
	}
	array get d
    }} e
} {e {p {q {a 2.1 b 2}}}}
test dict-22.18 {dict with: compiled} {
    set ::d {a 1 b 2}
    apply {{} {
	dict with ::d {
	    set a $b.$a
	}
	return $::d
    }}
} {a 2.1 b 2}
test dict-22.19 {dict with: compiled} {
    set ::d {p {q {r {a 1 b 2}}}}
    apply {{} {
	dict with ::d p q r {
	    set a $b.$a
	}
	return $::d
    }}
} {p {q {r {a 2.1 b 2}}}}
test dict-22.20 {dict with: compiled} {
    apply {d {
	dict with d {
	}
	return $a,$b
    }} {a 1 b 2}
} 1,2
test dict-22.21 {dict with: compiled} {
    apply {d {
	dict with d p q {
	}
	return $a,$b
    }} {p {q {a 1 b 2}}}
} 1,2
test dict-22.22 {dict with: compiled} {
    set ::d {a 1 b 2}
    apply {{} {
	dict with ::d {
	}
	return $a,$b
    }}
} 1,2
test dict-22.23 {dict with: compiled} {
    set ::d {p {q {a 1 b 2}}}
    apply {{} {
	dict with ::d p q {
	}
	return $a,$b
    }}
} 1,2

proc linenumber {} {
    dict get [info frame -1] line
}
test dict-23.1 {dict compilation crash: Bug 3487626} {
    apply {{} {apply {n {
	set e {}
	set k {}
	dict for {a b} {c {d {e {f g}}}} {
	    ::tcl::dict::for {h i} $b {
		dict update i e j {
		    ::tcl::dict::update j f k {
			return [expr {$n - [linenumber]}]
		    }
		}
	    }
	}
    }} [linenumber]}}
} 5
test dict-23.2 {dict compilation crash: Bug 3487626} knownBug {
    # Something isn't quite right in line number and continuation line
    # tracking; at time of writing, this test produces 7, not 5, which
    # indicates that the extra newlines in the non-script argument are
    # confusing things.
    apply {{} {apply {n {
	set e {}
	set k {}
	dict for {a {
b
}} {c {d {e {f g}}}} {
	    ::tcl::dict::for {h {
i
}} ${
b
} {
		dict update {
i
} e {
j
} {
		    ::tcl::dict::update {
j
} f k {
			return [expr {$n - [linenumber]}]
		    }
		}
	    }
	}
    }} [linenumber]}}
} 5
rename linenumber {}

test dict-24.1 {dict map command: syntax} -returnCodes error -body {
    dict map
} -result {wrong # args: should be "dict map {keyVar valueVar} dictionary script"}
test dict-24.2 {dict map command: syntax} -returnCodes error -body {
    dict map x
} -result {wrong # args: should be "dict map {keyVar valueVar} dictionary script"}
test dict-24.3 {dict map command: syntax} -returnCodes error -body {
    dict map x x
} -result {wrong # args: should be "dict map {keyVar valueVar} dictionary script"}
test dict-24.4 {dict map command: syntax} -returnCodes error -body {
    dict map x x x x
} -result {wrong # args: should be "dict map {keyVar valueVar} dictionary script"}
test dict-24.5 {dict map command: syntax} -returnCodes error -body {
    dict map x x x
} -result {must have exactly two variable names}
test dict-24.6 {dict map command: syntax} -returnCodes error -body {
    dict map {x x x} x x
} -result {must have exactly two variable names}
test dict-24.7 {dict map command: syntax} -returnCodes error -body {
    dict map "\{x" x x
} -result {unmatched open brace in list}
test dict-24.8 {dict map command} -body {
    # This test confirms that [dict keys], [dict values] and [dict map]
    # all traverse a dictionary in the same order.
    set dictv {a A b B c C}
    set values {}
    set keys [dict map {k v} $dictv {
	lappend values $v
	set k
    }]
    set result [expr {
	$keys eq [dict keys $dictv] && $values eq [dict values $dictv]
    }]
    expr {$result ? "YES" : [list "NO" $dictv $keys $values]}
} -cleanup {
    unset result keys values k v dictv
} -result YES
test dict-24.9 {dict map command} {
    dict map {k v} {} {
	error "unexpected execution of 'dict map' body"
    }
} {}
test dict-24.10 {dict map command: script results} -body {
    set times 0
    dict map {k v} {a a b b} {
	incr times
	continue
	error "shouldn't get here"
    }
    return $times
} -cleanup {
    unset times k v
} -result 2
test dict-24.11 {dict map command: script results} -body {
    set times 0
    dict map {k v} {a a b b} {
	incr times
	break
	error "shouldn't get here"
    }
    return $times
} -cleanup {
    unset times k v
} -result 1
test dict-24.12 {dict map command: script results} -body {
    set times 0
    list [catch {
	dict map {k v} {a a b b} {
	    incr times
	    error test
	}
    } msg] $msg $times $::errorInfo
} -cleanup {
    unset times k v msg
} -result {1 test 1 {test
    while executing
"error test"
    ("dict map" body line 3)
    invoked from within
"dict map {k v} {a a b b} {
	    incr times
	    error test
	}"}}
test dict-24.13 {dict map command: script results} {
    apply {{} {
	dict map {k v} {a b} {
	    return ok,$k,$v
	    error "skipped return completely"
	}
	error "return didn't go far enough"
    }}
} ok,a,b
test dict-24.14 {dict map command: handle representation loss} -body {
    set dictVar {a b c d e f g h}
    set values {}
    set keys [dict map {k v} $dictVar {
	if {[llength $dictVar]} {
	    lappend values $v
	    return -level 0 $k
	}
    }]
    list [lsort $keys] [lsort $values]
} -cleanup {
    unset dictVar keys values k v
} -result {{a c e g} {b d f h}}
test dict-24.15 {dict map command: keys are unique and iterated over once only} -setup {
    unset -nocomplain accum
    array set accum {}
} -body {
    set dictVar {a1 a a2 b b1 c b2 d foo bar bar foo}
    dict map {k v} $dictVar {
	append accum($k) $v,
    }
    set result [lsort [array names accum]]
    lappend result :
    foreach k $result {
	catch {lappend result $accum($k)}
    }
    return $result
} -cleanup {
    unset dictVar k v result accum
} -result {a1 a2 b1 b2 bar foo : a, b, c, d, foo, bar,}
test dict-24.16 {dict map command in compilation context} {
    apply {{} {
	set res {x x x x x x}
	dict map {k v} {a 0 b 1 c 2 d 3 e 4 f 5} {
	    lset res $v $k
	    continue
	}
	return $res
    }}
} {a b c d e f}
test dict-24.17 {dict map command in compilation context} {
    # Bug 1379349 (dict for)
    apply {{} {
	set d [dict create a 1]		;# Dict must be unshared!
	dict map {k v} $d {
	    dict set d $k 0		;# Any modification will do
	}
	return $d
    }}
} {a 0}
test dict-24.17a {dict map command in compilation context} {
    # Bug 1379349 (dict for)
    apply {{} {
	set d [dict create a 1]		;# Dict must be unshared!
	dict map {k v} $d {
	    dict set d $k 0		;# Any modification will do
	}
    }}
} {{a 0}}
test dict-24.18 {dict map command in compilation context} {
    # Bug 1382528 (dict for)
    apply {{} {
	dict map {k v} {} {}		;# Note empty dict
	catch { error foo }		;# Note compiled [catch]
    }}
} 1
test dict-24.19 {dict map and invalid dicts: 'dict for' bug 1531184} -body {
    di[list]ct map {k v} x {}
} -returnCodes 1 -result {missing value to go with key}
test dict-24.20 {dict map stack space compilation: 'dict for' bug 1903325} {
    apply {{x y args} {
	dict map {a b} $x {}
	concat "c=$y,$args"
    }} {} 1 2 3
} {c=1,2 3}
proc linenumber {} {
    dict get [info frame -1] line
}
test dict-24.20 {dict compilation crash: 'dict for' bug 3487626} {
    apply {{} {apply {n {
	set e {}
	set k {}
	dict map {a b} {c {d {e {f g}}}} {
	    ::tcl::dict::map {h i} $b {
		dict update i e j {
		    ::tcl::dict::update j f k {
			return [expr {$n - [linenumber]}]
		    }
		}
	    }
	}
    }} [linenumber]}}
} 5
test dict-24.21 {dict compilation crash: 'dict for' bug 3487626} knownBug {
    apply {{} {apply {n {
	set e {}
	set k {}
	dict map {a {
b
}} {c {d {e {f g}}}} {
	    ::tcl::dict::map {h {
i
}} ${
b
} {
		dict update {
i
} e {
j
} {
		    ::tcl::dict::update {
j
} f k {
			return [expr {$n - [linenumber]}]
		    }
		}
	    }
	}
    }} [linenumber]}}
} 5
rename linenumber {}
test dict-24.22 {dict map results (non-compiled)} {
    dict map {k v} [dict map {k v} {a 1 b 2 c 3 d 4} { list $v $k }] {
	return -level 0 "$k,$v"
    }
} {{1 a,2 b} {3 c,4 d}}
test dict-24.23 {dict map results (compiled)} {
    apply {{} {
	dict map {k v} [dict map {k v} {a 1 b 2 c 3 d 4} { list $v $k }] {
	    return -level 0 "$k,$v"
	}
    }}
} {{1 a,2 b} {3 c,4 d}}
test dict-24.23a {dict map results (compiled)} {
    apply {{list} {
	dict map {k v} [dict map {k v} $list { list $v $k }] {
	    return -level 0 "$k,$v"
	}
    }} {a 1 b 2 c 3 d 4}
} {{1 a,2 b} {3 c,4 d}}
test dict-24.24 {dict map with huge dict (non-compiled)} {
    tcl::mathop::+ {*}[dict map {k v} [lsearch -all [lrepeat 1000000 x] x] {
	expr { $k * $v }
    }]
} 166666416666500000
test dict-24.25 {dict map with huge dict (compiled)} {
    apply {{n} {
	tcl::mathop::+ {*}[dict map {k v} [lsearch -all [lrepeat $n y] y] {
	    expr { $k * $v }
	}]
    }} 1000000
} 166666416666500000


# cleanup
::tcltest::cleanupTests
return

# Local Variables:
# mode: tcl
# End: