| 12
 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
 
 | # Tests for the 'list' module in the 'struct' library. -*- tcl -*-
#
# This file contains a collection of tests for one or more of the Tcllib
# procedures.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 2003 by Kevin B. Kenny. All rights reserved.
#
# RCS: @(#) $Id: list.test,v 1.30 2009/09/24 22:22:28 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 list.tcl struct::list
}
# -------------------------------------------------------------------------
# Fake [lset] for Tcl releases that don't have it.  We need only
# lset into a flat list.
if { [string compare lset [info commands lset]] } {
    proc K { x y } { set x }
    proc lset { listVar index var } {
	upvar 1 $listVar list
	set list [lreplace [K $list [set list {}]] $index $index $var]
    }
}
#----------------------------------------------------------------------
interp alias {} lcs {} ::struct::list::list longestCommonSubsequence
test list-lcs-1.1 {longestCommonSubsequence, no args} {
    catch { lcs } msg
    set msg
} [tcltest::wrongNumArgs ::struct::list::LlongestCommonSubsequence \
       {sequence1 sequence2 ?maxOccurs?} 0]
test list-lcs-1.2 {longestCommonSubsequence, one arg} {
    catch { lcs x } msg
    set msg
} [tcltest::wrongNumArgs ::struct::list::LlongestCommonSubsequence \
       {sequence1 sequence2 ?maxOccurs?} 1]
test list-lcs-2.1 {longestCommonSubsequence, two empty lists} {
    list [catch { lcs {} {} } msg] $msg
} {0 {{} {}}}
test list-lcs-2.2 {longestCommonSubsequence, insert 1 into an empty list} {
    list [catch { lcs {} {a} } msg] $msg
} {0 {{} {}}}
test list-lcs-2.3 {longestCommonSubsequence, delete 1 from singleton list} {
    list [catch { lcs {a} {} } msg] $msg
} {0 {{} {}}}
test list-lcs-2.4 {longestCommonSubsequence, preserve singleton list} {
    list [catch { lcs {a} {a} } msg] $msg
} {0 {0 0}}
test list-lcs-2.5 {longestCommonSubsequence, 1-element change in singleton list} {
    list [catch { lcs {a} {b} } msg] $msg
} {0 {{} {}}}
test list-lcs-2.6 {longestCommonSubsequence, insert 1 in front of singleton list} {
    list [catch { lcs {a} {b a} } msg] $msg
} {0 {0 1}}
test list-lcs-2.7 {longestCommonSubsequence, insert 1 at end of singleton list} {
    list [catch {lcs {a} {a b}} msg] $msg
} {0 {0 0}}
test list-lcs-2.8 {longestCommonSubsequence, duplicate element} {
    list [catch {lcs {a} {a a}} msg] $msg
} {0 {0 0}}
test list-lcs-2.9 {longestCommonSubsequence, interchange 2} {
    list [catch {lcs {a b} {b a}} msg] $msg
} {0 {1 0}}
test list-lcs-2.10 {longestCommonSubsequence, insert before 2} {
    list [catch {lcs {a b} {b a b}} msg] $msg
} {0 {{0 1} {1 2}}}
test list-lcs-2.11 {longestCommonSubsequence, insert inside 2} {
    list [catch {lcs {a b} {a a b}} msg] $msg
} {0 {{0 1} {0 2}}}
test list-lcs-2.12 {longestCommonSubsequence, insert after 2} {
    list [catch {lcs {a b} {a b a}} msg] $msg
} {0 {{0 1} {0 1}}}
test list-lcs-2.13 {longestCommonSubsequence, delete first of 2} {
    list [catch {lcs {a b} a} msg] $msg
} {0 {0 0}}
test list-lcs-2.14 {longestCommonSubsequence, delete second of 2} {
    list [catch {lcs {a b} b} msg] $msg
} {0 {1 0}}
test list-lcs-2.15 {longestCommonSubsequence, change first of 2} {
    list [catch {lcs {a b} {c b}} msg] $msg
} {0 {1 1}}
test list-lcs-2.16 {longestCommonSubsequence, change first of 2 to dupe} {
    list [catch {lcs {a b} {b b}} msg] $msg
} {0 {1 0}}
test list-lcs-2.17 {longestCommonSubsequence, change second of 2} {
    list [catch {lcs {a b} {a c}} msg] $msg
} {0 {0 0}}
test list-lcs-2.18 {longestCommonSubsequence, change second of 2 to dupe} {
    list [catch {lcs {a b} {a a}} msg] $msg
} {0 {0 0}}
test list-lcs-2.19 {longestCommonSubsequence, mixed changes} {
    list [catch {lcs {a b r a c a d a b r a} {b r i c a b r a c}} msg] $msg
} {0 {{1 2 4 5 8 9 10} {0 1 3 4 5 6 7}}}
test list-lcs-2.20 {longestCommonSubsequence, mixed changes} {
    list [catch {lcs {b r i c a b r a c} {a b r a c a d a b r a}} msg] $msg
} {0 {{0 1 3 4 5 6 7} {1 2 4 5 8 9 10}}}
test list-lcs-3.1 {longestCommonSubsequence, length limit} {
    list [catch {lcs {b r i c a b r a c} {a b r a c a d a b r a} 5} msg] $msg
} {0 {{0 1 3 4 5 6 7} {1 2 4 5 8 9 10}}}
test list-lcs-3.2 {longestCommonSubsequence, length limit} {
    list [catch {lcs {b r i c a b r a c} {a b r a c a d a b r a} 4} msg] $msg
} {0 {{0 1 3 5 6} {1 2 4 8 9}}}
test list-lcs-3.3 {longestCommonSubsequence, length limit} {
    list [catch {lcs {b r i c a b r a c} {a b r a c a d a b r a} 1} msg] $msg
} {0 {3 4}}
test list-lcs-3.4 {longestCommonSubsequence, stupid length limit} {
    list [catch {lcs {b r i c a b r a c} {a b r a c a d a b r a} 0} msg] $msg
} {0 {{} {}}}
#----------------------------------------------------------------------
interp alias {} lcs2 {} ::struct::list::list longestCommonSubsequence2
test list-lcs2-1.1 {longestCommonSubsequence2, no args} {
    catch { lcs2 } msg
    set msg
} [tcltest::wrongNumArgs ::struct::list::LlongestCommonSubsequence2 \
       {sequence1 sequence2 ?maxOccurs?} 0]
