Skip to content

Commit

Permalink
Avoid using toBeTruthy() and toBeFalsy() because of type coercion.
Browse files Browse the repository at this point in the history
  • Loading branch information
trekhleb committed Jul 26, 2018
1 parent 8da83cd commit 39acb2b
Show file tree
Hide file tree
Showing 25 changed files with 367 additions and 367 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ describe('detectUndirectedCycleUsingDisjointSet', () => {
.addEdge(edgeBC)
.addEdge(edgeCD);

expect(detectUndirectedCycleUsingDisjointSet(graph)).toBeFalsy();
expect(detectUndirectedCycleUsingDisjointSet(graph)).toBe(false);

graph.addEdge(edgeDE);

expect(detectUndirectedCycleUsingDisjointSet(graph)).toBeTruthy();
expect(detectUndirectedCycleUsingDisjointSet(graph)).toBe(true);
});
});
34 changes: 17 additions & 17 deletions src/algorithms/math/is-power-of-two/__test__/isPowerOfTwo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
42 changes: 21 additions & 21 deletions src/algorithms/math/primality-test/__test__/trialDivision.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@ import regularExpressionMatching from '../regularExpressionMatching';

describe('regularExpressionMatching', () => {
it('should match regular expressions in a string', () => {
expect(regularExpressionMatching('', '')).toBeTruthy();
expect(regularExpressionMatching('a', 'a')).toBeTruthy();
expect(regularExpressionMatching('aa', 'aa')).toBeTruthy();
expect(regularExpressionMatching('aab', 'aab')).toBeTruthy();
expect(regularExpressionMatching('aab', 'aa.')).toBeTruthy();
expect(regularExpressionMatching('aab', '.a.')).toBeTruthy();
expect(regularExpressionMatching('aab', '...')).toBeTruthy();
expect(regularExpressionMatching('a', 'a*')).toBeTruthy();
expect(regularExpressionMatching('aaa', 'a*')).toBeTruthy();
expect(regularExpressionMatching('aaab', 'a*b')).toBeTruthy();
expect(regularExpressionMatching('aaabb', 'a*b*')).toBeTruthy();
expect(regularExpressionMatching('aaabb', 'a*b*c*')).toBeTruthy();
expect(regularExpressionMatching('', 'a*')).toBeTruthy();
expect(regularExpressionMatching('xaabyc', 'xa*b.c')).toBeTruthy();
expect(regularExpressionMatching('aab', 'c*a*b*')).toBeTruthy();
expect(regularExpressionMatching('mississippi', 'mis*is*.p*.')).toBeTruthy();
expect(regularExpressionMatching('ab', '.*')).toBeTruthy();
expect(regularExpressionMatching('', '')).toBe(true);
expect(regularExpressionMatching('a', 'a')).toBe(true);
expect(regularExpressionMatching('aa', 'aa')).toBe(true);
expect(regularExpressionMatching('aab', 'aab')).toBe(true);
expect(regularExpressionMatching('aab', 'aa.')).toBe(true);
expect(regularExpressionMatching('aab', '.a.')).toBe(true);
expect(regularExpressionMatching('aab', '...')).toBe(true);
expect(regularExpressionMatching('a', 'a*')).toBe(true);
expect(regularExpressionMatching('aaa', 'a*')).toBe(true);
expect(regularExpressionMatching('aaab', 'a*b')).toBe(true);
expect(regularExpressionMatching('aaabb', 'a*b*')).toBe(true);
expect(regularExpressionMatching('aaabb', 'a*b*c*')).toBe(true);
expect(regularExpressionMatching('', 'a*')).toBe(true);
expect(regularExpressionMatching('xaabyc', 'xa*b.c')).toBe(true);
expect(regularExpressionMatching('aab', 'c*a*b*')).toBe(true);
expect(regularExpressionMatching('mississippi', 'mis*is*.p*.')).toBe(true);
expect(regularExpressionMatching('ab', '.*')).toBe(true);

expect(regularExpressionMatching('', 'a')).toBeFalsy();
expect(regularExpressionMatching('a', '')).toBeFalsy();
expect(regularExpressionMatching('aab', 'aa')).toBeFalsy();
expect(regularExpressionMatching('aab', 'baa')).toBeFalsy();
expect(regularExpressionMatching('aabc', '...')).toBeFalsy();
expect(regularExpressionMatching('aaabbdd', 'a*b*c*')).toBeFalsy();
expect(regularExpressionMatching('mississippi', 'mis*is*p*.')).toBeFalsy();
expect(regularExpressionMatching('ab', 'a*')).toBeFalsy();
expect(regularExpressionMatching('abba', 'a*b*.c')).toBeFalsy();
expect(regularExpressionMatching('abba', '.*c')).toBeFalsy();
expect(regularExpressionMatching('', 'a')).toBe(false);
expect(regularExpressionMatching('a', '')).toBe(false);
expect(regularExpressionMatching('aab', 'aa')).toBe(false);
expect(regularExpressionMatching('aab', 'baa')).toBe(false);
expect(regularExpressionMatching('aabc', '...')).toBe(false);
expect(regularExpressionMatching('aaabbdd', 'a*b*c*')).toBe(false);
expect(regularExpressionMatching('mississippi', 'mis*is*p*.')).toBe(false);
expect(regularExpressionMatching('ab', 'a*')).toBe(false);
expect(regularExpressionMatching('abba', 'a*b*.c')).toBe(false);
expect(regularExpressionMatching('abba', '.*c')).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import backtrackingJumpGame from '../backtrackingJumpGame';

describe('backtrackingJumpGame', () => {
it('should solve Jump Game problem in backtracking manner', () => {
expect(backtrackingJumpGame([1, 0])).toBeTruthy();
expect(backtrackingJumpGame([100, 0])).toBeTruthy();
expect(backtrackingJumpGame([2, 3, 1, 1, 4])).toBeTruthy();
expect(backtrackingJumpGame([1, 1, 1, 1, 1])).toBeTruthy();
expect(backtrackingJumpGame([1, 1, 1, 10, 1])).toBeTruthy();
expect(backtrackingJumpGame([1, 5, 2, 1, 0, 2, 0])).toBeTruthy();
expect(backtrackingJumpGame([1, 0])).toBe(true);
expect(backtrackingJumpGame([100, 0])).toBe(true);
expect(backtrackingJumpGame([2, 3, 1, 1, 4])).toBe(true);
expect(backtrackingJumpGame([1, 1, 1, 1, 1])).toBe(true);
expect(backtrackingJumpGame([1, 1, 1, 10, 1])).toBe(true);
expect(backtrackingJumpGame([1, 5, 2, 1, 0, 2, 0])).toBe(true);

expect(backtrackingJumpGame([1, 0, 1])).toBeFalsy();
expect(backtrackingJumpGame([3, 2, 1, 0, 4])).toBeFalsy();
expect(backtrackingJumpGame([0, 0, 0, 0, 0])).toBeFalsy();
expect(backtrackingJumpGame([5, 4, 3, 2, 1, 0, 0])).toBeFalsy();
expect(backtrackingJumpGame([1, 0, 1])).toBe(false);
expect(backtrackingJumpGame([3, 2, 1, 0, 4])).toBe(false);
expect(backtrackingJumpGame([0, 0, 0, 0, 0])).toBe(false);
expect(backtrackingJumpGame([5, 4, 3, 2, 1, 0, 0])).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import dpBottomUpJumpGame from '../dpBottomUpJumpGame';

describe('dpBottomUpJumpGame', () => {
it('should solve Jump Game problem in bottom-up dynamic programming manner', () => {
expect(dpBottomUpJumpGame([1, 0])).toBeTruthy();
expect(dpBottomUpJumpGame([100, 0])).toBeTruthy();
expect(dpBottomUpJumpGame([2, 3, 1, 1, 4])).toBeTruthy();
expect(dpBottomUpJumpGame([1, 1, 1, 1, 1])).toBeTruthy();
expect(dpBottomUpJumpGame([1, 1, 1, 10, 1])).toBeTruthy();
expect(dpBottomUpJumpGame([1, 5, 2, 1, 0, 2, 0])).toBeTruthy();
expect(dpBottomUpJumpGame([1, 0])).toBe(true);
expect(dpBottomUpJumpGame([100, 0])).toBe(true);
expect(dpBottomUpJumpGame([2, 3, 1, 1, 4])).toBe(true);
expect(dpBottomUpJumpGame([1, 1, 1, 1, 1])).toBe(true);
expect(dpBottomUpJumpGame([1, 1, 1, 10, 1])).toBe(true);
expect(dpBottomUpJumpGame([1, 5, 2, 1, 0, 2, 0])).toBe(true);

expect(dpBottomUpJumpGame([1, 0, 1])).toBeFalsy();
expect(dpBottomUpJumpGame([3, 2, 1, 0, 4])).toBeFalsy();
expect(dpBottomUpJumpGame([0, 0, 0, 0, 0])).toBeFalsy();
expect(dpBottomUpJumpGame([5, 4, 3, 2, 1, 0, 0])).toBeFalsy();
expect(dpBottomUpJumpGame([1, 0, 1])).toBe(false);
expect(dpBottomUpJumpGame([3, 2, 1, 0, 4])).toBe(false);
expect(dpBottomUpJumpGame([0, 0, 0, 0, 0])).toBe(false);
expect(dpBottomUpJumpGame([5, 4, 3, 2, 1, 0, 0])).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import dpTopDownJumpGame from '../dpTopDownJumpGame';

describe('dpTopDownJumpGame', () => {
it('should solve Jump Game problem in top-down dynamic programming manner', () => {
expect(dpTopDownJumpGame([1, 0])).toBeTruthy();
expect(dpTopDownJumpGame([100, 0])).toBeTruthy();
expect(dpTopDownJumpGame([2, 3, 1, 1, 4])).toBeTruthy();
expect(dpTopDownJumpGame([1, 1, 1, 1, 1])).toBeTruthy();
expect(dpTopDownJumpGame([1, 1, 1, 10, 1])).toBeTruthy();
expect(dpTopDownJumpGame([1, 5, 2, 1, 0, 2, 0])).toBeTruthy();
expect(dpTopDownJumpGame([1, 0])).toBe(true);
expect(dpTopDownJumpGame([100, 0])).toBe(true);
expect(dpTopDownJumpGame([2, 3, 1, 1, 4])).toBe(true);
expect(dpTopDownJumpGame([1, 1, 1, 1, 1])).toBe(true);
expect(dpTopDownJumpGame([1, 1, 1, 10, 1])).toBe(true);
expect(dpTopDownJumpGame([1, 5, 2, 1, 0, 2, 0])).toBe(true);

expect(dpTopDownJumpGame([1, 0, 1])).toBeFalsy();
expect(dpTopDownJumpGame([3, 2, 1, 0, 4])).toBeFalsy();
expect(dpTopDownJumpGame([0, 0, 0, 0, 0])).toBeFalsy();
expect(dpTopDownJumpGame([5, 4, 3, 2, 1, 0, 0])).toBeFalsy();
expect(dpTopDownJumpGame([1, 0, 1])).toBe(false);
expect(dpTopDownJumpGame([3, 2, 1, 0, 4])).toBe(false);
expect(dpTopDownJumpGame([0, 0, 0, 0, 0])).toBe(false);
expect(dpTopDownJumpGame([5, 4, 3, 2, 1, 0, 0])).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import greedyJumpGame from '../greedyJumpGame';

describe('greedyJumpGame', () => {
it('should solve Jump Game problem in greedy manner', () => {
expect(greedyJumpGame([1, 0])).toBeTruthy();
expect(greedyJumpGame([100, 0])).toBeTruthy();
expect(greedyJumpGame([2, 3, 1, 1, 4])).toBeTruthy();
expect(greedyJumpGame([1, 1, 1, 1, 1])).toBeTruthy();
expect(greedyJumpGame([1, 1, 1, 10, 1])).toBeTruthy();
expect(greedyJumpGame([1, 5, 2, 1, 0, 2, 0])).toBeTruthy();
expect(greedyJumpGame([1, 0])).toBe(true);
expect(greedyJumpGame([100, 0])).toBe(true);
expect(greedyJumpGame([2, 3, 1, 1, 4])).toBe(true);
expect(greedyJumpGame([1, 1, 1, 1, 1])).toBe(true);
expect(greedyJumpGame([1, 1, 1, 10, 1])).toBe(true);
expect(greedyJumpGame([1, 5, 2, 1, 0, 2, 0])).toBe(true);

expect(greedyJumpGame([1, 0, 1])).toBeFalsy();
expect(greedyJumpGame([3, 2, 1, 0, 4])).toBeFalsy();
expect(greedyJumpGame([0, 0, 0, 0, 0])).toBeFalsy();
expect(greedyJumpGame([5, 4, 3, 2, 1, 0, 0])).toBeFalsy();
expect(greedyJumpGame([1, 0, 1])).toBe(false);
expect(greedyJumpGame([3, 2, 1, 0, 4])).toBe(false);
expect(greedyJumpGame([0, 0, 0, 0, 0])).toBe(false);
expect(greedyJumpGame([5, 4, 3, 2, 1, 0, 0])).toBe(false);
});
});
8 changes: 4 additions & 4 deletions src/data-structures/bloom-filter/__test__/BloomFilter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ describe('BloomFilter', () => {
it('should insert strings correctly and return true when checking for inserted values', () => {
people.forEach(person => bloomFilter.insert(person));

expect(bloomFilter.mayContain('Bruce Wayne')).toBeTruthy();
expect(bloomFilter.mayContain('Clark Kent')).toBeTruthy();
expect(bloomFilter.mayContain('Barry Allen')).toBeTruthy();
expect(bloomFilter.mayContain('Bruce Wayne')).toBe(true);
expect(bloomFilter.mayContain('Clark Kent')).toBe(true);
expect(bloomFilter.mayContain('Barry Allen')).toBe(true);

expect(bloomFilter.mayContain('Tony Stark')).toBeFalsy();
expect(bloomFilter.mayContain('Tony Stark')).toBe(false);
});
});
Loading

0 comments on commit 39acb2b

Please sign in to comment.