From b116ac03b021903ca5e0b64ed09825b45134d4c9 Mon Sep 17 00:00:00 2001 From: Revone Date: Tue, 26 Sep 2023 18:43:54 +0800 Subject: [PATCH] [test] test cases enriched --- .../binary-tree/binary-tree.test.ts | 146 ++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 test/unit/data-structures/binary-tree/binary-tree.test.ts diff --git a/test/unit/data-structures/binary-tree/binary-tree.test.ts b/test/unit/data-structures/binary-tree/binary-tree.test.ts new file mode 100644 index 0000000..e8804cf --- /dev/null +++ b/test/unit/data-structures/binary-tree/binary-tree.test.ts @@ -0,0 +1,146 @@ +import {BinaryTreeNode} from '../../../../src'; +import {BinaryTree} from 'binary-tree-typed'; + +describe('BinaryTreeNode', () => { + it('should create an instance of BinaryTreeNode', () => { + const node = new BinaryTreeNode(1); + expect(node).toBeInstanceOf(BinaryTreeNode); + }); + + it('should set and get the ID correctly', () => { + const node = new BinaryTreeNode(1); + expect(node.id).toBe(1); + + node.id = 2; + expect(node.id).toBe(2); + }); + + it('should set and get the value correctly', () => { + const node = new BinaryTreeNode(1, 42); + expect(node.val).toBe(42); + + node.val = 55; + expect(node.val).toBe(55); + }); + + it('should set and get the left child correctly', () => { + const node1 = new BinaryTreeNode(1); + const node2 = new BinaryTreeNode(2); + + node1.left = node2; + + expect(node1.left).toBe(node2); + expect(node2.parent).toBe(node1); + }); + + it('should set and get the right child correctly', () => { + const node1 = new BinaryTreeNode(1); + const node2 = new BinaryTreeNode(2); + + node1.right = node2; + + expect(node1.right).toBe(node2); + expect(node2.parent).toBe(node1); + }); + + it('should set and get the parent correctly', () => { + const node1 = new BinaryTreeNode(1); + const node2 = new BinaryTreeNode(2); + + node1.left = node2; + + expect(node2.parent).toBe(node1); + expect(node1.left).toBe(node2); + }); + + it('should set and get the height correctly', () => { + const node = new BinaryTreeNode(1); + expect(node.height).toBe(0); + + node.height = 3; + expect(node.height).toBe(3); + }); + + it('should determine family position correctly', () => { + const root = new BinaryTreeNode(1); + const leftChild = new BinaryTreeNode(2); + const rightChild = new BinaryTreeNode(3); + + root.left = leftChild; + root.right = rightChild; + + expect(leftChild.familyPosition).toBe('LEFT'); + expect(rightChild.familyPosition).toBe('RIGHT'); + expect(root.familyPosition).toBe('ROOT'); + }); +}); + + +describe('BinaryTree', () => { + let binaryTree: BinaryTree; + + beforeEach(() => { + binaryTree = new BinaryTree(); + }); + + afterEach(() => { + binaryTree.clear(); + }); + + test('should add a node', () => { + const node = binaryTree.add(1); + expect(node).not.toBeNull(); + expect(binaryTree.size).toBe(1); + }); + + test('should remove a node', () => { + const node = binaryTree.add(1); + expect(binaryTree.size).toBe(1); + + if (node) { + const result = binaryTree.remove(node); + expect(result).toHaveLength(1); + expect(binaryTree.size).toBe(0); + } + + }); + + + test('should add and find nodes', () => { + binaryTree.add(1); + binaryTree.add(2); + binaryTree.add(3); + + expect(binaryTree.has(1)).toBe(true); + expect(binaryTree.has(2)).toBe(true); + expect(binaryTree.has(3)).toBe(true); + expect(binaryTree.has(4)).toBe(false); + }); + + test('should traverse in-order', () => { + binaryTree.add(4); + binaryTree.add(2); + binaryTree.add(6); + binaryTree.add(1); + binaryTree.add(3); + binaryTree.add(5); + binaryTree.add(7); + + const inOrder = binaryTree.DFS('in'); + + expect(inOrder).toEqual([1, 2, 3, 4, 5, 6, 7]); + }); + + test('should clear the tree', () => { + binaryTree.add(1); + binaryTree.add(2); + + expect(binaryTree.size).toBe(2); + + binaryTree.clear(); + + expect(binaryTree.size).toBe(0); + expect(binaryTree.root).toBeNull(); + }); +}); +