test list-lcs2-1.2 {longestCommonSubsequence2, one arg} {
    catch { lcs2 x } msg
    set msg
} [tcltest::wrongNumArgs ::struct::list::LlongestCommonSubsequence2 \
       {sequence1 sequence2 ?maxOccurs?} 1]
test list-lcs2-2.1 {longestCommonSubsequence2, two empty lists} {
    list [catch { lcs2 {} {} } msg] $msg
} {0 {{} {}}}
test list-lcs2-2.2 {longestCommonSubsequence2, insert 1 into an empty list} {
    list [catch { lcs2 {} {a} } msg] $msg
} {0 {{} {}}}
test list-lcs2-2.3 {longestCommonSubsequence2, delete 1 from singleton list} {
    list [catch { lcs2 {a} {} } msg] $msg
} {0 {{} {}}}
test list-lcs2-2.4 {longestCommonSubsequence2, preserve singleton list} {
    list [catch { lcs2 {a} {a} } msg] $msg
} {0 {0 0}}
test list-lcs2-2.5 {longestCommonSubsequence2, 1-element change in singleton list} {
    list [catch { lcs2 {a} {b} } msg] $msg
} {0 {{} {}}}
test list-lcs2-2.6 {longestCommonSubsequence2, insert 1 in front of singleton list} {
    list [catch { lcs2 {a} {b a} } msg] $msg
} {0 {0 1}}
test list-lcs2-2.7 {longestCommonSubsequence2, insert 1 at end of singleton list} {
    list [catch {lcs2 {a} {a b}} msg] $msg
} {0 {0 0}}
test list-lcs2-2.8 {longestCommonSubsequence2, duplicate element} {
    list [catch {lcs2 {a} {a a}} msg] $msg
} {0 {0 0}}
test list-lcs2-2.9 {longestCommonSubsequence2, interchange 2} {
    list [catch {lcs2 {a b} {b a}} msg] $msg
} {0 {1 0}}
test list-lcs2-2.10 {longestCommonSubsequence2, insert before 2} {
    list [catch {lcs2 {a b} {b a b}} msg] $msg
} {0 {{0 1} {1 2}}}
test list-lcs2-2.11 {longestCommonSubsequence2, insert inside 2} {
    list [catch {lcs2 {a b} {a a b}} msg] $msg
} {0 {{0 1} {0 2}}}
test list-lcs2-2.12 {longestCommonSubsequence2, insert after 2} {
    list [catch {lcs2 {a b} {a b a}} msg] $msg
} {0 {{0 1} {0 1}}}
test list-lcs2-2.13 {longestCommonSubsequence2, delete first of 2} {
    list [catch {lcs2 {a b} a} msg] $msg
} {0 {0 0}}
test list-lcs2-2.14 {longestCommonSubsequence2, delete second of 2} {
    list [catch {lcs2 {a b} b} msg] $msg
} {0 {1 0}}
test list-lcs2-2.15 {longestCommonSubsequence2, change first of 2} {
    list [catch {lcs2 {a b} {c b}} msg] $msg
} {0 {1 1}}
test list-lcs2-2.16 {longestCommonSubsequence2, change first of 2 to dupe} {
    list [catch {lcs2 {a b} {b b}} msg] $msg
} {0 {1 0}}
test list-lcs2-2.17 {longestCommonSubsequence2, change second of 2} {
    list [catch {lcs2 {a b} {a c}} msg] $msg
} {0 {0 0}}
test list-lcs2-2.18 {longestCommonSubsequence2, change second of 2 to dupe} {
    list [catch {lcs2 {a b} {a a}} msg] $msg
} {0 {0 0}}
test list-lcs2-2.19 {longestCommonSubsequence2, mixed changes} {
    list [catch {lcs2 {a b r a c a d a b r a} {b r i c a b r a c}} msg] $msg
} {0 {{1 2 4 5 8 9 10} {0 1 3 4 5 6 7}}}
test list-lcs2-2.20 {longestCommonSubsequence2, mixed changes} {
    list [catch {lcs2 {b r i c a b r a c} {a b r a c a d a b r a}} msg] $msg
} {0 {{0 1 3 4 5 6 7} {1 2 4 5 8 9 10}}}
test list-lcs2-3.1 {longestCommonSubsequence2, length limit} {
    list [catch {lcs2 {b r i c a b r a c} {a b r a c a d a b r a} 5} msg] $msg
} {0 {{0 1 3 4 5 6 7} {1 2 4 5 8 9 10}}}
test list-lcs2-3.2 {longestCommonSubsequence2, length limit} {
    list [catch {lcs2 {b r i c a b r a c} {a b r a c a d a b r a} 4} msg] $msg
} {0 {{0 1 3 4 5 6 7} {1 2 4 5 8 9 10}}}
test list-lcs2-3.3 {longestCommonSubsequence2, length limit} {
    list [catch {lcs2 {b r i c a b r a c} {a b r a c a d a b r a} 1} msg] $msg
} {0 {{0 1 3 4 5 6 7} {1 2 4 5 8 9 10}}}
test list-lcs2-3.4 {longestCommonSubsequence2, stupid length limit} {
    list [catch {lcs2 {b r i c a b r a c} {a b r a c a d a b r a} 0} msg] $msg
} {0 {{0 1 3 4 5 6 7} {1 2 4 5 8 9 10}}}
#----------------------------------------------------------------------
interp alias {} lcsi  {} ::struct::list::list lcsInvert
interp alias {} lcsim {} ::struct::list::list lcsInvertMerge
test list-lcsInv-4.0 {longestCommonSubsequence, mixed changes} {
    # sequence 1 = a b r a c a d a b r a
    # lcs 1      =   1 2   4 5     8 9 10
    # lcs 2      =   0 1   3 4     5 6 7
    # sequence 2 =   b r i c a     b r a c
    #
    # Inversion  = deleted  {0  0} {-1 0}
    #              changed  {3  3}  {2 2}
    #              deleted  {6  7}  {4 5}
    #              added   {10 11}  {8 8}
    list [catch {lcsi [lcs {a b r a c a d a b r a} {b r i c a b r a c}] 11 9} msg] $msg
} {0 {{deleted {0 0} {-1 0}} {changed {3 3} {2 2}} {deleted {6 7} {4 5}} {added {10 11} {8 8}}}}
test list-lcsInv-4.1 {longestCommonSubsequence, mixed changes} {
    # sequence 1 = a b r a c a d a b r a
    # lcs 1      =   1 2   4 5     8 9 10
    # lcs 2      =   0 1   3 4     5 6 7
    # sequence 2 =   b r i c a     b r a c
    #
    # Inversion/Merge  = deleted   {0  0} {-1 0}
    #                    unchanged {1  2}  {0 1}
    #                    changed   {3  3}  {2 2}
    #                    unchanged {4  5}  {3 4}
    #                    deleted   {6  7}  {4 5}
    #                    unchanged {8 10}  {5 7}
    #                    added    {10 11}  {8 8}
    list [catch {lcsim [lcs {a b r a c a d a b r a} {b r i c a b r a c}] 11 9} msg] $msg
} {0 {{deleted {0 0} {-1 0}} {unchanged {1 2} {0 1}} {changed {3 3} {2 2}} {unchanged {4 5} {3 4}} {deleted {6 7} {4 5}} {unchanged {8 10} {5 7}} {added {10 11} {8 8}}}}
proc diff2 {s1 s2} {
    set l1 [split $s1 {}]
    set l2 [split $s2 {}]
    set x [lcs $l1 $l2]
    lcsim $x [llength $l1] [llength $l2]
}
test list-lcsInv-4.2 {lcsInvertMerge} {
    # Handling of 'unchanged' chunks at the beginning of the result
    # (when result actually empty).
    diff2 ab "a b" 
} {{unchanged {0 0} {0 0}} {added {0 1} {1 1}} {unchanged {1 1} {2 2}}}
test list-lcsInv-4.3 {lcsInvertMerge} {
    diff2 abcde afcge
} {{unchanged {0 0} {0 0}} {changed {1 1} {1 1}} {unchanged {2 2} {2 2}} {changed {3 3} {3 3}} {unchanged {4 4} {4 4}}}
#----------------------------------------------------------------------
interp alias {} reverse {} ::struct::list::list reverse
test reverse-1.1 {reverse method} {
    reverse {a b c}
} {c b a}
test reverse-1.2 {reverse method} {
    reverse a
} {a}
test reverse-1.3 {reverse method} {
    reverse {}
} {}
test reverse-2.1 {reverse errors} {
    list [catch {reverse} msg] $msg
} [list 1 [tcltest::wrongNumArgs ::struct::list::Lreverse {sequence} 0]]
#----------------------------------------------------------------------
interp alias {} assign {} ::struct::list::list assign
test assign-4.1 {assign method} {
    catch {unset ::x ::y}
    list [assign {foo bar} x y] $x $y
} {{} foo bar}
test assign-4.2 {assign method} {
    catch {unset x y}
    list [assign {foo bar baz} x y] $x $y
} {baz foo bar}
test assign-4.3 {assign method} {
    catch {unset x y z}
    list [assign {foo bar} x y z] $x $y $z
} {{} foo bar {}}
if {[package vcompare [package provide Tcl] 8.5] < 0} {
    # 8.4
    set err [tcltest::wrongNumArgs {::struct::list::Lassign} {sequence v args} 1]
} else {
    # 8.5+
    #set err [tcltest::wrongNumArgs {lassign}                 {list varName ?varName ...?} 1]
    set err [tcltest::wrongNumArgs {::struct::list::Lassign} {list varName ?varName ...?} 1]
}
# In 8.6+ assign is the native lassign and it does nothing gracefully,
# per TIP 323, making assign-4.4 not an error anymore.
test assign-4.4 {assign method} {!tcl8.6plus} {
    catch {assign {foo bar}} msg ; set msg
} $err
test assign-4.5 {assign method} {
    list [assign {foo bar} x] $x
} {bar foo}
catch {unset x y z}
#----------------------------------------------------------------------
interp alias {} flatten {} ::struct::list::list flatten
test flatten-1.1 {flatten command} {
    flatten {1 2 3 {4 5} {6 7} {{8 9}} 10}
} {1 2 3 4 5 6 7 {8 9} 10}
test flatten-1.2 {flatten command} {
    flatten -full {1 2 3 {4 5} {6 7} {{8 9}} 10}
} {1 2 3 4 5 6 7 8 9 10}
test flatten-1.3 {flatten command} {
    flatten {a b}
} {a b}
test flatten-1.4 {flatten command} {
    flatten [list "\[a\]" "\[b\]"]
} {{[a]} {[b]}}
test flatten-1.5 {flatten command} {
    flatten [list "'" "\""]
} {' {"}} ; # " help emacs highlighting
test flatten-1.6 {flatten command} {
    flatten [list "{" "}"]
} "\\\{ \\\}"
test flatten-2.1 {flatten errors} {
    list [catch {flatten} msg] $msg
} {1 {wrong#args: should be "::struct::list::Lflatten ?-full? ?--? sequence"}}
test flatten-2.2 {flatten errors} {
    list [catch {flatten -all {a {b c d} {e {f g}}}} msg] $msg
} {1 {Unknown option "-all", should be either -full, or --}}
#----------------------------------------------------------------------
interp alias {} map {} ::struct::list::list map
proc cc {a} {return $a$a}
proc +  {a} {expr {$a + $a}}
proc *  {a} {expr {$a * $a}}
proc projection {n list} {::lindex $list $n}
test map-4.1 {map command} {
    map {a b c d} cc
} {aa bb cc dd}
test map-4.2 {map command} {
    map {1 2 3 4 5} +
} {2 4 6 8 10}
test map-4.3 {map command} {
    map {1 2 3 4 5} *
} {1 4 9 16 25}
test map-4.4 {map command} {
    map {} *
} {}
test map-4.5 {map command} {
    map {{a b c} {1 2 3} {d f g}} {projection 1}
} {b 2 f}
#----------------------------------------------------------------------
interp alias {} mapfor {} ::struct::list::list mapfor
test mapfor-4.1 {mapfor command} {
    mapfor x {a b c d} { set x $x$x }
} {aa bb cc dd}
test mapfor-4.2 {mapfor command} {
    mapfor x {1 2 3 4 5} {expr {$x + $x}}
} {2 4 6 8 10}
test mapfor-4.3 {mapfor command} {
    mapfor x {1 2 3 4 5} {expr {$x * $x}}
} {1 4 9 16 25}
test mapfor-4.4 {mapfor command} {
    mapfor x {} {expr {$x * $x}}
} {}
test mapfor-4.5 {mapfor command} {
    mapfor x {{a b c} {1 2 3} {d f g}} {lindex $x 1}
} {b 2 f}
#----------------------------------------------------------------------
interp alias {} fold {} ::struct::list::list fold
proc cc {a b} {return $a$b}
proc +  {a b} {expr {$a + $b}}
proc *  {a b} {expr {$a * $b}}
test fold-4.1 {fold command} {
    fold {a b c d} {} cc
} {abcd}
test fold-4.2 {fold command} {
    fold {1 2 3 4 5} 0 +
} {15}
test fold-4.3 {fold command} {
    fold {1 2 3 4 5} 1 *
} {120}
test fold-4.4 {fold command} {
    fold {} 1 *
} {1}
#----------------------------------------------------------------------
interp alias {} filter {} ::struct::list::list filter
proc even {i} {expr {($i % 2) == 0}}
test filter-4.1 {filter command} {
    filter {1 2 3 4 5 6 7 8} even
} {2 4 6 8}
test filter-4.2 {filter command} {
    filter {} even
} {}
test filter-4.3 {filter command} {
    filter {3 5 7} even
} {}
test filter-4.4 {filter command} {
    filter {2 4 6} even
} {2 4 6}
#----------------------------------------------------------------------
interp alias {} filterfor {} ::struct::list::list filterfor
test filterfor-4.1 {filterfor command} {
    filterfor i {1 2 3 4 5 6 7 8} {($i % 2) == 0}
} {2 4 6 8}
test filterfor-4.2 {filterfor command} {
    filterfor i {} {($i % 2) == 0}
} {}
test filterfor-4.3 {filterfor command} {
    filterfor i {3 5 7} {($i % 2) == 0}
} {}
test filterfor-4.4 {filterfor command} {
    filterfor i {2 4 6} {($i % 2) == 0}
} {2 4 6}
#----------------------------------------------------------------------
interp alias {} lsplit {} ::struct::list::list split
proc even {i} {expr {($i % 2) == 0}}
test split-4.1 {split command} {
    lsplit {1 2 3 4 5 6 7 8} even
} {{2 4 6 8} {1 3 5 7}}
test split-4.2 {split command} {
    lsplit {} even
} {{} {}}
test split-4.3 {split command} {
    lsplit {3 5 7} even
} {{} {3 5 7}}
test split-4.4 {split command} {
    lsplit {2 4 6} even
} {{2 4 6} {}}
test split-4.5 {split command} {
    list [lsplit {1 2 3 4 5 6 7 8} even pass fail] $pass $fail
} {{4 4} {2 4 6 8} {1 3 5 7}}
test split-4.6 {split command} {
    list [lsplit {} even pass fail] $pass $fail
} {{0 0} {} {}}
test split-4.7 {split command} {
    list [lsplit {3 5 7} even pass fail] $pass $fail
} {{0 3} {} {3 5 7}}
test split-4.8 {split command} {
    list [lsplit {2 4 6} even pass fail] $pass $fail
} {{3 0} {2 4 6} {}}
#----------------------------------------------------------------------
interp alias {} shift {} ::struct::list::list shift
test shift-4.1 {shift command} {
    set v {1 2 3 4 5 6 7 8}
    list [shift v] $v
} {1 {2 3 4 5 6 7 8}}
test shift-4.2 {shift command} {
    set v {1}
    list [shift v] $v
} {1 {}}
test shift-4.3 {shift command} {
    set v {}
    list [shift v] $v
} {{} {}}
#----------------------------------------------------------------------
interp alias {} iota {} ::struct::list::list iota
test iota-4.1 {iota command} {
    iota 0
} {}
test iota-4.2 {iota command} {
    iota 1
} {0}
test iota-4.3 {iota command} {
    iota 11
} {0 1 2 3 4 5 6 7 8 9 10}
#----------------------------------------------------------------------
interp alias {} repeatn {} ::struct::list::list repeatn
test repeatn-4.1 {repeatn command} {
    repeatn 0
} {}
test repeatn-4.2 {repeatn command} {
    repeatn 0 3
} {0 0 0}
test repeatn-4.3 {repeatn command} {
    repeatn 0 3 4
} {{0 0 0} {0 0 0} {0 0 0} {0 0 0}}
test repeatn-4.4 {repeatn command} {
    repeatn 0 {3 4}
} {{0 0 0} {0 0 0} {0 0 0} {0 0 0}}
#----------------------------------------------------------------------
interp alias {} repeat {} ::struct::list::list repeat
if {[package vcompare [package provide Tcl] 8.5] < 0} {
    # 8.4
    set err [tcltest::wrongNumArgs {::struct::list::Lrepeat} {positiveCount value args} 0]
} elseif {![package vsatisfies [package provide Tcl] 8.6]} {
    # 8.5+
    #set err [tcltest::wrongNumArgs {lrepeat} {positiveCount value ?value ...?} 0]
    set err [tcltest::wrongNumArgs {::struct::list::Lrepeat} {positiveCount value ?value ...?} 0]
} else {
    # 8.6+
    set err [tcltest::wrongNumArgs {::struct::list::Lrepeat} {count ?value ...?} 1]
}
test repeat-4.1 {repeat command} {
    catch {repeat} msg
    set msg
} $err
if {[package vcompare [package provide Tcl] 8.5] < 0} {
    # 8.4
    set err [tcltest::wrongNumArgs {::struct::list::Lrepeat} {positiveCount value args} 1]
} elseif {![package vsatisfies [package provide Tcl] 8.6]} {
    # 8.5+
    #set err [tcltest::wrongNumArgs {lrepeat} {positiveCount value ?value ...?} 1]
    set err [tcltest::wrongNumArgs {::struct::list::Lrepeat} {positiveCount value ?value ...?} 1]
} else {
    # 8.6+
    set err [tcltest::wrongNumArgs {::struct::list::Lrepeat} {count ?value ...?} 1]
}
# In 8.6+ repeat is the native lrepeat and it does nothing gracefully,
# per TIP 323, making repeat-4.2 not an error anymore.
test repeat-4.2 {repeat command} {!tcl8.6plus} {
    catch {repeat a} msg
    set msg
} $err
test repeat-4.3 {repeat command} {
    catch {repeat a b} msg
    set msg
} {expected integer but got "a"}
# In 8.6+ repeat is the native lrepeat and it does nothing gracefully,
# per TIP 323, making repeat-4.2 not an error anymore.
test repeat-4.4 {repeat command} {!tcl8.6plus} {
    catch {repeat 0 b} msg
    set msg
} {must have a count of at least 1}
if {![package vsatisfies [package provide Tcl] 8.6]} {
    # before 8.6
    set err {must have a count of at least 1}
} else {
    # 8.6+, native lrepeat changed error message.
    set err {bad count "-1": must be integer >= 0}
}
test repeat-4.5 {repeat command} {
    catch {repeat -1 b} msg
    set msg
} $err
test repeat-4.6 {repeat command} {
    repeat 1 b c
} {b c}
test repeat-4.7 {repeat command} {
    repeat 3 a
} {a a a}
test repeat-4.8 {repeat command} {
    repeat 3 [repeat 3 0]
} {{0 0 0} {0 0 0} {0 0 0}}
test repeat-4.9 {repeat command} {
    repeat 3 a b c
} {a b c a b c a b c}
test repeat-4.10 {repeat command} {
    repeat 3 [repeat 2 a] b c
} {{a a} b c {a a} b c {a a} b c}
#----------------------------------------------------------------------
interp alias {} equal {} ::struct::list::list equal
test equal-4.1 {equal command} {
    equal 0 0
} 1
test equal-4.2 {equal command} {
    equal 0 1
} 0
test equal-4.3 {equal command} {
    equal {0 0 0} {0 0}
} 0
test equal-4.4 {equal command} {
    equal {{0 2 3} 1} {{0 2 3} 1}
} 1
#----------------------------------------------------------------------
interp alias {} delete {} ::struct::list::list delete
test delete-1.0 {delete command} {
    catch {delete} msg
    set msg
} {wrong # args: should be "::struct::list::Ldelete var item"}
test delete-1.1 {delete command} {
    catch {delete x} msg
    set msg
} {wrong # args: should be "::struct::list::Ldelete var item"}
test delete-1.2 {delete command} {
    set l {}
    delete l x
    set l
} {}
test delete-1.3 {delete command} {
    set l {a x b}
    delete l x
    set l
} {a b}
test delete-1.4 {delete command} {
    set l {x a b}
    delete l x
    set l
} {a b}
test delete-1.5 {delete command} {
    set l {a b x}
    delete l x
    set l
} {a b}
test delete-1.6 {delete command} {
    set l {a b}
    delete l x
    set l
} {a b}
unset l
#----------------------------------------------------------------------
interp alias {} dbjoin  {} ::struct::list::list dbJoin
interp alias {} dbjoink {} ::struct::list::list dbJoinKeyed
#----------------------------------------------------------------------
# Input data sets ...
set empty {}
set table_as [list \
	{0 foo} \
	{1 snarf} \
	{2 blue} \
	]
set table_am [list \
	{0 foo} \
	{0 bar} \
	{1 snarf} \
	{1 rim} \
	{2 blue} \
	{2 dog} \
    ]
set table_bs [list \
	{0 bagel} \
	{1 snatz} \
	{3 driver} \
	]
set table_bm [list \
	{0 bagel} \
	{0 loaf} \
	{1 snatz} \
	{1 grid} \
	{3 driver} \
	{3 tcl} \
	]
set table_cs [list \
	{0 smurf} \
	{3 bird} \
	{4 galapagos} \
	]
set table_cm [list \
	{0 smurf} \
	{0 blt} \
	{3 bird} \
	{3 itcl} \
	{4 galapagos} \
	{4 tk} \
	]
#----------------------------------------------------------------------
# Result data sets ...
set nyi __not_yet_written__
set ijss [list \
	[list 0 foo   0 bagel] \
	[list 1 snarf 1 snatz] \
	]
set ijsm [list \
	[list 0 foo   0 bagel] \
	[list 0 foo   0 loaf] \
	[list 1 snarf 1 snatz] \
	[list 1 snarf 1 grid] \
	]
set ijms [list \
	[list 0 foo   0 bagel] \
	[list 0 bar   0 bagel] \
	[list 1 snarf 1 snatz] \
	[list 1 rim   1 snatz] \
	]
set ijmm [list \
	[list 0 foo   0 bagel] \
	[list 0 foo   0 loaf] \
	[list 0 bar   0 bagel] \
	[list 0 bar   0 loaf] \
	[list 1 snarf 1 snatz] \
	[list 1 snarf 1 grid] \
	[list 1 rim   1 snatz] \
	[list 1 rim   1 grid] \
    ]
set ljss [list \
	[list 0 foo   0 bagel] \
	[list 1 snarf 1 snatz] \
	[list 2 blue {} {}] \
	]
set ljsm [list \
	[list 0 foo   0 bagel] \
	[list 0 foo   0 loaf] \
	[list 1 snarf 1 snatz] \
	[list 1 snarf 1 grid] \
	[list 2 blue {} {}] \
	]
set ljms [list \
	[list 0 foo   0 bagel] \
	[list 0 bar   0 bagel] \
	[list 1 snarf 1 snatz] \
	[list 1 rim   1 snatz] \
	[list 2 blue {} {}] \
	[list 2 dog  {} {}] \
	]
set ljmm [list \
	[list 0 foo   0 bagel] \
	[list 0 foo   0 loaf] \
	[list 0 bar   0 bagel] \
	[list 0 bar   0 loaf] \
	[list 1 snarf 1 snatz] \
	[list 1 snarf 1 grid] \
	[list 1 rim   1 snatz] \
	[list 1 rim   1 grid] \
	[list 2 blue {} {}] \
	[list 2 dog  {} {}] \
	]
set rjss [list \
	[list 0 foo   0 bagel] \
	[list 1 snarf 1 snatz] \
	[list {} {}   3 driver] \
	]
set rjsm [list \
	[list 0 foo   0 bagel] \
	[list 0 foo   0 loaf] \
	[list 1 snarf 1 snatz] \
	[list 1 snarf 1 grid] \
	[list {} {}   3 driver] \
	[list {} {}   3 tcl] \
	]
set rjms [list \
	[list 0 foo   0 bagel] \
	[list 0 bar   0 bagel] \
	[list 1 snarf 1 snatz] \
	[list 1 rim   1 snatz] \
	[list {} {}   3 driver] \
	]
set rjmm [list \
	[list 0 foo   0 bagel] \
	[list 0 foo   0 loaf] \
	[list 0 bar   0 bagel] \
	[list 0 bar   0 loaf] \
	[list 1 snarf 1 snatz] \
	[list 1 snarf 1 grid] \
	[list 1 rim   1 snatz] \
	[list 1 rim   1 grid] \
	[list {} {}   3 driver] \
	[list {} {}   3 tcl] \
    ]
set fjss [list \
	[list 0 foo   0 bagel] \
	[list 1 snarf 1 snatz] \
	[list 2 blue {} {}] \
	[list {} {}   3 driver] \
	]
set fjsm [list \
	[list 0 foo   0 bagel] \
	[list 0 foo   0 loaf] \
	[list 1 snarf 1 snatz] \
	[list 1 snarf 1 grid] \
	[list 2 blue {} {}] \
	[list {} {}   3 driver] \
	[list {} {}   3 tcl] \
	]
set fjms [list \
	[list 0 foo   0 bagel] \
	[list 0 bar   0 bagel] \
	[list 1 snarf 1 snatz] \
	[list 1 rim   1 snatz] \
	[list 2 blue {} {}] \
	[list 2 dog  {} {}] \
	[list {} {}   3 driver] \
	]
set fjmm [list \
	[list 0 foo   0 bagel] \
	[list 0 foo   0 loaf] \
	[list 0 bar   0 bagel] \
	[list 0 bar   0 loaf] \
	[list 1 snarf 1 snatz] \
	[list 1 snarf 1 grid] \
	[list 1 rim   1 snatz] \
	[list 1 rim   1 grid] \
	[list 2 blue {} {}] \
	[list 2 dog  {} {}] \
	[list {} {}   3 driver] \
	[list {} {}   3 tcl] \
    ]
set ijmmm {
    {0 bar 0 bagel 0 blt}
    {0 bar 0 bagel 0 smurf}
    {0 bar 0 loaf 0 blt}
    {0 bar 0 loaf 0 smurf}
    {0 foo 0 bagel 0 blt}
    {0 foo 0 bagel 0 smurf}
    {0 foo 0 loaf 0 blt}
    {0 foo 0 loaf 0 smurf}
}
set ljmmm {
    {0 bar 0 bagel 0 blt}
    {0 bar 0 bagel 0 smurf}
    {0 bar 0 loaf 0 blt}
    {0 bar 0 loaf 0 smurf}
    {0 foo 0 bagel 0 blt}
    {0 foo 0 bagel 0 smurf}
    {0 foo 0 loaf 0 blt}
    {0 foo 0 loaf 0 smurf}
    {1 rim 1 grid {} {}}
    {1 rim 1 snatz {} {}}
    {1 snarf 1 grid {} {}}
    {1 snarf 1 snatz {} {}}
    {2 blue {} {} {} {}}
    {2 dog {} {} {} {}}
}
set rjmmm {
    {0 bar 0 bagel 0 blt}
    {0 bar 0 bagel 0 smurf}
    {0 bar 0 loaf 0 blt}
    {0 bar 0 loaf 0 smurf} 
    {0 foo 0 bagel 0 blt}
    {0 foo 0 bagel 0 smurf}
    {0 foo 0 loaf 0 blt}
    {0 foo 0 loaf 0 smurf}
    {{} {} 3 driver 3 bird}
    {{} {} 3 driver 3 itcl}
    {{} {} 3 tcl 3 bird}
    {{} {} 3 tcl 3 itcl}
    {{} {} {} {} 4 galapagos}
    {{} {} {} {} 4 tk}
}
set fjmmm {
    {0 bar 0 bagel 0 blt}
    {0 bar 0 bagel 0 smurf}
    {0 bar 0 loaf 0 blt}
    {0 bar 0 loaf 0 smurf} 
    {0 foo 0 bagel 0 blt}
    {0 foo 0 bagel 0 smurf}
    {0 foo 0 loaf 0 blt}
    {0 foo 0 loaf 0 smurf}
    {1 rim 1 grid {} {}}
    {1 rim 1 snatz {} {}}
    {1 snarf 1 grid {} {}}
    {1 snarf 1 snatz {} {}}
    {2 blue {} {} {} {}}
    {2 dog {} {} {} {}}
    {{} {} 3 driver 3 bird}
    {{} {} 3 driver 3 itcl}
    {{} {} 3 tcl 3 bird}
    {{} {} 3 tcl 3 itcl}
    {{} {} {} {} 4 galapagos}
    {{} {} {} {} 4 tk}
}
#----------------------------------------------------------------------
# Helper, translation to keyed format.
proc keyed {table} {
    # Get the key out of the row, hardwired to column 0
    set res [list]
    foreach row $table {lappend res [list [lindex $row 0] $row]}
    return $res
}
#----------------------------------------------------------------------
# I.	One table joins
set n 0 ; # Counter for test cases
foreach {jtype inout} {
    -inner empty    -inner table_as    -inner table_am
    -left  empty    -left  table_as    -left  table_am
    -right empty    -right table_as    -right table_am
    -full  empty    -full  table_as    -full  table_am
} {
    test dbjoin-1.$n "1-table join $jtype $inout" {
	dbjoin $jtype 0 [set $inout]
    } [set $inout] ; # {}
    test dbjoinKeyed-1.$n "1-table join $jtype $inout" {
	dbjoink $jtype [keyed [set $inout]]
    } [set $inout] ; # {}
    incr n
}
#----------------------------------------------------------------------
# II.	Two table joins
set n 0 ; # Counter for test cases
foreach {jtype left right result} {
    -inner empty    empty    empty
    -inner empty    table_bs empty
    -inner table_as empty    empty
    -inner table_as table_bs ijss
    -inner table_as table_bm ijsm
    -inner table_am table_bs ijms
    -inner table_am table_bm ijmm
    -left  empty    empty    empty
    -left  empty    table_bs empty
    -left  table_as empty    table_as
    -left  table_as table_bs ljss
    -left  table_as table_bm ljsm
    -left  table_am table_bs ljms
    -left  table_am table_bm ljmm
    -right empty    empty    empty
    -right empty    table_bs table_bs
    -right table_as empty    empty
    -right table_as table_bs rjss
    -right table_as table_bm rjsm
    -right table_am table_bs rjms
    -right table_am table_bm rjmm
    -full  empty    empty    empty
    -full  empty    table_bs table_bs
    -full  table_as empty    table_as
    -full  table_as table_bs fjss
    -full  table_as table_bm fjsm
    -full  table_am table_bs fjms
    -full  table_am table_bm fjmm
} {
    test dbjoin-2.$n "2-table join $jtype ($left $right) = $result" {
	lsort [dbjoin $jtype 0 [set $left] 0 [set $right]]
    } [lsort [set $result]]
    test dbjoinKeyed-2.$n "2-table join $jtype ($left $right) = $result" {
	lsort [dbjoink $jtype [keyed [set $left]] [keyed [set $right]]]
    } [lsort [set $result]]
    incr n
}
#----------------------------------------------------------------------
# III.	Three table joins
set n 0 ; # Counter for test cases
foreach {jtype left middle right result} {
    -inner table_am table_bm table_cm ijmmm
    -left  table_am table_bm table_cm ljmmm
    -right table_am table_bm table_cm rjmmm
    -full  table_am table_bm table_cm fjmmm
} {
    test dbjoin-3.$n "3-table join $jtype ($left $middle $right) = $result" {
	lsort [dbjoin $jtype 0 [set $left] 0 [set $middle] 0 [set $right]]
    } [lsort [set $result]]
    test dbjoinKeyed-3.$n "3-table join $jtype ($left $middle $right) = $result" {
	lsort [dbjoink $jtype [keyed [set $left]] [keyed [set $middle]] [keyed [set $right]]]
    } [lsort [set $result]]
    incr n
}
#----------------------------------------------------------------------
interp alias {} swap {} ::struct::list::list swap
foreach {n list i j err res} {
    0  {}           0  0   1 {list index out of range}
    1  {}           3  4   1 {list index out of range}
    2  {a b c d e} -1  0   1 {list index out of range}
    3  {a b c d e}  0 -1   1 {list index out of range}
    4  {a b c d e}  6  0   1 {list index out of range}
    5  {a b c d e}  0  6   1 {list index out of range}
    6  {a b c d e}  0  0   0 {a b c d e}
    7  {a b c d e}  0  1   0 {b a c d e}
    8  {a b c d e}  1  0   0 {b a c d e}
    9  {a b c d e}  0  4   0 {e b c d a}
    10 {a b c d e}  4  0   0 {e b c d a}
    11 {a b c d e}  2  4   0 {a b e d c}
    12 {a b c d e}  4  2   0 {a b e d c}
    13 {a b c d e}  1  3   0 {a d c b e}
    14 {a b c d e}  3  1   0 {a d c b e}
} {
    if {$err} {
	test swap-1.$n {swap command error} {
	    set l $list
	    catch {swap l $i $j} msg
	    set msg
	} $res ; # {}
    } else {
	test swap-1.$n {swap command} {
	    set l $list
	    swap l $i $j
	} $res ; # {}
    }
}
#----------------------------------------------------------------------
interp alias {} firstperm    {} ::struct::list::list firstperm
interp alias {} nextperm     {} ::struct::list::list nextperm
interp alias {} foreachperm  {} ::struct::list::list foreachperm
interp alias {} permutations {} ::struct::list::list permutations
test permutations-0.0 {permutations command, single element list} {
    permutations a
} a
array set ps {
    {Tom Dick Harry Bob} {
	0	{Bob Dick Harry Tom}	{Tom Harry Bob Dick}
	{
	    {Bob Dick Harry Tom}    {Bob Dick Tom Harry}
	    {Bob Harry Dick Tom}    {Bob Harry Tom Dick}
	    {Bob Tom Dick Harry}    {Bob Tom Harry Dick}
	    {Dick Bob Harry Tom}    {Dick Bob Tom Harry}
	    {Dick Harry Bob Tom}    {Dick Harry Tom Bob}
	    {Dick Tom Bob Harry}    {Dick Tom Harry Bob}
	    {Harry Bob Dick Tom}    {Harry Bob Tom Dick}
	    {Harry Dick Bob Tom}    {Harry Dick Tom Bob}
	    {Harry Tom Bob Dick}    {Harry Tom Dick Bob}
	    {Tom Bob Dick Harry}    {Tom Bob Harry Dick}
	    {Tom Dick Bob Harry}    {Tom Dick Harry Bob}
	    {Tom Harry Bob Dick}    {Tom Harry Dick Bob}
	}
    }
    {3 2 1 4} {
	1	{1 2 3 4}	{3 2 4 1}
	{
	    {1 2 3 4} {1 2 4 3} {1 3 2 4} {1 3 4 2}
	    {1 4 2 3} {1 4 3 2} {2 1 3 4} {2 1 4 3}
	    {2 3 1 4} {2 3 4 1} {2 4 1 3} {2 4 3 1}
	    {3 1 2 4} {3 1 4 2} {3 2 1 4} {3 2 4 1}
	    {3 4 1 2} {3 4 2 1} {4 1 2 3} {4 1 3 2}
	    {4 2 1 3} {4 2 3 1} {4 3 1 2} {4 3 2 1}
	}
    }
}
foreach k [array names ps] {
    foreach {n firstp nextp allp} $ps($k) break
    test firstperm-1.$n {firstperm command} {
	firstperm $k
    } $firstp ; # {}
    test nextperm-1.$n {nextperm command} {
	nextperm $k
    } $nextp ; # {}
    # Note: The lrange below is necessary a trick/hack to kill the
    # existing string representation of allp, and get a pure list out
    # of it. Otherwise the string based comparison of test will fail,
    # seeing different string reps of the same list.
    test permutations-1.$n {permutations command} {
	permutations $k
    } [lrange $allp 0 end] ; # {}
    test foreachperm-1.$n {foreachperm command} {
	set res {}
	foreachperm x $k {lappend res $x}
	set res
    } [lrange $allp 0 end] ; # {}
}
#----------------------------------------------------------------------
testsuiteCleanup
 |