-
Notifications
You must be signed in to change notification settings - Fork 4
/
shell
executable file
·1382 lines (1323 loc) · 40.7 KB
/
shell
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
#!./pickle
# vim: syntax=tcl
#
# This file contains unit tests for the Pickle Interpreter. The things tested
# in this file are mainly the commands and sub-commands that perform simple
# functions, and not basic language constructions (and commands like 'if',
# or 'while') as we would have to use those constructs as part of the test
# framework itself. Instead, those things should be considered tested if
# the test framework itself runs.
#
# The file also contains a simple performance test and a fairly usable
# shell.
#
# Defining the function 'tracer' installs this function for tracing,
# which can be turned on or off with 'trace', a quirk of this interpreter
# implementation.
proc tracer {args} {
set args [string tr r "\t\x1b\r\n" " " ${args}]
set args [string tr s " " ${args}]
puts "+[info cmdcount]/[info level]: ${args}"
}
# Execute and monitor a command
proc monitor {args} {
set r [catch "uplevel 1 {trace on; $args; trace off}" v]
trace off
return $v $r
}
# A getopt inspired from <https://wiki.tcl-lang.org/page/getopt>
#
# Usage:
#
# getopt argv -x
# getopt argv -x opt
# getopt argv -x opt default
#
# Returns: 1 on argument found in ${argv}, 0 on argument not found in ${argv}
#
proc getopt {ax opt args} {
upvar 1 ${ax} alist
set var [lindex $args 0]
set def [lindex $args 1]
set pos [lsearch $alist ${opt}*]
if {>= ${pos} 0} {
set to ${pos}
if {ne ${var} ""} {
upvar 1 ${var} v
set v [lindex $alist [incr to]]
}
set alist [lreplace $alist $pos $to]
return 1
}
if {ne ${var} ""} {
upvar 1 ${var} v
set v $def
}
return 0
}
set testing off
set shell on
if {getopt argv -T} { trace on }
while {getopt argv -e line} { set shell off; eval $line }; unset line;
if {getopt argv -t} { set shell off; set testing on; };
if {getopt argv -s} { set shell on }
getopt argv -S seed [clock seconds]
getopt argv -F fail 0
heap fail-after $fail
unset fail
if {getopt argv -h} {
puts "Usage: [lindex $argv 1] -\[tsphT\] \[-S seed\] \[-e string\]"
puts ""
puts "\t-t\tRun the system tests"
puts "\t-s\tRun a shell (default)"
puts "\t-p\tRun a crude performance test"
puts "\t-h\tPrint this help message and quit"
puts "\t-e str\tEvaluate a string"
puts "\t-T\tTurn tracing on"
puts "\t-S #\tSet the seed for the PRNG"
puts "\t-F #\tIntroduce memory fault after # allocations"
puts ""
break
}
if {getopt argv -p} {
proc decr {x} { upvar 1 $x i; set i [- $i 1] }
proc waste1 {x} {
set loop $x;
while {!= 0 $loop} {
decr loop
}
}
proc waste2 {x} {
set loop $x;
while {!= 0 $loop} {
incr loop -1
}
}
proc performance {func} {
puts "testing: $func"
set c [clock clicks]
set t [info cmdcount]
# Takes about a second on my machine;
$func 200000;
puts "cmds: [- [info cmdcount] $t]"
puts "time: [- [clock clicks] $c]"
puts
}
performance waste1
performance waste2
rename decr ""
rename waste1 ""
rename waste2 ""
rename performance ""
break
}
if {getopt argv -} {
puts "Error command line option"
puts "Use '-h' to see a list of valid options"
return "" -1
}
# This Non Cryptographically Secure Pseudo Random Number Generator
# is based of off XORSHIFT 32 (see <https://en.wikipedia.org/wiki/Xorshift>).
# It SHOULD NOT be used for cryptographic purposes. A seed of zero is checked
# for, and changed to a one, as a seed of zero produces only zeros. One
# difference between XORSHIFT 32 and this version is that arithmetic is not
# done modulo 2 to the power of 32.
#
# The "seed" variable is seed to the clock in seconds, which is better than
# nothing but a very poor entropy source. The seed is set during argument
# processing.
proc random {args} {
upvar #0 seed x;
set alen [llength $args]
if {> $alen 1 } { return "Error args" -1 }
set ns [lindex $args 0]
if {> $alen 0 } { set x $ns }
if {== $x 0} { incr x; }
set x [xor $x [lshift $x 13]];
set x [xor $x [rshift $x 17]];
set x [xor $x [lshift $x 5]];
}
# Call 'random' a few times, using a small seed usually means the first
# few numbers are quite small as well.
for {set i 0} {< $i 10} {incr i} { random }
unset i
# TCL supports two version of 'foreach', this only does one of those
# versions: for each element in the list 'l' set the variable named by
# 'v' to that element then execute 'b'.
proc foreach {v l b} {
set ll [llength $l];
set t ""
upvar 1 $v m
for {set i 0} {< $i $ll} {incr i} {
set x [lindex $l $i]
uplevel 1 "set $v {[set x]};";
set r [uplevel 1 catch "{$b}" $v]
set t $m
set m $x
if {or [== $r 1] [== $r 2]} { return $t 0 }
if {!= $r 0} { return $t $r }
}
return $t 0
}
proc match {x y} { string match $x $y }
proc switch {args} {
set f eq
set nc 0
if {getopt args -glob} { set f match }
if {getopt args -nocase} { set nc 1 }
set w [llength $args]
set on [lindex $args 0]
set c [lindex $args [- $w 1]]
set w [llength $c]
set d ""
if {set nc} { set on [string tolower $on] }
for {set i 0} {< $i $w} {incr i} {
set m [lindex $c $i]
if {set nc} { set m [string tolower $m] }
set e [lindex $c [incr i]]
if {$f $on $m} { return [uplevel 1 eval $e] }
if {eq $m default} { set d $e }
}
if {ne $d ""} {
return [uplevel 1 eval $e]
}
return ""
}
# This is a limited version of the 'format' command, this could have been made
# to be a built in C function for the sake of efficiency, and if you find
# yourself using the command a lot then it might be worth doing that.
proc format {fmt args} {
set l [split $fmt ""]
set w [llength $l]
set r ""
for {set i 0; set j 0} {< $i $w} {incr i} {
set o [lindex $l $i]
if {eq $o "%"} {
set s [lindex $l [incr i]]
set v [lindex $args $j]
incr j
if {eq $s %} { set r $r%
} elseif {eq $s s} { set r $r$v
} elseif {eq $s x} { set r $r[string dec2hex $v]
} elseif {eq $s X} { set r $r[string toupper [string dec2hex $v]]
} elseif {eq $s o} { set r $r[string dec2base $v 8]
} elseif {eq $s c} { set r $r[string char $v]
} else { return "Error format $s" -1 }
} else {
set r $r$o
}
}
return $r
}
# ### Shell Handler ### #
proc color {c} {
if {ne [getenv COLOR] on} { return "" }
if {eq $c "reset"} { return "\x1b\[0m" }
set i [lsearch {black red green yellow blue magenta cyan white} $c]
return "\x1b\[3$i;1m"
}
# Making these variables would be more efficient, however they would have
# to be accessed via upvar, functions do not.
proc normal {} "return {[color reset]}"
proc red {} "return {[color red]}"
proc green {} "return {[color green]}"
proc blue {} "return {[color blue]}"
rename color ""
# Unfortunately this does not write to stderr.
proc error {msg args} {
set r -1
set alen [llength $args]
if {> $alen 1} { return "Error args" -1 }
if {> $alen 0} {
set d [lindex $args 0]
if {== 0 [string is integer $d]} { return "Error invalid number: $d" -1 }
set r $d
}
puts $msg;
return "" $r
}
proc lremove {lst args} {
set l [llength $args]
for {set i 0} {< $i $l} {incr i} {
set v [lindex $args $i]
set lst [lreplace $lst $v $v]
}
return $lst
}
proc lprepend {varName args} {
upvar 1 $varName var
lappend var
set var [eval [list linsert $var 0] $args]
}
proc lassign {dats args} {
set ld [llength $dats]
set la [llength $args]
for {set i 0} {< $i $la} {incr i} {
set var [lindex $args $i]
set val ""
if {< $i $la} { set val [lindex $dats $i] }
upvar 1 $var $var
set $var $val
}
return [lrange $dats $i $ld]
}
# Decompiler, of sorts. The name 'see' comes from Forth, like the function
# 'words' which is also from Forth.
proc words {} { lsort [info commands] }
proc see {w} {
if {eq "" [info commands $w]} {
return "'$w' is not defined" -1
}
set type proc
set args [info args $w]
if {eq "" [info procs $w]} {
set type "built-in"
set args [info private $w]
}
set body [info body $w]
return "$type [list $w] {$args} {$body}"
}
proc global {args} {
set l [llength $args]
for {set i 0} {< $i $l} {incr i} {
set x [lindex $args $i]
uplevel 1 upvar #0 $x $x
}
}
proc time {x args} {
set c 1;
if {ne $args ""} { set c $args }
set t [clock clicks]
for {set i 0} {< $i $c} {incr i} { uplevel 1 $x }
return [- [clock clicks] $t] 0
}
proc bye {} { exit }
proc quit {args} { exit }
proc {stack dump} {} {
set l [+ [info level] 0]
for {set i 1} {<= $i $l} {incr i} {
set q [uplevel $i {info locals}]
set m [llength $q]
set r ""
for {set j 0} {< $j $m} {incr j} {
set var [uplevel $i "lindex {$q} $j"]
set val [uplevel $i set $var]
lappend r [list $var $val]
}
puts "[- $i 1]: $r"
}
}
proc io {} {
set e [catch {gets} l]
if {or [eq $e 2] [eq $e -1]} {
if {eq $l "EOF"} { return "EOF" 2 }
return "ERROR" 2
}
return $l
}
set HOME "HOME"
set OS Unix
if {eq [getenv OS] "Windows_NT" } {
set OS Windows
set HOME "HOMEPATH"
}
if {set shell} {
unset shell testing
catch {source "[getenv [uplevel #0 set HOME]]/.picklerc"}
proc prompt {} { return "pickle> " }
set count 0
while { } {
incr count
puts -nonewline [prompt]
set line [string trim [io]]
set retcode [catch {eval $line} result]
set fail [red]
if {== $retcode 0} { set fail [green] }
puts "\[$fail$retcode[normal]\] $result"
unset fail retcode line result
}
puts
break
}
if {eq off $testing} {
break
}
# ### Clean up ### #
#rename tracer ""
rename monitor ""
rename see ""
rename words ""
rename io ""
unset shell testing
# ### Unit Tests ### #
set passed 0
set total 0
# BUG: If the unit test sets the value of 'x' then it can mess
# up printing the error.
proc test {result x} {
set retcode [catch $x r]
upvar #0 total t
incr t
set f "[red]FAIL[normal]: (expected \"$result\") "
if {and [eq $r $result] [eq 0 $retcode]} {
uplevel #0 { set passed [+ $passed 1] }
set f "[green]ok[normal]: "
}
puts "$f{$x} = \"$r\""
}
# Test failure cases
proc fails {x} {
set retcode [catch $x r]
upvar #0 total t
incr t
set f "[red]FAIL[normal]: "
if {== $retcode -1} {
uplevel #0 { set passed [+ $passed 1] }
set f "[green]ok[normal]: "
}
puts [string tr r "\n\r" " " "$f{$x} returns \"$r\""]
}
proc state {x} {
puts "[blue]state[normal]: $x"
eval $x
}
puts "SYSTEM OPTIONS"
puts "Version: [join [info version] .]"
puts "Pointer Size: [info system pointer]"
puts "Number Size: [info system number]"
puts "Limits: "
puts "\trecursion: [info system recursion]"
puts "\tstring: [info system length]"
puts "\tminimum: [info system min]"
puts "\tmaximum: [info system max]"
puts "Features:"
puts "\tstring: [info system string]"
puts "\tmaths: [info system maths]"
puts "\tdebugging: [info system debugging]"
puts "\tstrict: [info system strict]"
puts "Heap Usage:"
puts "\tallocs: [heap allocations]"
puts "\tfrees: [heap frees]"
puts "\trealloc: [heap reallocations]"
puts "\ttotal: [heap total] ([/ [heap total] 1024]KiB)"
puts "\n\[Pickle Tests\]\n"
puts "Date: [clock format [clock seconds] {%Y/%m/%d %H:%M:%S}]\n"
test hello {if {bool 1} { concat "hello" }}
test 1 {bool 4}
test 0 {bool 0}
fails {bool}
fails {$a}
if {== 0 [info system strict]} {
test 0 {bool 0b1}
test 1 {bool 9b1}
test 1 {== 0 abc}
test 1 {!= 0 1abc}
test 1 {not x}
}
fails {if}
fails {if { }}
# NB. Different from TCL, which is an error
test 1 {if { } { return 1 0 } else { return 2 0 }}
test 2 {if {== 0 1} { return 1 0 } else { return 2 0 }}
test 3 {if {== 0 1} { return 1 0 } elseif {== 1 1} { return 3 0 } else { return 2 0 }}
test 3 {if {== 0 1} { return 1 0 } elseif {== 1 1} { return 3 0 }}
# Quirk - the result of the last condition is returned
test "0" {if {== 0 1} { return 1 0 } elseif {== 1 2} { return 3 0 }}
fails {if {== 0 0} { puts a } not-else { puts b } else { puts c }}
fails {if {== 0 0} { puts a } else}
fails {if {== 0 0} { puts a } not-else { puts b }}
fails {if {== 0 0} { puts a } not-elseif { } { puts b }}
fails {set}
fails {while}
fails {while {== 0 x} { puts "not run" }}
state {proc recurse {} { recurse }}
fails {recurse}
state {rename recurse ""}
fails {[}
fails {]}
test 1 {== 2 2}
test 1 {!= 0 2}
test 1 {== -0 0}
test 0 {== -1 1}
test 4 {+ 2 2}
test -4 {+ 2 -6}
fails {+ 2 a}
fails {+ 2}
test -4 {- 2 6}
test 16 {* -2 -8}
test 720 {* 1 2 3 4 5 6}
test 1 "< 3 4"
test 0 {< 5 -5}
test -25 {* 5 -5}
test 60 {* 3 4 5}
test -60 {* -3 4 5}
test 60 {* -3 4 -5}
test 1 {< 6 9}
test 1 {< 6 9 10 7}
test 0 {< 6 9 10 6}
test 0 {> 6 9}
test 0 {> -6 9}
test 1 {>= -6 -6}
test 1 {>= 6 -6}
test 0 {>= -6 6}
test 4 {lshift 1 2}
test 5 {rshift 10 1}
test 8 {lshift 1 1 1 1}
test 16 {lshift 2 1 1 1}
test 9 {min 90 9}
test -9 {min 90 -9}
test -4 {max -5 -4}
test 4 {min 90 8 4 6 7 89}
test 4 {abs 4}
test 4 {abs -4}
test -1 {+ [invert 1] 1}
test 170 {and 255 [invert 85]}
test 170 {and 255 [~ 85]}
test 3 {or 1 2}
test 2 {or 2 2}
test 2 {or 2 2 2}
test 255 {or 85 170}
test 3 {| 1 2}
test 2 {| 2 2}
test 2 {| 2 2 2}
test 255 {| 85 170}
test 3 {xor 1 2}
test 0 {xor 2 2}
test 2 {xor 2 2 2}
test 255 {xor 85 170}
test 3 {^ 1 2}
test 0 {^ 2 2}
test 2 {^ 2 2 2}
test 1 {^ 1 1 1}
test 255 {^ 85 170}
test 0 {and 85 170}
test 1 {and 1 1 1}
test 0 {and 1 0 1 1}
test 0 {and 0 0 0}
test 0 {& 85 170}
test 1 {& 1 1 1}
test 0 {& 1 0 1 1}
test 0 {& 0 0 0}
test 0 {&& 0 0}
test 1 {&& 1 1}
test 1 {&& 1 2}
test 1 {&& 1 2 3}
test 0 {&& 1 2 0 3}
test 0 {|| 0 0}
test 0 {|| 0 0 0}
test 1 {|| 0 9 0}
test 1 {|| 1 9}
test 0 {not 3}
test 1 {not 0}
test 0 {! 3}
test 1 {! 0}
test 3 {/ 12 4}
test 2 {/ 20 5 2}
test 10 {+ 4 1 3 2}
test -2 {- 2 3 1}
fails {/ 1 0}
fails {/ 4 4 0}
fails {/ 0 0}
test 4 {mod 4 12}
fails {mod 4 0}
test 11 {mod 11 12}
test 0 {mod 12 12}
test 1 {mod 13 12}
test -3 {negate 3}
test 3 {negate -3}
test 8 {pow 2 3}
test 9 {pow 3 2}
test 81 {pow 3 2 2}
test 1 {pow 9 0}
test -8 {pow -2 3}
test 4 {pow -2 2}
test -2 {pow -2 1}
test 1 {pow -2 0}
test 16 {pow 2 2 2}
test 3 {log 8 2}
# Integer logarithm!
test 3 {log 9 2}
test 4 {log 16 2}
test 2 {log 100 10}
test 2 {log 16 2 2}
test 2 {log 81 3 2}
fails {log}
fails {log 10}
fails {log 10 0}
fails {log 0 10}
test "ABC" {set z A; set z ${z}BC }
test "ABC" {set z B; set z A${z}C }
test "ABB" {set z B; set z A${z}${z} }
test 120 {set cnt 5; set acc 1; while {> $cnt 1} { set acc [* $acc $cnt]; incr cnt -1 }; set acc; };
test 1 {eq a a}
fails {eq}
fails {eq a}
test 0 {eq a b}
test 1 {ne abc ""}
test 1 {eq "" ""}
test {a b} {concat a b}
test {a b c d e f {g h}} {concat a b "c d e " " f {g h}"}
test a,b,c {join {a b c} ,}
test "a,b c,d" {join {a {b c} d} ,}
test a {join a ,}
test "" {join "" ,}
fails {join}
fails {join {}}
test a##b##c##d {join {a b c d} ##}
test abcxd {join {a b cx d} ""}
test "e;f;g;h" {join {e f g h} ";"}
state {proc square {x} { * $x $x }}
test 16 {square 4}
fails {square a}
state {rename square sq}
state {proc fib {x} { if {<= $x 1} { return 1; } else { + [fib [- $x 1]] [fib [- $x 2]]; } }}
test 89 {fib 10}
test fib {info commands fib}
state {rename fib ""}
test "" {info commands fib}
test 16 {sq 4}
state {rename sq ""}
test 1 {info complete ""}
test 0 {info complete "\""}
test 1 {info complete "\"\""}
test 1 {info complete "{}"}
fails {info}
fails {set e {eval $e}; eval $e}
fails {set l [string repeat * [* 2 [info system recursion]]]; string match $l $l }
fails {string}
test 3 {string length 123}
test 4 {string length 1234}
test 4 {string length abcd}
test 0 {string length ""}
test 0 {string length {}}
fails {string length}
test 1 {string match "" ""}
test 1 {string match "*" ""}
test 1 {string match "*" "a"}
test 1 {string match "*" "aaa"}
test 0 {string match {\*} ""}
test 0 {string match {\*} "a"}
test 0 {string match {\*} "aaa"}
test 1 {string match "?" "?"}
test 1 {string match "?" "a"}
test 1 {string match {\*} *}
test 1 {string match {\?} ?}
test 1 {string match "???" "abc"}
test 1 {string match "???" "abc"}
test 0 {string match "???" "abcd"}
test 0 {string match "abc" "abcd"}
test 1 {string match "abc" "abc"}
test 1 {string match "abc*" "abc"}
test 0 {string match "abc*d" "abc"}
test 1 {string match "abc*d" "abcd"}
test 0 {string match "abc*d" "aBcd"}
test 1 {string match "abc*d" "abcXXXXd"}
test 1 {string match "*" "ahoy!"}
test 1 {string match "*abc*c?d" "xxxxxabcxxxxc3d"}
test 1 {string match "*abc*c?d" "xxxxxabcxxxxc?d"}
fails {string match}
fails {string match ""}
test "" {string reverse ""}
test "a" {string reverse "a"}
test "ba" {string reverse "ab"}
test "cba" {string reverse "abc"}
test "dcba" {string reverse "abcd"}
test "ba0" {string reverse "\x30ab"}
fails {string reverse}
test "" {string trimleft ""}
test "" {string trimleft " "}
test "x " {string trimleft " x "}
test "x b" {string trimleft " x b"}
test "123ABC." {string toupper "123aBc."}
test "123abc." {string tolower "123aBc."}
fails {string tolower}
test 0 {string equal a b}
test 1 {string equal a a}
test 1 {< 0 [string compare a A]}
test 1 {> 0 [string compare bA ba]}
test 0 {string compare-no-case a a}
test 0 {string compare-no-case a A}
test 0 {string compare-no-case B b}
test 0 {string compare-no-case abc ABC}
test 1 {> 0 [string compare-no-case a B]}
test 1 {> 0 [string compare-no-case A b]}
test h {string index hello 0}
test e {string index hello 1}
test l {string index hello 3}
test o {string index hello 4}
test o {string index hello 9}
test o {string index hello -1}
test l {string index hello -2}
test h {string index hello -9}
fails {string index hello x}
fails {string index hello}
fails {string index}
test "" {string repeat abc 0}
test "abc" {string repeat abc 1}
test "abcabc" {string repeat abc 2}
test "aaa" {string repeat a 3}
fails {string repeat}
fails {string repeat a}
fails {string repeat a -1}
fails {string repeat a a}
fails {string is}
test 1 {string is digit ""}
test 1 {string is digit 1234567890}
test 0 {string is digit 123a}
test 1 {string is alnum ""}
test 1 {string is alnum 123a}
test 0 {string is alnum 123.}
test 0 {string is alpha 123.}
test 0 {string is alpha 123a.}
test 1 {string is alpha abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ}
test 1 {string is ascii ""}
test 0 {string is ascii "a\x80a"}
test 0 {string is ascii "\x82"}
test 1 {string is ascii "\x01"}
test 1 {string is ascii "\x1 "}
test 1 {string is ascii "\x1"}
test 1 {string is ascii "aa"}
test 1 {string is ascii "\x7f"}
test 1 {string is xdigit ""}
test 1 {string is xdigit "1234567890ABCDEFabcdef"}
test 0 {string is xdigit "1234567890ABCDEFabcdefQ"}
test 0 {string is lower "aB"}
test 1 {string is lower "abcdefghijklmnopqrstuvwxyz"}
test 0 {string is lower "abcdefghijklmnopqrstuvwxyz "}
test 0 {string is lower "aa123"}
test 1 {string is lower ""}
test 0 {string is upper "aB"}
test 1 {string is upper "ABCDEFGHIJKLMNOPQRSTUVWXYZ"}
test 0 {string is upper "ABCDEFGHIJKLMNOPQRSTUVWXYZ "}
test 0 {string is upper "AA123"}
test 1 {string is upper ""}
test 1 {string is wordchar ""}
test 1 {string is wordchar "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345789abcdefghijklmnopqrstuvwxyz_"}
test 0 {string is wordchar " ABCDEFGHIJKLMNOPQRSTUVWXYZ012345789abcdefghijklmnopqrstuvwxyz_"}
test 0 {string is wordchar " "}
test 0 {string is wordchar ":"}
test 0 {string is integer ""}
test 0 {string is integer " 0"}
test 0 {string is integer "123a"}
test 0 {string is integer " 123"}
test 0 {string is integer "0x123"}
test 0 {string is integer "++0"}
test 0 {string is integer "-"}
test 0 {string is integer "+"}
test 1 {string is integer "0"}
test 1 {string is integer "12345"}
test 1 {string is integer "67890"}
test 1 {string is integer "-123"}
test 1 {string is integer "+123"}
test 1 {string is integer "0123"}
test 1 {string is integer "+0"}
test 1 {string is integer "-0"}
test 1 {string is boolean false}
test 1 {string is boolean no}
test 1 {string is boolean 0}
test 1 {string is boolean OFF}
test 1 {string is boolean true}
test 1 {string is boolean yes}
test 1 {string is boolean 1}
test 1 {string is boolean on}
test 0 {string is boolean XXX}
test 0 {string is boolean ""}
test 1 {string is true true}
test 1 {string is true yEs}
test 1 {string is true 1}
test 1 {string is true oN}
test 0 {string is true off}
test 0 {string is false oN}
test 3 {string first a bbbaca}
test 3 {string first a bbbaca 3}
test 5 {string first a bbbaca 4}
test -1 {string first d bbbaca}
test 0 {string ordinal ""}
test 0 {string ordinal "\x00"}
test 1 {string ordinal "\x01"}
test 2 {string ordinal "\x2"}
test 48 {string ordinal "0"}
test 49 {string ordinal "1"}
test 49 {string ordinal "1a"}
test 49 {string ordinal "1abc"}
fails {string ordinal }
test ff {string dec2hex 255}
test 1000 {string dec2hex 4096}
test 4096 {string hex2dec 1000}
if {== [info system number] 16} { test -1 {string hex2dec FffF} } else { test 65535 {string hex2dec FffF} }
test 101 {string dec2base 5 2}
fails {string dec2base A 2}
test 5 {string base2dec 101 2}
fails {string base2dec 0 0}
fails {string base2dec 0 1}
fails {string base2dec 0 37}
fails {string base2dec 2 2}
fails {string base2dec 2}
test 0 {string char 48}
test 1 {string char 49}
test a {string char 97}
test {} {string tr d abc ""}
test {ddeeff} {string tr d abc aabbccddeeff}
test {aabbcc} {string tr dc abc aabbccddeeff}
test {ddeeffddeeff} {string tr r abc def aabbccddeeff}
test {defdefgg} {string tr s abc def aabbccddeeffgg}
test {abcddeeffgg} {string tr s abc aabbccddeeffgg}
fails {string tr}
fails {string tr d}
test {ad} {string replace "abcd" 1 2 ""}
test {acd} {string replace "abcd" 1 1 ""}
test {abcd} {string replace "abcd" 2 1 ""}
test {a123d} {string replace "abcd" 1 2 "123"}
test {a1d} {string replace "abcd" 1 2 "1"}
test {123} {string replace "" 0 0 "123"}
test {$tv1 world} {set tv1 hello; return {$tv1 world} 0}
test {hello world} {set tv1 hello; subst {$tv1 world}}
test b {lindex {a b c} 1}
test a {lindex {a b c d e} 0}
test a {lindex " a " 0}
test a {lindex " \t \n a \t \n b " 0}
test b {lindex " \t \n a \t \n b " 1}
test {} {lindex " \t \n a \t \n b " 2}
test {hello world} {lindex " \t \n {hello world} \t \n " 0}
test {} {lindex " \t \n {hello world} \t \n " 1}
test b {lindex {{a} {b} {c}} 1}
test b {lindex {a b c} 1}
test {a b c} {lindex {a b c}}
test {} {lindex {}}
test {} {lindex {a b "c"} 3}
test {b"c"} {lindex {a b"c"} 1}
test {} {lindex {a b"c"} 2}
test a {lindex {a "b"c} 0}
test b {lindex {a "b"c} 1}
test c {lindex {a "b"c} 2}
test {a"b"c} {lindex {a"b"c} 0}
#test 5 {lindex {0 1 2 3 4 5} end}
#test 0 {lindex 0 end}
#test {} {lindex {} end}
test {} {lindex {} -42}
test {} {lindex {a b c} -1}
test {} {lindex {a b c} -2}
test {} {lindex {a b c} -3}
test {a c b} {linsert {a b} 1 c}
test {a c d b} {linsert {a b} 1 c d}
#test {a b c} {linsert {a b} end c}
#test {a b c d e} {linsert {a b} end c d e}
test {a b c d e} {linsert {a b} 3 c d e}
test {c d e a b} {linsert {a b} 0 c d e}
test {} list
test {a {b c} d} {list a "b c" d}
test {{} a} {list "" a}
test {a {} b} {list a {} b}
test {a {}} {list a {}}
test "a {\t}" {list a \t}
test "a {\n}" {list a \n}
test {a {\n}} {list a {\n}}
test {a {\r}} {list a {\r}}
test {a {\t}} {list a {\t}}
test {{$a}} {list {$a}}
test {{[hi]}} {list {[hi]}}
test {{[hi]}} {list {[hi]}}
test 0 {llength ""}
test 0 {llength " "}
test 1 {llength "a"}
test 2 {llength "a b"}
test 2 {llength "a b "}
test 2 {llength "a { b }"}
test 3 {llength "a { b } c"}
test 3 {llength "a { { b } } c"}
test 3 {llength {a { { \" b \" } } c}}
test 3 {llength {a " b " c}}
fails {llength}
test 0 {llength {}}
test 0 {llength { }}
test 0 {llength { }}
test 1 {llength { 1}}
test 1 {llength { 1 }}
test 2 {llength { 1 2}}
test 3 {llength { 1 2 3 }}
test 5 {llength {a b c d e}}
test 0 {llength " \t \n "}
test 1 {llength " \t \n a "}
test a {lindex "a b c" 0}
test b {lindex "a b c" 1}
test c {lindex "a b c" 2}
test c {lindex "a b c" 2}
# Note this is different to TCL, where it is an error
test 3 {llength "a { b }c"}
test { b } {lindex "a { b } c" 1}
test a {lindex "a { b } c" 0}
test c {lindex "a { b } c" 2}
test "" {lindex "a { b } c" 3}
test {$x} {subst -novariables {$x}}
test {$x 3} {subst -novariables {$x [+ 2 1]}}
fails {subst}
test {a 3} {subst {a [+ 2 1]}}
test {a [+ 2 1]} {subst -nocommands {a [+ 2 1]}}
test {a hello c} {set z "a b c"; lset z 1 hello}
test {a hello} {set z "a b"; lset z 1 hello}
test {a c} {set z "a b c"; lset z 1 ""}
test {a} {set z "a b"; lset z 1 ""}
test {} {set z "a"; lset z 0 ""}
test {b} {set z "a b"; lset z 0 ""}
test {a b c} {split a.b.c .}
test {a { } b { } c} {split "a b c" ""}
test {a b} {split "a.b" "."}
test {{a b} c} {split "a b.c" "."}
test {abc d} {split "abc.d" "."}
test {a {}} {split "a." "."}
test {} {split "" ""}
test {{} {}} {split "." "."}
test {{} {} {}} {split ".." "."}
fails {split}
test {a b {c d e } { f {g h}}} {list a b "c d e " " f {g h}"}
test {a b c xyz} {linsert {a b c} 99 xyz}
test {a b c {x y z}} {linsert {a b c} 99 {x y z}}
test {{x y z}} {linsert "" 3 {x y z}}
test {a b c xyz} {linsert {a b c} 3 xyz}
test {xyz a b c} {linsert {a b c} 0 xyz}
test {a {x yz} b c} {linsert {a b c} 1 {x yz}}
test {} {lrepeat 0 a}
test {a} {lrepeat 1 a}
test {a a} {lrepeat 2 a}
test {a a a} {lrepeat 3 a}
test {ab ab ab} {lrepeat 3 ab}
test {{a b} {a b} {a b}} {lrepeat 3 {a b}}
test {a,b,c} {join {a b c} ,}
test {abc} {join [split "abc" ""] ""}
test {a,b,c} {conjoin , a b c}
test {a} {conjoin , a}
test {a,b} {conjoin , a b}
test {} {conjoin , ""}
fails {conjoin}
test {} {lreverse {}}
test {a} {lreverse {a}}
test {b a} {lreverse {a b}}
test {c b a} {lreverse {a b c}}
test {{ c} a} {lreverse {a { c}}}
test {{y y} {x x}} {lreverse {{x x} {y y}}}
fails {lreverse}
fails {proc}
fails {proc x}
fails {proc x n}
state {proc def {x} {}}
test {} {proc def {x} {}}
state {rename def ""}
state {proc v1 {args} { return $args 0 }}
test {a b {c d}} {v1 a b {c d}}
test {a} {v1 a}
test {} {v1}
state {proc v2 {x y args} { set r "$x $y $args"; return $r 0 }}
test {a b {c d} {e f}} {v2 a b {c d} {e f}}
test {a b {c d}} {v2 a b {c d}}
fails {v2 a}
test {a b } {v2 a b}
fails {v2}
state {rename v1 ""}
state {rename v2 ""}
fails {rename}
fails {rename XXX}
fails {rename XXX YYY}
test {} {lsort {}}
test {a} {lsort {a}}
test {a b} {lsort {a b}}
test {a b} {lsort {b a}}
test {a b c} {lsort {b a c}}
test {c b a} {lsort -decreasing {b a c}}
test {a b c} {lsort {a b c}}
test {1 2 3} {lsort -integer {1 2 3}}
test {3 2 1} {lsort -integer -decreasing {1 2 3}}
test {a {b c} d} {lsort {d a {b c}}}
fails {lsort}
fails {lsort -integer {1 2 a}}
# Test upvar links
state {proc n2 {} { upvar 1 h u; set u [+ $u 1]; }}
state {proc n1 {} { upvar 1 u h; set h [+ $h 1]; n2 }}
test {done} {set u 5; puts "u = $u"; puts "[n1]"; test 1 "== $u 7"; unset u; return "done" 0; }
state {rename n1 ""}
state {rename n2 ""}
fails {lreplace}
fails {lreplace ""}
fails {lreplace "" 1}
test {a foo c d e} {lreplace {a b c d e} 1 1 foo}
test {a {x x} c d e} {lreplace {a b c d e} 1 1 {x x}}
test {a {} c d e} {lreplace {a b c d e} 1 1 ""}
test {a c d e} {lreplace {a b c d e} 1 1}
test {a b c d e f} {lreplace {a b c d e} 9 1 f}
test {a b c d e {f g h}} {lreplace {a b c d e} 9 1 {f g h}}
test {a b c d e {f g h} i} {lreplace {a b c d e} 9 1 {f g h} i}
test {a three more elements d e} {lreplace {a b c d e} 1 2 three more elements}
test {x y z a b c} {lreplace {a b c} -1 -2 x y z}
test {a b x y z c} {lreplace {a b c} 2 1 x y z}
test {a b {x y} z c} {lreplace {a b c} 2 1 {x y} z}