1
1
import operator
2
2
3
- hugenumbers = [
4
- 1 ,
5
- 17 ,
3
+ MAX_NUMBER = 2 ** 53 - 1
4
+ MIN_NUMBER = - 2 ** 53
5
+
6
+ hugenumbers = sorted ([
6
7
91 ** 7 ,
7
8
2 ** 30 ,
8
9
3 ** 50
9
- ]
10
+ ])
10
11
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 ])
12
13
13
14
arithmetic = {
14
15
'add' : {
15
- 'numbers' : hugenumbers ,
16
+ 'numbers' : smallnumbers + hugenumbers ,
16
17
'apply' : operator .add ,
17
18
'str' : '+'
18
19
} ,
19
20
'sub' : {
20
- 'numbers' : hugenumbers ,
21
+ 'numbers' : smallnumbers + hugenumbers ,
21
22
'apply' : operator .sub ,
22
23
'str' : '-'
23
24
} ,
24
25
'mul' : {
25
- 'numbers' : hugenumbers ,
26
+ 'numbers' : smallnumbers + hugenumbers ,
26
27
'apply' : operator .mul ,
27
28
'str' : '*'
28
29
} ,
32
33
'str' : '^'
33
34
} ,
34
35
'div' : {
35
- 'numbers' : hugenumbers ,
36
+ 'numbers' : smallnumbers + hugenumbers ,
36
37
'apply' : operator .floordiv ,
37
38
'str' : '/'
38
39
} ,
39
40
'mod' : {
40
- 'numbers' : hugenumbers ,
41
+ 'numbers' : smallnumbers + hugenumbers ,
41
42
'apply' : operator .mod ,
42
43
'str' : '%'
43
44
}
44
45
}
45
46
46
- def write ( f , numbers , name , t , opstr , a , ispow = True ) :
47
+ def write ( f , numbers , name , t , ispow = False , isn = False , isi = False ) :
47
48
48
49
f .write ("import test from 'ava' ;\n " )
49
50
f .write ("import {{ parse , stringify , {} }} from '../../../../src' ;\n \n " .format (name ))
50
51
51
- if ispow :
52
+ if isn :
52
53
53
54
f .write ("""function macro ( t , A , B , C ) {{
54
55
const a = parse( A ) ;
55
56
const c = {}( a , B ) ;
56
57
t.is( stringify( a ) , {} ) ;
57
58
t.is( stringify( c ) , C ) ;
58
- }}\n \n """ .format ( name , a ) )
59
+ }}\n \n """ .format ( name , 'C' if isi else 'A' ) )
59
60
60
61
else :
61
62
@@ -66,67 +67,62 @@ def write ( f , numbers , name , t , opstr , a , ispow = True ) :
66
67
t.is( stringify( a ) , {} ) ;
67
68
t.is( stringify( b ) , B ) ;
68
69
t.is( stringify( c ) , C ) ;
69
- }}\n \n """ .format ( name , a ) )
70
+ }}\n \n """ .format ( name , 'C' if isi else 'A' ) )
71
+
70
72
73
+ f .write ("macro.title = ( _ , A , B , C ) => `{}(${{A}},${{B}}) = ${{C}}` ;\n \n " .format (name ))
71
74
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 "
73
79
74
80
for a in numbers :
75
81
76
82
for b in numbers :
77
83
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 ))
79
89
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 ))
84
95
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 :
101
97
102
98
x = a
103
99
y = - b
104
100
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 ))
106
103
107
104
x = - a
108
105
y = - b
109
106
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 )
111
113
112
114
for name , op in arithmetic .items ():
113
115
114
116
t = op ['apply' ]
115
117
nb = op ['numbers' ]
116
118
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'
124
120
121
+ # standard op
122
+ open_and_write ( name , t , nb , ispow = ispow )
125
123
# 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