From 09020a226a72bb738fd9bbd087159bf6e0894353 Mon Sep 17 00:00:00 2001 From: euchangxian Date: Sat, 30 Dec 2023 17:35:45 +0800 Subject: [PATCH 1/5] refactor: refine checkstyle configurations - Checkstyle configurations taken from CS2103T. NOTE: PackageName check is disabled, until I can figure out a REGEX that accomodates the current camelCase package names. Otherwise, a change to the package names to adhere to conventions can be done too. --- config/checkstyle/checkstyle.xml | 783 ++++++++++++++++------------- config/checkstyle/suppressions.xml | 10 + 2 files changed, 432 insertions(+), 361 deletions(-) create mode 100644 config/checkstyle/suppressions.xml diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml index f455f74f..a859d026 100644 --- a/config/checkstyle/checkstyle.xml +++ b/config/checkstyle/checkstyle.xml @@ -1,374 +1,435 @@ - + + "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN" + "https://checkstyle.org/dtds/configuration_1_3.dtd"> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + This configuration file enforces rules for the coding standard at + https://se-education.org/guides/conventions/java/intermediate.html +--> - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - - - - - - - - - - - - - - - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + - - - + + + - - + + + + - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - diff --git a/config/checkstyle/suppressions.xml b/config/checkstyle/suppressions.xml new file mode 100644 index 00000000..6423654d --- /dev/null +++ b/config/checkstyle/suppressions.xml @@ -0,0 +1,10 @@ + + + + + + + + From 327c0e4d82e631c545c91d6e534e7e2f2151bee6 Mon Sep 17 00:00:00 2001 From: euchangxian Date: Sat, 30 Dec 2023 17:36:02 +0800 Subject: [PATCH 2/5] Fix checkstyle violations in test package --- src/test/README.md | 11 +- .../binarySearch/BinarySearchTest.java | 74 +++--- .../graphs/breadthFirstSearchTest.java | 133 +++++----- .../graphs/depthFirstSearchTest.java | 229 +++++++++--------- .../algorithms/patternFinding/KmpTest.java | 98 ++++---- .../sorting/bubbleSort/BubbleSortTest.java | 60 ++--- .../countingSort/CountingSortTest.java | 64 ++--- .../generalised/CyclicSortTest.java | 94 +++---- .../FindFirstMissingNonNegativeTest.java | 70 +++--- .../simple/SimpleCyclicSortTest.java | 44 ++-- .../insertionSort/InsertionSortTest.java | 70 +++--- .../mergeSort/iterative/MergeSortTest.java | 81 ++++--- .../mergeSort/recursive/MergeSortTest.java | 81 ++++--- .../quickSort/hoares/QuickSortTest.java | 101 ++++---- .../quickSort/lomuto/QuickSortTest.java | 93 +++---- .../quickSort/paranoid/QuickSortTest.java | 97 ++++---- .../threeWayPartitioning/QuickSortTest.java | 92 +++---- .../selectionSort/SelectionSortTest.java | 70 +++--- .../hashSet/chaining/HashSetTest.java | 124 +++++----- .../hashSet/openAddressing/HashSetTest.java | 218 ++++++++--------- .../java/dataStructures/heap/MaxHeapTest.java | 185 +++++++------- .../linkedList/LinkedListTest.java | 194 +++++++-------- .../queue/MonotonicQueueTest.java | 94 +++---- .../java/dataStructures/queue/dequeTest.java | 92 +++---- .../java/dataStructures/queue/queueTest.java | 76 +++--- .../java/dataStructures/stack/StackTest.java | 44 ++-- src/test/java/random/andre/Test.java | 44 ++++ src/test/java/random/andre/lruCache/Test.java | 27 +++ .../java/random/andre/radixSort/Test.java | 62 +++++ .../java/random/andre/segmentTree/Test.java | 21 ++ .../andre/segmentTree/improved/Test.java | 21 ++ .../random/changxian/linkedList/Test.java | 89 +++++++ .../minimumSpanningTree/kruskals/Test.java | 48 ++++ .../minimumSpanningTree/prims/Test.java | 49 ++++ .../stackAndQueue/monotonicQueue/Test.java | 37 +++ .../changxian/stackAndQueue/queue/Test.java | 26 ++ .../changxian/stackAndQueue/stack/Test.java | 37 +++ .../java/random/kaiting/avlTree/Test.java | 75 ++++++ .../kaiting/quickSort/Test.java} | 2 +- .../java/random/kaiting/trie/TrieTest.java | 29 +++ .../random/kaiting/trie/legacy/TrieTest.java | 24 ++ .../MatrixConverterTest.java | 76 ++++++ .../unionFind/quickFind/generalised/Test.java | 68 ++++++ .../unionFind/quickFind/simplified/Test.java | 43 ++++ .../owen/unionFind/weightedUnion/Test.java | 50 ++++ src/test/java/randomTests/andre/Test.java | 44 ---- .../java/randomTests/andre/lruCache/test.java | 27 --- .../randomTests/andre/radixSort/Test.java | 56 ----- .../andre/segmentTree/improved/test.java | 21 -- .../randomTests/andre/segmentTree/test.java | 21 -- .../changxian/linkedList/Test.java | 89 ------- .../minimumSpanningTree/kruskals/test.java | 49 ---- .../minimumSpanningTree/prims/test.java | 50 ---- .../stackAndQueue/monotonicQueue/Test.java | 37 --- .../changxian/stackAndQueue/queue/Test.java | 26 -- .../changxian/stackAndQueue/stack/Test.java | 37 --- .../randomTests/kaiting/avlTree/Test.java | 75 ------ .../randomTests/kaiting/trie/TrieTest.java | 29 --- .../kaiting/trie/legacy/TrieTest.java | 24 -- .../MatrixConverterTest.java | 76 ------ .../unionFind/quickFind/generalised/Test.java | 68 ------ .../unionFind/quickFind/simplified/Test.java | 43 ---- .../owen/unionFind/weightedUnion/Test.java | 50 ---- 63 files changed, 2145 insertions(+), 2094 deletions(-) create mode 100644 src/test/java/random/andre/Test.java create mode 100644 src/test/java/random/andre/lruCache/Test.java create mode 100644 src/test/java/random/andre/radixSort/Test.java create mode 100644 src/test/java/random/andre/segmentTree/Test.java create mode 100644 src/test/java/random/andre/segmentTree/improved/Test.java create mode 100644 src/test/java/random/changxian/linkedList/Test.java create mode 100644 src/test/java/random/changxian/minimumSpanningTree/kruskals/Test.java create mode 100644 src/test/java/random/changxian/minimumSpanningTree/prims/Test.java create mode 100644 src/test/java/random/changxian/stackAndQueue/monotonicQueue/Test.java create mode 100644 src/test/java/random/changxian/stackAndQueue/queue/Test.java create mode 100644 src/test/java/random/changxian/stackAndQueue/stack/Test.java create mode 100644 src/test/java/random/kaiting/avlTree/Test.java rename src/test/java/{randomTests/kaiting/quickSort/test.java => random/kaiting/quickSort/Test.java} (99%) create mode 100644 src/test/java/random/kaiting/trie/TrieTest.java create mode 100644 src/test/java/random/kaiting/trie/legacy/TrieTest.java create mode 100644 src/test/java/random/owen/unionFind/matrixToListConverter/MatrixConverterTest.java create mode 100644 src/test/java/random/owen/unionFind/quickFind/generalised/Test.java create mode 100644 src/test/java/random/owen/unionFind/quickFind/simplified/Test.java create mode 100644 src/test/java/random/owen/unionFind/weightedUnion/Test.java delete mode 100644 src/test/java/randomTests/andre/Test.java delete mode 100644 src/test/java/randomTests/andre/lruCache/test.java delete mode 100644 src/test/java/randomTests/andre/radixSort/Test.java delete mode 100644 src/test/java/randomTests/andre/segmentTree/improved/test.java delete mode 100644 src/test/java/randomTests/andre/segmentTree/test.java delete mode 100644 src/test/java/randomTests/changxian/linkedList/Test.java delete mode 100644 src/test/java/randomTests/changxian/minimumSpanningTree/kruskals/test.java delete mode 100644 src/test/java/randomTests/changxian/minimumSpanningTree/prims/test.java delete mode 100644 src/test/java/randomTests/changxian/stackAndQueue/monotonicQueue/Test.java delete mode 100644 src/test/java/randomTests/changxian/stackAndQueue/queue/Test.java delete mode 100644 src/test/java/randomTests/changxian/stackAndQueue/stack/Test.java delete mode 100644 src/test/java/randomTests/kaiting/avlTree/Test.java delete mode 100644 src/test/java/randomTests/kaiting/trie/TrieTest.java delete mode 100644 src/test/java/randomTests/kaiting/trie/legacy/TrieTest.java delete mode 100644 src/test/java/randomTests/owen/unionFind/matrixToListConverter/MatrixConverterTest.java delete mode 100644 src/test/java/randomTests/owen/unionFind/quickFind/generalised/Test.java delete mode 100644 src/test/java/randomTests/owen/unionFind/quickFind/simplified/Test.java delete mode 100644 src/test/java/randomTests/owen/unionFind/weightedUnion/Test.java diff --git a/src/test/README.md b/src/test/README.md index ac834a2a..dd548eaf 100644 --- a/src/test/README.md +++ b/src/test/README.md @@ -2,8 +2,11 @@ This folder contains the test code for the corresponding algorithms and data structures in **main/** folder. -Note to contributors: +Note to contributors: + - Naming convention for test methods: featureUnderTest_testScenario_expectedBehaviour() -The above is just a guide, slight deviation is fine as long as its clear. eg. no need to specifically mention "ordinary array" if testing some sorting algor. - - camelCase - - eg. sortList_emptyList_shouldReturnNoChange(), test_mergeSort_shouldReturnSortedArray, test{algor}_emptyList_shouldReturnEmptyResult + The above is just a guide, slight deviation is fine as long as its clear. eg. no need to specifically mention " + ordinary array" if testing some sorting algor. + - camelCase + - eg. sortList_emptyList_shouldReturnNoChange(), test_mergeSort_shouldReturnSortedArray, test{algor}_ + emptyList_shouldReturnEmptyResult diff --git a/src/test/java/algorithms/binarySearch/BinarySearchTest.java b/src/test/java/algorithms/binarySearch/BinarySearchTest.java index 0aeb9737..3d09ee9a 100644 --- a/src/test/java/algorithms/binarySearch/BinarySearchTest.java +++ b/src/test/java/algorithms/binarySearch/BinarySearchTest.java @@ -8,41 +8,41 @@ * Test cases for {@link BinarySearch}. */ public class BinarySearchTest { - @Test - public void test_binarySearch() { - // Test 1: even number of elements - int[] firstArray = {1, 5, 10, 12}; - int firstResult = BinarySearch.search(firstArray, 1); - - // Test 2: odd number of elements - int[] secondArray = {1, 5, 10, 11, 12}; - int secondResult = BinarySearch.search(secondArray, 11); - - // Test 3: target not in array - int[] thirdArray = {1, 5, 10, 11, 12}; - int thirdResult = BinarySearch.search(thirdArray, 3); - - assertEquals(0, firstResult); - assertEquals(3, secondResult); - assertEquals(-1, thirdResult); - } - - @Test - public void test_binarySearchTemplated() { - // Test 1: even number of elements - int[] firstArray = {1, 5, 10, 12}; - int firstResult = BinarySearchTemplated.search(firstArray, 1); - - // Test 2: odd number of elements - int[] secondArray = {1, 5, 10, 11, 12}; - int secondResult = BinarySearchTemplated.search(secondArray, 11); - - // Test 3: target not in array - int[] thirdArray = {1, 5, 10, 11, 12}; - int thirdResult = BinarySearchTemplated.search(thirdArray, 3); - - assertEquals(0, firstResult); - assertEquals(3, secondResult); - assertEquals(-1, thirdResult); - } + @Test + public void test_binarySearch() { + // Test 1: even number of elements + int[] firstArray = {1, 5, 10, 12}; + int firstResult = BinarySearch.search(firstArray, 1); + + // Test 2: odd number of elements + int[] secondArray = {1, 5, 10, 11, 12}; + int secondResult = BinarySearch.search(secondArray, 11); + + // Test 3: target not in array + int[] thirdArray = {1, 5, 10, 11, 12}; + int thirdResult = BinarySearch.search(thirdArray, 3); + + assertEquals(0, firstResult); + assertEquals(3, secondResult); + assertEquals(-1, thirdResult); + } + + @Test + public void test_binarySearchTemplated() { + // Test 1: even number of elements + int[] firstArray = {1, 5, 10, 12}; + int firstResult = BinarySearchTemplated.search(firstArray, 1); + + // Test 2: odd number of elements + int[] secondArray = {1, 5, 10, 11, 12}; + int secondResult = BinarySearchTemplated.search(secondArray, 11); + + // Test 3: target not in array + int[] thirdArray = {1, 5, 10, 11, 12}; + int thirdResult = BinarySearchTemplated.search(thirdArray, 3); + + assertEquals(0, firstResult); + assertEquals(3, secondResult); + assertEquals(-1, thirdResult); + } } diff --git a/src/test/java/algorithms/graphs/breadthFirstSearchTest.java b/src/test/java/algorithms/graphs/breadthFirstSearchTest.java index 7edc427c..c7edc85e 100644 --- a/src/test/java/algorithms/graphs/breadthFirstSearchTest.java +++ b/src/test/java/algorithms/graphs/breadthFirstSearchTest.java @@ -14,76 +14,77 @@ */ public class BreadthFirstSearchTest { - @Test - public void bfs_levelOrderTraversal_shouldReturnAccurate() { - // empty tree - List firstList = new ArrayList<>(); - BinaryTreeNode root1 = null; - List firstResult = breadthFirstSearch.levelOrder(root1); + @Test + public void bfs_levelOrderTraversal_shouldReturnAccurate() { + // empty tree + List firstList = new ArrayList<>(); + BinaryTreeNode root1 = null; + List firstResult = breadthFirstSearch.levelOrder(root1); - //standard tree - // 1 - // / \ - // 2 3 - // / \ - // 4 5 - List secondList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); - BinaryTreeNode rootRight2 = new BinaryTreeNode(3, new BinaryTreeNode(4), new BinaryTreeNode(5)); - BinaryTreeNode root2 = new BinaryTreeNode(1, new BinaryTreeNode(2), rootRight2); - List secondResult = breadthFirstSearch.levelOrder(root2); + //standard tree + // 1 + // / \ + // 2 3 + // / \ + // 4 5 + List secondList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); + BinaryTreeNode rootRight2 = new BinaryTreeNode(3, new BinaryTreeNode(4), new BinaryTreeNode(5)); + BinaryTreeNode root2 = new BinaryTreeNode(1, new BinaryTreeNode(2), rootRight2); + List secondResult = breadthFirstSearch.levelOrder(root2); - //standard tree 2 - // 1 - // / \ - // 2 7 - // / \ - // 3 5 - // / / - // 4 6 - List thirdList = new ArrayList<>(Arrays.asList(1, 2, 7, 3, 5, 4, 6)); - BinaryTreeNode rootLeft3 = - new BinaryTreeNode(2, new BinaryTreeNode(3, new BinaryTreeNode(4), null), - new BinaryTreeNode(5, new BinaryTreeNode(6), null)); - BinaryTreeNode root3 = new BinaryTreeNode(1, rootLeft3, new BinaryTreeNode(7)); - List thirdResult = breadthFirstSearch.levelOrder(root3); + //standard tree 2 + // 1 + // / \ + // 2 7 + // / \ + // 3 5 + // / / + // 4 6 + List thirdList = new ArrayList<>(Arrays.asList(1, 2, 7, 3, 5, 4, 6)); + BinaryTreeNode rootLeft3 = + new BinaryTreeNode(2, new BinaryTreeNode(3, new BinaryTreeNode(4), null), + new BinaryTreeNode(5, new BinaryTreeNode(6), null) + ); + BinaryTreeNode root3 = new BinaryTreeNode(1, rootLeft3, new BinaryTreeNode(7)); + List thirdResult = breadthFirstSearch.levelOrder(root3); - assert firstResult.equals(firstList); - assert secondResult.equals(secondList); - assert thirdResult.equals(thirdList); - } + assert firstResult.equals(firstList); + assert secondResult.equals(secondList); + assert thirdResult.equals(thirdList); + } - @Test - public void bfs_friendHops_shouldReturnAccurate() { + @Test + public void bfs_friendHops_shouldReturnAccurate() { - // Tests based on the following friend graph: - // Andre ------ Ben ------- Diana ------- Evelyn ------- Gerald - // | | | - // Cathy Felix-------------------------- - // | - // Harold ------ Iris Anonymous - GraphNode andre = new GraphNode<>("andre"); - GraphNode ben = new GraphNode<>("ben"); - GraphNode cathy = new GraphNode<>("cathy"); - GraphNode diana = new GraphNode<>("diana"); - GraphNode evelyn = new GraphNode<>("evelyn"); - GraphNode felix = new GraphNode<>("felix"); - GraphNode gerald = new GraphNode<>("gerald"); - GraphNode harold = new GraphNode<>("harold"); - GraphNode iris = new GraphNode<>("iris"); - GraphNode anonymous = new GraphNode<>("anonymous"); - GraphNode.connect(andre, ben); - GraphNode.connect(andre, cathy); - GraphNode.connect(cathy, harold); - GraphNode.connect(harold, iris); - GraphNode.connect(ben, felix); - GraphNode.connect(ben, diana); - GraphNode.connect(diana, evelyn); - GraphNode.connect(evelyn, felix); - GraphNode.connect(evelyn, gerald); + // Tests based on the following friend graph: + // Andre ------ Ben ------- Diana ------- Evelyn ------- Gerald + // | | | + // Cathy Felix-------------------------- + // | + // Harold ------ Iris Anonymous + GraphNode andre = new GraphNode<>("andre"); + GraphNode ben = new GraphNode<>("ben"); + GraphNode cathy = new GraphNode<>("cathy"); + GraphNode diana = new GraphNode<>("diana"); + GraphNode evelyn = new GraphNode<>("evelyn"); + GraphNode felix = new GraphNode<>("felix"); + GraphNode gerald = new GraphNode<>("gerald"); + GraphNode harold = new GraphNode<>("harold"); + GraphNode iris = new GraphNode<>("iris"); + GraphNode anonymous = new GraphNode<>("anonymous"); + GraphNode.connect(andre, ben); + GraphNode.connect(andre, cathy); + GraphNode.connect(cathy, harold); + GraphNode.connect(harold, iris); + GraphNode.connect(ben, felix); + GraphNode.connect(ben, diana); + GraphNode.connect(diana, evelyn); + GraphNode.connect(evelyn, felix); + GraphNode.connect(evelyn, gerald); - assert breadthFirstSearch.friendHops(anonymous, diana) == -1; - assert breadthFirstSearch.friendHops(iris, gerald) == 7; - assert breadthFirstSearch.friendHops(andre, gerald) == 4; - assert breadthFirstSearch.friendHops(felix, harold) == 4; - } + assert breadthFirstSearch.friendHops(anonymous, diana) == -1; + assert breadthFirstSearch.friendHops(iris, gerald) == 7; + assert breadthFirstSearch.friendHops(andre, gerald) == 4; + assert breadthFirstSearch.friendHops(felix, harold) == 4; + } } diff --git a/src/test/java/algorithms/graphs/depthFirstSearchTest.java b/src/test/java/algorithms/graphs/depthFirstSearchTest.java index df95f5b4..d3728324 100644 --- a/src/test/java/algorithms/graphs/depthFirstSearchTest.java +++ b/src/test/java/algorithms/graphs/depthFirstSearchTest.java @@ -13,117 +13,120 @@ */ public class DepthFirstSearchTest { - @Test - public void dfs_preOrderTraversal_shouldReturnAccurate() { - // empty tree - List firstList = new ArrayList<>(); - BinaryTreeNode root1 = null; - List firstResult = depthFirstSearch.preOrder(root1); - - //standard tree - // 1 - // / \ - // 2 3 - // / \ - // 4 5 - List secondList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); - BinaryTreeNode rootRight2 = new BinaryTreeNode(3, new BinaryTreeNode(4), new BinaryTreeNode(5)); - BinaryTreeNode root2 = new BinaryTreeNode(1, new BinaryTreeNode(2), rootRight2); - List secondResult = depthFirstSearch.preOrder(root2); - - //standard tree 2 - // 1 - // / \ - // 2 7 - // / \ - // 3 5 - // / / - // 4 6 - List thirdList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7)); - BinaryTreeNode rootLeft3 = - new BinaryTreeNode(2, new BinaryTreeNode(3, new BinaryTreeNode(4), null), - new BinaryTreeNode(5, new BinaryTreeNode(6), null)); - BinaryTreeNode root3 = new BinaryTreeNode(1, rootLeft3, new BinaryTreeNode(7)); - List thirdResult = depthFirstSearch.preOrder(root3); - - assert firstResult.equals(firstList); - assert secondResult.equals(secondList); - assert thirdResult.equals(thirdList); - } - - @Test - public void dfs_inOrderTraversal_shouldReturnAccurate() { - // empty tree - List firstList = new ArrayList<>(); - BinaryTreeNode root1 = null; - List firstResult = depthFirstSearch.inOrder(root1); - - //standard tree - // 1 - // / \ - // 2 3 - // / \ - // 4 5 - List secondList = new ArrayList<>(Arrays.asList(2, 1, 4, 3, 5)); - BinaryTreeNode rootRight2 = new BinaryTreeNode(3, new BinaryTreeNode(4), new BinaryTreeNode(5)); - BinaryTreeNode root2 = new BinaryTreeNode(1, new BinaryTreeNode(2), rootRight2); - List secondResult = depthFirstSearch.inOrder(root2); - - //standard tree 2 - // 1 - // / \ - // 2 7 - // / \ - // 3 5 - // / / - // 4 6 - List thirdList = new ArrayList<>(Arrays.asList(4, 3, 2, 6, 5, 1, 7)); - BinaryTreeNode rootLeft3 = - new BinaryTreeNode(2, new BinaryTreeNode(3, new BinaryTreeNode(4), null), - new BinaryTreeNode(5, new BinaryTreeNode(6), null)); - BinaryTreeNode root3 = new BinaryTreeNode(1, rootLeft3, new BinaryTreeNode(7)); - List thirdResult = depthFirstSearch.inOrder(root3); - - assert firstResult.equals(firstList); - assert secondResult.equals(secondList); - assert thirdResult.equals(thirdList); - } - - @Test - public void dfs_postOrderTraversal_shouldReturnAccurate() { - // empty tree - List firstList = new ArrayList<>(); - BinaryTreeNode root1 = null; - List firstResult = depthFirstSearch.inOrder(root1); - - //standard tree - // 1 - // / \ - // 2 3 - // / \ - // 4 5 - List secondList = new ArrayList<>(Arrays.asList(2, 4, 5, 3, 1)); - BinaryTreeNode rootRight2 = new BinaryTreeNode(3, new BinaryTreeNode(4), new BinaryTreeNode(5)); - BinaryTreeNode root2 = new BinaryTreeNode(1, new BinaryTreeNode(2), rootRight2); - List secondResult = depthFirstSearch.postOrder(root2); - - //standard tree 2 - // 1 - // / \ - // 2 7 - // / \ - // 3 5 - // / / - // 4 6 - List thirdList = new ArrayList<>(Arrays.asList(4, 3, 6, 5, 2, 7, 1)); - BinaryTreeNode rootLeft3 = - new BinaryTreeNode(2, new BinaryTreeNode(3, new BinaryTreeNode(4), null), - new BinaryTreeNode(5, new BinaryTreeNode(6), null)); - BinaryTreeNode root3 = new BinaryTreeNode(1, rootLeft3, new BinaryTreeNode(7)); - List thirdResult = depthFirstSearch.postOrder(root3); - - assert firstResult.equals(firstList); - assert secondResult.equals(secondList); - assert thirdResult.equals(thirdList); - } + @Test + public void dfs_preOrderTraversal_shouldReturnAccurate() { + // empty tree + List firstList = new ArrayList<>(); + BinaryTreeNode root1 = null; + List firstResult = depthFirstSearch.preOrder(root1); + + //standard tree + // 1 + // / \ + // 2 3 + // / \ + // 4 5 + List secondList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); + BinaryTreeNode rootRight2 = new BinaryTreeNode(3, new BinaryTreeNode(4), new BinaryTreeNode(5)); + BinaryTreeNode root2 = new BinaryTreeNode(1, new BinaryTreeNode(2), rootRight2); + List secondResult = depthFirstSearch.preOrder(root2); + + //standard tree 2 + // 1 + // / \ + // 2 7 + // / \ + // 3 5 + // / / + // 4 6 + List thirdList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7)); + BinaryTreeNode rootLeft3 = + new BinaryTreeNode(2, new BinaryTreeNode(3, new BinaryTreeNode(4), null), + new BinaryTreeNode(5, new BinaryTreeNode(6), null) + ); + BinaryTreeNode root3 = new BinaryTreeNode(1, rootLeft3, new BinaryTreeNode(7)); + List thirdResult = depthFirstSearch.preOrder(root3); + + assert firstResult.equals(firstList); + assert secondResult.equals(secondList); + assert thirdResult.equals(thirdList); + } + + @Test + public void dfs_inOrderTraversal_shouldReturnAccurate() { + // empty tree + List firstList = new ArrayList<>(); + BinaryTreeNode root1 = null; + List firstResult = depthFirstSearch.inOrder(root1); + + //standard tree + // 1 + // / \ + // 2 3 + // / \ + // 4 5 + List secondList = new ArrayList<>(Arrays.asList(2, 1, 4, 3, 5)); + BinaryTreeNode rootRight2 = new BinaryTreeNode(3, new BinaryTreeNode(4), new BinaryTreeNode(5)); + BinaryTreeNode root2 = new BinaryTreeNode(1, new BinaryTreeNode(2), rootRight2); + List secondResult = depthFirstSearch.inOrder(root2); + + //standard tree 2 + // 1 + // / \ + // 2 7 + // / \ + // 3 5 + // / / + // 4 6 + List thirdList = new ArrayList<>(Arrays.asList(4, 3, 2, 6, 5, 1, 7)); + BinaryTreeNode rootLeft3 = + new BinaryTreeNode(2, new BinaryTreeNode(3, new BinaryTreeNode(4), null), + new BinaryTreeNode(5, new BinaryTreeNode(6), null) + ); + BinaryTreeNode root3 = new BinaryTreeNode(1, rootLeft3, new BinaryTreeNode(7)); + List thirdResult = depthFirstSearch.inOrder(root3); + + assert firstResult.equals(firstList); + assert secondResult.equals(secondList); + assert thirdResult.equals(thirdList); + } + + @Test + public void dfs_postOrderTraversal_shouldReturnAccurate() { + // empty tree + List firstList = new ArrayList<>(); + BinaryTreeNode root1 = null; + List firstResult = depthFirstSearch.inOrder(root1); + + //standard tree + // 1 + // / \ + // 2 3 + // / \ + // 4 5 + List secondList = new ArrayList<>(Arrays.asList(2, 4, 5, 3, 1)); + BinaryTreeNode rootRight2 = new BinaryTreeNode(3, new BinaryTreeNode(4), new BinaryTreeNode(5)); + BinaryTreeNode root2 = new BinaryTreeNode(1, new BinaryTreeNode(2), rootRight2); + List secondResult = depthFirstSearch.postOrder(root2); + + //standard tree 2 + // 1 + // / \ + // 2 7 + // / \ + // 3 5 + // / / + // 4 6 + List thirdList = new ArrayList<>(Arrays.asList(4, 3, 6, 5, 2, 7, 1)); + BinaryTreeNode rootLeft3 = + new BinaryTreeNode(2, new BinaryTreeNode(3, new BinaryTreeNode(4), null), + new BinaryTreeNode(5, new BinaryTreeNode(6), null) + ); + BinaryTreeNode root3 = new BinaryTreeNode(1, rootLeft3, new BinaryTreeNode(7)); + List thirdResult = depthFirstSearch.postOrder(root3); + + assert firstResult.equals(firstList); + assert secondResult.equals(secondList); + assert thirdResult.equals(thirdList); + } } diff --git a/src/test/java/algorithms/patternFinding/KmpTest.java b/src/test/java/algorithms/patternFinding/KmpTest.java index 14b8d301..1d795d64 100644 --- a/src/test/java/algorithms/patternFinding/KmpTest.java +++ b/src/test/java/algorithms/patternFinding/KmpTest.java @@ -11,53 +11,53 @@ * Test cases for {@link KMP}. */ public class KmpTest { - @Test - public void test_findOccurrences_shouldReturnStartIndices() { - String seq = "abclaabcabcabc"; - String pattern = "abc"; - - List indices = KMP.findOccurrences(seq, pattern); - List expected = Arrays.asList(0, 5, 8, 11); - Assert.assertEquals(expected, indices); - } - - @Test - public void testEmptySequence_findOccurrences_shouldReturnStartIndices() { - String seq = ""; - String pattern = "a"; - - List indices = KMP.findOccurrences(seq, pattern); - List expected = new ArrayList<>(); - Assert.assertEquals(expected, indices); - } - - @Test - public void testNoOccurence_findOccurrences_shouldReturnStartIndices() { - String seq = "abcabcabc"; - String pattern = "noway"; - - List indices = KMP.findOccurrences(seq, pattern); - List expected = new ArrayList<>(); - Assert.assertEquals(expected, indices); - } - - @Test - public void testRepeatPatternOnly_findOccurrences_shouldReturnStartIndices() { - String seq = "abcabcabcabcabc"; - String pattern = "abc"; - - List indices = KMP.findOccurrences(seq, pattern); - List expected = Arrays.asList(0, 3, 6, 9, 12); - Assert.assertEquals(expected, indices); - } - - @Test - public void testAllSame_findOccurrences_shouldReturnStartIndices() { - String seq = "aaaaaaaaaaaaa"; - String pattern = "aa"; - - List indices = KMP.findOccurrences(seq, pattern); - List expected = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); - Assert.assertEquals(expected, indices); - } + @Test + public void test_findOccurrences_shouldReturnStartIndices() { + String seq = "abclaabcabcabc"; + String pattern = "abc"; + + List indices = KMP.findOccurrences(seq, pattern); + List expected = Arrays.asList(0, 5, 8, 11); + Assert.assertEquals(expected, indices); + } + + @Test + public void testEmptySequence_findOccurrences_shouldReturnStartIndices() { + String seq = ""; + String pattern = "a"; + + List indices = KMP.findOccurrences(seq, pattern); + List expected = new ArrayList<>(); + Assert.assertEquals(expected, indices); + } + + @Test + public void testNoOccurence_findOccurrences_shouldReturnStartIndices() { + String seq = "abcabcabc"; + String pattern = "noway"; + + List indices = KMP.findOccurrences(seq, pattern); + List expected = new ArrayList<>(); + Assert.assertEquals(expected, indices); + } + + @Test + public void testRepeatPatternOnly_findOccurrences_shouldReturnStartIndices() { + String seq = "abcabcabcabcabc"; + String pattern = "abc"; + + List indices = KMP.findOccurrences(seq, pattern); + List expected = Arrays.asList(0, 3, 6, 9, 12); + Assert.assertEquals(expected, indices); + } + + @Test + public void testAllSame_findOccurrences_shouldReturnStartIndices() { + String seq = "aaaaaaaaaaaaa"; + String pattern = "aa"; + + List indices = KMP.findOccurrences(seq, pattern); + List expected = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); + Assert.assertEquals(expected, indices); + } } diff --git a/src/test/java/algorithms/sorting/bubbleSort/BubbleSortTest.java b/src/test/java/algorithms/sorting/bubbleSort/BubbleSortTest.java index 828fba30..1ee78c78 100644 --- a/src/test/java/algorithms/sorting/bubbleSort/BubbleSortTest.java +++ b/src/test/java/algorithms/sorting/bubbleSort/BubbleSortTest.java @@ -10,32 +10,36 @@ * Test cases for {@link BubbleSort}. */ public class BubbleSortTest { - @Test - public void test_bubbleSort_shouldReturnSortedArray() { - int[] firstArray = - new int[] {2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2, 15, 12, 20, 21, 120, 11, 5, 7, - 85, 30}; - int[] firstResult = BubbleSort.sort(Arrays.copyOf(firstArray, firstArray.length)); - - int[] secondArray = - new int[] {9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, - 9, 10}; - int[] secondResult = BubbleSort.sort(Arrays.copyOf(secondArray, secondArray.length)); - - int[] thirdArray = new int[] {}; - int[] thirdResult = BubbleSort.sort(Arrays.copyOf(thirdArray, thirdArray.length)); - - int[] fourthArray = new int[] {1}; - int[] fourthResult = BubbleSort.sort(Arrays.copyOf(fourthArray, fourthArray.length)); - - Arrays.sort(firstArray); - Arrays.sort(secondArray); - Arrays.sort(thirdArray); - Arrays.sort(fourthArray); - - assertArrayEquals(firstResult, firstArray); - assertArrayEquals(secondResult, secondArray); - assertArrayEquals(thirdResult, thirdArray); - assertArrayEquals(fourthResult, fourthArray); - } + @Test + public void test_bubbleSort_shouldReturnSortedArray() { + int[] firstArray = + new int[] { + 2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2, 15, 12, 20, 21, 120, 11, 5, 7, + 85, 30 + }; + int[] firstResult = BubbleSort.sort(Arrays.copyOf(firstArray, firstArray.length)); + + int[] secondArray = + new int[] { + 9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10 + }; + int[] secondResult = BubbleSort.sort(Arrays.copyOf(secondArray, secondArray.length)); + + int[] thirdArray = new int[] {}; + int[] thirdResult = BubbleSort.sort(Arrays.copyOf(thirdArray, thirdArray.length)); + + int[] fourthArray = new int[] {1}; + int[] fourthResult = BubbleSort.sort(Arrays.copyOf(fourthArray, fourthArray.length)); + + Arrays.sort(firstArray); + Arrays.sort(secondArray); + Arrays.sort(thirdArray); + Arrays.sort(fourthArray); + + assertArrayEquals(firstResult, firstArray); + assertArrayEquals(secondResult, secondArray); + assertArrayEquals(thirdResult, thirdArray); + assertArrayEquals(fourthResult, fourthArray); + } } diff --git a/src/test/java/algorithms/sorting/countingSort/CountingSortTest.java b/src/test/java/algorithms/sorting/countingSort/CountingSortTest.java index adf1bff9..1f3343da 100644 --- a/src/test/java/algorithms/sorting/countingSort/CountingSortTest.java +++ b/src/test/java/algorithms/sorting/countingSort/CountingSortTest.java @@ -11,34 +11,38 @@ */ public class CountingSortTest { - @Test - public void countingSort_emptyArray_shouldReturnEmptyArray() { - int[] emptyArray = new int[10]; - int[] result = CountingSort.sort(emptyArray); - assertArrayEquals(emptyArray, result); - } - - @Test - public void test_countingSort_shouldReturnSortedArray() { - int[] firstArray = - new int[] {2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2, 15, 12, 20, 21, 120, 11, 5, 7, - 85, 30}; - int[] firstResult = CountingSort.sort(firstArray); - - int[] secondArray = - new int[] {9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, - 9, 10}; - int[] secondResult = CountingSort.sort(secondArray); - - int[] thirdArray = new int[] {}; - int[] thirdResult = CountingSort.sort(thirdArray); - - Arrays.sort(firstArray); // get expected result - Arrays.sort(secondArray); // get expected result - Arrays.sort(thirdArray); // get expected result - - assertArrayEquals(firstResult, firstArray); - assertArrayEquals(secondResult, secondArray); - assertArrayEquals(thirdResult, thirdArray); - } + @Test + public void countingSort_emptyArray_shouldReturnEmptyArray() { + int[] emptyArray = new int[10]; + int[] result = CountingSort.sort(emptyArray); + assertArrayEquals(emptyArray, result); + } + + @Test + public void test_countingSort_shouldReturnSortedArray() { + int[] firstArray = + new int[] { + 2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2, 15, 12, 20, 21, 120, 11, 5, 7, + 85, 30 + }; + int[] firstResult = CountingSort.sort(firstArray); + + int[] secondArray = + new int[] { + 9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10 + }; + int[] secondResult = CountingSort.sort(secondArray); + + int[] thirdArray = new int[] {}; + int[] thirdResult = CountingSort.sort(thirdArray); + + Arrays.sort(firstArray); // get expected result + Arrays.sort(secondArray); // get expected result + Arrays.sort(thirdArray); // get expected result + + assertArrayEquals(firstResult, firstArray); + assertArrayEquals(secondResult, secondArray); + assertArrayEquals(thirdResult, thirdArray); + } } diff --git a/src/test/java/algorithms/sorting/cyclicSort/generalised/CyclicSortTest.java b/src/test/java/algorithms/sorting/cyclicSort/generalised/CyclicSortTest.java index 93df2b01..a295122c 100644 --- a/src/test/java/algorithms/sorting/cyclicSort/generalised/CyclicSortTest.java +++ b/src/test/java/algorithms/sorting/cyclicSort/generalised/CyclicSortTest.java @@ -10,49 +10,53 @@ * Test cases for generalized {@link CyclicSort}. */ public class CyclicSortTest { - @Test - public void cyclicSort_emptyArray_shouldReturnEmptyArray() { - int[] emptyArray = new int[10]; - int[] expected = new int[10]; - CyclicSort.sort(emptyArray); - assertArrayEquals(emptyArray, expected); - } - - @Test - public void cyclicSort_positiveInputs_shouldReturnSortedArray() { - int[] array = - new int[] {2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2, 15, 12, 20, 21, 120, 11, 5, 7, - 85, 30}; - CyclicSort.sort(array); - - int[] expected = Arrays.copyOf(array, array.length); - Arrays.sort(expected); - - assertArrayEquals(array, expected); - } - - @Test - public void cyclicSort_someNegativeInputs_shouldReturnSortedArray() { - int[] array = - new int[] {2, 3, 4, 1, 2, -5, 6, 7, 10, -15, 20, 13, 15, 1, 2, 15, -12, 20, 21, 120, 11, 5, - 7, -85, 30}; - CyclicSort.sort(array); - - int[] expected = Arrays.copyOf(array, array.length); - Arrays.sort(expected); - - assertArrayEquals(array, expected); - } - - @Test - public void cyclicSort_alreadySorted_shouldReturnSortedArray() { - int[] array = - new int[] {1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15}; - CyclicSort.sort(array); - - int[] expected = Arrays.copyOf(array, array.length); - Arrays.sort(expected); - - assertArrayEquals(array, expected); - } + @Test + public void cyclicSort_emptyArray_shouldReturnEmptyArray() { + int[] emptyArray = new int[10]; + int[] expected = new int[10]; + CyclicSort.sort(emptyArray); + assertArrayEquals(emptyArray, expected); + } + + @Test + public void cyclicSort_positiveInputs_shouldReturnSortedArray() { + int[] array = + new int[] { + 2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2, 15, 12, 20, 21, 120, 11, 5, 7, + 85, 30 + }; + CyclicSort.sort(array); + + int[] expected = Arrays.copyOf(array, array.length); + Arrays.sort(expected); + + assertArrayEquals(array, expected); + } + + @Test + public void cyclicSort_someNegativeInputs_shouldReturnSortedArray() { + int[] array = + new int[] { + 2, 3, 4, 1, 2, -5, 6, 7, 10, -15, 20, 13, 15, 1, 2, 15, -12, 20, 21, 120, 11, 5, + 7, -85, 30 + }; + CyclicSort.sort(array); + + int[] expected = Arrays.copyOf(array, array.length); + Arrays.sort(expected); + + assertArrayEquals(array, expected); + } + + @Test + public void cyclicSort_alreadySorted_shouldReturnSortedArray() { + int[] array = + new int[] {1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15}; + CyclicSort.sort(array); + + int[] expected = Arrays.copyOf(array, array.length); + Arrays.sort(expected); + + assertArrayEquals(array, expected); + } } diff --git a/src/test/java/algorithms/sorting/cyclicSort/simple/FindFirstMissingNonNegativeTest.java b/src/test/java/algorithms/sorting/cyclicSort/simple/FindFirstMissingNonNegativeTest.java index 0cdf8c00..e10af613 100644 --- a/src/test/java/algorithms/sorting/cyclicSort/simple/FindFirstMissingNonNegativeTest.java +++ b/src/test/java/algorithms/sorting/cyclicSort/simple/FindFirstMissingNonNegativeTest.java @@ -8,39 +8,39 @@ * Test cases for simple {@link FindFirstMissingNonNegative}. */ public class FindFirstMissingNonNegativeTest { - @Test - public void findMissing_randomArray_shouldReturnFirstMissing() { - int[] arrayMissing3 = {0, 7, 1, 2, 4, 5, 6, 8}; // 3 - assertEquals(FindFirstMissingNonNegative.findMissing(arrayMissing3), 3); - } - - @Test - public void findMissing_duplicatesPresent_shouldReturnFirstMissing() { - int[] arrayMissing5 = {1, 3, 2, 0, 2, 7, 4}; // 5 - assertEquals(FindFirstMissingNonNegative.findMissing(arrayMissing5), 5); - } - - @Test - public void findMissing_firstNthPresent_shouldReturnFirstMissing() { - int[] arrayMissing7 = {0, 1, 2, 3, 4, 5, 6}; // 7 - assertEquals(FindFirstMissingNonNegative.findMissing(arrayMissing7), 7); - } - - @Test - public void findMissing_allNegatives_shouldReturnZero() { - int[] arrayAllNegatives = {-5, -10, -25, -500, -1}; // 0 - assertEquals(FindFirstMissingNonNegative.findMissing(arrayAllNegatives), 0); - } - - @Test - public void findMissing_allPositives_shouldReturnZero() { - int[] arrayAllPositives = {5, 10, 25, 500, 1}; // 0 - assertEquals(FindFirstMissingNonNegative.findMissing(arrayAllPositives), 0); - } - - @Test - public void findMissing_empty_shouldReturnZero() { - int[] empty = {}; - assertEquals(FindFirstMissingNonNegative.findMissing(empty), 0); - } + @Test + public void findMissing_randomArray_shouldReturnFirstMissing() { + int[] arrayMissing3 = {0, 7, 1, 2, 4, 5, 6, 8}; // 3 + assertEquals(FindFirstMissingNonNegative.findMissing(arrayMissing3), 3); + } + + @Test + public void findMissing_duplicatesPresent_shouldReturnFirstMissing() { + int[] arrayMissing5 = {1, 3, 2, 0, 2, 7, 4}; // 5 + assertEquals(FindFirstMissingNonNegative.findMissing(arrayMissing5), 5); + } + + @Test + public void findMissing_firstNthPresent_shouldReturnFirstMissing() { + int[] arrayMissing7 = {0, 1, 2, 3, 4, 5, 6}; // 7 + assertEquals(FindFirstMissingNonNegative.findMissing(arrayMissing7), 7); + } + + @Test + public void findMissing_allNegatives_shouldReturnZero() { + int[] arrayAllNegatives = {-5, -10, -25, -500, -1}; // 0 + assertEquals(FindFirstMissingNonNegative.findMissing(arrayAllNegatives), 0); + } + + @Test + public void findMissing_allPositives_shouldReturnZero() { + int[] arrayAllPositives = {5, 10, 25, 500, 1}; // 0 + assertEquals(FindFirstMissingNonNegative.findMissing(arrayAllPositives), 0); + } + + @Test + public void findMissing_empty_shouldReturnZero() { + int[] empty = {}; + assertEquals(FindFirstMissingNonNegative.findMissing(empty), 0); + } } diff --git a/src/test/java/algorithms/sorting/cyclicSort/simple/SimpleCyclicSortTest.java b/src/test/java/algorithms/sorting/cyclicSort/simple/SimpleCyclicSortTest.java index fbb211b0..9a7ddaec 100644 --- a/src/test/java/algorithms/sorting/cyclicSort/simple/SimpleCyclicSortTest.java +++ b/src/test/java/algorithms/sorting/cyclicSort/simple/SimpleCyclicSortTest.java @@ -8,30 +8,30 @@ * Test cases for Simple {@link CyclicSort}. */ public class SimpleCyclicSortTest { - @Test - public void test_cyclicSort_shouldReturnSortedArray() { - // unsorted array of 16 integers, from 0 to 15 - int[] firstArray = - new int[] {2, 5, 6, 4, 8, 15, 3, 1, 7, 12, 14, 0, 9, 13, 11, 10}; - int[] firstExpected = - new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; + @Test + public void test_cyclicSort_shouldReturnSortedArray() { + // unsorted array of 16 integers, from 0 to 15 + int[] firstArray = + new int[] {2, 5, 6, 4, 8, 15, 3, 1, 7, 12, 14, 0, 9, 13, 11, 10}; + int[] firstExpected = + new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; - // already sorted, do nothing - int[] secondArray = - new int[] {0, 1, 2, 3, 4, 5, 6}; - int[] secondExpected = - new int[] {0, 1, 2, 3, 4, 5, 6}; + // already sorted, do nothing + int[] secondArray = + new int[] {0, 1, 2, 3, 4, 5, 6}; + int[] secondExpected = + new int[] {0, 1, 2, 3, 4, 5, 6}; - // empty, do nothing - int[] thirdArray = new int[] {}; - int[] thirdExpected = new int[] {}; + // empty, do nothing + int[] thirdArray = new int[] {}; + int[] thirdExpected = new int[] {}; - CyclicSort.sort(firstArray); - CyclicSort.sort(secondArray); - CyclicSort.sort(thirdArray); + CyclicSort.sort(firstArray); + CyclicSort.sort(secondArray); + CyclicSort.sort(thirdArray); - assertArrayEquals(firstArray, firstExpected); - assertArrayEquals(secondArray, secondExpected); - assertArrayEquals(thirdArray, thirdExpected); - } + assertArrayEquals(firstArray, firstExpected); + assertArrayEquals(secondArray, secondExpected); + assertArrayEquals(thirdArray, thirdExpected); + } } diff --git a/src/test/java/algorithms/sorting/insertionSort/InsertionSortTest.java b/src/test/java/algorithms/sorting/insertionSort/InsertionSortTest.java index 5d777843..817b09bc 100644 --- a/src/test/java/algorithms/sorting/insertionSort/InsertionSortTest.java +++ b/src/test/java/algorithms/sorting/insertionSort/InsertionSortTest.java @@ -11,37 +11,41 @@ */ public class InsertionSortTest { - @Test - public void test_insertionSort_shouldReturnSortedArray() { - int[] firstArray = - new int[] {2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2, 15, 12, 20, 21, 120, 11, 5, 7, - 85, 30}; - int[] firstResult = InsertionSort.sort(Arrays.copyOf(firstArray, firstArray.length)); - - int[] secondArray = - new int[] {9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, - 9, 10}; - int[] secondResult = InsertionSort.sort(Arrays.copyOf(secondArray, secondArray.length)); - - int[] thirdArray = new int[] {}; - int[] thirdResult = InsertionSort.sort(Arrays.copyOf(thirdArray, thirdArray.length)); - - int[] fourthArray = new int[] {1}; - int[] fourthResult = InsertionSort.sort(Arrays.copyOf(fourthArray, fourthArray.length)); - - int[] fifthArray = new int[] {5, 1, 1, 2, 0, 0}; - int[] fifthResult = InsertionSort.sort(Arrays.copyOf(fifthArray, fifthArray.length)); - - Arrays.sort(firstArray); // get expected result - Arrays.sort(secondArray); // get expected result - Arrays.sort(thirdArray); // get expected result - Arrays.sort(fourthArray); // get expected result - Arrays.sort(fifthArray); // get expected result - - assertArrayEquals(firstResult, firstArray); - assertArrayEquals(secondResult, secondArray); - assertArrayEquals(thirdResult, thirdArray); - assertArrayEquals(fourthResult, fourthArray); - assertArrayEquals(fifthResult, fifthArray); - } + @Test + public void test_insertionSort_shouldReturnSortedArray() { + int[] firstArray = + new int[] { + 2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2, 15, 12, 20, 21, 120, 11, 5, 7, + 85, 30 + }; + int[] firstResult = InsertionSort.sort(Arrays.copyOf(firstArray, firstArray.length)); + + int[] secondArray = + new int[] { + 9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10 + }; + int[] secondResult = InsertionSort.sort(Arrays.copyOf(secondArray, secondArray.length)); + + int[] thirdArray = new int[] {}; + int[] thirdResult = InsertionSort.sort(Arrays.copyOf(thirdArray, thirdArray.length)); + + int[] fourthArray = new int[] {1}; + int[] fourthResult = InsertionSort.sort(Arrays.copyOf(fourthArray, fourthArray.length)); + + int[] fifthArray = new int[] {5, 1, 1, 2, 0, 0}; + int[] fifthResult = InsertionSort.sort(Arrays.copyOf(fifthArray, fifthArray.length)); + + Arrays.sort(firstArray); // get expected result + Arrays.sort(secondArray); // get expected result + Arrays.sort(thirdArray); // get expected result + Arrays.sort(fourthArray); // get expected result + Arrays.sort(fifthArray); // get expected result + + assertArrayEquals(firstResult, firstArray); + assertArrayEquals(secondResult, secondArray); + assertArrayEquals(thirdResult, thirdArray); + assertArrayEquals(fourthResult, fourthArray); + assertArrayEquals(fifthResult, fifthArray); + } } diff --git a/src/test/java/algorithms/sorting/mergeSort/iterative/MergeSortTest.java b/src/test/java/algorithms/sorting/mergeSort/iterative/MergeSortTest.java index 99965c52..7aa924a1 100644 --- a/src/test/java/algorithms/sorting/mergeSort/iterative/MergeSortTest.java +++ b/src/test/java/algorithms/sorting/mergeSort/iterative/MergeSortTest.java @@ -11,43 +11,46 @@ */ public class MergeSortTest { - @Test - public void test_insertionSort_shouldReturnSortedArray() { - int[] firstArray = - new int[] {2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2, 15, 12, 20, 21, 120, 11, 5, 7, - 85, 30}; - int[] firstResult = Arrays.copyOf(firstArray, firstArray.length); - MergeSort.sort(firstResult); - - int[] secondArray - = - new int[] {9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, - 9, 10}; - int[] secondResult = Arrays.copyOf(secondArray, secondArray.length); - MergeSort.sort(secondResult); - - int[] thirdArray = new int[] {}; - int[] thirdResult = Arrays.copyOf(thirdArray, thirdArray.length); - MergeSort.sort(thirdResult); - - int[] fourthArray = new int[] {1}; - int[] fourthResult = Arrays.copyOf(fourthArray, fourthArray.length); - MergeSort.sort(fourthResult); - - int[] fifthArray = new int[] {5, 1, 1, 2, 0, 0}; - int[] fifthResult = Arrays.copyOf(fifthArray, fifthArray.length); - MergeSort.sort(fifthResult); - - Arrays.sort(firstArray); // get expected result - Arrays.sort(secondArray); // get expected result - Arrays.sort(thirdArray); // get expected result - Arrays.sort(fourthArray); // get expected result - Arrays.sort(fifthArray); // get expected result - - assertArrayEquals(firstResult, firstArray); - assertArrayEquals(secondResult, secondArray); - assertArrayEquals(thirdResult, thirdArray); - assertArrayEquals(fourthResult, fourthArray); - assertArrayEquals(fifthResult, fifthArray); - } + @Test + public void test_insertionSort_shouldReturnSortedArray() { + int[] firstArray = + new int[] { + 2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2, 15, 12, 20, 21, 120, 11, 5, 7, + 85, 30 + }; + int[] firstResult = Arrays.copyOf(firstArray, firstArray.length); + MergeSort.sort(firstResult); + + int[] secondArray = + new int[] { + 9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10 + }; + int[] secondResult = Arrays.copyOf(secondArray, secondArray.length); + MergeSort.sort(secondResult); + + int[] thirdArray = new int[] {}; + int[] thirdResult = Arrays.copyOf(thirdArray, thirdArray.length); + MergeSort.sort(thirdResult); + + int[] fourthArray = new int[] {1}; + int[] fourthResult = Arrays.copyOf(fourthArray, fourthArray.length); + MergeSort.sort(fourthResult); + + int[] fifthArray = new int[] {5, 1, 1, 2, 0, 0}; + int[] fifthResult = Arrays.copyOf(fifthArray, fifthArray.length); + MergeSort.sort(fifthResult); + + Arrays.sort(firstArray); // get expected result + Arrays.sort(secondArray); // get expected result + Arrays.sort(thirdArray); // get expected result + Arrays.sort(fourthArray); // get expected result + Arrays.sort(fifthArray); // get expected result + + assertArrayEquals(firstResult, firstArray); + assertArrayEquals(secondResult, secondArray); + assertArrayEquals(thirdResult, thirdArray); + assertArrayEquals(fourthResult, fourthArray); + assertArrayEquals(fifthResult, fifthArray); + } } diff --git a/src/test/java/algorithms/sorting/mergeSort/recursive/MergeSortTest.java b/src/test/java/algorithms/sorting/mergeSort/recursive/MergeSortTest.java index 5680de94..c785a2dc 100644 --- a/src/test/java/algorithms/sorting/mergeSort/recursive/MergeSortTest.java +++ b/src/test/java/algorithms/sorting/mergeSort/recursive/MergeSortTest.java @@ -12,43 +12,46 @@ */ public class MergeSortTest { - @Test - public void test_insertionSort_shouldReturnSortedArray() { - int[] firstArray = - new int[] {2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2, 15, 12, 20, 21, 120, 11, 5, 7, - 85, 30}; - int[] firstResult = Arrays.copyOf(firstArray, firstArray.length); - MergeSort.sort(firstResult); - - int[] secondArray - = - new int[] {9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, - 9, 10}; - int[] secondResult = Arrays.copyOf(secondArray, secondArray.length); - MergeSort.sort(secondResult); - - int[] thirdArray = new int[] {}; - int[] thirdResult = Arrays.copyOf(thirdArray, thirdArray.length); - MergeSort.sort(thirdResult); - - int[] fourthArray = new int[] {1}; - int[] fourthResult = Arrays.copyOf(fourthArray, fourthArray.length); - MergeSort.sort(fourthResult); - - int[] fifthArray = new int[] {5, 1, 1, 2, 0, 0}; - int[] fifthResult = Arrays.copyOf(fifthArray, fifthArray.length); - MergeSort.sort(fifthResult); - - Arrays.sort(firstArray); // get expected result - Arrays.sort(secondArray); // get expected result - Arrays.sort(thirdArray); // get expected result - Arrays.sort(fourthArray); // get expected result - Arrays.sort(fifthArray); // get expected result - - assertArrayEquals(firstResult, firstArray); - assertArrayEquals(secondResult, secondArray); - assertArrayEquals(thirdResult, thirdArray); - assertArrayEquals(fourthResult, fourthArray); - assertArrayEquals(fifthResult, fifthArray); - } + @Test + public void test_insertionSort_shouldReturnSortedArray() { + int[] firstArray = + new int[] { + 2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2, 15, 12, 20, 21, 120, 11, 5, 7, + 85, 30 + }; + int[] firstResult = Arrays.copyOf(firstArray, firstArray.length); + MergeSort.sort(firstResult); + + int[] secondArray = + new int[] { + 9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10 + }; + int[] secondResult = Arrays.copyOf(secondArray, secondArray.length); + MergeSort.sort(secondResult); + + int[] thirdArray = new int[] {}; + int[] thirdResult = Arrays.copyOf(thirdArray, thirdArray.length); + MergeSort.sort(thirdResult); + + int[] fourthArray = new int[] {1}; + int[] fourthResult = Arrays.copyOf(fourthArray, fourthArray.length); + MergeSort.sort(fourthResult); + + int[] fifthArray = new int[] {5, 1, 1, 2, 0, 0}; + int[] fifthResult = Arrays.copyOf(fifthArray, fifthArray.length); + MergeSort.sort(fifthResult); + + Arrays.sort(firstArray); // get expected result + Arrays.sort(secondArray); // get expected result + Arrays.sort(thirdArray); // get expected result + Arrays.sort(fourthArray); // get expected result + Arrays.sort(fifthArray); // get expected result + + assertArrayEquals(firstResult, firstArray); + assertArrayEquals(secondResult, secondArray); + assertArrayEquals(thirdResult, thirdArray); + assertArrayEquals(fourthResult, fourthArray); + assertArrayEquals(fifthResult, fifthArray); + } } diff --git a/src/test/java/algorithms/sorting/quickSort/hoares/QuickSortTest.java b/src/test/java/algorithms/sorting/quickSort/hoares/QuickSortTest.java index b4135ad6..cbc61d6c 100644 --- a/src/test/java/algorithms/sorting/quickSort/hoares/QuickSortTest.java +++ b/src/test/java/algorithms/sorting/quickSort/hoares/QuickSortTest.java @@ -11,62 +11,65 @@ */ public class QuickSortTest { - @Test - public void test_selectionSort_shouldReturnSortedArray() { - int[] firstArray = - new int[] {22, 1, 6, 40, 32, 10, 18, 4, 50}; - int[] firstResult = Arrays.copyOf(firstArray, firstArray.length); - QuickSort.sort(firstResult); + @Test + public void test_selectionSort_shouldReturnSortedArray() { + int[] firstArray = + new int[] {22, 1, 6, 40, 32, 10, 18, 4, 50}; + int[] firstResult = Arrays.copyOf(firstArray, firstArray.length); + QuickSort.sort(firstResult); - int[] secondArray - = - new int[] {2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2, 15, 12, 20, 21, 120, 11, 5, 7, - 85, 30}; - int[] secondResult = Arrays.copyOf(secondArray, secondArray.length); - QuickSort.sort(secondResult); + int[] secondArray = + new int[] { + 2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2, 15, 12, 20, 21, 120, 11, 5, 7, + 85, 30 + }; + int[] secondResult = Arrays.copyOf(secondArray, secondArray.length); + QuickSort.sort(secondResult); - int[] thirdArray = - new int[] {9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, - 9, 10}; - int[] thirdResult = Arrays.copyOf(thirdArray, thirdArray.length); - QuickSort.sort(thirdResult); + int[] thirdArray = + new int[] { + 9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10 + }; + int[] thirdResult = Arrays.copyOf(thirdArray, thirdArray.length); + QuickSort.sort(thirdResult); - int[] fourthArray = new int[] {}; - int[] fourthResult = Arrays.copyOf(fourthArray, fourthArray.length); - QuickSort.sort(fourthResult); + int[] fourthArray = new int[] {}; + int[] fourthResult = Arrays.copyOf(fourthArray, fourthArray.length); + QuickSort.sort(fourthResult); - int[] fifthArray = new int[] {1}; - int[] fifthResult = Arrays.copyOf(fifthArray, fifthArray.length); - QuickSort.sort(fifthResult); + int[] fifthArray = new int[] {1}; + int[] fifthResult = Arrays.copyOf(fifthArray, fifthArray.length); + QuickSort.sort(fifthResult); - int[] sixthArray = new int[] {5, 1, 1, 2, 0, 0}; - int[] sixthResult = Arrays.copyOf(sixthArray, sixthArray.length); - QuickSort.sort(sixthResult); + int[] sixthArray = new int[] {5, 1, 1, 2, 0, 0}; + int[] sixthResult = Arrays.copyOf(sixthArray, sixthArray.length); + QuickSort.sort(sixthResult); - int[] seventhArray = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; - int[] seventhResult = Arrays.copyOf(seventhArray, seventhArray.length); - QuickSort.sort(seventhResult); + int[] seventhArray = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; + int[] seventhResult = Arrays.copyOf(seventhArray, seventhArray.length); + QuickSort.sort(seventhResult); - int[] eighthArray = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; - int[] eighthResult = Arrays.copyOf(eighthArray, eighthArray.length); - QuickSort.sort(eighthResult); + int[] eighthArray = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; + int[] eighthResult = Arrays.copyOf(eighthArray, eighthArray.length); + QuickSort.sort(eighthResult); - Arrays.sort(firstArray); // get expected result - Arrays.sort(secondArray); // get expected result - Arrays.sort(thirdArray); // get expected result - Arrays.sort(fourthArray); // get expected result - Arrays.sort(fifthArray); // get expected result - Arrays.sort(sixthArray); // get expected result - Arrays.sort(seventhArray); // get expected result - Arrays.sort(eighthArray); // get expected result + Arrays.sort(firstArray); // get expected result + Arrays.sort(secondArray); // get expected result + Arrays.sort(thirdArray); // get expected result + Arrays.sort(fourthArray); // get expected result + Arrays.sort(fifthArray); // get expected result + Arrays.sort(sixthArray); // get expected result + Arrays.sort(seventhArray); // get expected result + Arrays.sort(eighthArray); // get expected result - assertArrayEquals(firstResult, firstArray); - assertArrayEquals(secondResult, secondArray); - assertArrayEquals(thirdResult, thirdArray); - assertArrayEquals(fourthResult, fourthArray); - assertArrayEquals(fifthResult, fifthArray); - assertArrayEquals(sixthResult, sixthArray); - assertArrayEquals(seventhResult, seventhArray); - assertArrayEquals(eighthResult, eighthArray); - } + assertArrayEquals(firstResult, firstArray); + assertArrayEquals(secondResult, secondArray); + assertArrayEquals(thirdResult, thirdArray); + assertArrayEquals(fourthResult, fourthArray); + assertArrayEquals(fifthResult, fifthArray); + assertArrayEquals(sixthResult, sixthArray); + assertArrayEquals(seventhResult, seventhArray); + assertArrayEquals(eighthResult, eighthArray); + } } diff --git a/src/test/java/algorithms/sorting/quickSort/lomuto/QuickSortTest.java b/src/test/java/algorithms/sorting/quickSort/lomuto/QuickSortTest.java index 5b61efa8..c9868601 100644 --- a/src/test/java/algorithms/sorting/quickSort/lomuto/QuickSortTest.java +++ b/src/test/java/algorithms/sorting/quickSort/lomuto/QuickSortTest.java @@ -11,49 +11,52 @@ */ public class QuickSortTest { - @Test - public void test_selectionSort_shouldReturnSortedArray() { - int[] firstArray = - new int[] {2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2, 15, 12, 20, 21, 120, 11, 5, 7, - 85, 30}; - int[] firstResult = Arrays.copyOf(firstArray, firstArray.length); - QuickSort.sort(firstResult); - - int[] secondArray - = - new int[] {9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, - 9, 10}; - int[] secondResult = Arrays.copyOf(secondArray, secondArray.length); - QuickSort.sort(secondResult); - - int[] thirdArray = new int[] {}; - int[] thirdResult = Arrays.copyOf(thirdArray, thirdArray.length); - QuickSort.sort(thirdResult); - - int[] fourthArray = new int[] {1}; - int[] fourthResult = Arrays.copyOf(fourthArray, fourthArray.length); - QuickSort.sort(fourthResult); - - int[] fifthArray = new int[] {5, 1, 1, 2, 0, 0}; - int[] fifthResult = Arrays.copyOf(fifthArray, fifthArray.length); - QuickSort.sort(fifthResult); - - int[] sixthArray = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; - int[] sixthResult = Arrays.copyOf(sixthArray, sixthArray.length); - QuickSort.sort(sixthResult); - - Arrays.sort(firstArray); // get expected result - Arrays.sort(secondArray); // get expected result - Arrays.sort(thirdArray); // get expected result - Arrays.sort(fourthArray); // get expected result - Arrays.sort(fifthArray); // get expected result - Arrays.sort(sixthArray); // get expected result - - assertArrayEquals(firstResult, firstArray); - assertArrayEquals(secondResult, secondArray); - assertArrayEquals(thirdResult, thirdArray); - assertArrayEquals(fourthResult, fourthArray); - assertArrayEquals(fifthResult, fifthArray); - assertArrayEquals(sixthResult, sixthArray); - } + @Test + public void test_selectionSort_shouldReturnSortedArray() { + int[] firstArray = + new int[] { + 2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2, 15, 12, 20, 21, 120, 11, 5, 7, + 85, 30 + }; + int[] firstResult = Arrays.copyOf(firstArray, firstArray.length); + QuickSort.sort(firstResult); + + int[] secondArray = + new int[] { + 9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10 + }; + int[] secondResult = Arrays.copyOf(secondArray, secondArray.length); + QuickSort.sort(secondResult); + + int[] thirdArray = new int[] {}; + int[] thirdResult = Arrays.copyOf(thirdArray, thirdArray.length); + QuickSort.sort(thirdResult); + + int[] fourthArray = new int[] {1}; + int[] fourthResult = Arrays.copyOf(fourthArray, fourthArray.length); + QuickSort.sort(fourthResult); + + int[] fifthArray = new int[] {5, 1, 1, 2, 0, 0}; + int[] fifthResult = Arrays.copyOf(fifthArray, fifthArray.length); + QuickSort.sort(fifthResult); + + int[] sixthArray = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; + int[] sixthResult = Arrays.copyOf(sixthArray, sixthArray.length); + QuickSort.sort(sixthResult); + + Arrays.sort(firstArray); // get expected result + Arrays.sort(secondArray); // get expected result + Arrays.sort(thirdArray); // get expected result + Arrays.sort(fourthArray); // get expected result + Arrays.sort(fifthArray); // get expected result + Arrays.sort(sixthArray); // get expected result + + assertArrayEquals(firstResult, firstArray); + assertArrayEquals(secondResult, secondArray); + assertArrayEquals(thirdResult, thirdArray); + assertArrayEquals(fourthResult, fourthArray); + assertArrayEquals(fifthResult, fifthArray); + assertArrayEquals(sixthResult, sixthArray); + } } diff --git a/src/test/java/algorithms/sorting/quickSort/paranoid/QuickSortTest.java b/src/test/java/algorithms/sorting/quickSort/paranoid/QuickSortTest.java index 576c19dd..4708abe2 100644 --- a/src/test/java/algorithms/sorting/quickSort/paranoid/QuickSortTest.java +++ b/src/test/java/algorithms/sorting/quickSort/paranoid/QuickSortTest.java @@ -11,52 +11,55 @@ */ public class QuickSortTest { - @Test - public void test_selectionSort_shouldReturnSortedArray() { - int[] firstArray = - new int[] {2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2, 15, 12, 20, 21, 120, 11, 5, 7, - 85, 30}; - int[] firstResult = Arrays.copyOf(firstArray, firstArray.length); - QuickSort.sort(firstResult); - - int[] secondArray - = - new int[] {9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, - 9, 10}; - int[] secondResult = Arrays.copyOf(secondArray, secondArray.length); - QuickSort.sort(secondResult); - - int[] thirdArray = new int[] {}; - int[] thirdResult = Arrays.copyOf(thirdArray, thirdArray.length); - QuickSort.sort(thirdResult); - - int[] fourthArray = new int[] {1}; - int[] fourthResult = Arrays.copyOf(fourthArray, fourthArray.length); - QuickSort.sort(fourthResult); - - int[] fifthArray = new int[] {5, 1, 1, 2, 0, 0}; - int[] fifthResult = Arrays.copyOf(fifthArray, fifthArray.length); - QuickSort.sort(fifthResult); - - int[] sixthArray = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; - int[] sixthResult = Arrays.copyOf(sixthArray, sixthArray.length); - // testing for duplicate arrays of length >= 10, stack overflow is expected to happen - try { - QuickSort.sort(sixthResult); - } catch (StackOverflowError e) { - System.out.println("Stack overflow occurred for sixthResult"); - } + @Test + public void test_selectionSort_shouldReturnSortedArray() { + int[] firstArray = + new int[] { + 2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2, 15, 12, 20, 21, 120, 11, 5, 7, + 85, 30 + }; + int[] firstResult = Arrays.copyOf(firstArray, firstArray.length); + QuickSort.sort(firstResult); + + int[] secondArray = + new int[] { + 9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10 + }; + int[] secondResult = Arrays.copyOf(secondArray, secondArray.length); + QuickSort.sort(secondResult); + + int[] thirdArray = new int[] {}; + int[] thirdResult = Arrays.copyOf(thirdArray, thirdArray.length); + QuickSort.sort(thirdResult); + + int[] fourthArray = new int[] {1}; + int[] fourthResult = Arrays.copyOf(fourthArray, fourthArray.length); + QuickSort.sort(fourthResult); - Arrays.sort(firstArray); // get expected result - Arrays.sort(secondArray); // get expected result - Arrays.sort(thirdArray); // get expected result - Arrays.sort(fourthArray); // get expected result - Arrays.sort(fifthArray); // get expected result - - assertArrayEquals(firstResult, firstArray); - assertArrayEquals(secondResult, secondArray); - assertArrayEquals(thirdResult, thirdArray); - assertArrayEquals(fourthResult, fourthArray); - assertArrayEquals(fifthResult, fifthArray); - } + int[] fifthArray = new int[] {5, 1, 1, 2, 0, 0}; + int[] fifthResult = Arrays.copyOf(fifthArray, fifthArray.length); + QuickSort.sort(fifthResult); + + int[] sixthArray = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; + int[] sixthResult = Arrays.copyOf(sixthArray, sixthArray.length); + // testing for duplicate arrays of length >= 10, stack overflow is expected to happen + try { + QuickSort.sort(sixthResult); + } catch (StackOverflowError e) { + System.out.println("Stack overflow occurred for sixthResult"); + } + + Arrays.sort(firstArray); // get expected result + Arrays.sort(secondArray); // get expected result + Arrays.sort(thirdArray); // get expected result + Arrays.sort(fourthArray); // get expected result + Arrays.sort(fifthArray); // get expected result + + assertArrayEquals(firstResult, firstArray); + assertArrayEquals(secondResult, secondArray); + assertArrayEquals(thirdResult, thirdArray); + assertArrayEquals(fourthResult, fourthArray); + assertArrayEquals(fifthResult, fifthArray); + } } diff --git a/src/test/java/algorithms/sorting/quickSort/threeWayPartitioning/QuickSortTest.java b/src/test/java/algorithms/sorting/quickSort/threeWayPartitioning/QuickSortTest.java index 2bfd9b19..40b911b5 100644 --- a/src/test/java/algorithms/sorting/quickSort/threeWayPartitioning/QuickSortTest.java +++ b/src/test/java/algorithms/sorting/quickSort/threeWayPartitioning/QuickSortTest.java @@ -11,48 +11,52 @@ */ public class QuickSortTest { - @Test - public void test_selectionSort_shouldReturnSortedArray() { - int[] firstArray = - new int[] {2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2, 15, 12, 20, 21, 120, 11, 5, 7, - 85, 30}; - int[] firstResult = Arrays.copyOf(firstArray, firstArray.length); - QuickSort.sort(firstResult); - - int[] secondArray = - new int[] {9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, - 9, 10}; - int[] secondResult = Arrays.copyOf(secondArray, secondArray.length); - QuickSort.sort(secondResult); - - int[] thirdArray = new int[] {}; - int[] thirdResult = Arrays.copyOf(thirdArray, thirdArray.length); - QuickSort.sort(thirdResult); - - int[] fourthArray = new int[] {1}; - int[] fourthResult = Arrays.copyOf(fourthArray, fourthArray.length); - QuickSort.sort(fourthResult); - - int[] fifthArray = new int[] {5, 1, 1, 2, 0, 0}; - int[] fifthResult = Arrays.copyOf(fifthArray, fifthArray.length); - QuickSort.sort(fifthResult); - - int[] sixthArray = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; - int[] sixthResult = Arrays.copyOf(sixthArray, sixthArray.length); - QuickSort.sort(sixthResult); - - Arrays.sort(firstArray); // get expected result - Arrays.sort(secondArray); // get expected result - Arrays.sort(thirdArray); // get expected result - Arrays.sort(fourthArray); // get expected result - Arrays.sort(fifthArray); // get expected result - Arrays.sort(sixthArray); // get expected result - - assertArrayEquals(firstResult, firstArray); - assertArrayEquals(secondResult, secondArray); - assertArrayEquals(thirdResult, thirdArray); - assertArrayEquals(fourthResult, fourthArray); - assertArrayEquals(fifthResult, fifthArray); - assertArrayEquals(sixthResult, sixthArray); - } + @Test + public void test_selectionSort_shouldReturnSortedArray() { + int[] firstArray = + new int[] { + 2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2, 15, 12, 20, 21, 120, 11, 5, 7, + 85, 30 + }; + int[] firstResult = Arrays.copyOf(firstArray, firstArray.length); + QuickSort.sort(firstResult); + + int[] secondArray = + new int[] { + 9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10 + }; + int[] secondResult = Arrays.copyOf(secondArray, secondArray.length); + QuickSort.sort(secondResult); + + int[] thirdArray = new int[] {}; + int[] thirdResult = Arrays.copyOf(thirdArray, thirdArray.length); + QuickSort.sort(thirdResult); + + int[] fourthArray = new int[] {1}; + int[] fourthResult = Arrays.copyOf(fourthArray, fourthArray.length); + QuickSort.sort(fourthResult); + + int[] fifthArray = new int[] {5, 1, 1, 2, 0, 0}; + int[] fifthResult = Arrays.copyOf(fifthArray, fifthArray.length); + QuickSort.sort(fifthResult); + + int[] sixthArray = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; + int[] sixthResult = Arrays.copyOf(sixthArray, sixthArray.length); + QuickSort.sort(sixthResult); + + Arrays.sort(firstArray); // get expected result + Arrays.sort(secondArray); // get expected result + Arrays.sort(thirdArray); // get expected result + Arrays.sort(fourthArray); // get expected result + Arrays.sort(fifthArray); // get expected result + Arrays.sort(sixthArray); // get expected result + + assertArrayEquals(firstResult, firstArray); + assertArrayEquals(secondResult, secondArray); + assertArrayEquals(thirdResult, thirdArray); + assertArrayEquals(fourthResult, fourthArray); + assertArrayEquals(fifthResult, fifthArray); + assertArrayEquals(sixthResult, sixthArray); + } } diff --git a/src/test/java/algorithms/sorting/selectionSort/SelectionSortTest.java b/src/test/java/algorithms/sorting/selectionSort/SelectionSortTest.java index 378754db..8eca493d 100644 --- a/src/test/java/algorithms/sorting/selectionSort/SelectionSortTest.java +++ b/src/test/java/algorithms/sorting/selectionSort/SelectionSortTest.java @@ -11,37 +11,41 @@ */ public class SelectionSortTest { - @Test - public void test_selectionSort_shouldReturnSortedArray() { - int[] firstArray = - new int[] {2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2, 15, 12, 20, 21, 120, 11, 5, 7, - 85, 30}; - int[] firstResult = SelectionSort.sort(Arrays.copyOf(firstArray, firstArray.length)); - - int[] secondArray = - new int[] {9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, - 9, 10}; - int[] secondResult = SelectionSort.sort(Arrays.copyOf(secondArray, secondArray.length)); - - int[] thirdArray = new int[] {}; - int[] thirdResult = SelectionSort.sort(Arrays.copyOf(thirdArray, thirdArray.length)); - - int[] fourthArray = new int[] {1}; - int[] fourthResult = SelectionSort.sort(Arrays.copyOf(fourthArray, fourthArray.length)); - - int[] fifthArray = new int[] {5, 1, 1, 2, 0, 0}; - int[] fifthResult = SelectionSort.sort(Arrays.copyOf(fifthArray, fifthArray.length)); - - Arrays.sort(firstArray); // get expected result - Arrays.sort(secondArray); // get expected result - Arrays.sort(thirdArray); // get expected result - Arrays.sort(fourthArray); // get expected result - Arrays.sort(fifthArray); // get expected result - - assertArrayEquals(firstResult, firstArray); - assertArrayEquals(secondResult, secondArray); - assertArrayEquals(thirdResult, thirdArray); - assertArrayEquals(fourthResult, fourthArray); - assertArrayEquals(fifthResult, fifthArray); - } + @Test + public void test_selectionSort_shouldReturnSortedArray() { + int[] firstArray = + new int[] { + 2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2, 15, 12, 20, 21, 120, 11, 5, 7, + 85, 30 + }; + int[] firstResult = SelectionSort.sort(Arrays.copyOf(firstArray, firstArray.length)); + + int[] secondArray = + new int[] { + 9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10 + }; + int[] secondResult = SelectionSort.sort(Arrays.copyOf(secondArray, secondArray.length)); + + int[] thirdArray = new int[] {}; + int[] thirdResult = SelectionSort.sort(Arrays.copyOf(thirdArray, thirdArray.length)); + + int[] fourthArray = new int[] {1}; + int[] fourthResult = SelectionSort.sort(Arrays.copyOf(fourthArray, fourthArray.length)); + + int[] fifthArray = new int[] {5, 1, 1, 2, 0, 0}; + int[] fifthResult = SelectionSort.sort(Arrays.copyOf(fifthArray, fifthArray.length)); + + Arrays.sort(firstArray); // get expected result + Arrays.sort(secondArray); // get expected result + Arrays.sort(thirdArray); // get expected result + Arrays.sort(fourthArray); // get expected result + Arrays.sort(fifthArray); // get expected result + + assertArrayEquals(firstResult, firstArray); + assertArrayEquals(secondResult, secondArray); + assertArrayEquals(thirdResult, thirdArray); + assertArrayEquals(fourthResult, fourthArray); + assertArrayEquals(fifthResult, fifthArray); + } } diff --git a/src/test/java/dataStructures/hashSet/chaining/HashSetTest.java b/src/test/java/dataStructures/hashSet/chaining/HashSetTest.java index 8fe2d16b..63b2ad3b 100644 --- a/src/test/java/dataStructures/hashSet/chaining/HashSetTest.java +++ b/src/test/java/dataStructures/hashSet/chaining/HashSetTest.java @@ -14,73 +14,73 @@ * Test cases for {@link HashSet} implemented using Chaining to resolve collisions. */ public class HashSetTest { - @Test - public void testAdd_noDuplicates_shouldReturnTrue() { - HashSet hashSet = new HashSet<>(); - assertTrue(hashSet.add("Hello")); - assertTrue(hashSet.add("World")); - } + @Test + public void testAdd_noDuplicates_shouldReturnTrue() { + HashSet hashSet = new HashSet<>(); + assertTrue(hashSet.add("Hello")); + assertTrue(hashSet.add("World")); + } - @Test - public void testAdd_withDuplicates_shouldReturnFalse() { - HashSet hashSet = new HashSet<>(); - assertTrue(hashSet.add("Hello")); - assertTrue(hashSet.add("World")); - assertFalse(hashSet.add("Hello")); // Adding duplicate element should return false - } + @Test + public void testAdd_withDuplicates_shouldReturnFalse() { + HashSet hashSet = new HashSet<>(); + assertTrue(hashSet.add("Hello")); + assertTrue(hashSet.add("World")); + assertFalse(hashSet.add("Hello")); // Adding duplicate element should return false + } - @Test - public void testContains() { - HashSet hashSet = new HashSet<>(); - hashSet.add("Hello"); - hashSet.add("World"); - assertTrue(hashSet.contains("Hello")); - assertTrue(hashSet.contains("World")); - assertFalse(hashSet.contains("Universe")); // Element not in set - } + @Test + public void testContains() { + HashSet hashSet = new HashSet<>(); + hashSet.add("Hello"); + hashSet.add("World"); + assertTrue(hashSet.contains("Hello")); + assertTrue(hashSet.contains("World")); + assertFalse(hashSet.contains("Universe")); // Element not in set + } - @Test - public void testRemove() { - HashSet hashSet = new HashSet<>(); - hashSet.add("Hello"); - hashSet.add("World"); - assertTrue(hashSet.remove("Hello")); - assertFalse(hashSet.contains("Hello")); // Element should be removed - assertFalse(hashSet.remove("Universe")); // Removing non-existent element should return false - } + @Test + public void testRemove() { + HashSet hashSet = new HashSet<>(); + hashSet.add("Hello"); + hashSet.add("World"); + assertTrue(hashSet.remove("Hello")); + assertFalse(hashSet.contains("Hello")); // Element should be removed + assertFalse(hashSet.remove("Universe")); // Removing non-existent element should return false + } - @Test - public void testSize() { - HashSet hashSet = new HashSet<>(); - assertEquals(0, hashSet.size()); // Initial size should be 0 - hashSet.add("Hello"); - assertEquals(1, hashSet.size()); // Size after adding one element - hashSet.add("World"); - assertEquals(2, hashSet.size()); // Size after adding two elements - hashSet.remove("Hello"); - assertEquals(1, hashSet.size()); // Size after removing one element - } + @Test + public void testSize() { + HashSet hashSet = new HashSet<>(); + assertEquals(0, hashSet.size()); // Initial size should be 0 + hashSet.add("Hello"); + assertEquals(1, hashSet.size()); // Size after adding one element + hashSet.add("World"); + assertEquals(2, hashSet.size()); // Size after adding two elements + hashSet.remove("Hello"); + assertEquals(1, hashSet.size()); // Size after removing one element + } - @Test - public void testIsEmpty() { - HashSet hashSet = new HashSet<>(); - assertTrue(hashSet.isEmpty()); // Initial set should be empty - hashSet.add("Hello"); - assertFalse(hashSet.isEmpty()); // Set should not be empty after adding an element - hashSet.remove("Hello"); - assertTrue(hashSet.isEmpty()); // Set should be empty after removing the only element - } + @Test + public void testIsEmpty() { + HashSet hashSet = new HashSet<>(); + assertTrue(hashSet.isEmpty()); // Initial set should be empty + hashSet.add("Hello"); + assertFalse(hashSet.isEmpty()); // Set should not be empty after adding an element + hashSet.remove("Hello"); + assertTrue(hashSet.isEmpty()); // Set should be empty after removing the only element + } - @Test - public void test_hashSet_shouldNotHaveDuplicates() { - HashSet hashSet = new HashSet<>(); - hashSet.add("Hello"); - hashSet.add("World"); - hashSet.add("Hello"); - hashSet.add("World"); - List firstList = Stream.of("Hello", "World").sorted().collect(Collectors.toList()); - List firstResult = hashSet.toList().stream().sorted().collect(Collectors.toList()); + @Test + public void test_hashSet_shouldNotHaveDuplicates() { + HashSet hashSet = new HashSet<>(); + hashSet.add("Hello"); + hashSet.add("World"); + hashSet.add("Hello"); + hashSet.add("World"); + List firstList = Stream.of("Hello", "World").sorted().collect(Collectors.toList()); + List firstResult = hashSet.toList().stream().sorted().collect(Collectors.toList()); - assertEquals(firstList, firstResult); - } + assertEquals(firstList, firstResult); + } } diff --git a/src/test/java/dataStructures/hashSet/openAddressing/HashSetTest.java b/src/test/java/dataStructures/hashSet/openAddressing/HashSetTest.java index c9661326..6af466f4 100644 --- a/src/test/java/dataStructures/hashSet/openAddressing/HashSetTest.java +++ b/src/test/java/dataStructures/hashSet/openAddressing/HashSetTest.java @@ -14,127 +14,127 @@ * Test cases for {@link HashSet} implemented using Open Addressing to resolve collisions. */ public class HashSetTest { - @Test - public void testAdd_noDuplicates_shouldReturnTrue() { - HashSet hashSet = new HashSet<>(); - assertTrue(hashSet.add("Hello")); - assertTrue(hashSet.add("World")); - } - - @Test - public void testAdd_withDuplicates_shouldReturnFalse() { - HashSet hashSet = new HashSet<>(); - assertTrue(hashSet.add("Hello")); - assertTrue(hashSet.add("World")); - assertFalse(hashSet.add("Hello")); // Adding duplicate element should return false - } - - @Test - public void testContains() { - HashSet hashSet = new HashSet<>(); - hashSet.add("Hello"); - hashSet.add("World"); - assertTrue(hashSet.contains("Hello")); - assertTrue(hashSet.contains("World")); - assertFalse(hashSet.contains("Universe")); // Element not in set - } - - @Test - public void testRemove() { - HashSet hashSet = new HashSet<>(); - hashSet.add("Hello"); - hashSet.add("World"); - assertTrue(hashSet.remove("Hello")); - assertFalse(hashSet.contains("Hello")); // Element should be removed - assertFalse(hashSet.remove("Universe")); // Removing non-existent element should return false - } - - @Test - public void testSize() { - HashSet hashSet = new HashSet<>(); - assertEquals(0, hashSet.size()); // Initial size should be 0 - hashSet.add("Hello"); - assertEquals(1, hashSet.size()); // Size after adding one element - hashSet.add("World"); - assertEquals(2, hashSet.size()); // Size after adding two elements - hashSet.remove("Hello"); - assertEquals(1, hashSet.size()); // Size after removing one element - } - - @Test - public void testIsEmpty() { - HashSet hashSet = new HashSet<>(); - assertTrue(hashSet.isEmpty()); // Initial set should be empty - hashSet.add("Hello"); - assertFalse(hashSet.isEmpty()); // Set should not be empty after adding an element - hashSet.remove("Hello"); - assertTrue(hashSet.isEmpty()); // Set should be empty after removing the only element - } - - @Test - public void testContains_afterRemove() { - HashSet hashSet = new HashSet<>(); - Stream.iterate(0, i -> i + 1) // Populates the hashSet. - .limit(16) - .forEach(hashSet::add); - hashSet.remove(4); - assertTrue(hashSet.add(25)); // add should insert 25 at where 4 was at previously. - hashSet.remove(10); // Introduce a tombstone in the probe sequence for 25. - - assertTrue(hashSet.contains(25)); // contains should still find 25. - } - - @Test - public void testResize() { - // Create a HashSet with an initial capacity of 16 and load factor of 0.75 for testing. - HashSet set = new HashSet<>(); - for (int i = 1; i <= 12; i++) { - set.add(i); + @Test + public void testAdd_noDuplicates_shouldReturnTrue() { + HashSet hashSet = new HashSet<>(); + assertTrue(hashSet.add("Hello")); + assertTrue(hashSet.add("World")); } - // Verify that the HashSet has the initial capacity of 16. - assertEquals(16, set.capacity()); + @Test + public void testAdd_withDuplicates_shouldReturnFalse() { + HashSet hashSet = new HashSet<>(); + assertTrue(hashSet.add("Hello")); + assertTrue(hashSet.add("World")); + assertFalse(hashSet.add("Hello")); // Adding duplicate element should return false + } + + @Test + public void testContains() { + HashSet hashSet = new HashSet<>(); + hashSet.add("Hello"); + hashSet.add("World"); + assertTrue(hashSet.contains("Hello")); + assertTrue(hashSet.contains("World")); + assertFalse(hashSet.contains("Universe")); // Element not in set + } - // Adding one more element should trigger a resize operation to double the capacity to 32. - set.add(13); + @Test + public void testRemove() { + HashSet hashSet = new HashSet<>(); + hashSet.add("Hello"); + hashSet.add("World"); + assertTrue(hashSet.remove("Hello")); + assertFalse(hashSet.contains("Hello")); // Element should be removed + assertFalse(hashSet.remove("Universe")); // Removing non-existent element should return false + } - // Verify that the HashSet has resized and doubled its capacity to 32. - assertEquals(32, set.capacity()); + @Test + public void testSize() { + HashSet hashSet = new HashSet<>(); + assertEquals(0, hashSet.size()); // Initial size should be 0 + hashSet.add("Hello"); + assertEquals(1, hashSet.size()); // Size after adding one element + hashSet.add("World"); + assertEquals(2, hashSet.size()); // Size after adding two elements + hashSet.remove("Hello"); + assertEquals(1, hashSet.size()); // Size after removing one element + } + + @Test + public void testIsEmpty() { + HashSet hashSet = new HashSet<>(); + assertTrue(hashSet.isEmpty()); // Initial set should be empty + hashSet.add("Hello"); + assertFalse(hashSet.isEmpty()); // Set should not be empty after adding an element + hashSet.remove("Hello"); + assertTrue(hashSet.isEmpty()); // Set should be empty after removing the only element + } - // Removing elements until it triggers a resize operation to shrink the capacity. - // Currently size is 13. Therefore, remove 5 elements. - for (int i = 1; i <= 5; i++) { - set.remove(i); + @Test + public void testContains_afterRemove() { + HashSet hashSet = new HashSet<>(); + Stream.iterate(0, i -> i + 1) // Populates the hashSet. + .limit(16) + .forEach(hashSet::add); + hashSet.remove(4); + assertTrue(hashSet.add(25)); // add should insert 25 at where 4 was at previously. + hashSet.remove(10); // Introduce a tombstone in the probe sequence for 25. + + assertTrue(hashSet.contains(25)); // contains should still find 25. } - // Verify that the HashSet has not resized. - assertEquals(32, set.capacity()); + @Test + public void testResize() { + // Create a HashSet with an initial capacity of 16 and load factor of 0.75 for testing. + HashSet set = new HashSet<>(); + for (int i = 1; i <= 12; i++) { + set.add(i); + } - // Removing one more element should trigger a resize operation to halve the capacity back to 16. - set.remove(10); + // Verify that the HashSet has the initial capacity of 16. + assertEquals(16, set.capacity()); - // Verify that the HashSet has resized and halved its capacity back to 16. - assertEquals(16, set.capacity()); - } + // Adding one more element should trigger a resize operation to double the capacity to 32. + set.add(13); - @Test - public void testAdd_afterRemove() { - HashSet hashSet = new HashSet<>(); - // these elements all map to the same initial bucket, resulting in collisions. - hashSet.add(1); - hashSet.add(17); - hashSet.add(33); - // the hashSet will look like {1, 17, 33, ...} after the series of adds + // Verify that the HashSet has resized and doubled its capacity to 32. + assertEquals(32, set.capacity()); - hashSet.remove(17); - // hashSet now looks like {1, X, 33, ...} where X denotes a tombstone. + // Removing elements until it triggers a resize operation to shrink the capacity. + // Currently size is 13. Therefore, remove 5 elements. + for (int i = 1; i <= 5; i++) { + set.remove(i); + } - boolean isAdded = hashSet.add(33); // this should not be added into the hashSet. - assertFalse(isAdded); + // Verify that the HashSet has not resized. + assertEquals(32, set.capacity()); - List expectedList = List.of(1, 33); - List actualList = hashSet.toList(); + // Removing one more element should trigger a resize operation to halve the capacity back to 16. + set.remove(10); - assertEquals(expectedList, actualList); - } + // Verify that the HashSet has resized and halved its capacity back to 16. + assertEquals(16, set.capacity()); + } + + @Test + public void testAdd_afterRemove() { + HashSet hashSet = new HashSet<>(); + // these elements all map to the same initial bucket, resulting in collisions. + hashSet.add(1); + hashSet.add(17); + hashSet.add(33); + // the hashSet will look like {1, 17, 33, ...} after the series of adds + + hashSet.remove(17); + // hashSet now looks like {1, X, 33, ...} where X denotes a tombstone. + + boolean isAdded = hashSet.add(33); // this should not be added into the hashSet. + assertFalse(isAdded); + + List expectedList = List.of(1, 33); + List actualList = hashSet.toList(); + + assertEquals(expectedList, actualList); + } } diff --git a/src/test/java/dataStructures/heap/MaxHeapTest.java b/src/test/java/dataStructures/heap/MaxHeapTest.java index adb2f551..4c28bf2a 100644 --- a/src/test/java/dataStructures/heap/MaxHeapTest.java +++ b/src/test/java/dataStructures/heap/MaxHeapTest.java @@ -8,7 +8,7 @@ /** * Test cases for {@link MaxHeap}. * We will illustrate with the following elements: - * + *

