1- import operator
2-
31MAX_NUMBER = 2 ** 53 - 1
42MIN_NUMBER = - 2 ** 53
53
1412arithmetic = {
1513 'add' : {
1614 'numbers' : smallnumbers + hugenumbers ,
17- 'apply' : operator . add ,
15+ 'apply' : lambda a , b : ( a + b ,) ,
1816 'str' : '+'
1917 } ,
2018 'sub' : {
2119 'numbers' : smallnumbers + hugenumbers ,
22- 'apply' : operator . sub ,
20+ 'apply' : lambda a , b : ( a - b ,) ,
2321 'str' : '-'
2422 } ,
2523 'mul' : {
2624 'numbers' : smallnumbers + hugenumbers ,
27- 'apply' : operator . mul ,
25+ 'apply' : lambda a , b : ( a * b ,) ,
2826 'str' : '*'
2927 } ,
3028 'pow' : {
3129 'numbers' : smallnumbers ,
32- 'apply' : operator . pow ,
30+ 'apply' : lambda a , b : ( a ** b ,) ,
3331 'str' : '^'
3432 } ,
3533 'div' : {
3634 'numbers' : smallnumbers + hugenumbers ,
37- 'apply' : operator . floordiv ,
35+ 'apply' : lambda a , b : ( a // b ,) ,
3836 'str' : '/'
3937 } ,
4038 'mod' : {
4139 'numbers' : smallnumbers + hugenumbers ,
42- 'apply' : operator . mod ,
40+ 'apply' : lambda a , b : ( a % b ,) ,
4341 'str' : '%'
44- }
42+ } ,
43+ 'divmod' : {
44+ 'numbers' : smallnumbers + hugenumbers ,
45+ 'apply' : lambda a , b : (a // b , a % b ) ,
46+ 'str' : '/%'
47+ } ,
4548}
4649
4750def write ( f , numbers , name , t , ispow = False , isn = False , isi = False ) :
4851
52+ outputsize = 2 if 'divmod' in name else 1
53+
4954 f .write ("import test from 'ava' ;\n " )
5055 f .write ("import {{ parse , stringify , {} }} from '../../../../src' ;\n \n " .format (name ))
5156
52- if isn :
57+ if outputsize == 2 :
58+
59+ if isn :
60+
61+ f .write ("""function macro ( t , A , B , C , D ) {{
62+ const a = parse( A ) ;
63+ const [c, d] = {}( a , B ) ;
64+ t.is( stringify( a ) , {} ) ;
65+ t.is( stringify( c ) , C ) ;
66+ t.is( stringify( d ) , D ) ;
67+ }}\n \n """ .format ( name , 'D' if isi else 'A' ) )
68+
69+ else :
70+
71+ f .write ("""function macro ( t , A , B , C , D ) {{
72+ const a = parse( A ) ;
73+ const b = parse( B ) ;
74+ const [c, d] = {}( a , b ) ;
75+ t.is( stringify( a ) , {} ) ;
76+ t.is( stringify( b ) , B ) ;
77+ t.is( stringify( c ) , C ) ;
78+ t.is( stringify( d ) , D ) ;
79+ }}\n \n """ .format ( name , 'D' if isi else 'A' ) )
80+
81+ else :
5382
54- f .write ("""function macro ( t , A , B , C ) {{
83+ if isn :
84+
85+ f .write ("""function macro ( t , A , B , C ) {{
5586 const a = parse( A ) ;
5687 const c = {}( a , B ) ;
5788 t.is( stringify( a ) , {} ) ;
5889 t.is( stringify( c ) , C ) ;
5990}}\n \n """ .format ( name , 'C' if isi else 'A' ) )
6091
61- else :
92+ else :
6293
63- f .write ("""function macro ( t , A , B , C ) {{
94+ f .write ("""function macro ( t , A , B , C ) {{
6495 const a = parse( A ) ;
6596 const b = parse( B ) ;
6697 const c = {}( a , b ) ;
@@ -70,12 +101,23 @@ def write ( f , numbers , name , t , ispow = False , isn = False , isi = False)
70101}}\n \n """ .format ( name , 'C' if isi else 'A' ) )
71102
72103
73- f .write ("macro.title = ( _ , A , B , C ) => `{}(${{A}},${{B}}) = ${{C}}` ;\n \n " .format (name ))
104+ if outputsize == 2 :
105+
106+ f .write ("macro.title = ( _ , A , B , C , D ) => `{}(${{A}},${{B}}) = [${{C}},${{D}}]` ;\n \n " .format (name ))
107+
108+ if isn :
109+ LINE = "test( macro , '{}' , {} , '{}' , '{}' ) ;\n "
110+ else :
111+ LINE = "test( macro , '{}' , '{}' , '{}' , '{}' ) ;\n "
74112
75- if isn :
76- LINE = "test( macro , '{}' , {} , '{}' ) ;\n "
77113 else :
78- LINE = "test( macro , '{}' , '{}' , '{}' ) ;\n "
114+
115+ f .write ("macro.title = ( _ , A , B , C ) => `{}(${{A}},${{B}}) = ${{C}}` ;\n \n " .format (name ))
116+
117+ if isn :
118+ LINE = "test( macro , '{}' , {} , '{}' ) ;\n "
119+ else :
120+ LINE = "test( macro , '{}' , '{}' , '{}' ) ;\n "
79121
80122 for a in numbers :
81123
@@ -85,27 +127,27 @@ def write ( f , numbers , name , t , ispow = False , isn = False , isi = False)
85127 y = b
86128 c = t ( x , y )
87129 if not isn or MIN_NUMBER <= y <= MAX_NUMBER :
88- f .write (LINE .format (x ,y ,c ))
130+ f .write (LINE .format (x ,y ,* c ))
89131
90132 x = - a
91133 y = b
92134 c = t ( x , y )
93135 if not isn or MIN_NUMBER <= y <= MAX_NUMBER :
94- f .write (LINE .format (x ,y ,c ))
136+ f .write (LINE .format (x ,y ,* c ))
95137
96138 if not ispow :
97139
98140 x = a
99141 y = - b
100142 c = t ( x , y )
101143 if not isn or MIN_NUMBER <= y <= MAX_NUMBER :
102- f .write (LINE .format (x ,y ,c ))
144+ f .write (LINE .format (x ,y ,* c ))
103145
104146 x = - a
105147 y = - b
106148 c = t ( x , y )
107149 if not isn or MIN_NUMBER <= y <= MAX_NUMBER :
108- f .write (LINE .format (x ,y ,c ))
150+ f .write (LINE .format (x ,y ,* c ))
109151
110152def open_and_write ( opname , t , nb , ** kwargs ) :
111153 with open ( 'test/src/integer/arithmetic/{}.js' .format (opname ) , 'w' ) as f :
0 commit comments