Avoid using toBeTruthy() and toBeFalsy() because of type coercion.

This commit is contained in:
Oleksii Trekhleb 2018-07-26 16:14:26 +03:00
parent 8da83cd9dc
commit 39acb2b65d
25 changed files with 367 additions and 367 deletions

View File

@ -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);
});
});

View File

@ -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);
});
});

View File

@ -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);
});
});

View File

@ -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', () => {

View File

@ -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);
});
});

View File

@ -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);
});
});

View File

@ -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);
});
});

View File

@ -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);
});
});

View File

@ -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);
});
});

View File

@ -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);
});
});

View File

@ -36,15 +36,15 @@ describe('DisjointSet', () => {
disjointSet.makeSet('C');
expect(disjointSet.inSameSet('A', 'B')).toBeFalsy();
expect(disjointSet.inSameSet('A', 'B')).toBe(false);
disjointSet.union('A', 'B');
expect(disjointSet.find('A')).toBe('A');
expect(disjointSet.find('B')).toBe('A');
expect(disjointSet.inSameSet('A', 'B')).toBeTruthy();
expect(disjointSet.inSameSet('B', 'A')).toBeTruthy();
expect(disjointSet.inSameSet('A', 'C')).toBeFalsy();
expect(disjointSet.inSameSet('A', 'B')).toBe(true);
expect(disjointSet.inSameSet('B', 'A')).toBe(true);
expect(disjointSet.inSameSet('A', 'C')).toBe(false);
disjointSet.union('A', 'A');
@ -54,9 +54,9 @@ describe('DisjointSet', () => {
expect(disjointSet.find('B')).toBe('A');
expect(disjointSet.find('C')).toBe('A');
expect(disjointSet.inSameSet('A', 'B')).toBeTruthy();
expect(disjointSet.inSameSet('B', 'C')).toBeTruthy();
expect(disjointSet.inSameSet('A', 'C')).toBeTruthy();
expect(disjointSet.inSameSet('A', 'B')).toBe(true);
expect(disjointSet.inSameSet('B', 'C')).toBe(true);
expect(disjointSet.inSameSet('A', 'C')).toBe(true);
disjointSet
.makeSet('E')
@ -71,13 +71,13 @@ describe('DisjointSet', () => {
.union('G', 'H')
.union('H', 'I');
expect(disjointSet.inSameSet('A', 'I')).toBeFalsy();
expect(disjointSet.inSameSet('E', 'I')).toBeTruthy();
expect(disjointSet.inSameSet('A', 'I')).toBe(false);
expect(disjointSet.inSameSet('E', 'I')).toBe(true);
disjointSet.union('I', 'C');
expect(disjointSet.find('I')).toBe('E');
expect(disjointSet.inSameSet('A', 'I')).toBeTruthy();
expect(disjointSet.inSameSet('A', 'I')).toBe(true);
});
it('should union smaller set with bigger one making bigger one to be new root', () => {
@ -117,15 +117,15 @@ describe('DisjointSet', () => {
disjointSet.makeSet(itemC);
expect(disjointSet.inSameSet(itemA, itemB)).toBeFalsy();
expect(disjointSet.inSameSet(itemA, itemB)).toBe(false);
disjointSet.union(itemA, itemB);
expect(disjointSet.find(itemA)).toBe('A');
expect(disjointSet.find(itemB)).toBe('A');
expect(disjointSet.inSameSet(itemA, itemB)).toBeTruthy();
expect(disjointSet.inSameSet(itemB, itemA)).toBeTruthy();
expect(disjointSet.inSameSet(itemA, itemC)).toBeFalsy();
expect(disjointSet.inSameSet(itemA, itemB)).toBe(true);
expect(disjointSet.inSameSet(itemB, itemA)).toBe(true);
expect(disjointSet.inSameSet(itemA, itemC)).toBe(false);
disjointSet.union(itemA, itemC);
@ -133,8 +133,8 @@ describe('DisjointSet', () => {
expect(disjointSet.find(itemB)).toBe('A');
expect(disjointSet.find(itemC)).toBe('A');
expect(disjointSet.inSameSet(itemA, itemB)).toBeTruthy();
expect(disjointSet.inSameSet(itemB, itemC)).toBeTruthy();
expect(disjointSet.inSameSet(itemA, itemC)).toBeTruthy();
expect(disjointSet.inSameSet(itemA, itemB)).toBe(true);
expect(disjointSet.inSameSet(itemB, itemC)).toBe(true);
expect(disjointSet.inSameSet(itemA, itemC)).toBe(true);
});
});

View File

@ -11,8 +11,8 @@ describe('DisjointSetItem', () => {
expect(itemA.getChildren()).toEqual([]);
expect(itemA.getKey()).toBe('A');
expect(itemA.getRoot()).toEqual(itemA);
expect(itemA.isRoot()).toBeTruthy();
expect(itemB.isRoot()).toBeTruthy();
expect(itemA.isRoot()).toBe(true);
expect(itemB.isRoot()).toBe(true);
itemA.addChild(itemB);
itemD.setParent(itemC);
@ -38,17 +38,17 @@ describe('DisjointSetItem', () => {
expect(itemC.getRoot()).toEqual(itemC);
expect(itemD.getRoot()).toEqual(itemC);
expect(itemA.isRoot()).toBeTruthy();
expect(itemB.isRoot()).toBeFalsy();
expect(itemC.isRoot()).toBeTruthy();
expect(itemD.isRoot()).toBeFalsy();
expect(itemA.isRoot()).toBe(true);
expect(itemB.isRoot()).toBe(false);
expect(itemC.isRoot()).toBe(true);
expect(itemD.isRoot()).toBe(false);
itemA.addChild(itemC);
expect(itemA.isRoot()).toBeTruthy();
expect(itemB.isRoot()).toBeFalsy();
expect(itemC.isRoot()).toBeFalsy();
expect(itemD.isRoot()).toBeFalsy();
expect(itemA.isRoot()).toBe(true);
expect(itemB.isRoot()).toBe(false);
expect(itemC.isRoot()).toBe(false);
expect(itemD.isRoot()).toBe(false);
expect(itemA.getRank()).toEqual(3);
expect(itemB.getRank()).toEqual(0);
@ -69,8 +69,8 @@ describe('DisjointSetItem', () => {
expect(itemA.getChildren()).toEqual([]);
expect(itemA.getKey()).toBe('A');
expect(itemA.getRoot()).toEqual(itemA);
expect(itemA.isRoot()).toBeTruthy();
expect(itemB.isRoot()).toBeTruthy();
expect(itemA.isRoot()).toBe(true);
expect(itemB.isRoot()).toBe(true);
itemA.addChild(itemB);
itemD.setParent(itemC);
@ -96,17 +96,17 @@ describe('DisjointSetItem', () => {
expect(itemC.getRoot()).toEqual(itemC);
expect(itemD.getRoot()).toEqual(itemC);
expect(itemA.isRoot()).toBeTruthy();
expect(itemB.isRoot()).toBeFalsy();
expect(itemC.isRoot()).toBeTruthy();
expect(itemD.isRoot()).toBeFalsy();
expect(itemA.isRoot()).toBe(true);
expect(itemB.isRoot()).toBe(false);
expect(itemC.isRoot()).toBe(true);
expect(itemD.isRoot()).toBe(false);
itemA.addChild(itemC);
expect(itemA.isRoot()).toBeTruthy();
expect(itemB.isRoot()).toBeFalsy();
expect(itemC.isRoot()).toBeFalsy();
expect(itemD.isRoot()).toBeFalsy();
expect(itemA.isRoot()).toBe(true);
expect(itemB.isRoot()).toBe(false);
expect(itemC.isRoot()).toBe(false);
expect(itemD.isRoot()).toBe(false);
expect(itemA.getRank()).toEqual(3);
expect(itemB.getRank()).toEqual(0);

View File

@ -31,8 +31,8 @@ describe('GraphVertex', () => {
const edgeAB = new GraphEdge(vertexA, vertexB);
vertexA.addEdge(edgeAB);
expect(vertexA.hasEdge(edgeAB)).toBeTruthy();
expect(vertexB.hasEdge(edgeAB)).toBeFalsy();
expect(vertexA.hasEdge(edgeAB)).toBe(true);
expect(vertexB.hasEdge(edgeAB)).toBe(false);
expect(vertexA.getEdges().length).toBe(1);
expect(vertexA.getEdges()[0].toString()).toBe('A_B');
});
@ -48,11 +48,11 @@ describe('GraphVertex', () => {
.addEdge(edgeAB)
.addEdge(edgeAC);
expect(vertexA.hasEdge(edgeAB)).toBeTruthy();
expect(vertexB.hasEdge(edgeAB)).toBeFalsy();
expect(vertexA.hasEdge(edgeAB)).toBe(true);
expect(vertexB.hasEdge(edgeAB)).toBe(false);
expect(vertexA.hasEdge(edgeAC)).toBeTruthy();
expect(vertexC.hasEdge(edgeAC)).toBeFalsy();
expect(vertexA.hasEdge(edgeAC)).toBe(true);
expect(vertexC.hasEdge(edgeAC)).toBe(false);
expect(vertexA.getEdges().length).toBe(2);
@ -60,13 +60,13 @@ describe('GraphVertex', () => {
expect(vertexA.getEdges()[1].toString()).toBe('A_C');
vertexA.deleteEdge(edgeAB);
expect(vertexA.hasEdge(edgeAB)).toBeFalsy();
expect(vertexA.hasEdge(edgeAC)).toBeTruthy();
expect(vertexA.hasEdge(edgeAB)).toBe(false);
expect(vertexA.hasEdge(edgeAC)).toBe(true);
expect(vertexA.getEdges()[0].toString()).toBe('A_C');
vertexA.deleteEdge(edgeAC);
expect(vertexA.hasEdge(edgeAB)).toBeFalsy();
expect(vertexA.hasEdge(edgeAC)).toBeFalsy();
expect(vertexA.hasEdge(edgeAB)).toBe(false);
expect(vertexA.hasEdge(edgeAC)).toBe(false);
expect(vertexA.getEdges().length).toBe(0);
});
@ -81,21 +81,21 @@ describe('GraphVertex', () => {
.addEdge(edgeAB)
.addEdge(edgeAC);
expect(vertexA.hasEdge(edgeAB)).toBeTruthy();
expect(vertexB.hasEdge(edgeAB)).toBeFalsy();
expect(vertexA.hasEdge(edgeAB)).toBe(true);
expect(vertexB.hasEdge(edgeAB)).toBe(false);
expect(vertexA.hasEdge(edgeAC)).toBeTruthy();
expect(vertexC.hasEdge(edgeAC)).toBeFalsy();
expect(vertexA.hasEdge(edgeAC)).toBe(true);
expect(vertexC.hasEdge(edgeAC)).toBe(false);
expect(vertexA.getEdges().length).toBe(2);
vertexA.deleteAllEdges();
expect(vertexA.hasEdge(edgeAB)).toBeFalsy();
expect(vertexB.hasEdge(edgeAB)).toBeFalsy();
expect(vertexA.hasEdge(edgeAB)).toBe(false);
expect(vertexB.hasEdge(edgeAB)).toBe(false);
expect(vertexA.hasEdge(edgeAC)).toBeFalsy();
expect(vertexC.hasEdge(edgeAC)).toBeFalsy();
expect(vertexA.hasEdge(edgeAC)).toBe(false);
expect(vertexC.hasEdge(edgeAC)).toBe(false);
expect(vertexA.getEdges().length).toBe(0);
});
@ -148,8 +148,8 @@ describe('GraphVertex', () => {
const edgeAB = new GraphEdge(vertexA, vertexB);
vertexA.addEdge(edgeAB);
expect(vertexA.hasNeighbor(vertexB)).toBeTruthy();
expect(vertexA.hasNeighbor(vertexC)).toBeFalsy();
expect(vertexA.hasNeighbor(vertexB)).toBe(true);
expect(vertexA.hasNeighbor(vertexC)).toBe(false);
});
it('should edge by vertex', () => {

View File

@ -31,9 +31,9 @@ describe('HashTable', () => {
hashTable.set('c', 'earth');
hashTable.set('d', 'ocean');
expect(hashTable.has('x')).toBeFalsy();
expect(hashTable.has('b')).toBeTruthy();
expect(hashTable.has('c')).toBeTruthy();
expect(hashTable.has('x')).toBe(false);
expect(hashTable.has('b')).toBe(true);
expect(hashTable.has('c')).toBe(true);
const stringifier = value => `${value.key}:${value.value}`;
@ -77,13 +77,13 @@ describe('HashTable', () => {
hashTable.set('d', 'ocean');
expect(hashTable.getKeys()).toEqual(['a', 'b', 'c', 'd']);
expect(hashTable.has('a')).toBeTruthy();
expect(hashTable.has('x')).toBeFalsy();
expect(hashTable.has('a')).toBe(true);
expect(hashTable.has('x')).toBe(false);
hashTable.delete('a');
expect(hashTable.has('a')).toBeFalsy();
expect(hashTable.has('b')).toBeTruthy();
expect(hashTable.has('x')).toBeFalsy();
expect(hashTable.has('a')).toBe(false);
expect(hashTable.has('b')).toBe(true);
expect(hashTable.has('x')).toBe(false);
});
});

View File

@ -7,14 +7,14 @@ describe('MinHeap', () => {
expect(minHeap).toBeDefined();
expect(minHeap.peek()).toBeNull();
expect(minHeap.isEmpty()).toBeTruthy();
expect(minHeap.isEmpty()).toBe(true);
});
it('should add items to the heap and heapify it up', () => {
const minHeap = new MinHeap();
minHeap.add(5);
expect(minHeap.isEmpty()).toBeFalsy();
expect(minHeap.isEmpty()).toBe(false);
expect(minHeap.peek()).toBe(5);
expect(minHeap.toString()).toBe('5');

View File

@ -97,7 +97,7 @@ describe('PriorityQueue', () => {
priorityQueue.add(200, 0);
priorityQueue.add(15, 15);
expect(priorityQueue.hasValue(70)).toBeFalsy();
expect(priorityQueue.hasValue(15)).toBeTruthy();
expect(priorityQueue.hasValue(70)).toBe(false);
expect(priorityQueue.hasValue(15)).toBe(true);
});
});

View File

@ -44,11 +44,11 @@ describe('Queue', () => {
it('should check if queue is empty', () => {
const queue = new Queue();
expect(queue.isEmpty()).toBeTruthy();
expect(queue.isEmpty()).toBe(true);
queue.enqueue(1);
expect(queue.isEmpty()).toBeFalsy();
expect(queue.isEmpty()).toBe(false);
});
it('should dequeue from queue in FIFO order', () => {
@ -60,6 +60,6 @@ describe('Queue', () => {
expect(queue.dequeue()).toBe(1);
expect(queue.dequeue()).toBe(2);
expect(queue.dequeue()).toBeNull();
expect(queue.isEmpty()).toBeTruthy();
expect(queue.isEmpty()).toBe(true);
});
});

View File

@ -31,11 +31,11 @@ describe('Stack', () => {
it('should check if stack is empty', () => {
const stack = new Stack();
expect(stack.isEmpty()).toBeTruthy();
expect(stack.isEmpty()).toBe(true);
stack.push(1);
expect(stack.isEmpty()).toBeFalsy();
expect(stack.isEmpty()).toBe(false);
});
it('should pop data from stack', () => {
@ -47,7 +47,7 @@ describe('Stack', () => {
expect(stack.pop()).toBe(2);
expect(stack.pop()).toBe(1);
expect(stack.pop()).toBeNull();
expect(stack.isEmpty()).toBeTruthy();
expect(stack.isEmpty()).toBe(true);
});
it('should be possible to push/pop objects', () => {

View File

@ -63,13 +63,13 @@ describe('BinaryTreeNode', () => {
expect(rootNode.traverseInOrder()).toEqual([1, 2, 3]);
expect(rootNode.removeChild(rootNode.left)).toBeTruthy();
expect(rootNode.removeChild(rootNode.left)).toBe(true);
expect(rootNode.traverseInOrder()).toEqual([2, 3]);
expect(rootNode.removeChild(rootNode.right)).toBeTruthy();
expect(rootNode.removeChild(rootNode.right)).toBe(true);
expect(rootNode.traverseInOrder()).toEqual([2]);
expect(rootNode.removeChild(rootNode.right)).toBeFalsy();
expect(rootNode.removeChild(rootNode.right)).toBe(false);
expect(rootNode.traverseInOrder()).toEqual([2]);
});
@ -89,21 +89,21 @@ describe('BinaryTreeNode', () => {
expect(rootNode.traverseInOrder()).toEqual([1, 2, 3, 5]);
expect(rootNode.replaceChild(rootNode.right, rootNode.right.right)).toBeTruthy();
expect(rootNode.replaceChild(rootNode.right, rootNode.right.right)).toBe(true);
expect(rootNode.right.value).toBe(5);
expect(rootNode.right.right).toBeNull();
expect(rootNode.traverseInOrder()).toEqual([1, 2, 5]);
expect(rootNode.replaceChild(rootNode.right, rootNode.right.right)).toBeFalsy();
expect(rootNode.replaceChild(rootNode.right, rootNode.right.right)).toBe(false);
expect(rootNode.traverseInOrder()).toEqual([1, 2, 5]);
expect(rootNode.replaceChild(rootNode.right, replacementNode)).toBeTruthy();
expect(rootNode.replaceChild(rootNode.right, replacementNode)).toBe(true);
expect(rootNode.traverseInOrder()).toEqual([1, 2, 5]);
expect(rootNode.replaceChild(rootNode.left, replacementNode)).toBeTruthy();
expect(rootNode.replaceChild(rootNode.left, replacementNode)).toBe(true);
expect(rootNode.traverseInOrder()).toEqual([5, 2, 5]);
expect(rootNode.replaceChild(new BinaryTreeNode(), new BinaryTreeNode())).toBeFalsy();
expect(rootNode.replaceChild(new BinaryTreeNode(), new BinaryTreeNode())).toBe(false);
});
it('should calculate node height', () => {

View File

@ -30,8 +30,8 @@ describe('BinarySearchTree', () => {
bst.insert(20);
bst.insert(5);
expect(bst.contains(20)).toBeTruthy();
expect(bst.contains(40)).toBeFalsy();
expect(bst.contains(20)).toBe(true);
expect(bst.contains(40)).toBe(false);
});
it('should remove nodes', () => {
@ -45,11 +45,11 @@ describe('BinarySearchTree', () => {
const removed1 = bst.remove(5);
expect(bst.toString()).toBe('10,20');
expect(removed1).toBeTruthy();
expect(removed1).toBe(true);
const removed2 = bst.remove(20);
expect(bst.toString()).toBe('10');
expect(removed2).toBeTruthy();
expect(removed2).toBe(true);
});
it('should insert object values', () => {

View File

@ -24,33 +24,33 @@ describe('BinarySearchTreeNode', () => {
expect(insertedNode1.value).toBe(1);
expect(bstNode.toString()).toBe('1,2');
expect(bstNode.contains(1)).toBeTruthy();
expect(bstNode.contains(3)).toBeFalsy();
expect(bstNode.contains(1)).toBe(true);
expect(bstNode.contains(3)).toBe(false);
const insertedNode2 = bstNode.insert(3);
expect(insertedNode2.value).toBe(3);
expect(bstNode.toString()).toBe('1,2,3');
expect(bstNode.contains(3)).toBeTruthy();
expect(bstNode.contains(4)).toBeFalsy();
expect(bstNode.contains(3)).toBe(true);
expect(bstNode.contains(4)).toBe(false);
bstNode.insert(7);
expect(bstNode.toString()).toBe('1,2,3,7');
expect(bstNode.contains(7)).toBeTruthy();
expect(bstNode.contains(8)).toBeFalsy();
expect(bstNode.contains(7)).toBe(true);
expect(bstNode.contains(8)).toBe(false);
bstNode.insert(4);
expect(bstNode.toString()).toBe('1,2,3,4,7');
expect(bstNode.contains(4)).toBeTruthy();
expect(bstNode.contains(8)).toBeFalsy();
expect(bstNode.contains(4)).toBe(true);
expect(bstNode.contains(8)).toBe(false);
bstNode.insert(6);
expect(bstNode.toString()).toBe('1,2,3,4,6,7');
expect(bstNode.contains(6)).toBeTruthy();
expect(bstNode.contains(8)).toBeFalsy();
expect(bstNode.contains(6)).toBe(true);
expect(bstNode.contains(8)).toBe(false);
});
it('should not insert duplicates', () => {
@ -58,14 +58,14 @@ describe('BinarySearchTreeNode', () => {
bstNode.insert(1);
expect(bstNode.toString()).toBe('1,2');
expect(bstNode.contains(1)).toBeTruthy();
expect(bstNode.contains(3)).toBeFalsy();
expect(bstNode.contains(1)).toBe(true);
expect(bstNode.contains(3)).toBe(false);
bstNode.insert(1);
expect(bstNode.toString()).toBe('1,2');
expect(bstNode.contains(1)).toBeTruthy();
expect(bstNode.contains(3)).toBeFalsy();
expect(bstNode.contains(1)).toBe(true);
expect(bstNode.contains(3)).toBe(false);
});
it('should find min node', () => {
@ -127,11 +127,11 @@ describe('BinarySearchTreeNode', () => {
const removed1 = bstRootNode.remove(5);
expect(bstRootNode.toString()).toBe('10,20');
expect(removed1).toBeTruthy();
expect(removed1).toBe(true);
const removed2 = bstRootNode.remove(20);
expect(bstRootNode.toString()).toBe('10');
expect(removed2).toBeTruthy();
expect(removed2).toBe(true);
});
it('should remove nodes with one child', () => {
@ -233,13 +233,13 @@ describe('BinarySearchTreeNode', () => {
bstNode.insert(obj1);
expect(bstNode.toString()).toBe('obj1,obj2');
expect(bstNode.contains(obj1)).toBeTruthy();
expect(bstNode.contains(obj3)).toBeFalsy();
expect(bstNode.contains(obj1)).toBe(true);
expect(bstNode.contains(obj3)).toBe(false);
bstNode.insert(obj3);
expect(bstNode.toString()).toBe('obj1,obj2,obj3');
expect(bstNode.contains(obj3)).toBeTruthy();
expect(bstNode.contains(obj3)).toBe(true);
expect(bstNode.findMin().value).toEqual(obj1);
});

View File

@ -6,9 +6,9 @@ describe('RedBlackTree', () => {
const firstInsertedNode = tree.insert(10);
expect(tree.isNodeColored(firstInsertedNode)).toBeTruthy();
expect(tree.isNodeBlack(firstInsertedNode)).toBeTruthy();
expect(tree.isNodeRed(firstInsertedNode)).toBeFalsy();
expect(tree.isNodeColored(firstInsertedNode)).toBe(true);
expect(tree.isNodeBlack(firstInsertedNode)).toBe(true);
expect(tree.isNodeRed(firstInsertedNode)).toBe(false);
expect(tree.toString()).toBe('10');
expect(tree.root.height).toBe(0);
@ -21,9 +21,9 @@ describe('RedBlackTree', () => {
const secondInsertedNode = tree.insert(15);
const thirdInsertedNode = tree.insert(5);
expect(tree.isNodeBlack(firstInsertedNode)).toBeTruthy();
expect(tree.isNodeRed(secondInsertedNode)).toBeTruthy();
expect(tree.isNodeRed(thirdInsertedNode)).toBeTruthy();
expect(tree.isNodeBlack(firstInsertedNode)).toBe(true);
expect(tree.isNodeRed(secondInsertedNode)).toBe(true);
expect(tree.isNodeRed(thirdInsertedNode)).toBe(true);
expect(tree.toString()).toBe('5,10,15');
expect(tree.root.height).toBe(1);
@ -48,42 +48,42 @@ describe('RedBlackTree', () => {
const node1 = tree.insert(10);
expect(tree.isNodeBlack(node1)).toBeTruthy();
expect(tree.isNodeBlack(node1)).toBe(true);
const node2 = tree.insert(-10);
expect(tree.isNodeBlack(node1)).toBeTruthy();
expect(tree.isNodeRed(node2)).toBeTruthy();
expect(tree.isNodeBlack(node1)).toBe(true);
expect(tree.isNodeRed(node2)).toBe(true);
const node3 = tree.insert(20);
expect(tree.isNodeBlack(node1)).toBeTruthy();
expect(tree.isNodeRed(node2)).toBeTruthy();
expect(tree.isNodeRed(node3)).toBeTruthy();
expect(tree.isNodeBlack(node1)).toBe(true);
expect(tree.isNodeRed(node2)).toBe(true);
expect(tree.isNodeRed(node3)).toBe(true);
const node4 = tree.insert(-20);
expect(tree.isNodeBlack(node1)).toBeTruthy();
expect(tree.isNodeBlack(node2)).toBeTruthy();
expect(tree.isNodeBlack(node3)).toBeTruthy();
expect(tree.isNodeRed(node4)).toBeTruthy();
expect(tree.isNodeBlack(node1)).toBe(true);
expect(tree.isNodeBlack(node2)).toBe(true);
expect(tree.isNodeBlack(node3)).toBe(true);
expect(tree.isNodeRed(node4)).toBe(true);
const node5 = tree.insert(25);
expect(tree.isNodeBlack(node1)).toBeTruthy();
expect(tree.isNodeBlack(node2)).toBeTruthy();
expect(tree.isNodeBlack(node3)).toBeTruthy();
expect(tree.isNodeRed(node4)).toBeTruthy();
expect(tree.isNodeRed(node5)).toBeTruthy();
expect(tree.isNodeBlack(node1)).toBe(true);
expect(tree.isNodeBlack(node2)).toBe(true);
expect(tree.isNodeBlack(node3)).toBe(true);
expect(tree.isNodeRed(node4)).toBe(true);
expect(tree.isNodeRed(node5)).toBe(true);
const node6 = tree.insert(6);
expect(tree.isNodeBlack(node1)).toBeTruthy();
expect(tree.isNodeBlack(node2)).toBeTruthy();
expect(tree.isNodeBlack(node3)).toBeTruthy();
expect(tree.isNodeRed(node4)).toBeTruthy();
expect(tree.isNodeRed(node5)).toBeTruthy();
expect(tree.isNodeRed(node6)).toBeTruthy();
expect(tree.isNodeBlack(node1)).toBe(true);
expect(tree.isNodeBlack(node2)).toBe(true);
expect(tree.isNodeBlack(node3)).toBe(true);
expect(tree.isNodeRed(node4)).toBe(true);
expect(tree.isNodeRed(node5)).toBe(true);
expect(tree.isNodeRed(node6)).toBe(true);
expect(tree.toString()).toBe('-20,-10,6,10,20,25');
expect(tree.root.height).toBe(2);
@ -95,14 +95,14 @@ describe('RedBlackTree', () => {
expect(tree.toString()).toBe('-20,-10,4,6,10,20,25');
expect(tree.root.height).toBe(3);
expect(tree.isNodeBlack(node1)).toBeTruthy();
expect(tree.isNodeRed(node2)).toBeTruthy();
expect(tree.isNodeBlack(node3)).toBeTruthy();
expect(tree.isNodeBlack(node4)).toBeTruthy();
expect(tree.isNodeBlack(node4)).toBeTruthy();
expect(tree.isNodeRed(node5)).toBeTruthy();
expect(tree.isNodeBlack(node6)).toBeTruthy();
expect(tree.isNodeRed(node7)).toBeTruthy();
expect(tree.isNodeBlack(node1)).toBe(true);
expect(tree.isNodeRed(node2)).toBe(true);
expect(tree.isNodeBlack(node3)).toBe(true);
expect(tree.isNodeBlack(node4)).toBe(true);
expect(tree.isNodeBlack(node4)).toBe(true);
expect(tree.isNodeRed(node5)).toBe(true);
expect(tree.isNodeBlack(node6)).toBe(true);
expect(tree.isNodeRed(node7)).toBe(true);
});
it('should balance itself when uncle is red', () => {
@ -121,15 +121,15 @@ describe('RedBlackTree', () => {
expect(tree.toString()).toBe('-20,-10,2,6,8,10,15,20,25');
expect(tree.root.height).toBe(3);
expect(tree.isNodeBlack(node1)).toBeTruthy();
expect(tree.isNodeRed(node2)).toBeTruthy();
expect(tree.isNodeBlack(node3)).toBeTruthy();
expect(tree.isNodeBlack(node4)).toBeTruthy();
expect(tree.isNodeBlack(node5)).toBeTruthy();
expect(tree.isNodeRed(node6)).toBeTruthy();
expect(tree.isNodeRed(node7)).toBeTruthy();
expect(tree.isNodeRed(node8)).toBeTruthy();
expect(tree.isNodeRed(node9)).toBeTruthy();
expect(tree.isNodeBlack(node1)).toBe(true);
expect(tree.isNodeRed(node2)).toBe(true);
expect(tree.isNodeBlack(node3)).toBe(true);
expect(tree.isNodeBlack(node4)).toBe(true);
expect(tree.isNodeBlack(node5)).toBe(true);
expect(tree.isNodeRed(node6)).toBe(true);
expect(tree.isNodeRed(node7)).toBe(true);
expect(tree.isNodeRed(node8)).toBe(true);
expect(tree.isNodeRed(node9)).toBe(true);
const node10 = tree.insert(4);
@ -138,16 +138,16 @@ describe('RedBlackTree', () => {
expect(tree.root.value).toBe(node5.value);
expect(tree.isNodeBlack(node5)).toBeTruthy();
expect(tree.isNodeRed(node1)).toBeTruthy();
expect(tree.isNodeRed(node2)).toBeTruthy();
expect(tree.isNodeRed(node10)).toBeTruthy();
expect(tree.isNodeRed(node6)).toBeTruthy();
expect(tree.isNodeRed(node7)).toBeTruthy();
expect(tree.isNodeBlack(node4)).toBeTruthy();
expect(tree.isNodeBlack(node8)).toBeTruthy();
expect(tree.isNodeBlack(node9)).toBeTruthy();
expect(tree.isNodeBlack(node3)).toBeTruthy();
expect(tree.isNodeBlack(node5)).toBe(true);
expect(tree.isNodeRed(node1)).toBe(true);
expect(tree.isNodeRed(node2)).toBe(true);
expect(tree.isNodeRed(node10)).toBe(true);
expect(tree.isNodeRed(node6)).toBe(true);
expect(tree.isNodeRed(node7)).toBe(true);
expect(tree.isNodeBlack(node4)).toBe(true);
expect(tree.isNodeBlack(node8)).toBe(true);
expect(tree.isNodeBlack(node9)).toBe(true);
expect(tree.isNodeBlack(node3)).toBe(true);
});
it('should do left-left rotation', () => {
@ -162,23 +162,23 @@ describe('RedBlackTree', () => {
expect(tree.toString()).toBe('-10,7,10,15,20');
expect(tree.root.height).toBe(2);
expect(tree.isNodeBlack(node1)).toBeTruthy();
expect(tree.isNodeBlack(node2)).toBeTruthy();
expect(tree.isNodeBlack(node3)).toBeTruthy();
expect(tree.isNodeRed(node4)).toBeTruthy();
expect(tree.isNodeRed(node5)).toBeTruthy();
expect(tree.isNodeBlack(node1)).toBe(true);
expect(tree.isNodeBlack(node2)).toBe(true);
expect(tree.isNodeBlack(node3)).toBe(true);
expect(tree.isNodeRed(node4)).toBe(true);
expect(tree.isNodeRed(node5)).toBe(true);
const node6 = tree.insert(13);
expect(tree.toString()).toBe('-10,7,10,13,15,20');
expect(tree.root.height).toBe(2);
expect(tree.isNodeBlack(node1)).toBeTruthy();
expect(tree.isNodeBlack(node2)).toBeTruthy();
expect(tree.isNodeBlack(node5)).toBeTruthy();
expect(tree.isNodeRed(node4)).toBeTruthy();
expect(tree.isNodeRed(node6)).toBeTruthy();
expect(tree.isNodeRed(node3)).toBeTruthy();
expect(tree.isNodeBlack(node1)).toBe(true);
expect(tree.isNodeBlack(node2)).toBe(true);
expect(tree.isNodeBlack(node5)).toBe(true);
expect(tree.isNodeRed(node4)).toBe(true);
expect(tree.isNodeRed(node6)).toBe(true);
expect(tree.isNodeRed(node3)).toBe(true);
});
it('should do left-right rotation', () => {
@ -193,23 +193,23 @@ describe('RedBlackTree', () => {
expect(tree.toString()).toBe('-10,7,10,15,20');
expect(tree.root.height).toBe(2);
expect(tree.isNodeBlack(node1)).toBeTruthy();
expect(tree.isNodeBlack(node2)).toBeTruthy();
expect(tree.isNodeBlack(node3)).toBeTruthy();
expect(tree.isNodeRed(node4)).toBeTruthy();
expect(tree.isNodeRed(node5)).toBeTruthy();
expect(tree.isNodeBlack(node1)).toBe(true);
expect(tree.isNodeBlack(node2)).toBe(true);
expect(tree.isNodeBlack(node3)).toBe(true);
expect(tree.isNodeRed(node4)).toBe(true);
expect(tree.isNodeRed(node5)).toBe(true);
const node6 = tree.insert(17);
expect(tree.toString()).toBe('-10,7,10,15,17,20');
expect(tree.root.height).toBe(2);
expect(tree.isNodeBlack(node1)).toBeTruthy();
expect(tree.isNodeBlack(node2)).toBeTruthy();
expect(tree.isNodeBlack(node6)).toBeTruthy();
expect(tree.isNodeRed(node4)).toBeTruthy();
expect(tree.isNodeRed(node5)).toBeTruthy();
expect(tree.isNodeRed(node3)).toBeTruthy();
expect(tree.isNodeBlack(node1)).toBe(true);
expect(tree.isNodeBlack(node2)).toBe(true);
expect(tree.isNodeBlack(node6)).toBe(true);
expect(tree.isNodeRed(node4)).toBe(true);
expect(tree.isNodeRed(node5)).toBe(true);
expect(tree.isNodeRed(node3)).toBe(true);
});
it('should do recoloring, left-left and left-right rotation', () => {
@ -228,15 +228,15 @@ describe('RedBlackTree', () => {
expect(tree.toString()).toBe('-20,-10,1,6,9,10,15,20,30');
expect(tree.root.height).toBe(3);
expect(tree.isNodeBlack(node1)).toBeTruthy();
expect(tree.isNodeRed(node2)).toBeTruthy();
expect(tree.isNodeBlack(node3)).toBeTruthy();
expect(tree.isNodeBlack(node4)).toBeTruthy();
expect(tree.isNodeBlack(node5)).toBeTruthy();
expect(tree.isNodeRed(node6)).toBeTruthy();
expect(tree.isNodeRed(node7)).toBeTruthy();
expect(tree.isNodeRed(node8)).toBeTruthy();
expect(tree.isNodeRed(node9)).toBeTruthy();
expect(tree.isNodeBlack(node1)).toBe(true);
expect(tree.isNodeRed(node2)).toBe(true);
expect(tree.isNodeBlack(node3)).toBe(true);
expect(tree.isNodeBlack(node4)).toBe(true);
expect(tree.isNodeBlack(node5)).toBe(true);
expect(tree.isNodeRed(node6)).toBe(true);
expect(tree.isNodeRed(node7)).toBe(true);
expect(tree.isNodeRed(node8)).toBe(true);
expect(tree.isNodeRed(node9)).toBe(true);
tree.insert(4);
@ -257,12 +257,12 @@ describe('RedBlackTree', () => {
expect(tree.toString()).toBe('-20,-10,6,10,20,30');
expect(tree.root.height).toBe(2);
expect(tree.isNodeBlack(node1)).toBeTruthy();
expect(tree.isNodeBlack(node2)).toBeTruthy();
expect(tree.isNodeBlack(node3)).toBeTruthy();
expect(tree.isNodeRed(node4)).toBeTruthy();
expect(tree.isNodeRed(node5)).toBeTruthy();
expect(tree.isNodeRed(node6)).toBeTruthy();
expect(tree.isNodeBlack(node1)).toBe(true);
expect(tree.isNodeBlack(node2)).toBe(true);
expect(tree.isNodeBlack(node3)).toBe(true);
expect(tree.isNodeRed(node4)).toBe(true);
expect(tree.isNodeRed(node5)).toBe(true);
expect(tree.isNodeRed(node6)).toBe(true);
const node7 = tree.insert(25);
@ -277,13 +277,13 @@ describe('RedBlackTree', () => {
expect(tree.toString()).toBe('-20,-10,6,10,20,25,30');
expect(tree.root.height).toBe(2);
expect(tree.isNodeBlack(node1)).toBeTruthy();
expect(tree.isNodeBlack(node2)).toBeTruthy();
expect(tree.isNodeBlack(node7)).toBeTruthy();
expect(tree.isNodeRed(node4)).toBeTruthy();
expect(tree.isNodeRed(node5)).toBeTruthy();
expect(tree.isNodeRed(node3)).toBeTruthy();
expect(tree.isNodeRed(node6)).toBeTruthy();
expect(tree.isNodeBlack(node1)).toBe(true);
expect(tree.isNodeBlack(node2)).toBe(true);
expect(tree.isNodeBlack(node7)).toBe(true);
expect(tree.isNodeRed(node4)).toBe(true);
expect(tree.isNodeRed(node5)).toBe(true);
expect(tree.isNodeRed(node3)).toBe(true);
expect(tree.isNodeRed(node6)).toBe(true);
});
it('should do left-left rotation with left grand-parent', () => {

View File

@ -44,8 +44,8 @@ describe('Trie', () => {
trie.addWord('car');
trie.addWord('caption');
expect(trie.doesWordExist('cat')).toBeTruthy();
expect(trie.doesWordExist('cap')).toBeTruthy();
expect(trie.doesWordExist('call')).toBeFalsy();
expect(trie.doesWordExist('cat')).toBe(true);
expect(trie.doesWordExist('cap')).toBe(true);
expect(trie.doesWordExist('call')).toBe(false);
});
});

View File

@ -5,7 +5,7 @@ describe('TrieNode', () => {
const trieNode = new TrieNode('c', true);
expect(trieNode.character).toBe('c');
expect(trieNode.isCompleteWord).toBeTruthy();
expect(trieNode.isCompleteWord).toBe(true);
expect(trieNode.toString()).toBe('c*');
});
@ -35,9 +35,9 @@ describe('TrieNode', () => {
trieNode.addChild('a');
trieNode.addChild('o');
expect(trieNode.hasChild('a')).toBeTruthy();
expect(trieNode.hasChild('o')).toBeTruthy();
expect(trieNode.hasChild('b')).toBeFalsy();
expect(trieNode.hasChild('a')).toBe(true);
expect(trieNode.hasChild('o')).toBe(true);
expect(trieNode.hasChild('b')).toBe(false);
});
it('should suggest next children', () => {

View File

@ -4,22 +4,22 @@ describe('Comparator', () => {
it('should compare with default comparator function', () => {
const comparator = new Comparator();
expect(comparator.equal(0, 0)).toBeTruthy();
expect(comparator.equal(0, 1)).toBeFalsy();
expect(comparator.equal('a', 'a')).toBeTruthy();
expect(comparator.lessThan(1, 2)).toBeTruthy();
expect(comparator.lessThan(-1, 2)).toBeTruthy();
expect(comparator.lessThan('a', 'b')).toBeTruthy();
expect(comparator.lessThan('a', 'ab')).toBeTruthy();
expect(comparator.lessThan(10, 2)).toBeFalsy();
expect(comparator.lessThanOrEqual(10, 2)).toBeFalsy();
expect(comparator.lessThanOrEqual(1, 1)).toBeTruthy();
expect(comparator.lessThanOrEqual(0, 0)).toBeTruthy();
expect(comparator.greaterThan(0, 0)).toBeFalsy();
expect(comparator.greaterThan(10, 0)).toBeTruthy();
expect(comparator.greaterThanOrEqual(10, 0)).toBeTruthy();
expect(comparator.greaterThanOrEqual(10, 10)).toBeTruthy();
expect(comparator.greaterThanOrEqual(0, 10)).toBeFalsy();
expect(comparator.equal(0, 0)).toBe(true);
expect(comparator.equal(0, 1)).toBe(false);
expect(comparator.equal('a', 'a')).toBe(true);
expect(comparator.lessThan(1, 2)).toBe(true);
expect(comparator.lessThan(-1, 2)).toBe(true);
expect(comparator.lessThan('a', 'b')).toBe(true);
expect(comparator.lessThan('a', 'ab')).toBe(true);
expect(comparator.lessThan(10, 2)).toBe(false);
expect(comparator.lessThanOrEqual(10, 2)).toBe(false);
expect(comparator.lessThanOrEqual(1, 1)).toBe(true);
expect(comparator.lessThanOrEqual(0, 0)).toBe(true);
expect(comparator.greaterThan(0, 0)).toBe(false);
expect(comparator.greaterThan(10, 0)).toBe(true);
expect(comparator.greaterThanOrEqual(10, 0)).toBe(true);
expect(comparator.greaterThanOrEqual(10, 10)).toBe(true);
expect(comparator.greaterThanOrEqual(0, 10)).toBe(false);
});
it('should compare with custom comparator function', () => {
@ -31,20 +31,20 @@ describe('Comparator', () => {
return a.length < b.length ? -1 : 1;
});
expect(comparator.equal('a', 'b')).toBeTruthy();
expect(comparator.equal('a', '')).toBeFalsy();
expect(comparator.lessThan('b', 'aa')).toBeTruthy();
expect(comparator.greaterThanOrEqual('a', 'aa')).toBeFalsy();
expect(comparator.greaterThanOrEqual('aa', 'a')).toBeTruthy();
expect(comparator.greaterThanOrEqual('a', 'a')).toBeTruthy();
expect(comparator.equal('a', 'b')).toBe(true);
expect(comparator.equal('a', '')).toBe(false);
expect(comparator.lessThan('b', 'aa')).toBe(true);
expect(comparator.greaterThanOrEqual('a', 'aa')).toBe(false);
expect(comparator.greaterThanOrEqual('aa', 'a')).toBe(true);
expect(comparator.greaterThanOrEqual('a', 'a')).toBe(true);
comparator.reverse();
expect(comparator.equal('a', 'b')).toBeTruthy();
expect(comparator.equal('a', '')).toBeFalsy();
expect(comparator.lessThan('b', 'aa')).toBeFalsy();
expect(comparator.greaterThanOrEqual('a', 'aa')).toBeTruthy();
expect(comparator.greaterThanOrEqual('aa', 'a')).toBeFalsy();
expect(comparator.greaterThanOrEqual('a', 'a')).toBeTruthy();
expect(comparator.equal('a', 'b')).toBe(true);
expect(comparator.equal('a', '')).toBe(false);
expect(comparator.lessThan('b', 'aa')).toBe(false);
expect(comparator.greaterThanOrEqual('a', 'aa')).toBe(true);
expect(comparator.greaterThanOrEqual('aa', 'a')).toBe(false);
expect(comparator.greaterThanOrEqual('a', 'a')).toBe(true);
});
});