-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
executable file
·1312 lines (1126 loc) · 29.4 KB
/
tests.py
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
import sys
is_tinypy = "tinypy" in sys.version
if not is_tinypy:
from boot import *
import asm
import disasm
################################################################################
RM = 'rm -f '
VM = './tinypy '
TINYPY = './tinypy '
TMP = 'tmp.txt'
SANDBOX = '-sandbox' in ARGV
def system_rm(fname):
system(RM+fname)
################################################################################
#if not is_tinypy:
#v = chksize()
#assert (v < 65536)
################################################################################
def t_show(t):
if t.type == 'string': return '"'+t.val+'"'
if t.type == 'number': return t.val
if t.type == 'symbol': return t.val
if t.type == 'name': return '$'+t.val
return t.type
def t_tokenize(s,exp=''):
import tokenize
result = tokenize.tokenize(s)
res = ' '.join([t_show(t) for t in result])
#print(s); print(exp); print(res)
assert(res == exp)
if __name__ == '__main__':
t_tokenize("234",'234')
t_tokenize("234.234",'234.234')
t_tokenize("phil",'$phil')
t_tokenize("_phil234",'$_phil234')
t_tokenize("'phil'",'"phil"')
t_tokenize('"phil"','"phil"')
t_tokenize("'phil' x",'"phil" $x')
t_tokenize("#comment","")
t_tokenize("and","and")
t_tokenize("=","=")
t_tokenize("()","( )")
t_tokenize("(==)","( == )")
t_tokenize("phil=234","$phil = 234")
t_tokenize("a b","$a $b")
t_tokenize("a\nb","$a nl $b")
t_tokenize("a\n b","$a nl indent $b dedent")
t_tokenize("a\n b\n c", "$a nl indent $b nl indent $c dedent dedent")
t_tokenize("a\n b\n c", "$a nl indent $b nl $c dedent")
t_tokenize("a\n b\n \n c", "$a nl indent $b nl nl indent $c dedent dedent")
t_tokenize("a\n b\nc", "$a nl indent $b nl dedent $c")
t_tokenize("a\n b\n c\nd", "$a nl indent $b nl indent $c nl dedent dedent $d")
t_tokenize("(\n )","( )")
t_tokenize(" x","indent $x dedent")
t_tokenize(" #","")
t_tokenize("None","None")
################################################################################
def t_lisp(t):
if t.type == 'block':
return """{%s}"""%' '.join([t_lisp(tt) for tt in t.items])
if t.type == 'statement':
return """%s;"""%' '.join([t_lisp(tt) for tt in t.items])
if t.items == None: return t.val
args = ''.join([" "+t_lisp(tt) for tt in t.items])
return "("+t.val+args+")"
def t_parse(s,ex=''):
import tokenize, parse
r = ''
tokens = tokenize.tokenize(s)
tree = parse.parse(s,tokens)
r = t_lisp(tree)
#print(s); print(ex); print(r)
assert(r==ex)
def t_unparse(s):
import tokenize, parse
ok = False
try:
tokens = tokenize.tokenize(s)
tree = parse.parse(s,tokens)
except:
ok = True
assert(ok == True)
if __name__ == '__main__':
t_parse('2+4*3', '(+ 2 (* 4 3))')
t_parse('4*(2+3)', '(* 4 (+ 2 3))')
t_parse('(2+3)*4', '(* (+ 2 3) 4)')
t_parse('1<2', '(< 1 2)')
t_parse('x=3', '(= x 3)')
t_parse('x = 2*3', '(= x (* 2 3))')
t_parse('x = y', '(= x y)')
t_parse('2,3', '(, 2 3)')
t_parse('2,3,4', '(, 2 3 4)')
t_parse('[]', '([])')
t_parse('[1]', '([] 1)')
t_parse('[2,3,4]', '([] 2 3 4)')
t_parse('print(3)', '($ print 3)')
t_parse('print()', '($ print)')
t_parse('print(2,3)', '($ print 2 3)')
t_parse('def fnc():pass', '(def fnc (():) pass)')
t_parse('def fnc(x):pass', '(def fnc ((): x) pass)')
t_parse('def fnc(x,y):pass', '(def fnc ((): x y) pass)')
t_parse('x\ny\nz', '(; x y z)')
t_parse('return x', '(return x)')
t_parse('print(test(2,3))', '($ print ($ test 2 3))')
t_parse('x.y', '(. x y)')
t_parse('get(2).x', '(. ($ get 2) x)')
t_parse('{}', '({})')
t_parse('True', 'True')
t_parse('False', 'False')
t_parse('None', 'None')
t_parse('while 1:pass', '(while 1 pass)')
t_parse('for x in y:pass', '(for x y pass)')
t_parse('print("x")', '($ print x)')
t_parse('if 1: pass', '(if (elif 1 pass))')
t_parse('x = []', '(= x ([]))')
t_parse('x[1]', '(. x 1)')
t_parse('import sdl', '(import sdl)')
t_parse('2 is 3', '(is 2 3)')
t_parse('2 is not 3', '(isnot 2 3)')
t_parse('x is None', '(is x None)')
t_parse('2 is 3 or 4 is 5', '(or (is 2 3) (is 4 5))')
t_parse('e.x == 3 or e.x == 4', '(or (== (. e x) 3) (== (. e x) 4))')
t_parse('if 1==2: 3\nelif 4:5\nelse:6', '(if (elif (== 1 2) 3) (elif 4 5) (else 6))')
t_parse('x = y(2)*3 + y(4)*5', '(= x (+ (* ($ y 2) 3) (* ($ y 4) 5)))')
t_parse('x(1,2)+y(3,4)', '(+ ($ x 1 2) ($ y 3 4))')
t_parse('x(a,b,c[d])', '($ x a b (. c d))')
t_parse('x(1,2)*j+y(3,4)*k+z(5,6)*l', '(+ (+ (* ($ x 1 2) j) (* ($ y 3 4) k)) (* ($ z 5 6) l))')
t_parse('a = b.x/c * 2 - 1', '(= a (- (* (/ (. b x) c) 2) 1))')
t_parse('for x in y: z', '(for x y z)')
t_parse('min(255,n*2)','($ min 255 (* n 2))')
t_parse('c = pal[i*8]','(= c (. pal (* i 8)))')
t_parse('{x:y,a:b}','({} x y a b)')
t_parse('x[1:2:3]','(. x (: 1 2 3))')
if is_tinypy: t_parse('x - -234','(- x -234)')
else: t_parse('x - -234','(- x -234.0)')
t_parse('x - -y','(- x (- 0 y))')
t_parse('x = ((y*4)-2)','(= x (- (* y 4) 2))')
if is_tinypy: t_parse('print([1,2,"OK",4][-3:3][1])','($ print (. (. ([] 1 2 OK 4) (: -3 3)) 1))')
else: t_parse('print([1,2,"OK",4][-3:3][1])','($ print (. (. ([] 1 2 OK 4) (: -3.0 3)) 1))')
t_parse('a,b = 1,2','(= (, a b) (, 1 2))')
t_parse('class C: pass','(class C (methods pass))')
t_parse('def test(*v): pass','(def test ((): (* v)) pass)')
t_parse('def test(**v): pass','(def test ((): (** v)) pass)')
t_parse('test(*v)','($ test (* v))')
t_parse('test(**v)','($ test (** v))')
t_parse('def test(x=y): pass','(def test ((): (= x y)) pass)')
t_parse('test(x=y)','($ test (= x y))')
t_parse('def test(y="K",x="Z"): pass','(def test ((): (= y K) (= x Z)) pass)')
t_parse('return x+y','(return (+ x y))')
t_parse('if "a" is not "b": pass','(if (elif (isnot a b) pass))')
t_parse('z = 0\nfor x in y: pass','(; (= z 0) (for x y pass))')
t_parse('for k in {"OK":0}: pass','(for k ({} OK 0) pass)')
t_parse('print(test(10,3,z=50000,*[200],**{"x":4000}))','($ print ($ test 10 3 (= z 50000) (* ([] 200)) (** ({} x 4000))))')
t_parse('x="OK";print(x)','(; (= x OK) ($ print x))')
t_parse('[(1,3)]','([] (, 1 3))')
t_parse('x[:]','(. x (: None None))')
t_parse('x[:1]','(. x (: None 1))')
t_parse('x[1:]','(. x (: 1 None))')
t_parse('return\nx','(; return x)')
t_parse('"""test"""','test')
t_parse('return a,b','(return (, a b))')
# this should throw an error - bug #26
t_unparse("""
while 1:
pass
""")
# test cases for python 2.x print statements - bug #17
# since this is python 2.x syntax it should output an syntax Exception
# mini-block style
t_unparse("""
def x(): print "OK"
""")
# block level
t_unparse("""
def x():
print "OK"
""")
# module level
t_unparse("""
print "OK"
""")
################################################################################
def showerror(cmd, ss, ex, res):
print(cmd)
print("ss : '" + str(ss) + "'")
print("ex : '" + str(ex) + "'")
print("res: '" + str(res) + "'")
def t_render(ss,ex,exact=True):
import tokenize, parse, encode
if not istype(ss,'list'): ss =[ss]
n = 1
for s in ss:
fname = 'tmp'+str(n)+'.tpc'
system_rm(fname)
tokens = tokenize.tokenize(s)
t = parse.parse(s,tokens)
r = encode.encode(fname,s,t)
f = save(fname,r)
n += 1
system_rm('tmp.txt')
cmd = VM + fname + " > tmp.txt"
system(cmd)
res = load(TMP).strip()
#print(ss,ex,res)
if exact:
if res != ex: showerror(cmd, ss, ex, res)
assert(res == ex)
else:
if ex not in res: showerror(cmd, ss, ex, res)
assert(ex in res)
def t_unrender(s):
import tokenize, parse, encode
ok = False
try:
tokens = tokenize.tokenize(s)
t = parse.parse(s,tokens)
r = encode.encode('tmp.tpc',s,t)
except:
ok = True
assert(ok == True)
def test_range():
t_render("""print(str(range(4))[:5])""","<list")
t_render("""print(len(range(4)))""","4")
t_render("""print(range(4)[0])""","0")
t_render("""print(range(4)[1])""","1")
t_render("""print(range(4)[-1])""","3")
t_render("""print(str(range(-4))[:5])""","<list")
t_render("""print(len(range(-4)))""","0")
t_render("""print(str(range(0,5,3))[:5])""","<list")
t_render("""print(len(range(0,5,3)))""","2")
t_render("""print(range(0,5,3)[0])""","0")
t_render("""print(range(0,5,3)[1])""","3")
t_render("""print(range(0,5,3)[-1])""","3")
t_render("""print(str(range(5,0,-3))[:5])""","<list")
t_render("""print(len(range(5,0,-3)))""","2")
t_render("""print(range(5,0,-3)[0])""","5")
t_render("""print(range(5,0,-3)[1])""","2")
t_render("""print(range(5,0,-3)[-1])""","2")
t_render("""print(str(range(-8,-4))[:5])""","<list")
t_render("""print(len(range(-8,-4)))""","4")
t_render("""print(range(-8,-4)[0])""","-8")
t_render("""print(range(-8,-4)[1])""","-7")
t_render("""print(range(-8,-4)[-1])""","-5")
t_render("""print(str(range(-4,-8,-1))[:5])""","<list")
t_render("""print(len(range(-4,-8,-1)))""","4")
t_render("""print(range(-4,-8,-1)[0])""","-4")
t_render("""print(range(-4,-8,-1)[1])""","-5")
t_render("""print(range(-4,-8,-1)[-1])""","-7")
t_render("""print(str(range(-4,-8))[:5])""","<list")
t_render("""print(len(range(-4,-8)))""","0")
t_render("""print(str(range(-8,-4,-1))[:5])""","<list")
t_render("""print(len(range(-8,-4,-1)))""","0")
t_render("""print(str(range(0,4,0))[:5])""","<list")
t_render("""print(len(range(0,4,0)))""","0")
if 0 and __name__ == '__main__':
#t_render('print("hello world")',"(hello world)")
t_render('print(234)',"234")
t_render('a=3\nprint(a)',"3")
t_render('print(2+3)',"5")
t_render('print(4|1)',"5")
t_render('print(5&7)',"5")
t_render('print(2^7)',"5")
t_render('print(7^2&2)',"5")
t_render('print(7^2|4)',"5")
t_render('x=4\nx|=1\nprint(x)',"5")
t_render('x=5\nx&=7\nprint(x)',"5")
t_render('x=2\nx^=7\nprint(x)',"5")
t_render("""
x = 2
x += 3
print(x)
"""
,"5")
t_render("""
x = "OK"
print(x)
"""
,"OK")
t_render("""
a,b = 1,2
print(a+b)
"""
,"3")
t_render("""
x = 1
if x == 1:
print("yes")
""","yes")
t_render("""
if 0 == 1:
print("X")
else:
print("OK")
"""
,"OK")
t_render("""
if 0 == 1:
print("X")
elif 1 == 2:
print("Y")
else:
print("OK")
"""
,"OK")
t_render("""
def test(x,y):
return x+y
r = test(3,5)
print(r)
""","8")
t_render("""
x = 1
t = 0
while x<=5:
t = t+x
x = x+1
print(t)
""","15")
t_render("""
x = {}
x.y = "test"
print(x.y)
""","test")
t_render("""
if "a" is "a":
print("OK")
"""
,"OK")
t_render("""
if "a" is not "b":
print("OK")
"""
,"OK")
t_render("""
if None is None:
print("OK")
"""
,"OK")
t_render("""
if "x" is "x" or "y" is "y":
print("OK")
"""
,"OK")
t_render("""
x = 1
while x < 3:
break
x = x + 1
print(x)
"""
,"1")
t_render("""
x = 1
n = 0
while x < 10:
x = x + 1
if n == 2:
continue
n = n + 1
print(n)
"""
,"2")
t_render("""
def test(x): return x
y = test(1)*2 + test(3)*4 + test(5)*6
print(y)
"""
,"44")
t_render("""
def test(a,b): return a+b
print(test(1,1)+test(1,1))
"""
,"4")
t_render("""
def test(): print("OK")
x = test
x()
"""
,"OK")
t_render("""
x = [2,4,6]
print(x[1])
"""
,"4")
t_render("""
def test(): print("OK")
x = [1,test,2]
x[1]()
"""
,"OK")
t_render("""
z = 0
for x in [1,2,3]:
z += x
print(z)
"""
,"6")
t_render("""
z = 0
for x in range(1,4):
z += x
print(z)
"""
,"6")
t_render("""
x = {'a':'OK'}
print(x.a)
"""
,"OK")
t_render("""print("1234"[1:3])""","23")
t_render("""print("1234"[-3:3])""","23")
t_render("""print([1,2,"OK",4][-3:3][1])""","OK")
t_render("""
n = 0
for x in range(0,10,2):
n += 1
print(n)
"""
,"5")
t_render("""print(max(3,8,2,6))""","8")
t_render("""print(min(3,4,2,6))""","2")
t_render("""for k in {'OK':0}: print(k)""","OK")
t_render("""
X = "OK"
def test(): print(X)
test()
"""
,"OK")
t_render("""
a = 4
def test(z):
for i in range(0,a):
z += i
return z
print(test(1))
"""
,"7")
t_render("""
def test(self): print(self)
fnc = bind(test,"OK")
fnc()
"""
,"OK")
t_render("""
x = [v*v for v in range(0,5)]
print(x[3])
"""
,"9")
t_render("""
t = [[y*10+x for x in range(0,10)] for y in range(0,10)]
print(t[2][3])
"""
,"23")
t_render("""
x = [1]
x.extend([2,3])
print(x[1])
"""
,"2")
#t_render("""
#x = {'a':3}
#merge(x,{'b':4})
#print(x.b)
#"""
#,"4")
t_render("""
x = [1,2,3]
y = copy(x)
y[0] *= 10
print(x[0]+y[0])
"""
,"11")
t_render("""
x = {'a':3}
y = copy(x)
y.a *= 10
print(x.a+y.a)
"""
,"33")
t_render("""
x = {}
y = x['x']
"""
,'KeyError', False)
t_render("""
x = []
y = x[1]
"""
,'KeyError', False)
t_render("""print("O"+"K")""","OK")
t_render("""print("-".join(["O","K"]))""","O-K")
t_render("""print("OK-OK".split("-")[1])""","OK")
t_render("""
def test(*v): return max(v[2])
print(test(*[1,2,"OK"]))
"""
,"OK")
t_render("""
def test(**v): return v['x']
print(test(**{'x':'OK'}))
"""
,"OK")
#t_render("""
#def test(y='K',x='Z'): print(x+y)
#test(x='O')
#"""
#,"OK")
t_render("""
def test(y='K',x='Z'): print(x+y)
test('O')
"""
,"ZO")
#t_render("""
#def test(a,b=2,*c,**d): return a+b+c[0]+d['x']+d['z']
#print(test(10,3,z=50000,*[200],**{'x':4000}))
#"""
#,"54213")
t_render("""print("".join(["O"]+["K"]))""","OK")
t_render("""x="OK";print(x)""","OK")
t_render("""x = [1,2,] ; print(x[1])""","2")
t_render("""a,b,d = [0],0,'OK'; print(d)""","OK")
t_render("""
def test():
raise
try:
test()
except:
print("OK")
""","OK")
t_render("""print("OKx"[:-1])""","OK")
t_render("""print("xOK"[1:])""","OK")
t_render("""a,b = "OK"; print(a+b)""","OK")
t_render("""
def test(a,b):
print (a+b[2])
test(1,3)
""","Exception",False)
t_render("""
def test(): raise
test()
""","Exception",False)
t_render(['OK="OK"',"import tmp1\nprint(tmp1.OK)"],"OK")
t_render(['O="O"','K="K"',"import tmp1, tmp2\nprint(tmp1.O+tmp2.K)"],"OK")
t_render("""
def test(): return
x = 1
print(test())
""","None")
t_render("""
def test(): pass
x = 1
print(test())
""","None")
t_render("""
def test():
global x
x = "OK"
test()
print(x)
""","OK")
t_render("print(len([1,2,3]))","3")
t_render('if not "?" in "xyz": print("OK")',"OK")
t_render('print({1:"OK"}[1])',"OK")
t_render('print(len("\0"))',"1")
t_render('print(1 in {1:2})',"1")
t_render('x = {1:2}; del x[1]; print(len(x))','0')
t_render("""
def test(t):
t = "O"+t
print(t)
test("K")
""","OK")
t_render("""print([1,2,3].index(3))""","2")
t_render("""print("1,2,3".split(",").index("3"))""","2")
t_render("""v = [3,2,1]; v.sort(); print(v[0])""","1")
t_render("""print(abs(-5))""","5")
t_render("""print(int(1.234))""","1")
t_render("print(int(round(1.5)))","2")
t_render("print(ord('X'))","88")
t_render("print(ord(chr(128)))","128")
#t_render("print(fsize('LICENSE.txt'))","181")
t_render("print(int('ff',16))","255")
t_render("""
def test(x,y): print(x); return y
test('a',1) or test('b',1) and test('c',0)
""","a")
#t_render("def test(): print('OK')\n{'__call__':test}()","OK")
t_render("""
def test():
def fnc():
print("OK")
fnc()
test()
""","OK")
t_render("""print("aa..bbb...ccc".replace("..","X"))""","aaXbbbX.ccc")
t_render("""print("..bbb..".replace("..","X"))""","XbbbX")
t_render("""print("234".replace("\r\n","\n"))""","234")
t_render("""print("a\0b".replace("\0","X"))""","aXb")
t_render("""x = "a\0b"; x = x.replace("\0","c"); print(x)""","acb")
t_render("""print(0xff)""","255")
t_render("""x=(1,3);print({x:'OK'}[x])""","OK")
t_render("""x=(1,3);y=(1,3);print({x:'OK'}[y])""","OK")
t_render("""print({(1,3):'OK'}[(1,3)])""","OK")
t_render("def test(): test()\ntest()","Exception", False)
t_render("x = []; x.append(x); print(x<x)","0");
t_render("x = []; x.append(x); print({x:'OK'}[x])","OK")
#t_render("print(float(str(4294967296))==float('4294967296'))","1")
t_render("print(2**3)","8")
#t_render("x = 'OK',\nprint(x[0])","OK")
test_range()
t_render(['v="OK"',"from tmp1 import *\nprint(v)"],"OK")
t_render(['v="OK"',"from tmp1 import v\nprint(v)"],"OK")
t_render(['x="X";y="K"',"x = 'O'\nfrom tmp1 import y\nprint(x+y)"],"OK")
t_render("""
def test(**e):
print(e['x'])
test(x='OK')
""","OK")
# test register allocator
s = "def f():pass\n"+("f()\n"*256)+"print('OK')"
t_render(s,"OK")
t_render("print(2**3)","8")
t_render("print(2*3**2)", "18", False)
t_render("""
def test(**v): return 'OK'
print(test())
"""
,"OK")
t_render("""
def test(**v):
v['x'] = 'OK'
return v
print(test()['x'])
"""
,"OK")
t_render("""
def get(self,k):
return k+"K"
v = object()
v.__get__ = bind(get,v)
print(v.O)
""",
"OK")
t_render("""
def set(self,k,v):
self = getraw(self)
self[k] = v + "K"
v = object()
v.__set__ = bind(set,v)
v.x = "O"
print(v.x)
""",
"OK")
t_render("""
def call(self,x):
print(x)
v = object()
v.__call__ = bind(call,v)
v("OK")
"""
,"OK")
#a REG related test
t_render("""
def test():
init = True
if init and True:
pass
print("OK")
""","OK")
meta_objs_init = """
def my_new(klass,*p):
self = object()
setmeta(self,klass)
self.__init__(*p)
return self
def A_init(self,v):
if v: print("A_init")
def A_test1(self):
print("A_test1")
def A_test2(self):
print("A_test2")
A = {'__new__':my_new,'__init__':A_init,'test1':A_test1,'test2':A_test2}
def B_init(self,v):
if v: print("B_init")
def B_test2(self):
print("B_test2")
B = {'__init__':B_init,'test2':B_test2}
setmeta(B,A)
"""
t_render(meta_objs_init+"""A(True)""","A_init")
t_render(meta_objs_init+"""A(False).test1()""","A_test1")
t_render(meta_objs_init+"""A(False).test2()""","A_test2")
t_render(meta_objs_init+"""B(True)""","B_init")
t_render(meta_objs_init+"""B(False).test1()""","A_test1")
t_render(meta_objs_init+"""B(False).test2()""","B_test2")
#various class construct use tests
t_render("""
class C:
def __init__(self,data): self.data = data
def print(self): print(self.data)
C("OK").print()
"""
,"OK")
t_render("""
class X:
pass
y = X()
print("OK")
""","OK")
t_render("""
class X: pass
def test(): y = X()
test()
print("OK")
""","OK")
t_render(["class X: pass\ndef test(): y = X()","import tmp1\ntmp1.test();print('OK')"],"OK")
t_render("""
class A:
def __init__(self):
self.a = 'O'
self.b = 'x'
def test(self):
print("KO")
class B(A):
def __init__(self):
A.__init__(self)
self.b = 'K'
def test(self):
print(self.a+self.b)
B().test()
""","OK")
t_render("""
class A:
def test(self):
print(self)
A.test("OK")
""","OK")
#test that you can make a callable object
t_render("""
class Test:
def __init__(self,v):
self.value = v
def __call__(self):
print(self.value)
x = Test('OK')
x()
""","OK")
#test that you can use a __get__
t_render("""
class Test:
def __get__(self,k):
return k+"K"
x = Test()
print(x.O)
""","OK")
#test that you can use __set__
t_render("""
class Test:
def __set__(self,k,v):
getraw(self)[k] = "O"+v
x = Test()
x.v = "K"
print(x.v)
""","OK")
#test that exceptions are cleared after they are caught
#and not repeated
t_render("""
def test():
try:
pass
except:
pass
print("OK")
raise
try:
test()
except:
pass
""","OK")
#check that missing attributes throw an error
t_render("""
class A: pass
try:
A().x
except:
print('OK')
""","OK")
#check that a changed attribute gets changed
t_render("""
class A:
def x(self): pass
a = A()
a.x = "OK"
print(a.x)
""","OK")
#test that you can use a __get__ gets inherited
t_render("""
class A:
def __get__(self,k):
return k+"K"
class B(A):
pass
x = B()
print(x.O)
""","OK")
#test that meta methods aren't called on non-objects
t_render("""
def get(): pass
x = {"__get__":get}
try:
z = x.y
except:
print("OK")
""","OK")
#test that meta stuff is inheritited in dicts
t_render("""
x = {1:"O"}
y = {2:"K"}
setmeta(y,x)
print(y[1]+y[2])
""","OK")
#test that meta stuff doesn't change into methods in dicts
t_render("""
def get(k): return k
x = {"get":get}
print(x.get("OK"))
""","OK")
#tests issue 14: string.index() should give an exception if substring not found
t_render("""
try:
"test-".index("=")
except:
print("OK")
""","OK")
#issue 19: test that multiplying a string with a negative value returns an empty string
t_render("""
foo = "abc" * -1
print(foo)
""", "")
#issue 18: tests that strings containing NUL chars are printed correctly
t_render("""
foo = "abc" + chr(0) + "d"
print(foo)
""", "abc" + chr(0) + "d")
#issue 18 (related): tests that "".strip() treats strings containing NUL chars correctly
t_render("""
foo = "abc" + chr(0) + "d\n"
print(foo.strip())
""", "abc" + chr(0) + "d")
#test that class variables work as expected
t_render("""
class A:
foo = 42
s = str(A.foo)
A.foo += 1
s += str(A.foo)
print(s)
""", "4243")
#check that class variables are not leaked to the global scope
t_render("""
class A:
foo = 42
print(foo)
""", "KeyError", False)
#test that class variables can correctly be accessed by a subclass
t_render("""
class A:
foo = "OK"