1
1
import test from 'ava' ;
2
- import { _add , _sub , _mul , _div , _cmp , _cmp_no_bounds } from '../../src' ;
2
+ import { _add , _sub , _mul , _div , _cmp , _cmp_no_bounds , _simplify } from '../../src' ;
3
3
4
4
import int from 'int' ;
5
5
import { ZZ } from '@aureooms/js-integer' ;
6
6
7
7
const GOOGOL = '10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' ;
8
8
9
- function macro ( t , alu , [ [ _x , _y , factory ] , a , b , c , d , e ] ) {
9
+ function binary ( t , alu , [ [ _x , _y , factory ] , a , b , c , d , e ] ) {
10
10
11
11
const apply = factory ( alu ) ;
12
12
13
- //const repr = x => `${alu.str(x[0])} / ${alu.str(x[1])}` ;
14
13
const num = x => Number ( alu . str ( x [ 0 ] ) ) / Number ( alu . str ( x [ 1 ] ) ) ;
15
14
16
15
const a0 = alu . reg ( a ) ;
@@ -25,10 +24,29 @@ function macro ( t , alu , [ [ _x , _y , factory ] , a , b , c , d , e ] ) {
25
24
26
25
}
27
26
28
- macro . title = ( _ , alu , [ [ name , op , impl ] , a , b , c , d , e ] ) => {
27
+ binary . title = ( _ , alu , [ [ name , op , impl ] , a , b , c , d , e ] ) => {
29
28
return `${ name } <${ impl . name } , ${ alu . name } > ${ a } /${ b } ${ op } ${ c } /${ d } = ${ e } ` ;
30
29
} ;
31
30
31
+ function unary ( t , alu , [ [ _x , _y , factory ] , a , b , e ] ) {
32
+
33
+ const apply = factory ( alu ) ;
34
+
35
+ const repr = x => `${ alu . str ( x [ 0 ] ) } /${ alu . str ( x [ 1 ] ) } ` ;
36
+
37
+ const _a = alu . reg ( a ) ;
38
+ const _b = alu . reg ( b ) ;
39
+
40
+ const z = apply ( _a , _b ) ;
41
+
42
+ t . is ( e , repr ( z ) ) ;
43
+
44
+ }
45
+
46
+ unary . title = ( _ , alu , [ [ name , impl ] , a , b , e ] ) => {
47
+ return `${ name } <${ impl . name } , ${ alu . name } > ${ name } (${ a } /${ b } ) = ${ e } ` ;
48
+ } ;
49
+
32
50
const ALU = [
33
51
{
34
52
name : 'int' ,
@@ -42,6 +60,7 @@ const ALU = [
42
60
lt0 : x => x . lt ( 0 ) ,
43
61
cmp : ( a , b ) => a . cmp ( b ) ,
44
62
neg : x => x . neg ( ) ,
63
+ sgn : x => x . cmp ( 0 ) ,
45
64
divmod : ( a , b ) => [ a . div ( b ) , a . mod ( b ) ] ,
46
65
} ,
47
66
{
@@ -55,16 +74,19 @@ const ALU = [
55
74
jz : x => x . iszero ( ) ,
56
75
lt0 : x => x . sign ( ) < 0 ,
57
76
cmp : ( a , b ) => a . cmp ( b ) ,
77
+ sgn : x => x . sign ( ) ,
58
78
neg : x => x . opposite ( ) ,
59
79
divmod : ( a , b ) => a . divmod ( b ) ,
80
+ egcd : ( a , b ) => a . egcd ( b ) ,
60
81
}
61
82
] ;
62
83
63
- const add = [ 'add' , '+' , [ _add ] ] ;
64
- const sub = [ 'sub' , '-' , [ _sub ] ] ;
65
- const mul = [ 'mul' , '*' , [ _mul ] ] ;
66
- const div = [ 'div' , '/' , [ _div ] ] ;
67
- const cmp = [ 'cmp' , '~' , [ _cmp , _cmp_no_bounds ] ] ;
84
+ const add = [ 'add' , '+' , [ _add ] , binary ] ;
85
+ const sub = [ 'sub' , '-' , [ _sub ] , binary ] ;
86
+ const mul = [ 'mul' , '*' , [ _mul ] , binary ] ;
87
+ const div = [ 'div' , '/' , [ _div ] , binary ] ;
88
+ const cmp = [ 'cmp' , '~' , [ _cmp , _cmp_no_bounds ] , binary ] ;
89
+ const simplify = [ 'simplify' , '=' , [ _simplify ] , unary , alu => alu . egcd ] ;
68
90
69
91
const PARAMS = [
70
92
@@ -131,9 +153,23 @@ const PARAMS = [
131
153
[ cmp , GOOGOL , '3' , GOOGOL , '3' , 0 ] ,
132
154
[ cmp , GOOGOL , GOOGOL , GOOGOL , GOOGOL , 0 ] ,
133
155
156
+ [ simplify , GOOGOL , GOOGOL , '1/1' ] ,
157
+ [ simplify , '7' , '14' , '1/2' ] ,
158
+ [ simplify , '-7' , '14' , '-1/2' ] ,
159
+ [ simplify , '170141183460469231731687303715884105729' , '56713727820156410577229101238628035243' , '3/1' ] ,
160
+ [ simplify , '56713727820156410577229101238628035243' , '170141183460469231731687303715884105729' , '1/3' ] ,
161
+ [ simplify , '170141183460469231731687303715884105729' , '3' , '56713727820156410577229101238628035243/1' ] ,
162
+ [ simplify , '3' , '170141183460469231731687303715884105729' , '1/56713727820156410577229101238628035243' ] ,
163
+ [ simplify , '-170141183460469231731687303715884105729' , '56713727820156410577229101238628035243' , '-3/1' ] ,
164
+ [ simplify , '-56713727820156410577229101238628035243' , '170141183460469231731687303715884105729' , '-1/3' ] ,
165
+ [ simplify , '-170141183460469231731687303715884105729' , '3' , '-56713727820156410577229101238628035243/1' ] ,
166
+ [ simplify , '-3' , '170141183460469231731687303715884105729' , '-1/56713727820156410577229101238628035243' ] ,
167
+
134
168
] ;
135
169
136
170
for ( const alu of ALU )
137
- for ( const [ [ name , symbol , implementations ] , ...params ] of PARAMS )
138
- for ( const factory of implementations )
139
- test ( macro , alu , [ [ name , symbol , factory ] , ...params ] ) ;
171
+ for ( const [ [ name , symbol , implementations , macro , predicate ] , ...params ] of PARAMS )
172
+ if ( ! predicate || predicate ( alu ) ) {
173
+ for ( const factory of implementations )
174
+ test ( macro , alu , [ [ name , symbol , factory ] , ...params ] ) ;
175
+ }
0 commit comments