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

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