Skip to content

Commit 77a5d26

Browse files
🔍 test: Generate more tests and cover n instructions.
1 parent d7928c6 commit 77a5d26

File tree

1 file changed

+48
-52
lines changed

1 file changed

+48
-52
lines changed

test/generate.py

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
import operator
22

3-
hugenumbers = [
4-
1 ,
5-
17 ,
3+
MAX_NUMBER = 2**53 - 1
4+
MIN_NUMBER = -2**53
5+
6+
hugenumbers = sorted([
67
91**7 ,
78
2**30 ,
89
3**50
9-
]
10+
])
1011

11-
smallnumbers = [ 1 , 3 , 7 , 9 , 11 , 17 , 22 , 24 , 27 , 29 ]
12+
smallnumbers = sorted([ 1 , 3 , 7 , 9 , 11 , 17 , 22 , 24 , 27 , 29 , 1234 , 5678 ])
1213

1314
arithmetic = {
1415
'add' : {
15-
'numbers' : hugenumbers ,
16+
'numbers' : smallnumbers + hugenumbers ,
1617
'apply' : operator.add ,
1718
'str' : '+'
1819
} ,
1920
'sub' : {
20-
'numbers' : hugenumbers ,
21+
'numbers' : smallnumbers + hugenumbers ,
2122
'apply' : operator.sub ,
2223
'str' : '-'
2324
} ,
2425
'mul' : {
25-
'numbers' : hugenumbers ,
26+
'numbers' : smallnumbers + hugenumbers ,
2627
'apply' : operator.mul ,
2728
'str' : '*'
2829
} ,
@@ -32,30 +33,30 @@
3233
'str' : '^'
3334
} ,
3435
'div' : {
35-
'numbers' : hugenumbers ,
36+
'numbers' : smallnumbers + hugenumbers ,
3637
'apply' : operator.floordiv ,
3738
'str' : '/'
3839
} ,
3940
'mod' : {
40-
'numbers' : hugenumbers ,
41+
'numbers' : smallnumbers + hugenumbers ,
4142
'apply' : operator.mod ,
4243
'str' : '%'
4344
}
4445
}
4546

46-
def write ( f , numbers , name , t , opstr , a , ispow = True ) :
47+
def write ( f , numbers , name , t , ispow = False , isn = False , isi = False) :
4748

4849
f.write("import test from 'ava' ;\n")
4950
f.write("import {{ parse , stringify , {} }} from '../../../../src' ;\n\n".format(name))
5051

51-
if ispow :
52+
if isn :
5253

5354
f.write("""function macro ( t , A , B , C ) {{
5455
const a = parse( A ) ;
5556
const c = {}( a , B ) ;
5657
t.is( stringify( a ) , {} ) ;
5758
t.is( stringify( c ) , C ) ;
58-
}}\n\n""".format( name , a ) )
59+
}}\n\n""".format( name , 'C' if isi else 'A' ) )
5960

6061
else:
6162

@@ -66,67 +67,62 @@ def write ( f , numbers , name , t , opstr , a , ispow = True ) :
6667
t.is( stringify( a ) , {} ) ;
6768
t.is( stringify( b ) , B ) ;
6869
t.is( stringify( c ) , C ) ;
69-
}}\n\n""".format( name , a ) )
70+
}}\n\n""".format( name , 'C' if isi else 'A' ) )
71+
7072

73+
f.write("macro.title = ( _ , A , B , C ) => `{}(${{A}},${{B}}) = ${{C}}` ;\n\n".format(name))
7174

72-
f.write("macro.title = ( _ , A , B , C ) => `${{A}} {} ${{B}} = ${{C}}` ;\n\n".format(opstr))
75+
if isn:
76+
LINE = "test( macro , '{}' , {} , '{}' ) ;\n"
77+
else:
78+
LINE = "test( macro , '{}' , '{}' , '{}' ) ;\n"
7379

7480
for a in numbers :
7581

7682
for b in numbers :
7783

78-
if ispow:
84+
x = a
85+
y = b
86+
c = t( x , y )
87+
if not isn or MIN_NUMBER <= y <= MAX_NUMBER:
88+
f.write(LINE.format(x,y,c))
7989

80-
x = a
81-
y = b
82-
c = t( x , y )
83-
f.write("test( macro , '{}' , {} , '{}' ) ;\n".format(x,y,c))
90+
x = -a
91+
y = b
92+
c = t( x , y )
93+
if not isn or MIN_NUMBER <= y <= MAX_NUMBER:
94+
f.write(LINE.format(x,y,c))
8495

85-
x = -a
86-
y = b
87-
c = t( x , y )
88-
f.write("test( macro , '{}' , {} , '{}' ) ;\n".format(x,y,c))
89-
90-
else:
91-
92-
x = a
93-
y = b
94-
c = t( x , y )
95-
f.write("test( macro , '{}' , '{}' , '{}' ) ;\n".format(x,y,c))
96-
97-
x = -a
98-
y = b
99-
c = t( x , y )
100-
f.write("test( macro , '{}' , '{}' , '{}' ) ;\n".format(x,y,c))
96+
if not ispow:
10197

10298
x = a
10399
y = -b
104100
c = t( x , y )
105-
f.write("test( macro , '{}' , '{}' , '{}' ) ;\n".format(x,y,c))
101+
if not isn or MIN_NUMBER <= y <= MAX_NUMBER:
102+
f.write(LINE.format(x,y,c))
106103

107104
x = -a
108105
y = -b
109106
c = t( x , y )
110-
f.write("test( macro , '{}' , '{}' , '{}' ) ;\n".format(x,y,c))
107+
if not isn or MIN_NUMBER <= y <= MAX_NUMBER:
108+
f.write(LINE.format(x,y,c))
109+
110+
def open_and_write ( opname , t , nb , **kwargs ) :
111+
with open( 'test/src/integer/arithmetic/{}.js'.format(opname) , 'w' ) as f :
112+
write( f , nb , opname , t , **kwargs )
111113

112114
for name , op in arithmetic.items():
113115

114116
t = op['apply']
115117
nb = op['numbers']
116118

117-
# standard op
118-
with open( 'test/src/integer/arithmetic/{}.js'.format(name) , 'w' ) as f :
119-
120-
opstr = op['str']
121-
a = 'A'
122-
123-
write( f , nb , name , t , opstr , a , ispow = name == 'pow' )
119+
ispow = name == 'pow'
124120

121+
# standard op
122+
open_and_write( name , t , nb , ispow = ispow )
125123
# in-place op
126-
iname = 'i{}'.format(name)
127-
with open( 'test/src/integer/arithmetic/{}.js'.format(iname) , 'w' ) as f :
128-
129-
opstr = op['str'] + '='
130-
a = 'C'
131-
132-
write( f , nb , iname , t , opstr , a , ispow = name == 'pow' )
124+
open_and_write( 'i' + name , t , nb , isi = True , ispow = ispow )
125+
# standard op with number arg
126+
open_and_write( name + 'n' , t , nb , isn = True , ispow = ispow )
127+
# in-place op with number arg
128+
open_and_write( 'i' + name + 'n' , t , nb , isi = True , isn = True , ispow = ispow )

0 commit comments

Comments
 (0)