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
|
# tree.test: tests for the tree structure. -*- tcl -*-
#
# 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) 1998-2000 by Ajuba Solutions.
# All rights reserved.
#
# RCS: @(#) $Id: tree1.test,v 1.7 2006/10/09 21:41:42 andreas_kupries Exp $
# -------------------------------------------------------------------------
source [file join \
[file dirname [file dirname [file join [pwd] [info script]]]] \
devtools testutilities.tcl]
testsNeedTcl 8.2
testsNeedTcltest 1.0
testing {
useLocal tree1.tcl struct::tree
}
# -------------------------------------------------------------------------
namespace import struct::tree
#----------------------------------------------------------------------
test tree-0.1 {tree errors} {
tree mytree
catch {tree mytree} msg
mytree destroy
set msg
} "command \"::mytree\" already exists, unable to create tree"
test tree-0.2 {tree errors} {
tree mytree
catch {mytree} msg
mytree destroy
set msg
} "wrong # args: should be \"::mytree option ?arg arg ...?\""
test tree-0.3 {tree errors} {
tree mytree
catch {mytree foo} msg
mytree destroy
set msg
} "bad option \"foo\": must be append, children, cut, delete, depth, destroy, exists, get, getall, index, insert, isleaf, keyexists, keys, lappend, move, next, numchildren, parent, previous, serialize, set, size, splice, swap, unset, or walk"
test tree-0.4 {tree errors} {
catch {tree set} msg
set msg
} "command \"::set\" already exists, unable to create tree"
test tree-1.1 {children} {
tree mytree
set result [list ]
lappend result [mytree children root]
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1}
mytree insert root end {IT::EM 2}
mytree insert {IT::EM 0} end {IT::EM 3}
mytree insert {IT::EM 0} end {IT::EM 4}
lappend result [mytree children root]
lappend result [mytree children {IT::EM 0}]
mytree destroy
set result
} [list {} {{IT::EM 0} {IT::EM 1} {IT::EM 2}} {{IT::EM 3} {IT::EM 4}}]
test tree-1.2 {children, bad node} {
tree mytree
set result [catch {mytree children foobar} msg]
mytree destroy
list $result $msg
} [list 1 "node \"foobar\" does not exist in tree \"::mytree\""]
test tree-2.1 {create} {
tree mytree
set result [string equal [info commands ::mytree] "::mytree"]
mytree destroy
set result
} 1
test tree-2.2 {create} {
set name [tree]
set result [list $name [string equal [info commands $name] "$name"]]
$name destroy
set result
} [list ::tree1 1]
test tree-3.1 {destroy} {
tree mytree
mytree destroy
string equal [info commands ::mytree] ""
} 1
test tree-4.1 {delete} {
tree mytree
catch {mytree delete root} msg
mytree destroy
set msg
} "cannot delete root node"
test tree-4.2 {delete} {
tree mytree
catch {mytree delete {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-4.3 {delete} {
tree mytree
mytree insert root end {IT::EM 0}
mytree delete {IT::EM 0}
set result [list [mytree exists {IT::EM 0}] [mytree children root]]
mytree destroy
set result
} {0 {}}
test tree-4.4 {delete} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert {IT::EM 0} end {IT::EM 1}
mytree insert {IT::EM 1} end {IT::EM 2}
mytree delete {IT::EM 0}
set result [list [mytree exists {IT::EM 0}] \
[mytree exists {IT::EM 1}] \
[mytree exists {IT::EM 2}]]
mytree destroy
set result
} {0 0 0}
test tree-5.1 {exists} {
tree mytree
set result [list ]
lappend result [mytree exists root]
mytree insert root end {IT::EM 0}
lappend result [mytree exists {IT::EM 0}]
mytree delete {IT::EM 0}
lappend result [mytree exists {IT::EM 0}]
mytree destroy
set result
} {1 1 0}
test tree-6.1 {insert creates and initializes node} {
tree mytree
mytree insert root end {IT::EM 0}
set result [list ]
lappend result [mytree exists {IT::EM 0}]
lappend result [mytree parent {IT::EM 0}]
lappend result [mytree children {IT::EM 0}]
lappend result [mytree set {IT::EM 0}]
lappend result [mytree children root]
mytree destroy
set result
} {1 root {} {} {{IT::EM 0}}}
test tree-6.2 {insert insert nodes in correct location} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1}
mytree insert root 0 {IT::EM 2}
set result [mytree children root]
mytree destroy
set result
} {{IT::EM 2} {IT::EM 0} {IT::EM 1}}
test tree-6.3 {insert gives error when trying to insert to a fake parent} {
tree mytree
catch {mytree insert {IT::EM 0} end {IT::EM 1}} msg
mytree destroy
set msg
} "parent node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-6.4 {insert generates node name when none is given} {
tree mytree
set result [list [mytree insert root end]]
lappend result [mytree insert root end]
mytree insert root end {IT::EM 3}
lappend result [mytree insert root end]
mytree destroy
set result
} [list node1 node2 node3]
test tree-6.5 {insert inserts multiple nodes properly} {
tree mytree
mytree insert root end a b c d e f
set result [mytree children root]
mytree destroy
set result
} [list a b c d e f]
test tree-6.6 {insert moves nodes that exist} {
tree mytree
mytree insert root end {IT::EM 0} {IT::EM 1} {IT::EM 2} {IT::EM 3}
mytree insert {IT::EM 0} end {IT::EM 4} {IT::EM 5} {IT::EM 6}
mytree insert root end {IT::EM 4}
set result [list [mytree children root] [mytree children {IT::EM 0}]]
mytree destroy
set result
} [list [list {IT::EM 0} {IT::EM 1} {IT::EM 2} {IT::EM 3} {IT::EM 4}] [list {IT::EM 5} {IT::EM 6}]]
test tree-6.7 {insert moves nodes that already exist properly} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert {IT::EM 0} end {IT::EM 1}
mytree insert {IT::EM 1} end {IT::EM 2}
mytree insert root end {IT::EM 1} {IT::EM 2}
set result [list \
[mytree children root] \
[mytree children {IT::EM 0}] \
[mytree children {IT::EM 1}] \
[mytree parent {IT::EM 1}] \
[mytree parent {IT::EM 2}] \
]
mytree destroy
set result
} [list [list {IT::EM 0} {IT::EM 1} {IT::EM 2}] {} {} root root]
test tree-6.8 {insert moves multiple nodes properly} {
tree mytree
mytree insert root end {IT::EM 0} {IT::EM 1} {IT::EM 2}
mytree insert root 0 {IT::EM 1} {IT::EM 2}
set result [list \
[mytree children root] \
]
mytree destroy
set result
} [list [list {IT::EM 1} {IT::EM 2} {IT::EM 0}]]
test tree-6.9 {insert moves multiple nodes properly} {
tree mytree
mytree insert root end {IT::EM 0} {IT::EM 1} {IT::EM 2}
mytree insert root 1 {IT::EM 0} {IT::EM 1}
set result [mytree children root]
mytree destroy
set result
} [list {IT::EM 0} {IT::EM 1} {IT::EM 2}]
test tree-6.10 {insert moves node within parent properly} {
tree mytree
mytree insert root end {IT::EM 0} {IT::EM 1} {IT::EM 2} {IT::EM 3}
mytree insert root 2 {IT::EM 1}
set result [mytree children root]
mytree destroy
set result
} [list {IT::EM 0} {IT::EM 1} {IT::EM 2} {IT::EM 3}]
test tree-6.11 {insert moves node within parent properly} {
tree mytree
mytree insert root end {IT::EM 0} {IT::EM 1} {IT::EM 2} {IT::EM 3}
mytree insert {IT::EM 3} end {IT::EM 4} {IT::EM 5} {IT::EM 6}
mytree insert root 2 {IT::EM 0} {IT::EM 4} {IT::EM 5} {IT::EM 6}
set result [mytree children root]
mytree destroy
set result
} [list {IT::EM 1} {IT::EM 0} {IT::EM 4} {IT::EM 5} {IT::EM 6} {IT::EM 2} {IT::EM 3}]
test tree-6.12 {insert moves node in parent properly when oldInd < newInd} {
tree mytree
mytree insert root end {IT::EM 0} {IT::EM 1} {IT::EM 2} {IT::EM 3}
mytree insert root 2 {IT::EM 0}
set result [mytree children root]
mytree destroy
set result
} [list {IT::EM 1} {IT::EM 0} {IT::EM 2} {IT::EM 3}]
test tree-6.13 {insert gives error when trying to move root} {
tree mytree
catch {mytree insert root end root} msg
mytree destroy
set msg
} "cannot move root node"
test tree-6.14 {insert gives error when trying to make node its descendant} {
tree mytree
mytree insert root end {IT::EM 0}
catch {mytree insert {IT::EM 0} end {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" cannot be its own descendant"
test tree-6.15 {insert gives error when trying to make node its descendant} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert {IT::EM 0} end {IT::EM 1}
mytree insert {IT::EM 1} end {IT::EM 2}
catch {mytree insert {IT::EM 2} end {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" cannot be its own descendant"
test tree-6.17 {check node names with spaces} {
tree mytree
catch {mytree insert root end ":\n\t "} msg
mytree destroy
set msg
} [list ":\n\t "]
test tree-6.18 {extended node names with spaces check} {
tree mytree
set ::FOO {}
proc walker {node} {
lappend ::FOO $node
}
set node ":\n\t "
set msg [mytree insert root end $node]
lappend msg [mytree isleaf $node]
mytree insert $node end yummy
lappend msg [mytree size $node]
lappend msg [mytree isleaf $node]
mytree set $node foo
mytree walk root -command [list walker %n]
lappend msg $::FOO
lappend msg [mytree keys $node]
lappend msg [mytree parent $node]
lappend msg [mytree set $node]
mytree destroy
set msg
} [list ":\n\t " 1 1 0 [list root ":\n\t " yummy] data root foo]
test tree-7.1 {move gives error when trying to move root} {
tree mytree
mytree insert root end {IT::EM 0}
catch {mytree move {IT::EM 0} end root} msg
mytree destroy
set msg
} "cannot move root node"
test tree-7.2 {move gives error when trying to move non existant node} {
tree mytree
catch {mytree move root end {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-7.3 {move gives error when trying to move to non existant parent} {
tree mytree
catch {mytree move {IT::EM 0} end {IT::EM 0}} msg
mytree destroy
set msg
} "parent node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-7.4 {move gives error when trying to make node its own descendant} {
tree mytree
mytree insert root end {IT::EM 0}
catch {mytree move {IT::EM 0} end {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" cannot be its own descendant"
test tree-7.5 {move gives error when trying to make node its own descendant} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert {IT::EM 0} end {IT::EM 1}
mytree insert {IT::EM 1} end {IT::EM 2}
catch {mytree move {IT::EM 2} end {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" cannot be its own descendant"
test tree-7.6 {move correctly moves a node} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert {IT::EM 0} end {IT::EM 1}
mytree insert {IT::EM 1} end {IT::EM 2}
mytree move {IT::EM 0} end {IT::EM 2}
set result [list [mytree children {IT::EM 0}] [mytree children {IT::EM 1}]]
lappend result [mytree parent {IT::EM 2}]
mytree destroy
set result
} {{{IT::EM 1} {IT::EM 2}} {} {IT::EM 0}}
test tree-7.7 {move moves multiple nodes properly} {
tree mytree
mytree insert root end {IT::EM 0} {IT::EM 1} {IT::EM 2}
mytree move root 0 {IT::EM 1} {IT::EM 2}
set result [list \
[mytree children root] \
]
mytree destroy
set result
} [list [list {IT::EM 1} {IT::EM 2} {IT::EM 0}]]
test tree-7.8 {move moves multiple nodes properly} {
tree mytree
mytree insert root end {IT::EM 0} {IT::EM 1} {IT::EM 2}
mytree move root 1 {IT::EM 0} {IT::EM 1}
set result [mytree children root]
mytree destroy
set result
} [list {IT::EM 2} {IT::EM 0} {IT::EM 1}]
test tree-7.9 {move moves node within parent properly} {
tree mytree
mytree insert root end {IT::EM 0} {IT::EM 1} {IT::EM 2} {IT::EM 3}
mytree move root 2 {IT::EM 1}
set result [mytree children root]
mytree destroy
set result
} [list {IT::EM 0} {IT::EM 2} {IT::EM 1} {IT::EM 3}]
test tree-7.10 {move moves node within parent properly} {
tree mytree
mytree insert root end {IT::EM 0} {IT::EM 1} {IT::EM 2} {IT::EM 3}
mytree insert {IT::EM 3} end {IT::EM 4} {IT::EM 5} {IT::EM 6}
mytree move root 2 {IT::EM 0} {IT::EM 4} {IT::EM 5} {IT::EM 6}
set result [mytree children root]
mytree destroy
set result
} [list {IT::EM 1} {IT::EM 2} {IT::EM 0} {IT::EM 4} {IT::EM 5} {IT::EM 6} {IT::EM 3}]
test tree-7.11 {move moves node in parent properly when oldInd < newInd} {
tree mytree
mytree insert root end {IT::EM 0} {IT::EM 1} {IT::EM 2} {IT::EM 3}
mytree move root 2 {IT::EM 0}
set result [mytree children root]
mytree destroy
set result
} [list {IT::EM 1} {IT::EM 2} {IT::EM 0} {IT::EM 3}]
test tree-7.12 {move node up one} {
tree mytree
mytree insert root end {IT::EM 0} {IT::EM 1} {IT::EM 2} {IT::EM 3}
mytree move root [mytree index [mytree next {IT::EM 0}]] {IT::EM 0}
set result [mytree children root]
mytree destroy
set result
} [list {IT::EM 1} {IT::EM 0} {IT::EM 2} {IT::EM 3}]
test tree-7.13 {move node down one} {
tree mytree
mytree insert root end {IT::EM 0} {IT::EM 1} {IT::EM 2} {IT::EM 3}
mytree move root [mytree index [mytree previous {IT::EM 2}]] {IT::EM 2}
set result [mytree children root]
mytree destroy
set result
} [list {IT::EM 0} {IT::EM 2} {IT::EM 1} {IT::EM 3}]
test tree-8.1 {parent gives error on fake node} {
tree mytree
catch {mytree parent {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-8.2 {parent gives correct value} {
tree mytree
mytree insert root end {IT::EM 0}
set result [list [mytree parent {IT::EM 0}] [mytree parent root]]
mytree destroy
set result
} {root {}}
test tree-9.1 {size gives error on bogus node} {
tree mytree
catch {mytree size {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-9.2 {size uses root node as default} {
tree mytree
set result [mytree size]
mytree destroy
set result
} 0
test tree-9.3 {size gives correct value} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1}
mytree insert root end {IT::EM 2}
mytree insert root end {IT::EM 3}
mytree insert root end {IT::EM 4}
mytree insert root end {IT::EM 5}
set result [mytree size]
mytree destroy
set result
} 6
test tree-9.4 {size gives correct value} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert {IT::EM 0} end {IT::EM 1}
mytree insert {IT::EM 0} end {IT::EM 2}
mytree insert {IT::EM 0} end {IT::EM 3}
mytree insert {IT::EM 1} end {IT::EM 4}
mytree insert {IT::EM 1} end {IT::EM 5}
set result [mytree size {IT::EM 0}]
mytree destroy
set result
} 5
test tree-9.5 {size gives correct value} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert {IT::EM 0} end {IT::EM 1}
mytree insert {IT::EM 0} end {IT::EM 2}
mytree insert {IT::EM 0} end {IT::EM 3}
mytree insert {IT::EM 1} end {IT::EM 4}
mytree insert {IT::EM 1} end {IT::EM 5}
set result [mytree size {IT::EM 1}]
mytree destroy
set result
} 2
test tree-10.1 {set gives error on bogus node} {
tree mytree
catch {mytree set {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-10.2 {set with node name gets/sets "data" value} {
tree mytree
mytree insert root end {IT::EM 0}
mytree set {IT::EM 0} foobar
set result [mytree set {IT::EM 0}]
mytree destroy
set result
} "foobar"
test tree-10.3 {set with node name and key gets/sets key value} {
tree mytree
mytree insert root end {IT::EM 0}
mytree set {IT::EM 0} -key baz foobar
set result [list [mytree set {IT::EM 0}] [mytree set {IT::EM 0} -key baz]]
mytree destroy
set result
} [list "" "foobar"]
test tree-10.4 {set with too many args gives error} {
tree mytree
mytree insert root end {IT::EM 0}
catch {mytree set {IT::EM 0} foo bar baz boo} msg
mytree destroy
set msg
} "wrong # args: should be \"::mytree set {IT::EM 0} ?-key key? ?value?\""
test tree-10.5 {set with bad args} {
tree mytree
mytree insert root end {IT::EM 0}
catch {mytree set {IT::EM 0} foo bar} msg
mytree destroy
set msg
} "invalid option \"foo\": should be key"
test tree-10.6 {set with bad args} {
tree mytree
mytree insert root end {IT::EM 0}
catch {mytree set {IT::EM 0} foo bar baz} msg
mytree destroy
set msg
} "invalid option \"foo\": should be key"
test tree-10.7 {set with bad key gives error} {
tree mytree
mytree insert root end {IT::EM 0}
catch {mytree set {IT::EM 0} -key foo} msg
mytree destroy
set msg
} "invalid key \"foo\" for node \"IT::EM 0\""
test tree-11.1 {depth} {
tree mytree
catch {mytree depth {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-11.2 {depth of root is 0} {
tree mytree
set result [mytree depth root]
mytree destroy
set result
} 0
test tree-11.3 {depth is computed correctly} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert {IT::EM 0} end {IT::EM 1}
mytree insert {IT::EM 1} end {IT::EM 2}
mytree insert {IT::EM 2} end {IT::EM 3}
set result [mytree depth {IT::EM 3}]
mytree destroy
set result
} 4
test tree-12.1 {pre dfs walk} {
tree mytree
set t [list ]
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1}
mytree insert {IT::EM 0} end {IT::EM 0.1}
mytree insert {IT::EM 0} end {IT::EM 0.2}
mytree insert {IT::EM 1} end {IT::EM 1.1}
mytree insert {IT::EM 1} end {IT::EM 1.2}
mytree walk root -type dfs -command {lappend t %a %t %n}
mytree destroy
set t
} [list enter ::mytree root enter ::mytree {IT::EM 0} enter ::mytree {IT::EM 0.1} \
enter ::mytree {IT::EM 0.2} enter ::mytree {IT::EM 1} \
enter ::mytree {IT::EM 1.1} enter ::mytree {IT::EM 1.2}]
test tree-12.1.0 {post dfs walk} {
tree mytree
set t [list ]
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1}
mytree insert {IT::EM 0} end {IT::EM 0.1}
mytree insert {IT::EM 0} end {IT::EM 0.2}
mytree insert {IT::EM 1} end {IT::EM 1.1}
mytree insert {IT::EM 1} end {IT::EM 1.2}
mytree walk root -order post -type dfs -command {lappend t %a %t %n}
mytree destroy
set t
} [list leave ::mytree {IT::EM 0.1} leave ::mytree {IT::EM 0.2} leave ::mytree {IT::EM 0} \
leave ::mytree {IT::EM 1.1} leave ::mytree {IT::EM 1.2} \
leave ::mytree {IT::EM 1} leave ::mytree root]
test tree-12.1.1 {both dfs walk} {
tree mytree
set t [list ]
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1}
mytree insert {IT::EM 0} end {IT::EM 0.1}
mytree insert {IT::EM 0} end {IT::EM 0.2}
mytree insert {IT::EM 1} end {IT::EM 1.1}
mytree insert {IT::EM 1} end {IT::EM 1.2}
mytree walk root -order both -type dfs -command {lappend t %a %t %n}
mytree destroy
set t
} [list enter ::mytree root enter ::mytree {IT::EM 0} enter ::mytree {IT::EM 0.1} \
leave ::mytree {IT::EM 0.1} enter ::mytree {IT::EM 0.2} leave ::mytree {IT::EM 0.2} \
leave ::mytree {IT::EM 0} enter ::mytree {IT::EM 1} enter ::mytree {IT::EM 1.1} \
leave ::mytree {IT::EM 1.1} enter ::mytree {IT::EM 1.2} leave ::mytree {IT::EM 1.2} \
leave ::mytree {IT::EM 1} leave ::mytree root]
test tree-12.1.3 {in dfs walk} {
tree mytree
set t [list ]
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1}
mytree insert {IT::EM 0} end {IT::EM 0.1}
mytree insert {IT::EM 0} end {IT::EM 0.2}
mytree insert {IT::EM 1} end {IT::EM 1.1}
mytree insert {IT::EM 1} end {IT::EM 1.2}
mytree walk root -order in -type dfs -command {lappend t %a %t %n}
mytree destroy
set t
} [list visit ::mytree {IT::EM 0.1} visit ::mytree {IT::EM 0} visit ::mytree {IT::EM 0.2} \
visit ::mytree root visit ::mytree {IT::EM 1.1} visit ::mytree {IT::EM 1} \
visit ::mytree {IT::EM 1.2}]
test tree-12.1.4 {pre dfs walk, different % specifiers} {
tree mytree
set t [list ]
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1}
mytree insert {IT::EM 0} end {IT::EM 0.1}
mytree insert {IT::EM 0} end {IT::EM 0.2}
mytree insert {IT::EM 1} end {IT::EM 1.1}
mytree insert {IT::EM 1} end {IT::EM 1.2}
mytree walk root -type dfs -command {lappend t %n %%}
mytree destroy
set t
} [list root % {IT::EM 0} % {IT::EM 0.1} % \
{IT::EM 0.2} % {IT::EM 1} % \
{IT::EM 1.1} % {IT::EM 1.2} %]
test tree-12.1.5 {pre dfs walk, different % specifiers} {
tree mytree
set t [list ]
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1}
mytree insert {IT::EM 0} end {IT::EM 0.1}
mytree insert {IT::EM 0} end {IT::EM 0.2}
mytree insert {IT::EM 1} end {IT::EM 1.1}
mytree insert {IT::EM 1} end {IT::EM 1.2}
mytree walk root -type dfs -command {lappend t %% %t}
mytree destroy
set t
} [list % ::mytree % ::mytree % ::mytree \
% ::mytree % ::mytree \
% ::mytree % ::mytree]
test tree-12.1.6 {pre dfs walk, nodes with spaces in names} {
tree mytree
set t [list ]
mytree insert root end "node/0"
mytree insert root end "node/1"
mytree insert "node/0" end "node/0/1"
mytree insert "node/0" end "node/0/2"
mytree insert "node/1" end "node/1/1"
mytree insert "node/1" end "node/1/2"
mytree walk root -type dfs -command {lappend t %n}
mytree destroy
set t
} [list root "node/0" "node/0/1" "node/0/2" "node/1" "node/1/1" "node/1/2"]
test tree-12.2 {pre bfs walk} {
tree mytree
set t [list ]
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1}
mytree insert {IT::EM 0} end {IT::EM 0.1}
mytree insert {IT::EM 0} end {IT::EM 0.2}
mytree insert {IT::EM 1} end {IT::EM 1.1}
mytree insert {IT::EM 1} end {IT::EM 1.2}
mytree walk root -type bfs -command {lappend t %a %t %n}
mytree destroy
set t
} [list enter ::mytree root enter ::mytree {IT::EM 0} enter ::mytree {IT::EM 1} \
enter ::mytree {IT::EM 0.1} enter ::mytree {IT::EM 0.2} enter ::mytree {IT::EM 1.1} \
enter ::mytree {IT::EM 1.2}]
test tree-12.2.0 {post bfs walk} {
tree mytree
set t [list ]
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1}
mytree insert {IT::EM 0} end {IT::EM 0.1}
mytree insert {IT::EM 0} end {IT::EM 0.2}
mytree insert {IT::EM 1} end {IT::EM 1.1}
mytree insert {IT::EM 1} end {IT::EM 1.2}
mytree walk root -type bfs -order post -command {lappend t %a %t %n}
mytree destroy
set t
} [list leave ::mytree {IT::EM 1.2} leave ::mytree {IT::EM 1.1} leave ::mytree {IT::EM 0.2} \
leave ::mytree {IT::EM 0.1} leave ::mytree {IT::EM 1} leave ::mytree {IT::EM 0} \
leave ::mytree root]
test tree-12.2.1 {both bfs walk} {
tree mytree
set t [list ]
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1}
mytree insert {IT::EM 0} end {IT::EM 0.1}
mytree insert {IT::EM 0} end {IT::EM 0.2}
mytree insert {IT::EM 1} end {IT::EM 1.1}
mytree insert {IT::EM 1} end {IT::EM 1.2}
mytree walk root -type bfs -order both -command {lappend t %a %t %n}
mytree destroy
set t
} [list enter ::mytree root enter ::mytree {IT::EM 0} enter ::mytree {IT::EM 1} \
enter ::mytree {IT::EM 0.1} enter ::mytree {IT::EM 0.2} enter ::mytree {IT::EM 1.1} \
enter ::mytree {IT::EM 1.2} leave ::mytree {IT::EM 1.2} leave ::mytree {IT::EM 1.1} \
leave ::mytree {IT::EM 0.2} leave ::mytree {IT::EM 0.1} leave ::mytree {IT::EM 1} \
leave ::mytree {IT::EM 0} leave ::mytree root]
test tree-12.3 {pre dfs is default walk} {
tree mytree
set t [list ]
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1}
mytree insert {IT::EM 0} end {IT::EM 0.1}
mytree insert {IT::EM 0} end {IT::EM 0.2}
mytree insert {IT::EM 1} end {IT::EM 1.1}
mytree insert {IT::EM 1} end {IT::EM 1.2}
mytree walk root -command {lappend t %a %t %n}
mytree destroy
set t
} [list enter ::mytree root enter ::mytree {IT::EM 0} enter ::mytree {IT::EM 0.1} \
enter ::mytree {IT::EM 0.2} enter ::mytree {IT::EM 1} \
enter ::mytree {IT::EM 1.1} enter ::mytree {IT::EM 1.2}]
test tree-12.4 {walk with too few args} {badTest} {
tree mytree
catch {mytree walk} msg
mytree destroy
set msg
} "no value given for parameter \"node\" to \"::struct::tree::_walk\""
test tree-12.5 {walk with too few args} {
tree mytree
catch {mytree walk root} msg
mytree destroy
set msg
} "wrong # args: should be \"::mytree walk root ?-type {bfs|dfs}? ?-order {pre|post|in|both}? -command cmd\""
test tree-12.6 {walk with too many args} {
tree mytree
catch {mytree walk root -foo bar -baz boo -foo2 boo -foo3 baz} msg
mytree destroy
set msg
} "wrong # args: should be \"::mytree walk root ?-type {bfs|dfs}? ?-order {pre|post|in|both}? -command cmd\""
test tree-12.7 {walk with fake node} {
tree mytree
catch {mytree walk {IT::EM 0} -command {}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-12.8 {walk gives error on invalid search type} {
tree mytree
catch {mytree walk root -type foo -command foo} msg
mytree destroy
set msg
} {invalid search type "foo": should be dfs, or bfs}
test tree-12.9 {walk gives error on invalid search order} {
tree mytree
catch {mytree walk root -order foo -command foo} msg
mytree destroy
set msg
} {invalid search order "foo": should be pre, post, both, or in}
test tree-12.10 {walk gives error on invalid combination of order and type} {
tree mytree
catch {mytree walk root -order in -type bfs -command foo} msg
mytree destroy
set msg
} {unable to do a in-order breadth first walk}
test tree-12.11 {walk with unknown options} {
tree mytree
catch {mytree walk root -foo bar} msg
mytree destroy
set msg
} "unknown option \"-foo\": should be \"::mytree walk root ?-type {bfs|dfs}? ?-order {pre|post|in|both}? -command cmd\""
test tree-12.12 {walk, option without value} {
tree mytree
catch {mytree walk root -type dfs -order} msg
mytree destroy
set msg
} "value for \"-order\" missing: should be \"::mytree walk root ?-type {bfs|dfs}? ?-order {pre|post|in|both}? -command cmd\""
test tree-12.13 {walk without command} {
tree mytree
catch {mytree walk root -order pre} msg
mytree destroy
set msg
} "no command specified: should be \"::mytree walk root ?-type {bfs|dfs}? ?-order {pre|post|in|both}? -command cmd\""
test tree-13.1 {swap gives error when trying to swap root} {
tree mytree
catch {mytree swap root {IT::EM 0}} msg
mytree destroy
set msg
} "cannot swap root node"
test tree-13.2 {swap gives error when trying to swap non existant node} {
tree mytree
catch {mytree swap {IT::EM 0} {IT::EM 1}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-13.3 {swap gives error when trying to swap non existant node} {
tree mytree
mytree insert root end {IT::EM 0}
catch {mytree swap {IT::EM 0} {IT::EM 1}} msg
mytree destroy
set msg
} "node \"IT::EM 1\" does not exist in tree \"::mytree\""
test tree-13.4 {swap gives error when trying to swap node with self} {
tree mytree
mytree insert root end {IT::EM 0}
catch {mytree swap {IT::EM 0} {IT::EM 0}} msg
mytree destroy
set msg
} "cannot swap node \"IT::EM 0\" with itself"
test tree-13.5 {swap swaps node relationships correctly} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert {IT::EM 0} end {IT::EM 0.1}
mytree insert {IT::EM 0} end {IT::EM 0.2}
mytree insert {IT::EM 0.1} end {IT::EM 0.1.1}
mytree insert {IT::EM 0.1} end {IT::EM 0.1.2}
mytree swap {IT::EM 0} {IT::EM 0.1}
set t [list ]
mytree walk root -command {lappend t %a %t %n}
mytree destroy
set t
} [list enter ::mytree root enter ::mytree {IT::EM 0.1} enter ::mytree {IT::EM 0} \
enter ::mytree {IT::EM 0.1.1} enter ::mytree {IT::EM 0.1.2} enter ::mytree {IT::EM 0.2}]
test tree-13.6 {swap swaps node relationships correctly} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert {IT::EM 0} end {IT::EM 0.1}
mytree insert {IT::EM 0} end {IT::EM 0.2}
mytree insert {IT::EM 0.1} end {IT::EM 0.1.1}
mytree insert {IT::EM 0.1} end {IT::EM 0.1.2}
mytree swap {IT::EM 0} {IT::EM 0.1.1}
set t [list ]
mytree walk root -command {lappend t %a %t %n}
mytree destroy
set t
} [list enter ::mytree root enter ::mytree {IT::EM 0.1.1} enter ::mytree {IT::EM 0.1} \
enter ::mytree {IT::EM 0} enter ::mytree {IT::EM 0.1.2} enter ::mytree {IT::EM 0.2}]
test tree-13.7 {swap swaps node relationships correctly} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1}
mytree insert {IT::EM 0} end {IT::EM 0.1}
mytree insert {IT::EM 1} end {IT::EM 1.1}
mytree swap {IT::EM 0} {IT::EM 1}
set t [list ]
mytree walk root -command {lappend t %a %t %n}
mytree destroy
set t
} [list enter ::mytree root enter ::mytree {IT::EM 1} enter ::mytree {IT::EM 0.1} \
enter ::mytree {IT::EM 0} enter ::mytree {IT::EM 1.1}]
test tree-13.8 {swap swaps node relationships correctly} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert {IT::EM 0} end {IT::EM 0.1}
mytree insert {IT::EM 0} end {IT::EM 0.2}
mytree insert {IT::EM 0.1} end {IT::EM 0.1.1}
mytree insert {IT::EM 0.1} end {IT::EM 0.1.2}
mytree swap {IT::EM 0.1} {IT::EM 0}
set t [list ]
mytree walk root -command {lappend t %a %t %n}
mytree destroy
set t
} [list enter ::mytree root enter ::mytree {IT::EM 0.1} enter ::mytree {IT::EM 0} \
enter ::mytree {IT::EM 0.1.1} enter ::mytree {IT::EM 0.1.2} enter ::mytree {IT::EM 0.2}]
test tree-14.1 {get gives error on bogus node} {
tree mytree
catch {mytree get {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-14.2 {get gives error on bogus key} {
tree mytree
mytree insert root end {IT::EM 0}
catch {mytree get {IT::EM 0} -key bogus} msg
mytree destroy
set msg
} "invalid key \"bogus\" for node \"IT::EM 0\""
test tree-14.3 {get uses data as default key} {
tree mytree
mytree insert root end {IT::EM 0}
mytree set {IT::EM 0} foobar
set result [mytree get {IT::EM 0}]
mytree destroy
set result
} "foobar"
test tree-14.4 {get respects -key flag} {
tree mytree
mytree insert root end {IT::EM 0}
mytree set {IT::EM 0} -key boom foobar
set result [mytree get {IT::EM 0} -key boom]
mytree destroy
set result
} "foobar"
test tree-15.1 {unset gives error on bogus node} {
tree mytree
catch {mytree unset {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-15.2 {unset does not give error on bogus key} {
tree mytree
mytree insert root end {IT::EM 0}
set result [catch {mytree unset {IT::EM 0} -key bogus}]
mytree destroy
set result
} 0
test tree-15.3 {unset removes a keyed value from a node} {
tree mytree
mytree insert root end {IT::EM 0}
mytree set {IT::EM 0} -key foobar foobar
mytree unset {IT::EM 0} -key foobar
catch {mytree get {IT::EM 0} -key foobar} msg
mytree destroy
set msg
} "invalid key \"foobar\" for node \"IT::EM 0\""
test tree-15.4 {unset requires -key} {
tree mytree
mytree insert root end {IT::EM 0}
mytree set {IT::EM 0} -key foobar foobar
catch {mytree unset {IT::EM 0} flaboozle foobar} msg
mytree destroy
set msg
} "invalid option \"flaboozle\": should be \"::mytree unset {IT::EM 0} ?-key key?\""
test tree-16.1 {isleaf} {
tree mytree
set t [mytree isleaf root]
mytree insert root end {IT::EM 0}
lappend t [mytree isleaf root] [mytree isleaf {IT::EM 0}]
mytree destroy
set t
} [list 1 0 1]
test tree-16.2 {isleaf} {
tree mytree
catch {mytree isleaf {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-17.1 {index of root fails} {
tree mytree
catch {mytree index root} msg
mytree destroy
set msg
} "cannot determine index of root node"
test tree-17.2 {index} {
tree mytree
mytree insert root end {IT::EM 1}
mytree insert root end {IT::EM 0}
set result [list [mytree index {IT::EM 0}] [mytree index {IT::EM 1}]]
mytree destroy
set result
} [list 1 0]
test tree-17.3 {index of non-existant node} {
tree mytree
catch {mytree index {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-18.1 {numchildren} {
tree mytree
set t [mytree numchildren root]
mytree insert root end {IT::EM 0}
lappend t [mytree numchildren root] [mytree numchildren {IT::EM 0}]
mytree destroy
set t
} [list 0 1 0]
test tree-18.2 {numchildren} {
tree mytree
catch {mytree numchildren {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-19.1 {next from root} {
tree mytree
set res [mytree next root]
mytree destroy
set res
} {}
test tree-19.2 {next from fake node} {
tree mytree
catch {mytree next {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-19.3 {next} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1}
set res [list [mytree next {IT::EM 0}] [mytree next {IT::EM 1}]]
mytree destroy
set res
} [list {IT::EM 1} {}]
test tree-20.1 {previous from root} {
tree mytree
set res [mytree previous root]
mytree destroy
set res
} {}
test tree-20.2 {previous from fake node} {
tree mytree
catch {mytree previous {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-20.3 {next} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1}
set res [list [mytree previous {IT::EM 0}] [mytree previous {IT::EM 1}]]
mytree destroy
set res
} [list {} {IT::EM 0}]
test tree-21.1 {cutting nodes} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1}
mytree insert root end {IT::EM 2}
mytree insert {IT::EM 1} end {IT::EM 1.0}
mytree insert {IT::EM 1} end {IT::EM 1.1}
mytree insert {IT::EM 1} end {IT::EM 1.2}
mytree cut {IT::EM 1}
set t [list ]
mytree walk root -command {lappend t %a %t %n}
mytree destroy
set t
} {enter ::mytree root enter ::mytree {IT::EM 0} enter ::mytree {IT::EM 1.0} enter ::mytree {IT::EM 1.1} enter ::mytree {IT::EM 1.2} enter ::mytree {IT::EM 2}}
test tree-21.2 {cutting nodes} {
tree mytree
catch {mytree cut root} msg
mytree destroy
set msg
} {cannot cut root node}
test tree-21.3 {cut sets parent values of relocated nodes} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1}
mytree insert root end {IT::EM 2}
mytree insert {IT::EM 1} end {IT::EM 1.0}
mytree insert {IT::EM 1} end {IT::EM 1.1}
mytree insert {IT::EM 1} end {IT::EM 1.2}
mytree cut {IT::EM 1}
set res [list \
[mytree parent {IT::EM 1.0}] \
[mytree parent {IT::EM 1.1}] \
[mytree parent {IT::EM 1.2}]]
mytree destroy
set res
} [list root root root]
test tree-21.4 {cut removes node} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1}
mytree insert root end {IT::EM 2}
mytree insert {IT::EM 1} end {IT::EM 1.0}
mytree insert {IT::EM 1} end {IT::EM 1.1}
mytree insert {IT::EM 1} end {IT::EM 1.2}
mytree cut {IT::EM 1}
set res [mytree exists {IT::EM 1}]
mytree destroy
set res
} 0
test tree-21.5 {cut removes node} {
tree mytree
catch {mytree cut {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-22.1 {splicing nodes} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1.0}
mytree insert root end {IT::EM 1.1}
mytree insert root end {IT::EM 1.2}
mytree insert root end {IT::EM 2}
mytree splice root 1 3 {IT::EM 1}
set t [list ]
mytree walk root -command {lappend t %a %t %n}
mytree destroy
set t
} {enter ::mytree root enter ::mytree {IT::EM 0} enter ::mytree {IT::EM 1} enter ::mytree {IT::EM 1.0} enter ::mytree {IT::EM 1.1} enter ::mytree {IT::EM 1.2} enter ::mytree {IT::EM 2}}
test tree-22.2 {splicing nodes with no node name given} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1.0}
mytree insert root end {IT::EM 1.1}
mytree insert root end {IT::EM 1.2}
mytree insert root end {IT::EM 2}
set res [mytree splice root 1 3]
set t [list ]
mytree walk root -command {lappend t %a %t %n}
mytree destroy
list $res $t
} [list node1 {enter ::mytree root enter ::mytree {IT::EM 0} enter ::mytree node1 enter ::mytree {IT::EM 1.0} enter ::mytree {IT::EM 1.1} enter ::mytree {IT::EM 1.2} enter ::mytree {IT::EM 2}}]
test tree-22.3 {splicing nodes errors on duplicate node name} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1.0}
mytree insert root end {IT::EM 1.1}
mytree insert root end {IT::EM 1.2}
mytree insert root end {IT::EM 2}
catch {mytree splice root 1 3 {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" already exists in tree \"::mytree\""
test tree-22.4 {splicing node sets parent values correctly} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1.0}
mytree insert root end {IT::EM 1.1}
mytree insert root end {IT::EM 1.2}
mytree insert root end {IT::EM 2}
mytree splice root 1 3 {IT::EM 1}
set res [list \
[mytree parent {IT::EM 1}] \
[mytree parent {IT::EM 1.0}] \
[mytree parent {IT::EM 1.1}] \
[mytree parent {IT::EM 1.2}]]
mytree destroy
set res
} [list root {IT::EM 1} {IT::EM 1} {IT::EM 1}]
test tree-22.5 {splicing node works with strange index} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1.0}
mytree insert root end {IT::EM 1.1}
mytree insert root end {IT::EM 1.2}
mytree insert root end {IT::EM 2}
mytree splice root -5 12 {IT::EM 1}
set t [list ]
mytree walk root -command {lappend t %a %t %n}
mytree destroy
set t
} {enter ::mytree root enter ::mytree {IT::EM 1} enter ::mytree {IT::EM 0} enter ::mytree {IT::EM 1.0} enter ::mytree {IT::EM 1.1} enter ::mytree {IT::EM 1.2} enter ::mytree {IT::EM 2}}
test tree-22.6 {splicing nodes with no node name and no "to" index given} {
tree mytree
mytree insert root end {IT::EM 0}
mytree insert root end {IT::EM 1.0}
mytree insert root end {IT::EM 1.1}
mytree insert root end {IT::EM 1.2}
mytree insert root end {IT::EM 2}
mytree splice root 1
set t [list ]
mytree walk root -command {lappend t %a %t %n}
mytree destroy
set t
} {enter ::mytree root enter ::mytree {IT::EM 0} enter ::mytree node1 enter ::mytree {IT::EM 1.0} enter ::mytree {IT::EM 1.1} enter ::mytree {IT::EM 1.2} enter ::mytree {IT::EM 2}}
test tree-23.1 {getall gives error on bogus node} {
tree mytree
catch {mytree getall {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-23.2 {getall gives error when key specified} {
tree mytree
catch {mytree getall {IT::EM 0} -key data} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-23.3 {getall with node name returns list of key/value pairs} {
tree mytree
mytree insert root end {IT::EM 0}
mytree set {IT::EM 0} foobar
mytree set {IT::EM 0} -key other thing
set results [mytree getall {IT::EM 0}]
mytree destroy
lsort $results
} "data foobar other thing"
test tree-24.1 {keys gives error on bogus node} {
tree mytree
catch {mytree keys {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-24.2 {keys gives error when key specified} {
tree mytree
catch {mytree keys {IT::EM 0} -key data} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-24.3 {keys with node name returns list of keys} {
tree mytree
mytree insert root end {IT::EM 0}
mytree set {IT::EM 0} foobar
mytree set {IT::EM 0} -key other thing
set results [mytree keys {IT::EM 0}]
mytree destroy
lsort $results
} "data other"
test tree-25.1 {keyexists gives error on bogus node} {
tree mytree
catch {mytree keyexists {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-25.2 {keyexists returns false on non-existant key} {
tree mytree
mytree insert root end {IT::EM 0}
catch {mytree keyexists {IT::EM 0} -key bogus} msg
mytree destroy
set msg
} "0"
test tree-25.3 {keyexists uses data as default key} {
tree mytree
mytree insert root end {IT::EM 0}
mytree set {IT::EM 0} foobar
set result [mytree keyexists {IT::EM 0}]
mytree destroy
set result
} "1"
test tree-25.4 {keyexists respects -key flag} {
tree mytree
mytree insert root end {IT::EM 0}
mytree set {IT::EM 0} -key boom foobar
set result [mytree keyexists {IT::EM 0} -key boom]
mytree destroy
set result
} "1"
test tree-26.1 {append gives error on bogus node} {
tree mytree
catch {mytree append {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-26.2 {append with node name appends to "data" value} {
tree mytree
mytree insert root end {IT::EM 0}
mytree set {IT::EM 0} foo
set result [mytree append {IT::EM 0} bar]
mytree destroy
set result
} "foobar"
test tree-26.3 {append with node name and key appends key value} {
tree mytree
mytree insert root end {IT::EM 0}
mytree set {IT::EM 0} -key baz foo
set result [mytree append {IT::EM 0} -key baz bar]
mytree destroy
set result
} "foobar"
test tree-26.4 {append with too many args gives error} {
tree mytree
mytree insert root end {IT::EM 0}
catch {mytree append {IT::EM 0} foo bar baz boo} msg
mytree destroy
set msg
} "wrong # args: should be \"::mytree set {IT::EM 0} ?-key key? value\""
test tree-26.5 {append with bad args} {
tree mytree
mytree insert root end {IT::EM 0}
catch {mytree append {IT::EM 0} -foo bar baz} msg
mytree destroy
set msg
} "invalid option \"-foo\": should be -key"
test tree-26.6 {append respects -key flag} {
tree mytree
mytree insert root end {IT::EM 0}
mytree set {IT::EM 0} -key baz foo
set result [mytree append {IT::EM 0} -key baz bar]
mytree destroy
set result
} "foobar"
test tree-27.1 {lappend gives error on bogus node} {
tree mytree
catch {mytree lappend {IT::EM 0}} msg
mytree destroy
set msg
} "node \"IT::EM 0\" does not exist in tree \"::mytree\""
test tree-27.2 {lappend with node name appends to "data" value} {
tree mytree
mytree insert root end {IT::EM 0}
mytree set {IT::EM 0} foo
set result [mytree lappend {IT::EM 0} bar]
mytree destroy
set result
} "foo bar"
test tree-27.3 {lappend with node name and key appends key value} {
tree mytree
mytree insert root end {IT::EM 0}
mytree set {IT::EM 0} -key baz foo
set result [mytree lappend {IT::EM 0} -key baz bar]
mytree destroy
set result
} "foo bar"
test tree-27.4 {lappend with too many args gives error} {
tree mytree
mytree insert root end {IT::EM 0}
catch {mytree lappend {IT::EM 0} foo bar baz boo} msg
mytree destroy
set msg
} "wrong # args: should be \"::mytree lappend {IT::EM 0} ?-key key? value\""
test tree-27.5 {lappend with bad args} {
tree mytree
mytree insert root end {IT::EM 0}
catch {mytree lappend {IT::EM 0} -foo bar baz} msg
mytree destroy
set msg
} "invalid option \"-foo\": should be -key"
test tree-27.6 {lappend respects -key flag} {
tree mytree
mytree insert root end {IT::EM 0}
mytree set {IT::EM 0} -key baz foo
set result [mytree lappend {IT::EM 0} -key baz bar]
mytree destroy
set result
} "foo bar"
test tree-28.1 {serialization} {
tree mytree
mytree insert root end 0
mytree insert root end 1
mytree insert root end 2
mytree insert 0 end 3
mytree insert 0 end 4
set result [mytree serialize]
mytree destroy
foreach {t a} $result break
list $t [dictsort $a]
} {{root {{0 {{3 {}} {4 {}}}} {1 {}} {2 {}}}} {0 {} 1 {} 2 {} 3 {} 4 {} root {}}}
test tree-28.2 {serialization} {
tree mytree
mytree insert root end 0
mytree insert root end 1
mytree insert root end 2
mytree insert 0 end 3
mytree insert 0 end 4
set result [mytree serialize 0]
mytree destroy
foreach {t a} $result break
list $t [dictsort $a]
} {{0 {{3 {}} {4 {}}}} {0 {} 3 {} 4 {}}}
testsuiteCleanup
|