mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-07-19 12:24:59 +08:00
Avoid using toBeTruthy() and toBeFalsy() because of type coercion.
This commit is contained in:
@ -2,22 +2,22 @@ import isPowerOfTwo from '../isPowerOfTwo';
|
||||
|
||||
describe('isPowerOfTwo', () => {
|
||||
it('should check if the number is made by multiplying twos', () => {
|
||||
expect(isPowerOfTwo(-1)).toBeFalsy();
|
||||
expect(isPowerOfTwo(0)).toBeFalsy();
|
||||
expect(isPowerOfTwo(1)).toBeTruthy();
|
||||
expect(isPowerOfTwo(2)).toBeTruthy();
|
||||
expect(isPowerOfTwo(3)).toBeFalsy();
|
||||
expect(isPowerOfTwo(4)).toBeTruthy();
|
||||
expect(isPowerOfTwo(5)).toBeFalsy();
|
||||
expect(isPowerOfTwo(6)).toBeFalsy();
|
||||
expect(isPowerOfTwo(7)).toBeFalsy();
|
||||
expect(isPowerOfTwo(8)).toBeTruthy();
|
||||
expect(isPowerOfTwo(10)).toBeFalsy();
|
||||
expect(isPowerOfTwo(12)).toBeFalsy();
|
||||
expect(isPowerOfTwo(16)).toBeTruthy();
|
||||
expect(isPowerOfTwo(31)).toBeFalsy();
|
||||
expect(isPowerOfTwo(64)).toBeTruthy();
|
||||
expect(isPowerOfTwo(1024)).toBeTruthy();
|
||||
expect(isPowerOfTwo(1023)).toBeFalsy();
|
||||
expect(isPowerOfTwo(-1)).toBe(false);
|
||||
expect(isPowerOfTwo(0)).toBe(false);
|
||||
expect(isPowerOfTwo(1)).toBe(true);
|
||||
expect(isPowerOfTwo(2)).toBe(true);
|
||||
expect(isPowerOfTwo(3)).toBe(false);
|
||||
expect(isPowerOfTwo(4)).toBe(true);
|
||||
expect(isPowerOfTwo(5)).toBe(false);
|
||||
expect(isPowerOfTwo(6)).toBe(false);
|
||||
expect(isPowerOfTwo(7)).toBe(false);
|
||||
expect(isPowerOfTwo(8)).toBe(true);
|
||||
expect(isPowerOfTwo(10)).toBe(false);
|
||||
expect(isPowerOfTwo(12)).toBe(false);
|
||||
expect(isPowerOfTwo(16)).toBe(true);
|
||||
expect(isPowerOfTwo(31)).toBe(false);
|
||||
expect(isPowerOfTwo(64)).toBe(true);
|
||||
expect(isPowerOfTwo(1024)).toBe(true);
|
||||
expect(isPowerOfTwo(1023)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
@ -2,22 +2,22 @@ import isPowerOfTwoBitwise from '../isPowerOfTwoBitwise';
|
||||
|
||||
describe('isPowerOfTwoBitwise', () => {
|
||||
it('should check if the number is made by multiplying twos', () => {
|
||||
expect(isPowerOfTwoBitwise(-1)).toBeFalsy();
|
||||
expect(isPowerOfTwoBitwise(0)).toBeFalsy();
|
||||
expect(isPowerOfTwoBitwise(1)).toBeTruthy();
|
||||
expect(isPowerOfTwoBitwise(2)).toBeTruthy();
|
||||
expect(isPowerOfTwoBitwise(3)).toBeFalsy();
|
||||
expect(isPowerOfTwoBitwise(4)).toBeTruthy();
|
||||
expect(isPowerOfTwoBitwise(5)).toBeFalsy();
|
||||
expect(isPowerOfTwoBitwise(6)).toBeFalsy();
|
||||
expect(isPowerOfTwoBitwise(7)).toBeFalsy();
|
||||
expect(isPowerOfTwoBitwise(8)).toBeTruthy();
|
||||
expect(isPowerOfTwoBitwise(10)).toBeFalsy();
|
||||
expect(isPowerOfTwoBitwise(12)).toBeFalsy();
|
||||
expect(isPowerOfTwoBitwise(16)).toBeTruthy();
|
||||
expect(isPowerOfTwoBitwise(31)).toBeFalsy();
|
||||
expect(isPowerOfTwoBitwise(64)).toBeTruthy();
|
||||
expect(isPowerOfTwoBitwise(1024)).toBeTruthy();
|
||||
expect(isPowerOfTwoBitwise(1023)).toBeFalsy();
|
||||
expect(isPowerOfTwoBitwise(-1)).toBe(false);
|
||||
expect(isPowerOfTwoBitwise(0)).toBe(false);
|
||||
expect(isPowerOfTwoBitwise(1)).toBe(true);
|
||||
expect(isPowerOfTwoBitwise(2)).toBe(true);
|
||||
expect(isPowerOfTwoBitwise(3)).toBe(false);
|
||||
expect(isPowerOfTwoBitwise(4)).toBe(true);
|
||||
expect(isPowerOfTwoBitwise(5)).toBe(false);
|
||||
expect(isPowerOfTwoBitwise(6)).toBe(false);
|
||||
expect(isPowerOfTwoBitwise(7)).toBe(false);
|
||||
expect(isPowerOfTwoBitwise(8)).toBe(true);
|
||||
expect(isPowerOfTwoBitwise(10)).toBe(false);
|
||||
expect(isPowerOfTwoBitwise(12)).toBe(false);
|
||||
expect(isPowerOfTwoBitwise(16)).toBe(true);
|
||||
expect(isPowerOfTwoBitwise(31)).toBe(false);
|
||||
expect(isPowerOfTwoBitwise(64)).toBe(true);
|
||||
expect(isPowerOfTwoBitwise(1024)).toBe(true);
|
||||
expect(isPowerOfTwoBitwise(1023)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
@ -4,30 +4,30 @@ import trialDivision from '../trialDivision';
|
||||
* @param {function(n: number)} testFunction
|
||||
*/
|
||||
function primalityTest(testFunction) {
|
||||
expect(testFunction(1)).toBeFalsy();
|
||||
expect(testFunction(2)).toBeTruthy();
|
||||
expect(testFunction(3)).toBeTruthy();
|
||||
expect(testFunction(5)).toBeTruthy();
|
||||
expect(testFunction(11)).toBeTruthy();
|
||||
expect(testFunction(191)).toBeTruthy();
|
||||
expect(testFunction(191)).toBeTruthy();
|
||||
expect(testFunction(199)).toBeTruthy();
|
||||
expect(testFunction(1)).toBe(false);
|
||||
expect(testFunction(2)).toBe(true);
|
||||
expect(testFunction(3)).toBe(true);
|
||||
expect(testFunction(5)).toBe(true);
|
||||
expect(testFunction(11)).toBe(true);
|
||||
expect(testFunction(191)).toBe(true);
|
||||
expect(testFunction(191)).toBe(true);
|
||||
expect(testFunction(199)).toBe(true);
|
||||
|
||||
expect(testFunction(-1)).toBeFalsy();
|
||||
expect(testFunction(0)).toBeFalsy();
|
||||
expect(testFunction(4)).toBeFalsy();
|
||||
expect(testFunction(6)).toBeFalsy();
|
||||
expect(testFunction(12)).toBeFalsy();
|
||||
expect(testFunction(14)).toBeFalsy();
|
||||
expect(testFunction(25)).toBeFalsy();
|
||||
expect(testFunction(192)).toBeFalsy();
|
||||
expect(testFunction(200)).toBeFalsy();
|
||||
expect(testFunction(400)).toBeFalsy();
|
||||
expect(testFunction(-1)).toBe(false);
|
||||
expect(testFunction(0)).toBe(false);
|
||||
expect(testFunction(4)).toBe(false);
|
||||
expect(testFunction(6)).toBe(false);
|
||||
expect(testFunction(12)).toBe(false);
|
||||
expect(testFunction(14)).toBe(false);
|
||||
expect(testFunction(25)).toBe(false);
|
||||
expect(testFunction(192)).toBe(false);
|
||||
expect(testFunction(200)).toBe(false);
|
||||
expect(testFunction(400)).toBe(false);
|
||||
|
||||
// It should also deal with floats.
|
||||
expect(testFunction(0.5)).toBeFalsy();
|
||||
expect(testFunction(1.3)).toBeFalsy();
|
||||
expect(testFunction(10.5)).toBeFalsy();
|
||||
expect(testFunction(0.5)).toBe(false);
|
||||
expect(testFunction(1.3)).toBe(false);
|
||||
expect(testFunction(10.5)).toBe(false);
|
||||
}
|
||||
|
||||
describe('trialDivision', () => {
|
||||
|
Reference in New Issue
Block a user