* 13 13 * / \ INSERT 10 / \ REMOVE 10 * 9 11 -----------> 10 11 -----------> ORIGINAL @@ -16,7 +16,7 @@ * 4 7 6 3 4 9 6 3 * / \ / \ / * 2 1 2 1 7 - * + *

* 13 11 * / \ DEC 13 to 5 / \ INC 5 to 13 * 9 11 -----------> 9 6 ------------> ORIGINAL @@ -26,94 +26,95 @@ * 2 1 2 1 */ public class MaxHeapTest { - @Test - public void test_offer_shouldConstructArray() { - MaxHeap heap = new MaxHeap<>(); - heap.offer(13); - heap.offer(9); - heap.offer(11); - heap.offer(4); - heap.offer(7); - heap.offer(6); - heap.offer(3); - heap.offer(2); - heap.offer(1); - - String expected = "[13, 9, 11, 4, 7, 6, 3, 2, 1]"; - Assert.assertEquals(expected, heap.toString()); - - // insert 10 - heap.offer(10); - expected = "[13, 10, 11, 4, 9, 6, 3, 2, 1, 7]"; - Assert.assertEquals(expected, heap.toString()); - } - - @Test - public void test_bothHeapifyMethods_shouldConstructArray() { - MaxHeap heap = new MaxHeap<>(); - // 1st heapify, takes in a list of values - heap.heapify(Arrays.asList(1, 9, 11, 4, 7, 6, 3, 2, 13)); - String firstHeap = heap.toString(); - - // 2nd heapify, take sin sequence of values - heap.heapify(2, 9, 11, 4, 7, 6, 3, 13, 1); - String secondHeap = heap.toString(); - - Assert.assertEquals(firstHeap, secondHeap); // should be the same - String expected = "[13, 9, 11, 4, 7, 6, 3, 2, 1]"; - Assert.assertEquals(expected, firstHeap); - } - - @Test - public void test_size_shouldReturnSizeOfHeap() { - MaxHeap heap = new MaxHeap<>(); - heap.heapify(2, 9, 11, 4, 7, 6, 3, 13, 1); - - Assert.assertEquals(9, heap.size()); - } - - @Test - public void test_peek_shouldReturnHighestKey() { - MaxHeap heap = new MaxHeap<>(); - heap.heapify(2, 9, 11, 4, 7, 6, 3, 13, 1); - - Assert.assertEquals((Integer) 13, - (Integer) heap.peek()); // no method definition for comparing primitive int - } - - @Test - public void test_poll_shouldRemoveAndReturnHighestKey() { - MaxHeap heap = new MaxHeap<>(); - heap.heapify(2, 9, 11, 4, 7, 6, 3, 13, 1); - Integer removed = (Integer) heap.poll(); - - Assert.assertEquals((Integer) 13, removed); - Assert.assertEquals("[11, 9, 6, 4, 7, 1, 3, 2]", heap.toString()); - } - - @Test - public void test_remove_shouldRemoveSpecifiedKey() { - // As illustrated in the example above, we will insert and remove 10 - MaxHeap heap = new MaxHeap<>(); - heap.heapify(2, 9, 11, 4, 7, 6, 3, 13, 1); - heap.offer(10); - Assert.assertEquals("[13, 10, 11, 4, 9, 6, 3, 2, 1, 7]", heap.toString()); // check added - - heap.remove(10); - // Note that element 10 was specifically chosen to simulate the example shown (bubble up/down - // operations) - Assert.assertEquals("[13, 9, 11, 4, 7, 6, 3, 2, 1]", heap.toString()); - } - - @Test - public void test_decreaseKey_increaseKey_shouldUpdateHeapToMaintainProperty() { - // As illustrated in the example above, we will insert and remove 10 - MaxHeap heap = new MaxHeap<>(); - heap.heapify(2, 9, 11, 4, 7, 6, 3, 13, 1); - heap.decreaseKey(13, 5); - Assert.assertEquals("[11, 9, 6, 4, 7, 5, 3, 2, 1]", heap.toString()); // check updated - - heap.increaseKey(5, 13); - Assert.assertEquals("[13, 9, 11, 4, 7, 6, 3, 2, 1]", heap.toString()); - } + @Test + public void test_offer_shouldConstructArray() { + MaxHeap heap = new MaxHeap<>(); + heap.offer(13); + heap.offer(9); + heap.offer(11); + heap.offer(4); + heap.offer(7); + heap.offer(6); + heap.offer(3); + heap.offer(2); + heap.offer(1); + + String expected = "[13, 9, 11, 4, 7, 6, 3, 2, 1]"; + Assert.assertEquals(expected, heap.toString()); + + // insert 10 + heap.offer(10); + expected = "[13, 10, 11, 4, 9, 6, 3, 2, 1, 7]"; + Assert.assertEquals(expected, heap.toString()); + } + + @Test + public void test_bothHeapifyMethods_shouldConstructArray() { + MaxHeap heap = new MaxHeap<>(); + // 1st heapify, takes in a list of values + heap.heapify(Arrays.asList(1, 9, 11, 4, 7, 6, 3, 2, 13)); + String firstHeap = heap.toString(); + + // 2nd heapify, take sin sequence of values + heap.heapify(2, 9, 11, 4, 7, 6, 3, 13, 1); + String secondHeap = heap.toString(); + + Assert.assertEquals(firstHeap, secondHeap); // should be the same + String expected = "[13, 9, 11, 4, 7, 6, 3, 2, 1]"; + Assert.assertEquals(expected, firstHeap); + } + + @Test + public void test_size_shouldReturnSizeOfHeap() { + MaxHeap heap = new MaxHeap<>(); + heap.heapify(2, 9, 11, 4, 7, 6, 3, 13, 1); + + Assert.assertEquals(9, heap.size()); + } + + @Test + public void test_peek_shouldReturnHighestKey() { + MaxHeap heap = new MaxHeap<>(); + heap.heapify(2, 9, 11, 4, 7, 6, 3, 13, 1); + + Assert.assertEquals((Integer) 13, + heap.peek() + ); // no method definition for comparing primitive int + } + + @Test + public void test_poll_shouldRemoveAndReturnHighestKey() { + MaxHeap heap = new MaxHeap<>(); + heap.heapify(2, 9, 11, 4, 7, 6, 3, 13, 1); + Integer removed = heap.poll(); + + Assert.assertEquals((Integer) 13, removed); + Assert.assertEquals("[11, 9, 6, 4, 7, 1, 3, 2]", heap.toString()); + } + + @Test + public void test_remove_shouldRemoveSpecifiedKey() { + // As illustrated in the example above, we will insert and remove 10 + MaxHeap heap = new MaxHeap<>(); + heap.heapify(2, 9, 11, 4, 7, 6, 3, 13, 1); + heap.offer(10); + Assert.assertEquals("[13, 10, 11, 4, 9, 6, 3, 2, 1, 7]", heap.toString()); // check added + + heap.remove(10); + // Note that element 10 was specifically chosen to simulate the example shown (bubble up/down + // operations) + Assert.assertEquals("[13, 9, 11, 4, 7, 6, 3, 2, 1]", heap.toString()); + } + + @Test + public void test_decreaseKeyThenIncreaseKey_shouldUpdateHeapToMaintainProperty() { + // As illustrated in the example above, we will insert and remove 10 + MaxHeap heap = new MaxHeap<>(); + heap.heapify(2, 9, 11, 4, 7, 6, 3, 13, 1); + heap.decreaseKey(13, 5); + Assert.assertEquals("[11, 9, 6, 4, 7, 5, 3, 2, 1]", heap.toString()); // check updated + + heap.increaseKey(5, 13); + Assert.assertEquals("[13, 9, 11, 4, 7, 6, 3, 2, 1]", heap.toString()); + } } diff --git a/src/test/java/dataStructures/linkedList/LinkedListTest.java b/src/test/java/dataStructures/linkedList/LinkedListTest.java index e17e5816..98f7cfa5 100644 --- a/src/test/java/dataStructures/linkedList/LinkedListTest.java +++ b/src/test/java/dataStructures/linkedList/LinkedListTest.java @@ -9,101 +9,101 @@ * Test cases for {@link LinkedList}. */ public class LinkedListTest { - @Test - public void testInsert() { - LinkedList ll = new LinkedList<>(); - Assert.assertEquals(null, ll.toString()); - - ll.insertFront(2); - ll.insertEnd(3); - ll.insertEnd(5); - ll.insertFront(1); - Assert.assertEquals("1 2 3 5 ", ll.toString()); - - ll.insert(0, 0); - ll.insert(4, 4); - ll.insert(7, 6); - ll.insert(6, 6); - Assert.assertEquals("0 1 2 3 4 5 6 7 ", ll.toString()); - } - - @Test - public void testSearchAndGet() { - LinkedList ll = new LinkedList<>(); - ll.insertFront(2); - ll.insertEnd(3); - ll.insertEnd(5); - ll.insertFront(1); - ll.insert(0, 0); - ll.insert(4, 4); - ll.insert(7, 6); - ll.insert(6, 6); - - Node test1 = ll.get(4); - Assert.assertEquals("4", test1.toString()); - - Node test2 = ll.get(3); - Assert.assertEquals("3", test2.toString()); - - Integer test3 = ll.search(4); - Assert.assertEquals("4", test3.toString()); - - Integer test4 = ll.search(3); - Assert.assertEquals("3", test4.toString()); - - Assert.assertEquals("0 1 2 3 4 5 6 7 ", ll.toString()); - } - - @Test - public void testRemove() { - - LinkedList ll = new LinkedList<>(); - ll.insertFront(2); - ll.insertEnd(3); - ll.insertEnd(5); - ll.insertFront(1); - ll.insert(0, 0); - ll.insert(4, 4); - ll.insert(7, 6); - ll.insert(6, 6); - - ll.remove(5); - ll.delete(6); - ll.pop(); - ll.poll(); - - String expected = "1 2 3 4 "; - Assert.assertEquals(expected, ll.toString()); - } - - @Test - public void testReverse() { - LinkedList ll = new LinkedList<>(); - ll.reverse(); - Assert.assertEquals(null, ll.toString()); - - ll.insertFront(3); - ll.reverse(); - Assert.assertEquals("3 ", ll.toString()); - - ll.insertFront(2); - ll.insertFront(1); - ll.insertEnd(4); - ll.reverse(); - Assert.assertEquals("4 3 2 1 ", ll.toString()); - } - - @Test - public void testSort() { - LinkedList ll = new LinkedList<>(); - ll.insertFront(5); - ll.insertEnd(2); - ll.insertFront(7); - ll.insertEnd(3); - ll.insertEnd(4); - ll.insertEnd(3); - ll.sort(); - - Assert.assertEquals("2 3 3 4 5 7 ", ll.toString()); - } + @Test + public void testInsert() { + LinkedList ll = new LinkedList<>(); + Assert.assertNull(ll.toString()); + + ll.insertFront(2); + ll.insertEnd(3); + ll.insertEnd(5); + ll.insertFront(1); + Assert.assertEquals("1 2 3 5 ", ll.toString()); + + ll.insert(0, 0); + ll.insert(4, 4); + ll.insert(7, 6); + ll.insert(6, 6); + Assert.assertEquals("0 1 2 3 4 5 6 7 ", ll.toString()); + } + + @Test + public void testSearchAndGet() { + LinkedList ll = new LinkedList<>(); + ll.insertFront(2); + ll.insertEnd(3); + ll.insertEnd(5); + ll.insertFront(1); + ll.insert(0, 0); + ll.insert(4, 4); + ll.insert(7, 6); + ll.insert(6, 6); + + Node test1 = ll.get(4); + Assert.assertEquals("4", test1.toString()); + + Node test2 = ll.get(3); + Assert.assertEquals("3", test2.toString()); + + Integer test3 = ll.search(4); + Assert.assertEquals("4", test3.toString()); + + Integer test4 = ll.search(3); + Assert.assertEquals("3", test4.toString()); + + Assert.assertEquals("0 1 2 3 4 5 6 7 ", ll.toString()); + } + + @Test + public void testRemove() { + + LinkedList ll = new LinkedList<>(); + ll.insertFront(2); + ll.insertEnd(3); + ll.insertEnd(5); + ll.insertFront(1); + ll.insert(0, 0); + ll.insert(4, 4); + ll.insert(7, 6); + ll.insert(6, 6); + + ll.remove(5); + ll.delete(6); + ll.pop(); + ll.poll(); + + String expected = "1 2 3 4 "; + Assert.assertEquals(expected, ll.toString()); + } + + @Test + public void testReverse() { + LinkedList ll = new LinkedList<>(); + ll.reverse(); + Assert.assertNull(ll.toString()); + + ll.insertFront(3); + ll.reverse(); + Assert.assertEquals("3 ", ll.toString()); + + ll.insertFront(2); + ll.insertFront(1); + ll.insertEnd(4); + ll.reverse(); + Assert.assertEquals("4 3 2 1 ", ll.toString()); + } + + @Test + public void testSort() { + LinkedList ll = new LinkedList<>(); + ll.insertFront(5); + ll.insertEnd(2); + ll.insertFront(7); + ll.insertEnd(3); + ll.insertEnd(4); + ll.insertEnd(3); + ll.sort(); + + Assert.assertEquals("2 3 3 4 5 7 ", ll.toString()); + } } diff --git a/src/test/java/dataStructures/queue/MonotonicQueueTest.java b/src/test/java/dataStructures/queue/MonotonicQueueTest.java index c91c4885..8bca1530 100644 --- a/src/test/java/dataStructures/queue/MonotonicQueueTest.java +++ b/src/test/java/dataStructures/queue/MonotonicQueueTest.java @@ -7,54 +7,54 @@ * This class implements tests for the monotonic queue. */ public class MonotonicQueueTest { - @Test - public void testEmpty() { - MonotonicQueue q = new MonotonicQueue<>(); - Assert.assertEquals(true, q.isEmpty()); - Assert.assertEquals(null, q.max()); - Assert.assertEquals(null, q.pop()); - } + @Test + public void testEmpty() { + MonotonicQueue q = new MonotonicQueue<>(); + Assert.assertTrue(q.isEmpty()); + Assert.assertNull(q.max()); + Assert.assertNull(q.pop()); + } - @Test - public void testMax() { - MonotonicQueue q = new MonotonicQueue<>(); - q.push(2); - Assert.assertEquals("2", q.max().toString()); - q.push(7); - Assert.assertEquals("7", q.max().toString()); - q.push(1); - Assert.assertEquals("7", q.max().toString()); - q.push(7); - Assert.assertEquals("7", q.max().toString()); - q.push(5); - Assert.assertEquals("7", q.max().toString()); - q.push(4); - Assert.assertEquals("7", q.max().toString()); - q.push(3); - q.push(2); - q.push(5); - Assert.assertEquals("7", q.max().toString()); - } + @Test + public void testMax() { + MonotonicQueue q = new MonotonicQueue<>(); + q.push(2); + Assert.assertEquals("2", q.max().toString()); + q.push(7); + Assert.assertEquals("7", q.max().toString()); + q.push(1); + Assert.assertEquals("7", q.max().toString()); + q.push(7); + Assert.assertEquals("7", q.max().toString()); + q.push(5); + Assert.assertEquals("7", q.max().toString()); + q.push(4); + Assert.assertEquals("7", q.max().toString()); + q.push(3); + q.push(2); + q.push(5); + Assert.assertEquals("7", q.max().toString()); + } - @Test - public void testPop() { - MonotonicQueue q = new MonotonicQueue<>(); - q.push(2); - q.push(7); - q.push(1); - q.push(7); - q.push(5); - q.push(4); - q.push(3); - q.push(2); - q.push(5); - q.push(2); + @Test + public void testPop() { + MonotonicQueue q = new MonotonicQueue<>(); + q.push(2); + q.push(7); + q.push(1); + q.push(7); + q.push(5); + q.push(4); + q.push(3); + q.push(2); + q.push(5); + q.push(2); - Assert.assertEquals("7", q.pop().toString()); - Assert.assertEquals("7", q.pop().toString()); - Assert.assertEquals("5", q.pop().toString()); - q.pop(); - Assert.assertEquals("2", q.pop().toString()); - Assert.assertEquals(null, q.pop()); - } + Assert.assertEquals("7", q.pop().toString()); + Assert.assertEquals("7", q.pop().toString()); + Assert.assertEquals("5", q.pop().toString()); + q.pop(); + Assert.assertEquals("2", q.pop().toString()); + Assert.assertNull(q.pop()); + } } diff --git a/src/test/java/dataStructures/queue/dequeTest.java b/src/test/java/dataStructures/queue/dequeTest.java index d2612712..0ef262c1 100644 --- a/src/test/java/dataStructures/queue/dequeTest.java +++ b/src/test/java/dataStructures/queue/dequeTest.java @@ -11,53 +11,53 @@ * This class implements tests for the deque. */ public class DequeTest { - @Test - public void testEmpty() { - Deque d = new Deque<>(); - Assert.assertEquals(true, d.isEmpty()); - Assert.assertEquals(0, d.getSize()); - Assert.assertEquals(d.peekFirst(), null); - Assert.assertEquals(d.peekLast(), null); - } + @Test + public void testEmpty() { + Deque d = new Deque<>(); + Assert.assertTrue(d.isEmpty()); + Assert.assertEquals(0, d.getSize()); + Assert.assertNull(d.peekFirst()); + Assert.assertNull(d.peekLast()); + } - @Test - public void testInsertion() { - Deque d = new Deque<>(); - Assert.assertEquals("[]", d.toString()); - d.addElement(2); - d.addElement(3); - d.addFirst(1); - Assert.assertEquals("[ 1 2 3 ]", d.toString()); - Assert.assertEquals(3, d.getSize()); - } + @Test + public void testInsertion() { + Deque d = new Deque<>(); + Assert.assertEquals("[]", d.toString()); + d.addElement(2); + d.addElement(3); + d.addFirst(1); + Assert.assertEquals("[ 1 2 3 ]", d.toString()); + Assert.assertEquals(3, d.getSize()); + } - @Test - public void testPeek() { - Deque d = new Deque<>(); - Assert.assertEquals(null, d.peekFirst()); - Assert.assertEquals(null, d.peekLast()); - d.addElement(1); - d.addElement(2); - d.addElement(3); - d.peekLast(); - Assert.assertEquals(3, d.getSize()); - Assert.assertEquals(Optional.of(1).get(), d.peekFirst()); - Assert.assertEquals(Optional.of(3).get(), d.peekLast()); - } + @Test + public void testPeek() { + Deque d = new Deque<>(); + Assert.assertNull(d.peekFirst()); + Assert.assertNull(d.peekLast()); + d.addElement(1); + d.addElement(2); + d.addElement(3); + d.peekLast(); + Assert.assertEquals(3, d.getSize()); + Assert.assertEquals(Optional.of(1).get(), d.peekFirst()); + Assert.assertEquals(Optional.of(3).get(), d.peekLast()); + } - @Test - public void testPoll() { - Deque d = new Deque<>(); - Assert.assertEquals(null, d.pollFirst()); - Assert.assertEquals(null, d.pollLast()); - d.addElement(1); - d.addElement(2); - d.addElement(3); - Assert.assertEquals(Optional.of(1).get(), d.pollFirst()); - Assert.assertEquals(Optional.of(3).get(), d.pollLast()); - Assert.assertEquals(1, d.getSize()); - Assert.assertEquals("[ 2 ]", d.toString()); - Assert.assertEquals(Optional.of(2).get(), d.pollLast()); - Assert.assertEquals(0, d.getSize()); - } + @Test + public void testPoll() { + Deque d = new Deque<>(); + Assert.assertNull(d.pollFirst()); + Assert.assertNull(d.pollLast()); + d.addElement(1); + d.addElement(2); + d.addElement(3); + Assert.assertEquals(Optional.of(1).get(), d.pollFirst()); + Assert.assertEquals(Optional.of(3).get(), d.pollLast()); + Assert.assertEquals(1, d.getSize()); + Assert.assertEquals("[ 2 ]", d.toString()); + Assert.assertEquals(Optional.of(2).get(), d.pollLast()); + Assert.assertEquals(0, d.getSize()); + } } diff --git a/src/test/java/dataStructures/queue/queueTest.java b/src/test/java/dataStructures/queue/queueTest.java index 7d04fa38..132d2410 100644 --- a/src/test/java/dataStructures/queue/queueTest.java +++ b/src/test/java/dataStructures/queue/queueTest.java @@ -7,46 +7,46 @@ * This class implements the test cases for the queue. */ public class QueueTest { - @Test - public void testEmptyQueue() { - Queue q = new Queue<>(); - Assert.assertEquals(0, q.size()); - Assert.assertEquals(true, q.isEmpty()); - Assert.assertEquals(null, q.dequeue()); - } + @Test + public void testEmptyQueue() { + Queue q = new Queue<>(); + Assert.assertEquals(0, q.size()); + Assert.assertTrue(q.isEmpty()); + Assert.assertNull(q.dequeue()); + } - @Test - public void testEnqueue() { - Queue q = new Queue<>(); - q.enqueue(1); - q.enqueue(2); - q.enqueue(3); - Assert.assertEquals(3, q.size()); - } + @Test + public void testEnqueue() { + Queue q = new Queue<>(); + q.enqueue(1); + q.enqueue(2); + q.enqueue(3); + Assert.assertEquals(3, q.size()); + } - @Test - public void testPeek() { - Queue q = new Queue<>(); - q.enqueue(1); - Assert.assertEquals("1", q.peek().toString()); - q.enqueue(2); - q.enqueue(3); - q.peek(); - Assert.assertEquals("1", q.peek().toString()); - } + @Test + public void testPeek() { + Queue q = new Queue<>(); + q.enqueue(1); + Assert.assertEquals("1", q.peek().toString()); + q.enqueue(2); + q.enqueue(3); + q.peek(); + Assert.assertEquals("1", q.peek().toString()); + } - @Test - public void testDequeue() { - Queue q = new Queue<>(); - q.enqueue(1); - q.enqueue(2); - q.enqueue(3); - Assert.assertEquals("1", q.dequeue().toString()); - Assert.assertEquals(2, q.size()); - q.dequeue(); - Assert.assertEquals(1, q.size()); - Assert.assertEquals("3", q.dequeue().toString()); - Assert.assertEquals(0, q.size()); - } + @Test + public void testDequeue() { + Queue q = new Queue<>(); + q.enqueue(1); + q.enqueue(2); + q.enqueue(3); + Assert.assertEquals("1", q.dequeue().toString()); + Assert.assertEquals(2, q.size()); + q.dequeue(); + Assert.assertEquals(1, q.size()); + Assert.assertEquals("3", q.dequeue().toString()); + Assert.assertEquals(0, q.size()); + } } diff --git a/src/test/java/dataStructures/stack/StackTest.java b/src/test/java/dataStructures/stack/StackTest.java index 62b24956..02aa199c 100644 --- a/src/test/java/dataStructures/stack/StackTest.java +++ b/src/test/java/dataStructures/stack/StackTest.java @@ -7,28 +7,28 @@ * This class implements the test for the stack. */ public class StackTest { - @Test - public void testEmpty() { - Stack stk = new Stack<>(); - Assert.assertEquals(null, stk.pop()); - Assert.assertEquals(null, stk.peek()); - } + @Test + public void testEmpty() { + Stack stk = new Stack<>(); + Assert.assertNull(stk.pop()); + Assert.assertNull(stk.peek()); + } - @Test - public void testPopAndPeek() { - Stack stk = new Stack<>(1, 2, 3); - Assert.assertEquals("3", stk.peek().toString()); - Assert.assertEquals("3", stk.pop().toString()); - Assert.assertEquals("2", stk.peek().toString()); - } + @Test + public void testPopAndPeek() { + Stack stk = new Stack<>(1, 2, 3); + Assert.assertEquals("3", stk.peek().toString()); + Assert.assertEquals("3", stk.pop().toString()); + Assert.assertEquals("2", stk.peek().toString()); + } - @Test - public void testPush() { - Stack stk = new Stack<>(); - stk.push(1); - Assert.assertEquals("1", stk.peek().toString()); - stk.push(2); - stk.push(3); - Assert.assertEquals("3", stk.peek().toString()); - } + @Test + public void testPush() { + Stack stk = new Stack<>(); + stk.push(1); + Assert.assertEquals("1", stk.peek().toString()); + stk.push(2); + stk.push(3); + Assert.assertEquals("3", stk.peek().toString()); + } } diff --git a/src/test/java/random/andre/Test.java b/src/test/java/random/andre/Test.java new file mode 100644 index 00000000..be56064a --- /dev/null +++ b/src/test/java/random/andre/Test.java @@ -0,0 +1,44 @@ +package random.andre; + +import java.util.ArrayList; +import java.util.Arrays; + +import dataStructures.heap.MaxHeap; + +/** + * Basic testing of MaxHeap structure. + */ +public class Test { + /** + * Runs custom test. + * + * @param args unused. + */ + public static void main(String[] args) { + MaxHeap heap = new MaxHeap<>(); + heap.offer(1); + heap.offer(2); + heap.offer(9); + heap.offer(4); + heap.offer(5); + System.out.println(heap); + + System.out.println("Peek: " + heap.peek()); + System.out.println("Poll: " + heap.poll()); + System.out.println("Peek: " + heap.peek()); + + heap.offer(6); + + System.out.println("Peek: " + heap.peek()); + System.out.println("Poll: " + heap.poll()); + System.out.println("Poll: " + heap.poll()); + System.out.println(heap); + + heap.heapify(new ArrayList<>(Arrays.asList(5, 4, 6, 7, 2, 1, 9, 8, 0, 3))); + System.out.println(heap); + heap.remove(10); + heap.remove(5); + System.out.println(heap); + } +} + diff --git a/src/test/java/random/andre/lruCache/Test.java b/src/test/java/random/andre/lruCache/Test.java new file mode 100644 index 00000000..8e55c58f --- /dev/null +++ b/src/test/java/random/andre/lruCache/Test.java @@ -0,0 +1,27 @@ +package random.andre.lruCache; + +import dataStructures.lruCache.LRU; + +/** + * Basic testing. + */ +public class Test { + /** + * Runs the custom test. + * + * @param args unused. + */ + public static void main(String[] args) { + LRU lru = new LRU(4); + lru.update(5, "abc"); + lru.update(2, "def"); + lru.update(3, "ghi"); + lru.update(1, "jkl"); + lru.print(); // 5->2->3->1 + lru.update(2, "lmn"); + lru.print(); // 5->3->1->2 + lru.update(7, "opq"); // 5 should be evicted + lru.update(8, "rst"); // 3 should be evicted + lru.print(); // 1->2->7->8 + } +} diff --git a/src/test/java/random/andre/radixSort/Test.java b/src/test/java/random/andre/radixSort/Test.java new file mode 100644 index 00000000..2569c242 --- /dev/null +++ b/src/test/java/random/andre/radixSort/Test.java @@ -0,0 +1,62 @@ +package random.andre.radixSort; + +import algorithms.sorting.radixSort.RadixSort; + +/** + * Custom Test class for radixSort. To be phased out to JUnit tests. + */ +public class Test { + /** + * Helper method to print int elements in an array. + * + * @param arr The given array. + */ + private static void print(int[] arr) { + for (int num : arr) { + System.out.printf("%d ", num); + } + System.out.println(); + } + + /** + * Runs custom test cases. + * + * @param args unused. + */ + public static void main(String[] args) { + int[] test1 = + new int[] { + 2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 1, 2, 15, 12, 20, 21, 12, 11, 5, 7, 23, + 30 + }; + RadixSort.radixSort(test1); + print(test1); + // Arrays.sort(test1); + // print(test1); + + int[] test2 = + new int[] { + 9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10 + }; + RadixSort.radixSort(test2); + print(test2); + // Arrays.sort(test2); + // print(test2); + + int[] test3 = new int[] {9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100}; + RadixSort.radixSort(test3); + print(test3); + // Arrays.sort(test3); + // print(test3); + + int[] test4 = + new int[] { + 1 << 25 + 123, 1 << 28 + 340230, 34954043, 5678, 987654, 121234, 1, 0, 20493943, + 1 << 20 + 543298 + }; + // System.out.println(1 << 25 + 123); + RadixSort.radixSort(test4); + print(test4); + } +} diff --git a/src/test/java/random/andre/segmentTree/Test.java b/src/test/java/random/andre/segmentTree/Test.java new file mode 100644 index 00000000..5f1e06bc --- /dev/null +++ b/src/test/java/random/andre/segmentTree/Test.java @@ -0,0 +1,21 @@ +package random.andre.segmentTree; + +import dataStructures.segmentTree.SegmentTree; + +/** + * Basic testing. + */ +public class Test { + /** + * Custom test case for SegmentTree. + * + * @param args unused. + */ + public static void main(String[] args) { + int[] t = new int[] {1, 2, 3, 4, 5, 6, 7}; + SegmentTree tree = new SegmentTree(t); + System.out.println(tree.sumRange(2, 5)); + tree.update(3, 10); + System.out.println(tree.sumRange(2, 5)); + } +} diff --git a/src/test/java/random/andre/segmentTree/improved/Test.java b/src/test/java/random/andre/segmentTree/improved/Test.java new file mode 100644 index 00000000..53af119c --- /dev/null +++ b/src/test/java/random/andre/segmentTree/improved/Test.java @@ -0,0 +1,21 @@ +package random.andre.segmentTree.improved; + +import dataStructures.segmentTree.improvedSegmentTree.ImprovedSegmentTree; + +/** + * Basic testing. + */ +public class Test { + /** + * Custom test case for ImprovedSegmentTree. + * + * @param args unused. + */ + public static void main(String[] args) { + int[] t = new int[] {1, 2, 3, 4, 5, 6, 7}; + ImprovedSegmentTree tree = new ImprovedSegmentTree(t); + System.out.println(tree.sumRange(2, 5)); + tree.update(3, 10); + System.out.println(tree.sumRange(2, 5)); + } +} diff --git a/src/test/java/random/changxian/linkedList/Test.java b/src/test/java/random/changxian/linkedList/Test.java new file mode 100644 index 00000000..4045995f --- /dev/null +++ b/src/test/java/random/changxian/linkedList/Test.java @@ -0,0 +1,89 @@ +package random.changxian.linkedList; + +import dataStructures.linkedList.LinkedList; + +/** + * Basic Testing. + */ +public class Test { + /** + * Run custom test script. + * + * @param args unused. + */ + public static void main(String[] args) { + /* + * Testing insert methods + */ + System.out.println("Testing insert methods: "); + LinkedList linkedList = new LinkedList<>(); + System.out.println(linkedList); + linkedList.insertFront(2); + linkedList.insertEnd(3); + linkedList.insertEnd(5); + linkedList.insertFront(1); + System.out.println(linkedList); + linkedList.insert(0, 0); + linkedList.insert(4, 4); + linkedList.insert(6, 7); + linkedList.insert(7, 6); + linkedList.insert(6, 6); + System.out.println(linkedList); + System.out.println(); + + /* + * Testing search and get methods + */ + System.out.println("Testing search & get methods: "); + System.out.println(linkedList.search(4)); + System.out.println(linkedList.search(3)); + System.out.println(linkedList.get(3)); + System.out.println(linkedList.get(10)); + System.out.println(linkedList); + System.out.println(); + + /* + * Testing remove methods + */ + System.out.println("Testing remove methods: "); + linkedList.remove(5); + linkedList.delete(6); + linkedList.pop(); + linkedList.poll(); + System.out.println(linkedList); + System.out.println(); + + /* + * Testing reverse of Linked Lists + */ + System.out.println("Testing reverse method: "); + LinkedList newLinkedList = new LinkedList<>(); + newLinkedList.reverse(); + System.out.println(newLinkedList); + newLinkedList.insertFront(3); + System.out.println(newLinkedList); + linkedList.reverse(); + System.out.println(linkedList); + linkedList.reverse(); + System.out.println(linkedList); + System.out.println(); + + /* + * Test sorting + * Note: testing with entirely new nodes and linked list to avoid mutating linkedList + */ + LinkedList sortLinkedList = new LinkedList<>(); + sortLinkedList.insertFront(5); + sortLinkedList.insertEnd(2); + sortLinkedList.insertFront(7); + sortLinkedList.insertEnd(3); + sortLinkedList.insertEnd(4); + sortLinkedList.insertEnd(3); + System.out.println(sortLinkedList); + sortLinkedList.sort(); + System.out.println(sortLinkedList); + sortLinkedList.insertFront(8); + sortLinkedList.sort(); + System.out.println(sortLinkedList); + } +} diff --git a/src/test/java/random/changxian/minimumSpanningTree/kruskals/Test.java b/src/test/java/random/changxian/minimumSpanningTree/kruskals/Test.java new file mode 100644 index 00000000..5b325a34 --- /dev/null +++ b/src/test/java/random/changxian/minimumSpanningTree/kruskals/Test.java @@ -0,0 +1,48 @@ +package random.changxian.minimumSpanningTree.kruskals; + +import algorithms.minimumSpanningTree.kruskals.Kruskal; + +/** + * Basic testing. + * Motivating Example: Minimum Cost to Connect All Points + * A -9- C -2- E + * / / \ \ + * 3 4 7 2 + * / / \ / + * F -1- B --5-- D + */ +public class Test { + /** + * Runs the custom test. + * + * @param args unused. + */ + public static void main(String[] args) { + int[][] adjM = new int[6][6]; + adjM[0][2] = 9; + adjM[2][0] = 9; + + adjM[0][5] = 3; + adjM[5][0] = 3; + + adjM[1][5] = 1; + adjM[5][1] = 1; + + adjM[1][3] = 5; + adjM[3][1] = 5; + + adjM[1][2] = 4; + adjM[2][1] = 4; + + adjM[2][3] = 7; + adjM[3][2] = 7; + + adjM[2][4] = 2; + adjM[4][2] = 2; + + adjM[3][4] = 2; + adjM[4][3] = 2; + Kruskal kruskalAlgor = new Kruskal(); + System.out.println(kruskalAlgor.minCostConnectPoints(adjM)); + } +} diff --git a/src/test/java/random/changxian/minimumSpanningTree/prims/Test.java b/src/test/java/random/changxian/minimumSpanningTree/prims/Test.java new file mode 100644 index 00000000..73f9d116 --- /dev/null +++ b/src/test/java/random/changxian/minimumSpanningTree/prims/Test.java @@ -0,0 +1,49 @@ +package random.changxian.minimumSpanningTree.prims; + +import algorithms.minimumSpanningTree.prims.Prim; + +/** + * Basic testing. + * Motivating Example: Minimum Cost to Connect All Points + * A -9- C -2- E + * / / \ \ + * 3 4 7 2 + * / / \ / + * F -1- B --5-- D + */ +public class Test { + /** + * Runs the custom test. + * + * @param args unused. + */ + public static void main(String[] args) { + int[][] adjM = new int[6][6]; + adjM[0][2] = 9; + adjM[2][0] = 9; + + adjM[0][5] = 3; + adjM[5][0] = 3; + + adjM[1][5] = 1; + adjM[5][1] = 1; + + adjM[1][3] = 5; + adjM[3][1] = 5; + + adjM[1][2] = 4; + adjM[2][1] = 4; + + adjM[2][3] = 7; + adjM[3][2] = 7; + + adjM[2][4] = 2; + adjM[4][2] = 2; + + adjM[3][4] = 2; + adjM[4][3] = 2; + Prim primsAlgor = new Prim(); + System.out.println(primsAlgor.minCostConnectPoints(adjM)); + System.out.println(primsAlgor.minCostConnectPoints2(adjM)); + } +} diff --git a/src/test/java/random/changxian/stackAndQueue/monotonicQueue/Test.java b/src/test/java/random/changxian/stackAndQueue/monotonicQueue/Test.java new file mode 100644 index 00000000..2851a1db --- /dev/null +++ b/src/test/java/random/changxian/stackAndQueue/monotonicQueue/Test.java @@ -0,0 +1,37 @@ +package random.changxian.stackAndQueue.monotonicQueue; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import dataStructures.queue.MonotonicQueue; + +/** + * Basic Testing. + * Maintains a non-increasing queue structure; useful for maximum/minimum sliding window fixed + * length problem + * Expected behaviour: Numbers that come before a larger number will be displayed as that larger + * number + */ +public class Test { + /** + * Runs the custom test. + * + * @param args unused. + */ + public static void main(String[] args) { + List toInsert = + new ArrayList<>(Arrays.asList(2, 7, 1, 3, 7, 5, 4, 3, 2, 5)); + MonotonicQueue mq = new MonotonicQueue<>(); + for (int num : toInsert) { + System.out.printf("Pushing %d%n", num); + mq.push(num); + System.out.printf("Max in queue now is %d%n", mq.max()); + } + + System.out.println("\nPopping thrice: "); + for (int i = 0; i < 7; i++) { + System.out.println(mq.pop()); + } + } +} diff --git a/src/test/java/random/changxian/stackAndQueue/queue/Test.java b/src/test/java/random/changxian/stackAndQueue/queue/Test.java new file mode 100644 index 00000000..f49eb6a3 --- /dev/null +++ b/src/test/java/random/changxian/stackAndQueue/queue/Test.java @@ -0,0 +1,26 @@ +package random.changxian.stackAndQueue.queue; + +import dataStructures.queue.Queue; + +/** + * Basic Testing. + */ +public class Test { + /** + * Runs the custom test. + * + * @param args unused. + */ + public static void main(String[] args) { + Queue myQueue = new Queue<>(); + System.out.println("Adding 7, 17, 27"); + myQueue.enqueue(7); + myQueue.enqueue(17); + myQueue.enqueue(27); + System.out.println("size: " + myQueue.size()); + System.out.println("peek: " + myQueue.peek()); + System.out.println("dequeue: " + myQueue.dequeue()); + System.out.println("peek: " + myQueue.peek()); + System.out.println("size: " + myQueue.size()); + } +} diff --git a/src/test/java/random/changxian/stackAndQueue/stack/Test.java b/src/test/java/random/changxian/stackAndQueue/stack/Test.java new file mode 100644 index 00000000..b3bb71eb --- /dev/null +++ b/src/test/java/random/changxian/stackAndQueue/stack/Test.java @@ -0,0 +1,37 @@ +package random.changxian.stackAndQueue.stack; + +import dataStructures.stack.Stack; + +/** + * Basic Testing. + */ +public class Test { + /** + * Runs custom test script. + * + * @param args unused. + */ + public static void main(String[] args) { + System.out.println("Testing on empty stack: "); + Stack myStack = new Stack<>(); + System.out.println(myStack.pop()); + System.out.println(myStack.peek()); + + System.out.println("Initialize stack with 'abc', 'def', 'ghi'"); + myStack = new Stack<>("abc", "def", "ghi"); + + System.out.println("call peek(): "); + System.out.println(myStack.peek()); + + System.out.println("call pop(): "); + System.out.println(myStack.pop()); + + System.out.println("call peek(): "); + System.out.println(myStack.peek()); + + System.out.println("pushing an element: "); + myStack.push("midnight prince"); + System.out.println("peek to see pushed element: "); + System.out.println(myStack.peek()); + } +} diff --git a/src/test/java/random/kaiting/avlTree/Test.java b/src/test/java/random/kaiting/avlTree/Test.java new file mode 100644 index 00000000..fc232d40 --- /dev/null +++ b/src/test/java/random/kaiting/avlTree/Test.java @@ -0,0 +1,75 @@ +package random.kaiting.avlTree; + +import dataStructures.avlTree.AVLTree; + +/** + * Basic testing. + * Note that a tree structure can be entirely + * determined with one of the following: + * 1. In-order + pre-order + * 2. In-order + post-order + * 3. In-order + level-order + */ +public class Test { + /** + * Runs the custom test. + * + * @param args unused. + */ + public static void main(String[] args) { + AVLTree tree = new AVLTree<>(); + System.out.println("Inserting..."); + tree.insert(5); + tree.insert(4); + tree.insert(8); + tree.insert(6); + tree.insert(7); + tree.insert(3); + tree.insert(1); + + // tree: + // 5 + // / \ + // 3 7 + // / \ / \ + // 1 4 6 8 + // + + Integer x = 6; + System.out.println("parent of " + x + " is " + tree.search(x).parent.key.toString()); + + System.out.println("Deleting..."); + tree.delete(5); + tree.delete(7); + tree.printInorder(); + tree.printPreorder(); + tree.printLevelorder(); + + System.out.println("Inserting..."); + tree.insert(9); + tree.insert(2); + tree.printInorder(); + tree.printPreorder(); + tree.printPostorder(); + + // updated tree: + // 6 + // / \ + // 3 8 + // / \ \ + // 1 4 9 + // \ + // 2 + + x = 4; + System.out.println("parent of " + x + " is " + tree.search(x).parent.key.toString()); + + System.out.println("Testing successors & predecessors queries..."); + System.out.println(tree.predecessor(1)); + System.out.println(tree.predecessor(6)); + + System.out.println(tree.successor(1)); + System.out.println(tree.successor(6)); + System.out.println(tree.successor(9)); + } +} diff --git a/src/test/java/randomTests/kaiting/quickSort/test.java b/src/test/java/random/kaiting/quickSort/Test.java similarity index 99% rename from src/test/java/randomTests/kaiting/quickSort/test.java rename to src/test/java/random/kaiting/quickSort/Test.java index d9f45613..3269873d 100644 --- a/src/test/java/randomTests/kaiting/quickSort/test.java +++ b/src/test/java/random/kaiting/quickSort/Test.java @@ -23,4 +23,4 @@ // } // System.out.println(); // } -//} \ No newline at end of file +//} diff --git a/src/test/java/random/kaiting/trie/TrieTest.java b/src/test/java/random/kaiting/trie/TrieTest.java new file mode 100644 index 00000000..ab4bc78b --- /dev/null +++ b/src/test/java/random/kaiting/trie/TrieTest.java @@ -0,0 +1,29 @@ +package random.kaiting.trie; + +import dataStructures.trie.Trie; + +/** + * Basic testing of trie. + */ +public class TrieTest { + /** + * Runs the custom test. + * + * @param args unused. + */ + public static void main(String[] args) { + Trie trie = new Trie(); + trie.insert("apple"); + trie.insert("coconut"); + trie.insert("apollo"); + trie.insert("fail"); + trie.insert("failure"); + + System.out.println(trie.search("apple")); + System.out.println(trie.search("app")); + System.out.println(trie.startsWith("app")); + trie.prune("fail"); + System.out.println(trie.search("fail")); + System.out.println(trie.search("failure")); + } +} diff --git a/src/test/java/random/kaiting/trie/legacy/TrieTest.java b/src/test/java/random/kaiting/trie/legacy/TrieTest.java new file mode 100644 index 00000000..eca86e9a --- /dev/null +++ b/src/test/java/random/kaiting/trie/legacy/TrieTest.java @@ -0,0 +1,24 @@ +package random.kaiting.trie.legacy; + +import dataStructures.trie.legacy.Trie; + +/** + * Basic testing of trie. + */ +public class TrieTest { + /** + * Runs the custom test. + * + * @param args unused. + */ + public static void main(String[] args) { + Trie trie = new Trie(); + trie.insert("apple"); + trie.insert("coconut"); + trie.insert("apollo"); + + System.out.println(trie.search("apple")); + System.out.println(trie.search("app")); + System.out.println(trie.startsWith("app")); + } +} diff --git a/src/test/java/random/owen/unionFind/matrixToListConverter/MatrixConverterTest.java b/src/test/java/random/owen/unionFind/matrixToListConverter/MatrixConverterTest.java new file mode 100644 index 00000000..e558eab4 --- /dev/null +++ b/src/test/java/random/owen/unionFind/matrixToListConverter/MatrixConverterTest.java @@ -0,0 +1,76 @@ +package random.owen.unionFind.matrixToListConverter; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.junit.Test; + +import algorithms.util.matrixToListConverter; +import algorithms.util.matrixToListConverter.InvalidMatrixException; + +/** + * Test cases for {@link matrixToListConverter}. + */ +public class MatrixConverterTest { + @Test + public void test_convertArray_shouldReturnAccurateRepresentation() { + // empty matrix + int[][] firstMatrix = new int[0][0]; + ArrayList[] firstList = new ArrayList[0]; + List[] firstResult = matrixToListConverter.convert(firstMatrix); + + // symmetric matrix (undirected graph) + int[][] secondMatrix = { + {0, 1, 1}, + {1, 0, 1}, + {1, 1, 0}, + }; + ArrayList[] secondList = new ArrayList[3]; + for (int i = 0; i < 3; i++) { + secondList[i] = new ArrayList<>(); + } + Collections.addAll(secondList[0], 1, 2); + Collections.addAll(secondList[1], 0, 2); + Collections.addAll(secondList[2], 0, 1); + List[] secondResult = matrixToListConverter.convert(secondMatrix); + + //asymmetric matrix (directed graph) + int[][] thirdMatrix = { + {0, 0, 1}, + {1, 0, 0}, + {1, 1, 1}, + }; + ArrayList[] thirdList = new ArrayList[3]; + for (int i = 0; i < 3; i++) { + thirdList[i] = new ArrayList<>(); + } + Collections.addAll(thirdList[0], 2); + Collections.addAll(thirdList[1], 0); + Collections.addAll(thirdList[2], 0, 1, 2); + List[] thirdResult = matrixToListConverter.convert(thirdMatrix); + + //invalid matrix (n x m graph, n != m) + int[][] fourthMatrix = { + {0, 0}, + {1, 0}, + {1, 1}, + }; + List[] fourthList = new ArrayList[3]; + + assert Arrays.equals(firstList, firstResult); + for (int i = 0; i < 1; i++) { + assert secondList[i].equals(secondResult[i]); + } + for (int i = 0; i < 1; i++) { + assert thirdList[i].equals(thirdResult[i]); + } + try { + matrixToListConverter.convert(fourthMatrix); + } catch (InvalidMatrixException error) { + assert error.getMessage() == "The matrix must be N x N, not N x M!"; + } + + } +} diff --git a/src/test/java/random/owen/unionFind/quickFind/generalised/Test.java b/src/test/java/random/owen/unionFind/quickFind/generalised/Test.java new file mode 100644 index 00000000..2090bcb7 --- /dev/null +++ b/src/test/java/random/owen/unionFind/quickFind/generalised/Test.java @@ -0,0 +1,68 @@ +package random.owen.unionFind.quickFind.generalised; + +import java.util.ArrayList; +import java.util.List; + +import dataStructures.disjointSet.quickFind.generalised.QuickFind; + +/** + * Some simple tests. + */ +public class Test { + /** + * Runs custom test script. + * + * @param args unused. + */ + public static void main(String[] args) { + List lst = new ArrayList<>(); + lst.add("Dog"); + lst.add("Egg"); + lst.add("Carabiner"); + lst.add("Cat"); + lst.add("Potatoes"); + lst.add("Hamster"); + lst.add("Rope"); + lst.add("Fox"); + lst.add("Chalk"); + for (int i = 0; i < lst.size(); i++) { + System.out.println("Added " + lst.get(i)); + } + + QuickFind qf = new QuickFind<>(lst); + qf.union("Dog", "Cat"); + qf.union("Fox", "Hamster"); + qf.union("Fox", "Cat"); + + qf.union("Carabiner", "Rope"); + qf.union("Chalk", "Rope"); + + qf.union("Egg", "Potatoes"); + + List comp1 = qf.retrieveComponent("Hamster"); + System.out.print("Comp1: "); + for (int i = 0; i < comp1.size(); i++) { + System.out.print(comp1.get(i) + ", "); + } + + List comp2 = qf.retrieveComponent("Chalk"); + System.out.print("\nComp2: "); + for (int i = 0; i < comp2.size(); i++) { + System.out.print(comp2.get(i) + ", "); + } + + List comp3 = qf.retrieveComponent("Egg"); + System.out.print("\nComp3: "); + for (int i = 0; i < comp3.size(); i++) { + System.out.print(comp3.get(i) + ", "); + } + + qf.add("Coconut"); + qf.union("Egg", "Coconut"); + List update = qf.retrieveComponent("Egg"); + System.out.print("\nUpdate Comp3: "); + for (int i = 0; i < update.size(); i++) { + System.out.print(update.get(i) + ", "); + } + } +} diff --git a/src/test/java/random/owen/unionFind/quickFind/simplified/Test.java b/src/test/java/random/owen/unionFind/quickFind/simplified/Test.java new file mode 100644 index 00000000..32c6c03c --- /dev/null +++ b/src/test/java/random/owen/unionFind/quickFind/simplified/Test.java @@ -0,0 +1,43 @@ +package random.owen.unionFind.quickFind.simplified; + +import java.util.List; + +import dataStructures.disjointSet.quickFind.simplified.QuickFind; + +/** + * Basic testing. + */ +public class Test { + /** + * Runs custom test script. + * + * @param args unused. + */ + public static void main(String[] args) { + QuickFind qf = new QuickFind(10); + + qf.union(1, 9); + qf.union(4, 6); + qf.union(1, 4); + + qf.union(2, 8); + qf.union(5, 7); + qf.union(2, 7); + + List comp1 = qf.retrieveComponent(1); + for (int i = 0; i < comp1.size(); i++) { + System.out.printf("%d, ", comp1.get(i)); + } + System.out.println("\nTesting next component:"); + List comp2 = qf.retrieveComponent(2); + for (int i = 0; i < comp2.size(); i++) { + System.out.printf("%d, ", comp2.get(i)); + } + System.out.println("\nTesting next component:"); + List comp3 = qf.retrieveComponent(3); + for (int i = 0; i < comp3.size(); i++) { + System.out.printf("%d, ", comp3.get(i)); + } + System.out.println(); + } +} diff --git a/src/test/java/random/owen/unionFind/weightedUnion/Test.java b/src/test/java/random/owen/unionFind/weightedUnion/Test.java new file mode 100644 index 00000000..3a60e800 --- /dev/null +++ b/src/test/java/random/owen/unionFind/weightedUnion/Test.java @@ -0,0 +1,50 @@ +package random.owen.unionFind.weightedUnion; + +import java.util.ArrayList; +import java.util.List; + +import dataStructures.disjointSet.weightedUnion.Union; + +/** + * Some simple tests. + */ +public class Test { + /** + * Runs the custom test. + * + * @param args unused. + */ + public static void main(String[] args) { + List lst = new ArrayList<>(); + lst.add("Dog"); + lst.add("Egg"); + lst.add("Carabiner"); + lst.add("Cat"); + lst.add("Potatoes"); + lst.add("Hamster"); + lst.add("Rope"); + lst.add("Fox"); + lst.add("Chalk"); + for (int i = 0; i < lst.size(); i++) { + System.out.println("Added " + lst.get(i)); + } + + Union wu = new Union<>(lst); + wu.combine("Dog", "Cat"); + wu.combine("Fox", "Hamster"); + wu.combine("Fox", "Cat"); + System.out.println(wu.isSameComponent("Dog", "Fox")); // true + System.out.println(wu.isSameComponent("Rope", "Fox")); // false + + wu.combine("Carabiner", "Rope"); + wu.combine("Chalk", "Rope"); + System.out.println(wu.isSameComponent("Rope", "Carabiner")); // true + + wu.combine("Egg", "Potatoes"); + System.out.println(wu.isSameComponent("Egg", "pot")); // duuhh.. + false + + wu.add("Coconut"); + wu.combine("Egg", "Coconut"); + System.out.println(wu.isSameComponent("Coconut", "Potatoes")); // true + } +} diff --git a/src/test/java/randomTests/andre/Test.java b/src/test/java/randomTests/andre/Test.java deleted file mode 100644 index 09937fda..00000000 --- a/src/test/java/randomTests/andre/Test.java +++ /dev/null @@ -1,44 +0,0 @@ -package randomTests.andre; - -import java.util.ArrayList; -import java.util.Arrays; - -import dataStructures.heap.MaxHeap; - -/** - * Basic testing of MaxHeap structure. - */ -public class Test { - /** - * Runs custom test. - * - * @param args unused. - */ - public static void main(String[] args) { - MaxHeap heap = new MaxHeap<>(); - heap.offer(1); - heap.offer(2); - heap.offer(9); - heap.offer(4); - heap.offer(5); - System.out.println(heap); - - System.out.println("Peek: " + heap.peek()); - System.out.println("Poll: " + heap.poll()); - System.out.println("Peek: " + heap.peek()); - - heap.offer(6); - - System.out.println("Peek: " + heap.peek()); - System.out.println("Poll: " + heap.poll()); - System.out.println("Poll: " + heap.poll()); - System.out.println(heap); - - heap.heapify(new ArrayList<>(Arrays.asList(5, 4, 6, 7, 2, 1, 9, 8, 0, 3))); - System.out.println(heap); - heap.remove(10); - heap.remove(5); - System.out.println(heap); - } -} - diff --git a/src/test/java/randomTests/andre/lruCache/test.java b/src/test/java/randomTests/andre/lruCache/test.java deleted file mode 100644 index 29e4cff2..00000000 --- a/src/test/java/randomTests/andre/lruCache/test.java +++ /dev/null @@ -1,27 +0,0 @@ -package randomTests.andre.lruCache; - -import dataStructures.lruCache.LRU; - -/** - * Basic testing. - */ -public class Test { - /** - * Runs the custom test. - * - * @param args unused. - */ - public static void main(String[] args) { - LRU lru = new LRU(4); - lru.update(5, "abc"); - lru.update(2, "def"); - lru.update(3, "ghi"); - lru.update(1, "jkl"); - lru.print(); // 5->2->3->1 - lru.update(2, "lmn"); - lru.print(); // 5->3->1->2 - lru.update(7, "opq"); // 5 should be evicted - lru.update(8, "rst"); // 3 should be evicted - lru.print(); // 1->2->7->8 - } -} diff --git a/src/test/java/randomTests/andre/radixSort/Test.java b/src/test/java/randomTests/andre/radixSort/Test.java deleted file mode 100644 index fdc8b4ab..00000000 --- a/src/test/java/randomTests/andre/radixSort/Test.java +++ /dev/null @@ -1,56 +0,0 @@ -package randomTests.andre.radixSort; - -import algorithms.sorting.radixSort.RadixSort; - -/** - * Custom Test class for radixSort. To be phased out to JUnit tests. - */ -public class Test { - /** - * Helper method to print int elements in an array. - * - * @param arr The given array. - */ - private static void print(int[] arr) { - for (int num : arr) { - System.out.print(String.format("%d ", num)); - } - System.out.println(); - } - - /** - * Runs custom test cases. - * - * @param args unused. - */ - public static void main(String[] args) { - int[] test1 = - new int[] {2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 1, 2, 15, 12, 20, 21, 12, 11, 5, 7, 23, - 30}; - RadixSort.radixSort(test1); - print(test1); - // Arrays.sort(test1); - // print(test1); - - int[] test2 = - new int[] {9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, - 9, 10}; - RadixSort.radixSort(test2); - print(test2); - // Arrays.sort(test2); - // print(test2); - - int[] test3 = new int[] {9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100}; - RadixSort.radixSort(test3); - print(test3); - // Arrays.sort(test3); - // print(test3); - - int[] test4 = - new int[] {1 << 25 + 123, 1 << 28 + 340230, 34954043, 5678, 987654, 121234, 1, 0, 20493943, - 1 << 20 + 543298}; - // System.out.println(1 << 25 + 123); - RadixSort.radixSort(test4); - print(test4); - } -} diff --git a/src/test/java/randomTests/andre/segmentTree/improved/test.java b/src/test/java/randomTests/andre/segmentTree/improved/test.java deleted file mode 100644 index 5ceaffef..00000000 --- a/src/test/java/randomTests/andre/segmentTree/improved/test.java +++ /dev/null @@ -1,21 +0,0 @@ -package randomTests.andre.segmentTree.improved; - -import dataStructures.segmentTree.improvedSegmentTree.ImprovedSegmentTree; - -/** - * Basic testing. - */ -public class Test { - /** - * Custom test case for ImprovedSegmentTree. - * - * @param args unused. - */ - public static void main(String[] args) { - int[] t = new int[] {1, 2, 3, 4, 5, 6, 7}; - ImprovedSegmentTree tree = new ImprovedSegmentTree(t); - System.out.println(tree.sumRange(2, 5)); - tree.update(3, 10); - System.out.println(tree.sumRange(2, 5)); - } -} diff --git a/src/test/java/randomTests/andre/segmentTree/test.java b/src/test/java/randomTests/andre/segmentTree/test.java deleted file mode 100644 index 07ee86a4..00000000 --- a/src/test/java/randomTests/andre/segmentTree/test.java +++ /dev/null @@ -1,21 +0,0 @@ -package randomTests.andre.segmentTree; - -import dataStructures.segmentTree.SegmentTree; - -/** - * Basic testing. - */ -public class Test { - /** - * Custom test case for SegmentTree. - * - * @param args unused. - */ - public static void main(String[] args) { - int[] t = new int[] {1, 2, 3, 4, 5, 6, 7}; - SegmentTree tree = new SegmentTree(t); - System.out.println(tree.sumRange(2, 5)); - tree.update(3, 10); - System.out.println(tree.sumRange(2, 5)); - } -} diff --git a/src/test/java/randomTests/changxian/linkedList/Test.java b/src/test/java/randomTests/changxian/linkedList/Test.java deleted file mode 100644 index e722fc3a..00000000 --- a/src/test/java/randomTests/changxian/linkedList/Test.java +++ /dev/null @@ -1,89 +0,0 @@ -package randomTests.changxian.linkedList; - -import dataStructures.linkedList.LinkedList; - -/** - * Basic Testing. - */ -public class Test { - /** - * Run custom test script. - * - * @param args unused. - */ - public static void main(String[] args) { - /* - * Testing insert methods - */ - System.out.println("Testing insert methods: "); - LinkedList linkedList = new LinkedList<>(); - System.out.println(linkedList); - linkedList.insertFront(2); - linkedList.insertEnd(3); - linkedList.insertEnd(5); - linkedList.insertFront(1); - System.out.println(linkedList); - linkedList.insert(0, 0); - linkedList.insert(4, 4); - linkedList.insert(6, 7); - linkedList.insert(7, 6); - linkedList.insert(6, 6); - System.out.println(linkedList); - System.out.println(); - - /* - * Testing search and get methods - */ - System.out.println("Testing search & get methods: "); - System.out.println(linkedList.search(4)); - System.out.println(linkedList.search(3)); - System.out.println(linkedList.get(3)); - System.out.println(linkedList.get(10)); - System.out.println(linkedList); - System.out.println(); - - /* - * Testing remove methods - */ - System.out.println("Testing remove methods: "); - linkedList.remove(5); - linkedList.delete(6); - linkedList.pop(); - linkedList.poll(); - System.out.println(linkedList); - System.out.println(); - - /* - * Testing reverse of Linked Lists - */ - System.out.println("Testing reverse method: "); - LinkedList newLinkedList = new LinkedList<>(); - newLinkedList.reverse(); - System.out.println(newLinkedList); - newLinkedList.insertFront(3); - System.out.println(newLinkedList); - linkedList.reverse(); - System.out.println(linkedList); - linkedList.reverse(); - System.out.println(linkedList); - System.out.println(); - - /* - * Test sorting - * Note: testing with entirely new nodes and linked list to avoid mutating linkedList - */ - LinkedList sortLinkedList = new LinkedList<>(); - sortLinkedList.insertFront(5); - sortLinkedList.insertEnd(2); - sortLinkedList.insertFront(7); - sortLinkedList.insertEnd(3); - sortLinkedList.insertEnd(4); - sortLinkedList.insertEnd(3); - System.out.println(sortLinkedList); - sortLinkedList.sort(); - System.out.println(sortLinkedList); - sortLinkedList.insertFront(8); - sortLinkedList.sort(); - System.out.println(sortLinkedList); - } -} diff --git a/src/test/java/randomTests/changxian/minimumSpanningTree/kruskals/test.java b/src/test/java/randomTests/changxian/minimumSpanningTree/kruskals/test.java deleted file mode 100644 index 9451ae52..00000000 --- a/src/test/java/randomTests/changxian/minimumSpanningTree/kruskals/test.java +++ /dev/null @@ -1,49 +0,0 @@ -package randomTests.changxian.minimumSpanningTree.kruskals; - -import algorithms.minimumSpanningTree.kruskals.Kruskal; - -/** - * Basic testing. - * Motivating Example: Minimum Cost to Connect All Points - * A -9- C -2- E - * / / \ \ - * 3 4 7 2 - * / / \ / - * F -1- B --5-- D - */ - -public class Test { - /** - * Runs the custom test. - * - * @param args unused. - */ - public static void main(String[] args) { - int[][] adjM = new int[6][6]; - adjM[0][2] = 9; - adjM[2][0] = 9; - - adjM[0][5] = 3; - adjM[5][0] = 3; - - adjM[1][5] = 1; - adjM[5][1] = 1; - - adjM[1][3] = 5; - adjM[3][1] = 5; - - adjM[1][2] = 4; - adjM[2][1] = 4; - - adjM[2][3] = 7; - adjM[3][2] = 7; - - adjM[2][4] = 2; - adjM[4][2] = 2; - - adjM[3][4] = 2; - adjM[4][3] = 2; - Kruskal kruskalAlgor = new Kruskal(); - System.out.println(kruskalAlgor.minCostConnectPoints(adjM)); - } -} diff --git a/src/test/java/randomTests/changxian/minimumSpanningTree/prims/test.java b/src/test/java/randomTests/changxian/minimumSpanningTree/prims/test.java deleted file mode 100644 index 455ebe29..00000000 --- a/src/test/java/randomTests/changxian/minimumSpanningTree/prims/test.java +++ /dev/null @@ -1,50 +0,0 @@ -package randomTests.changxian.minimumSpanningTree.prims; - -import algorithms.minimumSpanningTree.prims.Prim; - -/** - * Basic testing. - * Motivating Example: Minimum Cost to Connect All Points - * A -9- C -2- E - * / / \ \ - * 3 4 7 2 - * / / \ / - * F -1- B --5-- D - */ - -public class Test { - /** - * Runs the custom test. - * - * @param args unused. - */ - public static void main(String[] args) { - int[][] adjM = new int[6][6]; - adjM[0][2] = 9; - adjM[2][0] = 9; - - adjM[0][5] = 3; - adjM[5][0] = 3; - - adjM[1][5] = 1; - adjM[5][1] = 1; - - adjM[1][3] = 5; - adjM[3][1] = 5; - - adjM[1][2] = 4; - adjM[2][1] = 4; - - adjM[2][3] = 7; - adjM[3][2] = 7; - - adjM[2][4] = 2; - adjM[4][2] = 2; - - adjM[3][4] = 2; - adjM[4][3] = 2; - Prim primsAlgor = new Prim(); - System.out.println(primsAlgor.minCostConnectPoints(adjM)); - System.out.println(primsAlgor.minCostConnectPoints2(adjM)); - } -} diff --git a/src/test/java/randomTests/changxian/stackAndQueue/monotonicQueue/Test.java b/src/test/java/randomTests/changxian/stackAndQueue/monotonicQueue/Test.java deleted file mode 100644 index 5d472d60..00000000 --- a/src/test/java/randomTests/changxian/stackAndQueue/monotonicQueue/Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package randomTests.changxian.stackAndQueue.monotonicQueue; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import dataStructures.queue.MonotonicQueue; - -/** - * Basic Testing. - * Maintains a non-increasing queue structure; useful for maximum/minimum sliding window fixed - * length problem - * Expected behaviour: Numbers that come before a larger number will be displayed as that larger - * number - */ -public class Test { - /** - * Runs the custom test. - * - * @param args unused. - */ - public static void main(String[] args) { - List toInsert = - new ArrayList<>(Arrays.asList(2, 7, 1, 3, 7, 5, 4, 3, 2, 5)); - MonotonicQueue mq = new MonotonicQueue<>(); - for (int num : toInsert) { - System.out.println(String.format("Pushing %d", num)); - mq.push(num); - System.out.println(String.format("Max in queue now is %d", mq.max())); - } - - System.out.println("\nPopping thrice: "); - for (int i = 0; i < 7; i++) { - System.out.println(mq.pop()); - } - } -} diff --git a/src/test/java/randomTests/changxian/stackAndQueue/queue/Test.java b/src/test/java/randomTests/changxian/stackAndQueue/queue/Test.java deleted file mode 100644 index e58d484f..00000000 --- a/src/test/java/randomTests/changxian/stackAndQueue/queue/Test.java +++ /dev/null @@ -1,26 +0,0 @@ -package randomTests.changxian.stackAndQueue.queue; - -import dataStructures.queue.Queue; - -/** - * Basic Testing. - */ -public class Test { - /** - * Runs the custom test. - * - * @param args unused. - */ - public static void main(String[] args) { - Queue myQueue = new Queue<>(); - System.out.println("Adding 7, 17, 27"); - myQueue.enqueue(7); - myQueue.enqueue(17); - myQueue.enqueue(27); - System.out.println("size: " + myQueue.size()); - System.out.println("peek: " + myQueue.peek()); - System.out.println("dequeue: " + myQueue.dequeue()); - System.out.println("peek: " + myQueue.peek()); - System.out.println("size: " + myQueue.size()); - } -} diff --git a/src/test/java/randomTests/changxian/stackAndQueue/stack/Test.java b/src/test/java/randomTests/changxian/stackAndQueue/stack/Test.java deleted file mode 100644 index 30ff8674..00000000 --- a/src/test/java/randomTests/changxian/stackAndQueue/stack/Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package randomTests.changxian.stackAndQueue.stack; - -import dataStructures.stack.Stack; - -/** - * Basic Testing. - */ -public class Test { - /** - * Runs custom test script. - * - * @param args unused. - */ - public static void main(String[] args) { - System.out.println("Testing on empty stack: "); - Stack myStack = new Stack<>(); - System.out.println(myStack.pop()); - System.out.println(myStack.peek()); - - System.out.println("Initialize stack with 'abc', 'def', 'ghi'"); - myStack = new Stack<>("abc", "def", "ghi"); - - System.out.println("call peek(): "); - System.out.println(myStack.peek()); - - System.out.println("call pop(): "); - System.out.println(myStack.pop()); - - System.out.println("call peek(): "); - System.out.println(myStack.peek()); - - System.out.println("pushing an element: "); - myStack.push("midnight prince"); - System.out.println("peek to see pushed element: "); - System.out.println(myStack.peek()); - } -} diff --git a/src/test/java/randomTests/kaiting/avlTree/Test.java b/src/test/java/randomTests/kaiting/avlTree/Test.java deleted file mode 100644 index 8cc67eca..00000000 --- a/src/test/java/randomTests/kaiting/avlTree/Test.java +++ /dev/null @@ -1,75 +0,0 @@ -package randomTests.kaiting.avlTree; - -import dataStructures.avlTree.AVLTree; - -/** - * Basic testing. - * Note that a tree structure can be entirely - * determined with one of the following: - * 1. In-order + pre-order - * 2. In-order + post-order - * 3. In-order + level-order - */ -public class Test { - /** - * Runs the custom test. - * - * @param args unused. - */ - public static void main(String[] args) { - AVLTree tree = new AVLTree<>(); - System.out.println("Inserting..."); - tree.insert(5); - tree.insert(4); - tree.insert(8); - tree.insert(6); - tree.insert(7); - tree.insert(3); - tree.insert(1); - - // tree: - // 5 - // / \ - // 3 7 - // / \ / \ - // 1 4 6 8 - // - - Integer x = 6; - System.out.println("parent of " + x.toString() + " is " + tree.search(x).parent.key.toString()); - - System.out.println("Deleting..."); - tree.delete(5); - tree.delete(7); - tree.printInorder(); - tree.printPreorder(); - tree.printLevelorder(); - - System.out.println("Inserting..."); - tree.insert(9); - tree.insert(2); - tree.printInorder(); - tree.printPreorder(); - tree.printPostorder(); - - // updated tree: - // 6 - // / \ - // 3 8 - // / \ \ - // 1 4 9 - // \ - // 2 - - x = 4; - System.out.println("parent of " + x.toString() + " is " + tree.search(x).parent.key.toString()); - - System.out.println("Testing successors & predecessors queries..."); - System.out.println(tree.predecessor(1)); - System.out.println(tree.predecessor(6)); - - System.out.println(tree.successor(1)); - System.out.println(tree.successor(6)); - System.out.println(tree.successor(9)); - } -} diff --git a/src/test/java/randomTests/kaiting/trie/TrieTest.java b/src/test/java/randomTests/kaiting/trie/TrieTest.java deleted file mode 100644 index 57096835..00000000 --- a/src/test/java/randomTests/kaiting/trie/TrieTest.java +++ /dev/null @@ -1,29 +0,0 @@ -package randomTests.kaiting.trie; - -import dataStructures.trie.Trie; - -/** - * Basic testing of trie. - */ -public class TrieTest { - /** - * Runs the custom test. - * - * @param args unused. - */ - public static void main(String[] args) { - Trie trie = new Trie(); - trie.insert("apple"); - trie.insert("coconut"); - trie.insert("apollo"); - trie.insert("fail"); - trie.insert("failure"); - - System.out.println(trie.search("apple")); - System.out.println(trie.search("app")); - System.out.println(trie.startsWith("app")); - trie.prune("fail"); - System.out.println(trie.search("fail")); - System.out.println(trie.search("failure")); - } -} diff --git a/src/test/java/randomTests/kaiting/trie/legacy/TrieTest.java b/src/test/java/randomTests/kaiting/trie/legacy/TrieTest.java deleted file mode 100644 index d0af03ca..00000000 --- a/src/test/java/randomTests/kaiting/trie/legacy/TrieTest.java +++ /dev/null @@ -1,24 +0,0 @@ -package randomTests.kaiting.trie.legacy; - -import dataStructures.trie.legacy.Trie; - -/** - * Basic testing of trie. - */ -public class TrieTest { - /** - * Runs the custom test. - * - * @param args unused. - */ - public static void main(String[] args) { - Trie trie = new Trie(); - trie.insert("apple"); - trie.insert("coconut"); - trie.insert("apollo"); - - System.out.println(trie.search("apple")); - System.out.println(trie.search("app")); - System.out.println(trie.startsWith("app")); - } -} diff --git a/src/test/java/randomTests/owen/unionFind/matrixToListConverter/MatrixConverterTest.java b/src/test/java/randomTests/owen/unionFind/matrixToListConverter/MatrixConverterTest.java deleted file mode 100644 index 1ed3750f..00000000 --- a/src/test/java/randomTests/owen/unionFind/matrixToListConverter/MatrixConverterTest.java +++ /dev/null @@ -1,76 +0,0 @@ -package randomTests.owen.unionFind.matrixToListConverter; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import org.junit.Test; - -import algorithms.util.matrixToListConverter; -import algorithms.util.matrixToListConverter.InvalidMatrixException; - -/** - * Test cases for {@link matrixToListConverter}. - */ -public class MatrixConverterTest { - @Test - public void test_convertArray_shouldReturnAccurateRepresentation() { - // empty matrix - int[][] firstMatrix = new int[0][0]; - ArrayList[] firstList = new ArrayList[0]; - List[] firstResult = matrixToListConverter.convert(firstMatrix); - - // symmetric matrix (undirected graph) - int[][] secondMatrix = { - {0, 1, 1}, - {1, 0, 1}, - {1, 1, 0}, - }; - ArrayList[] secondList = new ArrayList[3]; - for (int i = 0; i < 3; i++) { - secondList[i] = new ArrayList<>(); - } - Collections.addAll(secondList[0], 1, 2); - Collections.addAll(secondList[1], 0, 2); - Collections.addAll(secondList[2], 0, 1); - List[] secondResult = matrixToListConverter.convert(secondMatrix); - - //asymmetric matrix (directed graph) - int[][] thirdMatrix = { - {0, 0, 1}, - {1, 0, 0}, - {1, 1, 1}, - }; - ArrayList[] thirdList = new ArrayList[3]; - for (int i = 0; i < 3; i++) { - thirdList[i] = new ArrayList<>(); - } - Collections.addAll(thirdList[0], 2); - Collections.addAll(thirdList[1], 0); - Collections.addAll(thirdList[2], 0, 1, 2); - List[] thirdResult = matrixToListConverter.convert(thirdMatrix); - - //invalid matrix (n x m graph, n != m) - int[][] fourthMatrix = { - {0, 0}, - {1, 0}, - {1, 1}, - }; - List[] fourthList = new ArrayList[3]; - - assert Arrays.equals(firstList, firstResult); - for (int i = 0; i < 1; i++) { - assert secondList[i].equals(secondResult[i]); - } - for (int i = 0; i < 1; i++) { - assert thirdList[i].equals(thirdResult[i]); - } - try { - matrixToListConverter.convert(fourthMatrix); - } catch (InvalidMatrixException error) { - assert error.getMessage() == "The matrix must be N x N, not N x M!"; - } - - } -} diff --git a/src/test/java/randomTests/owen/unionFind/quickFind/generalised/Test.java b/src/test/java/randomTests/owen/unionFind/quickFind/generalised/Test.java deleted file mode 100644 index bd308f89..00000000 --- a/src/test/java/randomTests/owen/unionFind/quickFind/generalised/Test.java +++ /dev/null @@ -1,68 +0,0 @@ -package randomTests.owen.unionFind.quickFind.generalised; - -import java.util.ArrayList; -import java.util.List; - -import dataStructures.disjointSet.quickFind.generalised.QuickFind; - -/** - * Some simple tests. - */ -public class Test { - /** - * Runs custom test script. - * - * @param args unused. - */ - public static void main(String[] args) { - List lst = new ArrayList<>(); - lst.add("Dog"); - lst.add("Egg"); - lst.add("Carabiner"); - lst.add("Cat"); - lst.add("Potatoes"); - lst.add("Hamster"); - lst.add("Rope"); - lst.add("Fox"); - lst.add("Chalk"); - for (int i = 0; i < lst.size(); i++) { - System.out.println("Added " + lst.get(i)); - } - - QuickFind qf = new QuickFind<>(lst); - qf.union("Dog", "Cat"); - qf.union("Fox", "Hamster"); - qf.union("Fox", "Cat"); - - qf.union("Carabiner", "Rope"); - qf.union("Chalk", "Rope"); - - qf.union("Egg", "Potatoes"); - - List comp1 = qf.retrieveComponent("Hamster"); - System.out.print("Comp1: "); - for (int i = 0; i < comp1.size(); i++) { - System.out.print(comp1.get(i) + ", "); - } - - List comp2 = qf.retrieveComponent("Chalk"); - System.out.print("\nComp2: "); - for (int i = 0; i < comp2.size(); i++) { - System.out.print(comp2.get(i) + ", "); - } - - List comp3 = qf.retrieveComponent("Egg"); - System.out.print("\nComp3: "); - for (int i = 0; i < comp3.size(); i++) { - System.out.print(comp3.get(i) + ", "); - } - - qf.add("Coconut"); - qf.union("Egg", "Coconut"); - List update = qf.retrieveComponent("Egg"); - System.out.print("\nUpdate Comp3: "); - for (int i = 0; i < update.size(); i++) { - System.out.print(update.get(i) + ", "); - } - } -} diff --git a/src/test/java/randomTests/owen/unionFind/quickFind/simplified/Test.java b/src/test/java/randomTests/owen/unionFind/quickFind/simplified/Test.java deleted file mode 100644 index aec59351..00000000 --- a/src/test/java/randomTests/owen/unionFind/quickFind/simplified/Test.java +++ /dev/null @@ -1,43 +0,0 @@ -package randomTests.owen.unionFind.quickFind.simplified; - -import java.util.List; - -import dataStructures.disjointSet.quickFind.simplified.QuickFind; - -/** - * Basic testing. - */ -public class Test { - /** - * Runs custom test script. - * - * @param args unused. - */ - public static void main(String[] args) { - QuickFind qf = new QuickFind(10); - - qf.union(1, 9); - qf.union(4, 6); - qf.union(1, 4); - - qf.union(2, 8); - qf.union(5, 7); - qf.union(2, 7); - - List comp1 = qf.retrieveComponent(1); - for (int i = 0; i < comp1.size(); i++) { - System.out.print(String.format("%d, ", comp1.get(i))); - } - System.out.println("\nTesting next component:"); - List comp2 = qf.retrieveComponent(2); - for (int i = 0; i < comp2.size(); i++) { - System.out.print(String.format("%d, ", comp2.get(i))); - } - System.out.println("\nTesting next component:"); - List comp3 = qf.retrieveComponent(3); - for (int i = 0; i < comp3.size(); i++) { - System.out.print(String.format("%d, ", comp3.get(i))); - } - System.out.println(); - } -} diff --git a/src/test/java/randomTests/owen/unionFind/weightedUnion/Test.java b/src/test/java/randomTests/owen/unionFind/weightedUnion/Test.java deleted file mode 100644 index 5b8af374..00000000 --- a/src/test/java/randomTests/owen/unionFind/weightedUnion/Test.java +++ /dev/null @@ -1,50 +0,0 @@ -package randomTests.owen.unionFind.weightedUnion; - -import java.util.ArrayList; -import java.util.List; - -import dataStructures.disjointSet.weightedUnion.Union; - -/** - * Some simple tests. - */ -public class Test { - /** - * Runs the custom test. - * - * @param args unused. - */ - public static void main(String[] args) { - List lst = new ArrayList<>(); - lst.add("Dog"); - lst.add("Egg"); - lst.add("Carabiner"); - lst.add("Cat"); - lst.add("Potatoes"); - lst.add("Hamster"); - lst.add("Rope"); - lst.add("Fox"); - lst.add("Chalk"); - for (int i = 0; i < lst.size(); i++) { - System.out.println("Added " + lst.get(i)); - } - - Union wu = new Union<>(lst); - wu.combine("Dog", "Cat"); - wu.combine("Fox", "Hamster"); - wu.combine("Fox", "Cat"); - System.out.println(wu.isSameComponent("Dog", "Fox")); // true - System.out.println(wu.isSameComponent("Rope", "Fox")); // false - - wu.combine("Carabiner", "Rope"); - wu.combine("Chalk", "Rope"); - System.out.println(wu.isSameComponent("Rope", "Carabiner")); // true - - wu.combine("Egg", "Potatoes"); - System.out.println(wu.isSameComponent("Egg", "pot")); // duuhh.. + false - - wu.add("Coconut"); - wu.combine("Egg", "Coconut"); - System.out.println(wu.isSameComponent("Coconut", "Potatoes")); // true - } -} From 296625897111b33b36875132a478e3669293055c Mon Sep 17 00:00:00 2001 From: euchangxian Date: Sat, 6 Jan 2024 22:46:20 +0800 Subject: [PATCH 3/5] chore: refine checkstyle configuration - Increase length of allowable consecutive capital letters. - Remove requirement to group overloaded methods together. --- config/checkstyle/checkstyle.xml | 4 ++-- .../{breadthFirstSearch.java => BreadthFirstSearch.java} | 0 .../graphs/{depthFirstSearch.java => DepthFirstSearch.java} | 0 ...{matrixToListConverter.java => MatrixToListConverter.java} | 0 ...readthFirstSearchTest.java => BreadthFirstSearchTest.java} | 0 .../{depthFirstSearchTest.java => DepthFirstSearchTest.java} | 0 .../dataStructures/queue/{dequeTest.java => DequeTest.java} | 0 .../dataStructures/queue/{queueTest.java => QueueTest.java} | 0 8 files changed, 2 insertions(+), 2 deletions(-) rename src/main/java/algorithms/graphs/{breadthFirstSearch.java => BreadthFirstSearch.java} (100%) rename src/main/java/algorithms/graphs/{depthFirstSearch.java => DepthFirstSearch.java} (100%) rename src/main/java/algorithms/util/{matrixToListConverter.java => MatrixToListConverter.java} (100%) rename src/test/java/algorithms/graphs/{breadthFirstSearchTest.java => BreadthFirstSearchTest.java} (100%) rename src/test/java/algorithms/graphs/{depthFirstSearchTest.java => DepthFirstSearchTest.java} (100%) rename src/test/java/dataStructures/queue/{dequeTest.java => DequeTest.java} (100%) rename src/test/java/dataStructures/queue/{queueTest.java => QueueTest.java} (100%) diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml index a859d026..0162f2d1 100644 --- a/config/checkstyle/checkstyle.xml +++ b/config/checkstyle/checkstyle.xml @@ -86,7 +86,7 @@ - + @@ -256,7 +256,7 @@ --> - +