summaryrefslogtreecommitdiffstats
path: root/tests/assemble.test
blob: d64f0046011a250da2802681ca731f6c85587e34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
# Commands covered: assemble

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest 2.2
    namespace import -force ::tcltest::*
}
set assemble tcl::unsupported::assemble

test assemble-1.1 {wrong # args, direct eval} {
    -body {
	eval [list ::tcl::unsupported::assemble]
    }
    -returnCodes error
    -result {wrong # args*}
    -match glob
}

test assemble-1.2 {wrong # args, direct eval} {
    -body {
	eval [list ::tcl::unsupported::assemble too many]
    }
    -returnCodes error
    -result {wrong # args*}
    -match glob
}

test assemble-1.3 {error reporting, direct eval} {
    -body {
	list [catch {
	    eval [list ::tcl::unsupported::assemble {
		# bad opcode
		rubbish
	    }]
	} result] $result $errorInfo
    }
    -match glob
    -result {1 {bad instruction "rubbish":*} {bad instruction "rubbish":*
    while executing
"rubbish"
    ("::tcl::unsupported::assemble" body, line 3)*}}
    -cleanup {unset result}
}

test assemble-1.4 {simple direct eval} {
    -body {
	eval [list ::tcl::unsupported::assemble {push {this is a test}}]
    }
    -result {this is a test}
}

test assemble-2.1 {bytecode reuse, direct eval} {
    -body {
	set x {push "this is a test"}
	list [eval [list ::tcl::unsupported::assemble $x]] \
	    [eval [list ::tcl::unsupported::assemble $x]]
    }
    -result {{this is a test} {this is a test}}
}

test assemble-2.2 {bytecode discard, direct eval} {
    -body {
	set x {load value}
	proc p1 {x} {
	    set value value1
	    tcl::unsupported::assemble $x
	}
	proc p2 {x} {
	    set a b
	    set value value2
	    tcl::unsupported::assemble $x
	}
	list [p1 $x] [p2 $x]
    }
    -result {value1 value2}
    -cleanup {
	unset x
	rename p1 {}
	rename p2 {}
    }
}

test assemble-2.3 {null script, direct eval} {
    -body {
	set x {}
	tcl::unsupported::assemble $x
    }
    -result {}
    -cleanup {unset x}
}

test assemble-3.1 {wrong # args, compiled path} {
    -body {
	proc x {} {
	    tcl::unsupported::assemble
	}
	x
    }
    -returnCodes error
    -match glob
    -result {wrong # args:*}
}

test assemble-3.2 {wrong # args, compiled path} {
    -body {
	proc x {} {
	    tcl::unsupported::assemble too many
	}
	x
    }
    -returnCodes error
    -match glob
    -result {wrong # args:*}
}

if 0 {

test assemble-1.3 {empty body} {
    -body { $assemble "" }
    -result {}
}

test assemble-1.4 {empty body} {
    -body { 
	proc x y {
	    tcl::unsupported::assemble ""
	}
	x 1}
    -result {}
}

test assemble-1.4.1 {unknown assembly instruction} {
    -body {
	list [catch {
	    tcl::unsupported::assemble {
		push 2
		push 2
		rubbish
	    }
	} result] $result $errorCode $errorInfo
    }
    -match glob
    -result {1 {bad instruction "rubbish":*} {bad instruction "rubbish":*
    while executing
"rubbish"
    ("tcl::unsupported::assemble" body, line 4)*}}
}

test assemble-1.5 {Testing push and add} {
    -body { tcl::unsupported::assemble {
	push 2
	push 2
	add
    } 
    }
    -result {4}
}

test assemble-1.6 {Testing push, dup, add} {
    -body { 
	tcl::unsupported::assemble {
	    push 2
	    dup
	    add
	}
    }
    -result {4}
}

test assemble-1.6a {wrong # args} {
    -body {
	catch {
	    tcl::unsupported::assemble {
		push 2 2
		dup
		add
	    }
	}
	set ::errorInfo
    }
    -match glob
    -result {wrong # args: should be "push value"
    while executing
"push 2 2"
    ("tcl::unsupported::assemble" body, line 2)*
    }
}

test assemble-1.7 {longer sequence} {
    -body {
	tcl::unsupported::assemble {
	    push 3
	    dup
	    mult
	    push 4
	    dup
	    mult
	    expon
	}
    }
    -result {1853020188851841}
}

test assemble-1.8 {unbalanced stack} {
    -body {
	list \
	    [catch {
		tcl::unsupported::assemble {
		    push 3
		    dup 
		    mult 
		    push 4
		    dup 
		    mult 
		    pop 
		    expon
		}
	    } result] $result $::errorInfo
    }
    -result {1 {stack underflow} {stack underflow
    in assembly code between lines 1 and end of assembly code*}}
    -match glob
   -returnCodes ok
}

test assemble-1.8a {unbalanced stack} {*}{
    -body {
	list \
	    [catch {
		tcl::unsupported::assemble {
		    label a
		    push {}
		    label b
		    pop
		    label c
		    pop
		    label d
		    push {}
		}
	    } result] $result $::errorInfo
    }
    -result {1 {stack underflow} {stack underflow
    in assembly code between lines 6 and 8*}}
    -match glob
   -returnCodes ok
}

# Tests for load and store

test assemble-1.9 { Testing load within a proc } {
    -body { 
	proc x y { 
	    tcl::unsupported::assemble {
		load y
		dup 
		mult
	    }
	}
	x 10
    }
    -result {100}
}

test assemble-1.10 {store and load again} {
    -body {  
	proc x arg { 
	    tcl::unsupported::assemble {
		push aString
		store arg
		pop
		load arg
	    } 
	    } 
	x "not_aString"	
    }
    -result {aString}
}

test assemble-1.11 {Testing storeArray and loadArray} {
    -body { 
	set anArray(1,2) "not_aString" 
	proc x arg { 
	    upvar $arg anArray
	    tcl::unsupported::assemble {
		push 1,2
		push aString
		storeArray anArray
		pop
		push 1,2
		loadArray anArray
	    } 
	}
	x anArray 
    }
    -cleanup {unset anArray}
    -result {aString}
}

test assemble-1.12 {Testing loadStk with a variable} {
    -body { 
	set vara 10
	tcl::unsupported::assemble {
	    push vara
	    loadStk
	    dup
	    mult
	    push 4
	    dup 
	    mult 
	    add
	}
    }
    -cleanup {unset vara}
    -result {116}
}

test assemble-1.13 {Testing loadStk with an array variable} {
    -body { 
	set vararr(1,2) 10
	tcl::unsupported::assemble {
	    push vararr(1,2)
	    loadStk
	    dup
	    mult
	    push 4
	    dup
	    mult
	    add
	}
    }
    -cleanup {unset vararr}
    -result {116}
}

test assemble-1.14 {Testing loadStk and storeStk} {
    -body {
	set aVar 5
	tcl::unsupported::assemble {
	    push aVar
	    push aString
	    storeStk
	    pop
	    push aVar
	    loadStk 
	}
    }
    -cleanup {unset aVar}
    -result {aString}
}

test assemble-1.15 {Testing loadArrayStk and storeArrayStk} {
    -body { 
	set aVar(1,2) 5
	tcl::unsupported::assemble {
	    push aVar
	    push 1,2
	    push aString
	    storeArrayStk
	    pop
	    push aVar
	    push 1,2
	    loadArrayStk 
	}
    }
    -cleanup {unset aVar}
    -result {aString}
}

# Tests for incr instructions 

test assemble-1.16 { Testing incr } {
    -body {
	proc x arg {
	    set i 5 
	    tcl::unsupported::assemble {
		push 5
		incr i
	    }
	}
	x 1
    }
    -result {10}
}

test assemble-1.17 { Testing incrImm } {
    -body { 
	proc x arg {
	    set i 5 
	    tcl::unsupported::assemble {
		incrImm i 5
	    }
	} 
	x 1
    }
    -result {10}
}

test assemble-1.18 { Testing incrStk } {
    -body {
	proc x arg {
	    set i 5 
	    tcl::unsupported::assemble {
		push i
		push 5
		incrStk
	    }
	}
	x 1
    }
    -result {10}
}

test assemble-1.19 { Testing incrStkImm } {
    -body {
	proc x arg {
	    set i 5 
	    tcl::unsupported::assemble {
		push i
		incrStkImm 5
	    }
	} 
	x 1
    }
    -result {10}
}

test assemble-1.20 { Testing incrStkImm } {
    -body {
	proc x arg {
	    set i 5 
	    tcl::unsupported::assemble {
		push i
		incrStkImm 5
	    }
	} 
	x 1
    }
    -result {10}
}

test assemble-1.21 { Testing incrArray } {
    -body {
	proc x arg {
	    set i(1,2) 5 
	    tcl::unsupported::assemble {
		push 1,2
		push 5
		incrArray i
	    }
	} 
	x 1
    }
    -result {10}
}

test assemble-1.22 { Testing incrArrayImm } {
    -body { 
	proc x arg {
	    set i(1,2) 5 
	    tcl::unsupported::assemble {
		push 1,2
		incrArrayImm i 5
	    }
	} 
	x 1
    }
    -result {10}
}

test assemble-1.23 { Testing incrArrayStk } {
    -body {
	proc x arg {
	    set i(1,2) 5 
	    tcl::unsupported::assemble {
		push i
		push 1,2
		push 5
		incrArrayStk
	    }
	} 
	x 1
    }
    -result {10}
}

test assemble-1.24 { Testing incrArrayStkImm } {
    -body {
	proc x arg {
	    set i(1,2) 5 
	    tcl::unsupported::assemble {
		push i
		push 1,2
		incrArrayStkImm 5
	    }
	} 
	x 1
    }
    -result {10}
}

# Tests for STRs -NOT HERE YET
# Tests for LIST BYTECODES -NOT HERE YET

# Tests for label and jump  {push NotEqual}

test assemble-1.25 { Testing label and jumpTrue  } {
    -body { 
	proc x {arg1 arg2} {
	    tcl::unsupported::assemble {
		label a
		load arg2 
		push 2
		mult
		store arg2
		pop
		load arg1
		push 1
		sub
		store arg1
		push 0
		neq
		jumpTrue a
		load arg2
	    }
	} 
	x 4 2
    }
    -result {32}
}	

test assemble-1.26 { Testing label and jumpFalse } {
    -body { 
	proc x {arg1 arg2} {
	    tcl::unsupported::assemble {
		label a
		load arg2
		push 2
		mult
		store arg2
		pop
		load arg1
		push 1
		sub
		store arg1 
		push 0
		eq
		jumpFalse a
		load arg2
	    }
	} 
	x 4 2
    }
    -result {32}
}

test assemble-1.27 {forward jumps} {
    -body { 
	proc x {arg1 arg2 arg3} {
	    tcl::unsupported::assemble {
		jump a
		push aString
		store arg1
		pop
		label a
		jump b
		push aString
		store arg2
		label b
		jump c
		push aString
		store arg3
		label c
		load arg1
	    }
	}
	x 1 2 3
    }
    -result {1}
}

test assemble-1.28 {forward jumps} {
    -body { 
	proc x {arg1 arg2 arg3} {
	    tcl::unsupported::assemble {
		jump a
		push aString 
		store arg1
		pop
		label a
		jump b
		push aString
		store arg2
		label b
		jump c 
		push aString
		store arg3
		label c
		load arg2
	    }
	}
	x 1 2 3
    }
    -result {2}
}

test assemble-1.29 {forward jumps} {
    -body { 
	proc x {arg1 arg2 arg3} {
	    tcl::unsupported::assemble {
		jump a
		push aString 
		store arg1
		pop
		label a
		jump b
		push aString
		store arg2
		label b
		jump c
		push aString
		store arg3
		label c
		load arg3
	    }
	}
	x 1 2 3
    }
    -result {3}
}

test assemble-1.30 {Inconsistent stack usage} {*}{
    -body {
	proc x {y} {
	    tcl::unsupported::assemble {
		load y
		jumpFalse else
		push 0
		jump then
	      label else
		push 1
		push 2
	      label then
		pop
	    }
	}
	catch {x 1}
	set errorInfo
    }
    -match glob
    -result {inconsistent stack depths on two execution paths
    ("tcl::unsupported::assemble" body, line 9)*}
}

test assemble-1.31 {unset, exists, lappend - smoke test} {
    -body {
	proc x {y} {
	    tcl::unsupported::assemble {
		exist result
		store result
		pop

		exist result
		lappend result
		pop

		push result
		push y
		existStk
		lappendStk
		pop

		push y
		unsetStk true
		exist y
		lappend result
		pop

		push {}
		store foo
		pop
		exist foo
		lappend result
		pop

		unset true foo
		exist foo
		lappend result
		pop

		push a
		existArray z
		lappend result
		pop

		push a
		push b
		lappendArray z
		lappend result
		pop

		push z
		push a
		existArrayStk
		lappend result
		pop

		push z
		push a
		push c
		lappendArrayStk
		lappend result

	    }
	}
	x 1
    }
    -result {0 1 1 0 1 0 0 b 1 {b c}}
}

test assemble-2.1 {concat} {
    -body {
	::tcl::unsupported::assemble {
	    push a
	    push b
	    push c
	    concat 3
	}
    }
    -result abc
}

test assemble-3.1 {expr} {
    -body {
	::tcl::unsupported::assemble {
	    push {1+2+3+4+5}
	    exprStk
	}
    }
    -result 15
}
test assemble-4.1 {eval} {
    -body {
	::tcl::unsupported::assemble {
	    push {join [list [expr {1+2+3+4+5}] a] {}}
	    evalStk
	}
    }
    -result 15a
}
test assemble-4.2 {eval} {
    -body {
	proc x {} {
	    ::tcl::unsupported::assemble {
		push 3
		store n
		pop
		eval {expr {3*$n + 1}}
		push 1
		add
	    }
	}
	x
    }
    -result 11
}

test assemble-4.3 {expr} {
    -body {
	proc x {} {
	    ::tcl::unsupported::assemble {
		push 3
		store n
		pop
		expr {3*$n + 1}
		push 1
		add
	    }
	}
	x
    }
    -result 11
}
		
test assemble-30.1 {Ulam's 3n+1 problem, TAL implementation} {
    -body {
	proc ulam {n} {
	    tcl::unsupported::assemble {
		load n;		# max
		dup;		# max n
		jump start;     # max n
	    
		label loop;	# max n
		over 1;         # max n max
		over 1;		# max in max n
		ge;             # man n max>=n
		jumpTrue skip;  # max n

		reverse 2;      # n max
		pop;            # n
		dup;            # n n
	    
		label skip;	# max n
		dup;            # max n n
		push 2;         # max n n 2
		mod;            # max n n%2
		jumpTrue odd;   # max n
	    
		push 2;         # max n 2
		div;            # max n/2 -> max n
		jump start;     # max n
	     
		label odd;	# max n
		push 3;         # max n 3
		mult;           # max 3*n
		push 1;         # max 3*n 1
		add;            # max 3*n+1
	    
		label start;	# max n
		dup;		# max n n
		push 1;		# max n n 1
		neq;		# max n n>1
		jumpTrue loop;	# max n
	    
		pop;		# max
	    }
	}
	set result {}
	for {set i 1} {$i < 30} {incr i} {
	    lappend result [ulam $i]
	}
	set result
    }
    -result {1 2 16 4 16 16 52 8 52 16 52 16 40 52 160 16 52 52 88 20 64 52 160 24 88 40 9232 52 88}
}

}

::tcltest::cleanupTests
return

# Local Variables:
# mode: tcl
# End: