mpzjs
Version:
Arbitrary-precision integer arithmetic using libgmp
60 lines (51 loc) • 2.1 kB
JavaScript
const MPZ = require('../');
test('mul', () => {
[-100, -10, -5, -1, 0, 1, 5, 10, 100].forEach(i => {
[-100, -10, -5, -1, 0, 1, 5, 10, 100].forEach(j => {
const js = j.toString();
const ks = (i * j).toString();
const result = MPZ();
expect(MPZ(i).mul(j).toString()).toBe(ks);
expect(MPZ(i).mul(js).toString()).toBe(ks);
expect(MPZ(i).mul(MPZ(j)).toString()).toBe(ks);
expect(MPZ(i).mul(BigInt(j)).toString()).toBe(ks);
MPZ.mul(result, i, j);
expect(result.toString()).toBe(ks);
MPZ.mul(result, i, js);
expect(result.toString()).toBe(ks);
MPZ.mul(result, MPZ(i), MPZ(j));
expect(result.toString()).toBe(ks);
});
});
expect(
MPZ(
'433593290010590489671135819286259593426549306666324008679782084292'
+ '2446494189019075159822930571858728009485237489829138626896756141'
+ '8738958337632249177044975686477011571044266'
).mul(
'127790264841901718791915669264129510947625523373763053776083279450'
+ '3886212911067061184379695097643279217271150419129022856601771338'
+ '794256383410400076210073482253089544155377'
).toString()
).toBe(
'5540900136412485758752141142221047463857522755277604708501015732755989'
+ '17659432099233635577634197309727815375309484297883528869192732141328'
+ '99346769031695550850320602049507618052164677667378189154076988316301'
+ '23719953859959804490669091769150047414629675184805332001182298088891'
+ '58079529848220802017396422115936618644438110463469902675126288489182'
+ '82'
);
expect(MPZ('10000000000000000000000000000').mul(-123).toString())
.toBe('-1230000000000000000000000000000');
});
test('mul exceptions', () => {
expect(() => {
MPZ.mul(1, 2);
}).toThrow();
expect(() => {
MPZ.mul(MPZ(1), MPZ(2));
}).toThrow();
expect(() => {
MPZ.mul(MPZ(1));
}).toThrow();
});