Refactored and added BigInts

This commit is contained in:
Ricardo Fernández Serrata 2022-06-11 10:16:58 -04:00 committed by GitHub
parent 3e338da514
commit 1ea5d556c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,33 +1,27 @@
import isPowerOfTwo from '../isPowerOfTwo'; import f from '../isPowerOfTwo';
describe('isPowerOfTwo', () => { describe('isPowerOfTwo', () => {
it('should check if the number is made by multiplying twos', () => { it('should check if the number is made by multiplying twos', () => {
expect(isPowerOfTwo(-8.5)).toBe(false); // test all pows until it gets as close as possible to `Number.MAX_VALUE`
expect(isPowerOfTwo(-1)).toBe(false); for (let i = 0; i <= 0x3ff; i++) {
expect(isPowerOfTwo(0)).toBe(false); expect( f(2 ** i) && f(1n << BigInt(i)) ).toBe(true)
expect(isPowerOfTwo(0.5)).toBe(false); }
expect(isPowerOfTwo(1)).toBe(true); expect(f(-8.5)).toBe(false);
expect(isPowerOfTwo(2)).toBe(true); expect(f(-1) || f(-1n)).toBe(false);
expect(isPowerOfTwo(Math.E)).toBe(false); expect(f(0) || f(0n)).toBe(false);
expect(isPowerOfTwo(3)).toBe(false); expect(f(0.5)).toBe(false);
expect(isPowerOfTwo(Math.PI)).toBe(false); expect(f(Math.E)).toBe(false);
expect(isPowerOfTwo(4)).toBe(true); expect(f(3) || f(3n)).toBe(false);
expect(isPowerOfTwo(5)).toBe(false); expect(f(Math.PI)).toBe(false);
expect(isPowerOfTwo(6)).toBe(false); expect(f(5) || f(5n)).toBe(false);
expect(isPowerOfTwo(7)).toBe(false); expect(f(6) || f(6n)).toBe(false);
expect(isPowerOfTwo(8)).toBe(true); expect(f(7) || f(7n)).toBe(false);
expect(isPowerOfTwo(10)).toBe(false); expect(f(10) || f(10n)).toBe(false);
expect(isPowerOfTwo(12)).toBe(false); expect(f(12) || f(12n)).toBe(false);
expect(isPowerOfTwo(16)).toBe(true); expect(f(31) || f(31n)).toBe(false);
expect(isPowerOfTwo(31)).toBe(false); expect(f(1023) || f(1023n)).toBe(false);
expect(isPowerOfTwo(64)).toBe(true); expect( f(2 ** 32 - 1) || f(~(-1n << 32n)) ).toBe(false);
expect(isPowerOfTwo(1023)).toBe(false); expect(f(Infinity) || f(-Infinity)).toBe(false);
expect(isPowerOfTwo(1024)).toBe(true); expect(f(NaN)).toBe(false);
expect(isPowerOfTwo(2 ** 32 - 1)).toBe(false);
expect(isPowerOfTwo(2 ** 32)).toBe(true);
expect(isPowerOfTwo(2 ** 64)).toBe(true);
expect(isPowerOfTwo(2 ** 69)).toBe(true); //nice, lol
expect(isPowerOfTwo(2 ** 0x3ff)).toBe(true); //closest to `Number.MAX_VALUE`
expect(isPowerOfTwo(Infinity)).toBe(false);
}); });
}); });