From c3734abe8ce311b9226fabf2ce4a7114d15accfe Mon Sep 17 00:00:00 2001 From: Lumi Pakkanen Date: Sat, 20 Apr 2024 11:10:06 +0300 Subject: [PATCH] Improve monzo conversion and prime limit test coverage --- src/__tests__/monzo.spec.ts | 72 +++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/__tests__/monzo.spec.ts b/src/__tests__/monzo.spec.ts index 3ca637b..a6bad7b 100644 --- a/src/__tests__/monzo.spec.ts +++ b/src/__tests__/monzo.spec.ts @@ -82,6 +82,54 @@ describe('Monzo converter', () => { BigInt(5) ** BigInt(monzo[2]) ).toBe(BigInt('360000000000000000000000')); }); + + it('works just below the int32 boundary', () => { + const monzo = toMonzo(2 ** 30); + expect(monzo).toEqual([30]); + }); + + it('works at the int32 boundary', () => { + const monzo = toMonzo(2 ** 31); + expect(monzo).toEqual([31]); + }); + + it('works just above the int32 boundary', () => { + const monzo = toMonzo(2 ** 32); + expect(monzo).toEqual([32]); + }); + + it('works just below the IEEE limit', () => { + const monzo = toMonzo(2n ** 1023n); + expect(monzo).toEqual([1023]); + }); + + it('works at the IEEE limit', () => { + const monzo = toMonzo(2n ** 1024n); + expect(monzo).toEqual([1024]); + }); + + it('works just above the IEEE limit', () => { + const monzo = toMonzo(2n ** 1025n); + expect(monzo).toEqual([1025]); + }); + + it('agrees with the reference implementation', () => { + for (let n = 1; n <= 1000; ++n) { + const monzo = toMonzo(n); + const bigMonzo = toMonzo(BigInt(n)); + let [reference, refResidual] = toMonzoAndResidual11(n); + reference = reference.slice(0, monzo.length); + expect(monzo.slice(0, 5)).toEqual(reference); + expect(bigMonzo.slice(0, 5)).toEqual(reference); + if (refResidual !== 1) { + expect(monzo.length).toBeGreaterThan(5); + expect(bigMonzo.length).toBeGreaterThan(5); + } else { + expect(monzo.length).toBeLessThanOrEqual(5); + expect(bigMonzo.length).toBeLessThanOrEqual(5); + } + } + }); }); describe('Fraction to monzo converter', () => { @@ -299,4 +347,28 @@ describe('Prime limit calculator', () => { const limit = primeLimit(BigInt('1561327220802586898249028')); expect(limit).toBe(19); }); + + it('works just below the int32 boundary', () => { + expect(primeLimit(2 ** 30)).toEqual(2); + }); + + it('works at the int32 boundary', () => { + expect(primeLimit(2 ** 31)).toEqual(2); + }); + + it('works just above the int32 boundary', () => { + expect(primeLimit(2 ** 32)).toEqual(2); + }); + + it('works just below the IEEE limit', () => { + expect(primeLimit(2n ** 1023n)).toEqual(2); + }); + + it('works at the IEEE limit', () => { + expect(primeLimit(2n ** 1024n)).toEqual(2); + }); + + it('works just above the IEEE limit', () => { + expect(primeLimit(2n ** 1025n)).toEqual(2); + }); });