Skip to content

Commit 1b5bd9a

Browse files
committed
tests
1 parent d52cc2f commit 1b5bd9a

File tree

11 files changed

+139
-42
lines changed

11 files changed

+139
-42
lines changed

assembly/__benches__/add.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@ import { BigInt } from 'as-bigint/assembly/BigInt';
33

44
const a = '0xFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAED';
55
const b = '0xDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF';
6-
const mpzA = blackbox(MpZ.from(a));
7-
const mpzB = blackbox(MpZ.from(b));
6+
const mpzA = MpZ.from(a);
7+
const mpzB = MpZ.from(b);
88

9-
const bigIntA = blackbox(BigInt.from(a));
10-
const bigIntB = blackbox(BigInt.from(b));
9+
const bigIntA = BigInt.from(a);
10+
const bigIntB = BigInt.from(b);
1111

12-
suite('add large', () => {
13-
bench('MpZ#__uadd2 (large)', () => {
14-
// @ts-ignore
15-
blackbox(mpzA.__uadd2(mpzB));
16-
});
12+
assert(`${mpzA.add(mpzB)}` === `${bigIntA.add(bigIntB)}`);
1713

14+
suite('add large', () => {
1815
bench('MpZ#__uadd (large)', () => {
1916
// @ts-ignore
2017
blackbox(mpzA.__uadd(mpzB));

assembly/__benches__/div.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { BigInt } from 'as-bigint/assembly/BigInt';
33

44
const a = '0xFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAED';
55
const b = '0x0000DEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEAD';
6-
const mpzA = blackbox(MpZ.from(a));
7-
const mpzB = blackbox(MpZ.from(b));
6+
const mpzA = MpZ.from(a);
7+
const mpzB = MpZ.from(b);
88

9-
const bigIntA = blackbox(BigInt.from(a));
10-
const bigIntB = blackbox(BigInt.from(b));
9+
const bigIntA = BigInt.from(a);
10+
const bigIntB = BigInt.from(b);
11+
12+
assert(`${mpzA.div(mpzB)}` === `${bigIntA.div(bigIntB)}`);
1113

1214
suite('div large', () => {
1315
bench('MpZ#_udiv (large)', () => {
@@ -25,8 +27,10 @@ suite('div large', () => {
2527
});
2628

2729
const c = '0xDEAD';
28-
const mpzC = blackbox(MpZ.from(c));
29-
const bigIntC = blackbox(BigInt.from(c));
30+
const mpzC = MpZ.from(c);
31+
const bigIntC = BigInt.from(c);
32+
33+
assert(`${mpzA.div(mpzC)}` === `${bigIntA.div(bigIntC)}`);
3034

3135
suite('div small', () => {
3236
bench('MpZ#_udiv (small)', () => {
@@ -42,3 +46,30 @@ suite('div small', () => {
4246
blackbox(bigIntA.div(bigIntC));
4347
});
4448
});
49+
50+
function fact<T>(one: T, n: T): T {
51+
let a = one;
52+
while (n > one) {
53+
a *= n;
54+
n -= one;
55+
}
56+
return a;
57+
}
58+
59+
const f1 = fact<MpZ>(MpZ.from(1), MpZ.from(1000));
60+
const f2 = fact<MpZ>(MpZ.from(1), MpZ.from(500));
61+
62+
const f3 = fact(BigInt.from(1), BigInt.from(1000));
63+
const f4 = fact(BigInt.from(1), BigInt.from(500));
64+
65+
assert(`${f3 / f4}` === `${f1 / f2}`);
66+
67+
suite('mul very large', () => {
68+
bench('MpZ#mul (very large)', () => {
69+
blackbox(f1.mul(f2));
70+
});
71+
72+
bench('BigInt#mul (very large)', () => {
73+
blackbox(f3.mul(f4));
74+
});
75+
});

assembly/__benches__/mod.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { BigInt } from 'as-bigint/assembly/BigInt';
44
const a = '542101086242752217003726400434970855712890625'; // 5^4^3
55
const m = '100000000000000000000';
66

7-
const mpzA = blackbox(MpZ.from(a));
8-
const mpzM = blackbox(MpZ.from(m));
7+
const mpzA = MpZ.from(a);
8+
const mpzM = MpZ.from(m);
99

10-
const bigIntA = blackbox(BigInt.from(a));
11-
const bigIntM = blackbox(BigInt.from(m));
10+
const bigIntA = BigInt.from(a);
11+
const bigIntM = BigInt.from(m);
1212

1313
suite('mod large', () => {
1414
bench('MpZ#rem (large)', () => {
@@ -25,8 +25,8 @@ suite('mod large', () => {
2525
});
2626

2727
const c = '100';
28-
const mpzC = blackbox(MpZ.from(c));
29-
const bigIntC = blackbox(BigInt.from(c));
28+
const mpzC = MpZ.from(c);
29+
const bigIntC = BigInt.from(c);
3030

3131
suite('mod (small)', () => {
3232
bench('MpZ#rem (small)', () => {

assembly/__benches__/mul.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ import { BigInt } from 'as-bigint/assembly/BigInt';
33

44
const a = '0xFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAED';
55
const b = '0xDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF';
6-
const mpzA = blackbox(MpZ.from(a));
7-
const mpzB = blackbox(MpZ.from(b));
86

9-
const bigIntA = blackbox(BigInt.from(a));
10-
const bigIntB = blackbox(BigInt.from(b));
7+
const mpzA = MpZ.from(a);
8+
const mpzB = MpZ.from(b);
9+
10+
const bigIntA = BigInt.from(a);
11+
const bigIntB = BigInt.from(b);
12+
13+
assert(`${mpzA.mul(mpzB)}` === `${bigIntA.mul(bigIntB)}`);
1114

1215
suite('mul large', () => {
1316
bench('MpZ#mul (large)', () => {
@@ -20,8 +23,10 @@ suite('mul large', () => {
2023
});
2124

2225
const c = '0xDEAD';
23-
const mpzC = blackbox(MpZ.from(c));
24-
const bigIntC = blackbox(BigInt.from(c));
26+
const mpzC = MpZ.from(c);
27+
const bigIntC = BigInt.from(c);
28+
29+
assert(`${mpzA.mul(mpzC)}` === `${bigIntA.mul(bigIntC)}`);
2530

2631
suite('mul small', () => {
2732
bench('MpZ#mul (small)', () => {
@@ -32,3 +37,30 @@ suite('mul small', () => {
3237
blackbox(bigIntA.mul(bigIntC));
3338
});
3439
});
40+
41+
function fact<T>(one: T, n: T): T {
42+
let a = one;
43+
while (n > one) {
44+
a *= n;
45+
n -= one;
46+
}
47+
return a;
48+
}
49+
50+
const f1 = fact<MpZ>(MpZ.from(1), MpZ.from(1000));
51+
const f2 = fact<MpZ>(MpZ.from(1), MpZ.from(500));
52+
53+
const f3 = fact(BigInt.from(1), BigInt.from(1000));
54+
const f4 = fact(BigInt.from(1), BigInt.from(500));
55+
56+
assert(`${f3 * f4}` === `${f1 * f2}`);
57+
58+
suite('mul very large', () => {
59+
bench('MpZ#mul (very large)', () => {
60+
blackbox(f1.mul(f2));
61+
});
62+
63+
bench('BigInt#mul (very large)', () => {
64+
blackbox(f3.mul(f4));
65+
});
66+
});

assembly/__benches__/pow.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { BigInt } from 'as-bigint/assembly/BigInt';
44
const a = 5;
55
const b = 3 ** 2;
66

7-
const mpzA = blackbox(MpZ.from(a));
8-
const mpzB = blackbox(MpZ.from(b));
9-
const bigIntA = blackbox(BigInt.from(a));
7+
const mpzA = MpZ.from(a);
8+
const mpzB = MpZ.from(b);
9+
const bigIntA = BigInt.from(a);
1010

1111
suite('pow', () => {
1212
bench('MpZ#_upowU32', () => {

assembly/__benches__/sqr.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { BigInt } from 'as-bigint/assembly/BigInt';
33

44
const a =
55
'0xFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAED';
6-
const mpzA = blackbox(MpZ.from(a));
7-
const bigIntA = blackbox(BigInt.from(a));
6+
const mpzA = MpZ.from(a);
7+
const bigIntA = BigInt.from(a);
88

99
suite('sqr', () => {
1010
bench('MpZ#mul', () => {

assembly/__benches__/sub.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { BigInt } from 'as-bigint/assembly/BigInt';
33

44
const a = '0xFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAEDFEEBDAED';
55
const b = '0xDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF';
6-
const mpzA = blackbox(MpZ.from(a));
7-
const mpzB = blackbox(MpZ.from(b));
6+
const mpzA = MpZ.from(a);
7+
const mpzB = MpZ.from(b);
88

9-
const bigIntA = blackbox(BigInt.from(a));
10-
const bigIntB = blackbox(BigInt.from(b));
9+
const bigIntA = BigInt.from(a);
10+
const bigIntB = BigInt.from(b);
1111

1212
suite('sub large', () => {
1313
bench('MpZ#__usub (large)', () => {
@@ -25,8 +25,8 @@ suite('sub large', () => {
2525
});
2626

2727
const c = 0xdead;
28-
const mpzC = blackbox(MpZ.from(c));
29-
const bigIntC = blackbox(BigInt.from(c));
28+
const mpzC = MpZ.from(c);
29+
const bigIntC = BigInt.from(c);
3030

3131
suite('sub small', () => {
3232
bench('MpZ#_usubFromU32 (small)', () => {

assembly/__benches__/to-string.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { MpZ } from '..';
22
import { BigInt } from 'as-bigint/assembly/BigInt';
33

44
const s = '0xDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF';
5-
const mpz = blackbox(MpZ.from(s));
6-
const bigInt = blackbox(BigInt.from(s));
5+
const mpz = MpZ.from(s);
6+
const bigInt = BigInt.from(s);
7+
8+
assert(`${mpz}` === `${bigInt}`);
79

810
suite('to string', () => {
911
bench('MpZ#toHex', () => {
@@ -22,3 +24,35 @@ suite('to string', () => {
2224
blackbox(bigInt.toString(16));
2325
});
2426
});
27+
28+
function fact<T>(one: T, n: T): T {
29+
let a = one;
30+
while (n > one) {
31+
a *= n;
32+
n -= one;
33+
}
34+
return a;
35+
}
36+
37+
const f1 = fact<MpZ>(MpZ.from(1), MpZ.from(1001));
38+
const f2 = fact(BigInt.from(1), BigInt.from(1001));
39+
40+
assert(`${f1}` === `${f2}`);
41+
42+
suite('to string (large)', () => {
43+
bench('MpZ#toHex (large)', () => {
44+
blackbox(f1.toHex());
45+
});
46+
47+
bench('MpZ#toDecimal (large)', () => {
48+
blackbox(f1.toDecimal());
49+
});
50+
51+
// bench('BigInt#toString() (large)', () => {
52+
// blackbox(f2.toString());
53+
// });
54+
55+
// bench('BigInt#toString(16) (large)', () => {
56+
// blackbox(f2.toString(16));
57+
// });
58+
});

tests/bits.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ t.test('identities', t => {
162162
t.equal(to(mpz.not(mpz.not(X))), x); // ~~x = x
163163
t.equal(to(mpz.not(mpz.and(X, Y))), ~x | ~y); // ~(x & y) = ~x | ~y
164164
t.equal(to(mpz.not(mpz.or(X, Y))), ~x & ~y); // ~(x | y) = ~x & ~y
165+
// identity x^y == x|y &~ x&y
165166
})
166167
);
167168

tests/mod-rem.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ t.test('modulo', t => {
4343
t.end();
4444
});
4545

46+
// identity ((x/y))*y + x%y - x == 0
47+
4648
function mod(n, m) {
4749
return ((n % m) + m) % m;
4850
}

0 commit comments

Comments
 (0)