[JAVA-18123] Upgraded algorithms-modules to java 11 + Upgraded unit t… (#13437)

* [JAVA-18123] Upgraded algorithms-modules to java 11 + Upgraded unit tests to junit5

* [JAVA-18123] Clean up properties

---------

Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
panos-kakos
2023-02-07 14:42:21 +00:00
committed by GitHub
parent 2c0c3b269e
commit d47969ce2d
82 changed files with 740 additions and 721 deletions

View File

@@ -1,80 +1,81 @@
package com.baeldung.algorithms.balancedbrackets;
import org.junit.Before;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class BalancedBracketsUsingDequeUnitTest {
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class BalancedBracketsUsingDequeUnitTest {
private BalancedBracketsUsingDeque balancedBracketsUsingDeque;
@Before
@BeforeEach
public void setup() {
balancedBracketsUsingDeque = new BalancedBracketsUsingDeque();
}
@Test
public void givenNullInput_whenCheckingForBalance_shouldReturnFalse() {
void givenNullInput_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingDeque.isBalanced(null);
assertThat(result).isFalse();
}
@Test
public void givenEmptyString_whenCheckingForBalance_shouldReturnTrue() {
void givenEmptyString_whenCheckingForBalance_shouldReturnTrue() {
boolean result = balancedBracketsUsingDeque.isBalanced("");
assertThat(result).isTrue();
}
@Test
public void givenInvalidCharacterString_whenCheckingForBalance_shouldReturnFalse() {
void givenInvalidCharacterString_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingDeque.isBalanced("abc[](){}");
assertThat(result).isFalse();
}
@Test
public void givenOddLengthString_whenCheckingForBalance_shouldReturnFalse() {
void givenOddLengthString_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingDeque.isBalanced("{{[]()}}}");
assertThat(result).isFalse();
}
@Test
public void givenEvenLengthString_whenCheckingForBalance_shouldReturnFalse() {
void givenEvenLengthString_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingDeque.isBalanced("{{[]()}}}}");
assertThat(result).isFalse();
}
@Test
public void givenEvenLengthUnbalancedString_whenCheckingForBalance_shouldReturnFalse() {
void givenEvenLengthUnbalancedString_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingDeque.isBalanced("{[(])}");
assertThat(result).isFalse();
}
@Test
public void givenAnotherEvenLengthUnbalancedString_whenCheckingForBalance_shouldReturnFalse() {
void givenAnotherEvenLengthUnbalancedString_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingDeque.isBalanced("{{}(");
assertThat(result).isFalse();
}
@Test
public void givenEvenLengthBalancedString_whenCheckingForBalance_shouldReturnTrue() {
void givenEvenLengthBalancedString_whenCheckingForBalance_shouldReturnTrue() {
boolean result = balancedBracketsUsingDeque.isBalanced("{[()]}");
assertThat(result).isTrue();
}
@Test
public void givenBalancedString_whenCheckingForBalance_shouldReturnTrue() {
void givenBalancedString_whenCheckingForBalance_shouldReturnTrue() {
boolean result = balancedBracketsUsingDeque.isBalanced("{{[[(())]]}}");
assertThat(result).isTrue();
}
@Test
public void givenAnotherBalancedString_whenCheckingForBalance_shouldReturnTrue() {
void givenAnotherBalancedString_whenCheckingForBalance_shouldReturnTrue() {
boolean result = balancedBracketsUsingDeque.isBalanced("{{([])}}");
assertThat(result).isTrue();
}
@Test
public void givenUnBalancedString_whenCheckingForBalance_shouldReturnFalse() {
void givenUnBalancedString_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingDeque.isBalanced("{{)[](}}");
assertThat(result).isFalse();
}

View File

@@ -1,80 +1,81 @@
package com.baeldung.algorithms.balancedbrackets;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class BalancedBracketsUsingStringUnitTest {
class BalancedBracketsUsingStringUnitTest {
private BalancedBracketsUsingString balancedBracketsUsingString;
@Before
@BeforeEach
public void setup() {
balancedBracketsUsingString = new BalancedBracketsUsingString();
}
@Test
public void givenNullInput_whenCheckingForBalance_shouldReturnFalse() {
void givenNullInput_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingString.isBalanced(null);
assertThat(result).isFalse();
}
@Test
public void givenEmptyString_whenCheckingForBalance_shouldReturnTrue() {
void givenEmptyString_whenCheckingForBalance_shouldReturnTrue() {
boolean result = balancedBracketsUsingString.isBalanced("");
assertThat(result).isTrue();
}
@Test
public void givenInvalidCharacterString_whenCheckingForBalance_shouldReturnFalse() {
void givenInvalidCharacterString_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingString.isBalanced("abc[](){}");
assertThat(result).isFalse();
}
@Test
public void givenOddLengthString_whenCheckingForBalance_shouldReturnFalse() {
void givenOddLengthString_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingString.isBalanced("{{[]()}}}");
assertThat(result).isFalse();
}
@Test
public void givenEvenLengthString_whenCheckingForBalance_shouldReturnFalse() {
void givenEvenLengthString_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingString.isBalanced("{{[]()}}}}");
assertThat(result).isFalse();
}
@Test
public void givenEvenLengthUnbalancedString_whenCheckingForBalance_shouldReturnFalse() {
void givenEvenLengthUnbalancedString_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingString.isBalanced("{[(])}");
assertThat(result).isFalse();
}
@Test
public void givenAnotherEvenLengthUnbalancedString_whenCheckingForBalance_shouldReturnFalse() {
void givenAnotherEvenLengthUnbalancedString_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingString.isBalanced("{{}(");
assertThat(result).isFalse();
}
@Test
public void givenEvenLengthBalancedString_whenCheckingForBalance_shouldReturnTrue() {
void givenEvenLengthBalancedString_whenCheckingForBalance_shouldReturnTrue() {
boolean result = balancedBracketsUsingString.isBalanced("{[()]}");
assertThat(result).isTrue();
}
@Test
public void givenBalancedString_whenCheckingForBalance_shouldReturnTrue() {
void givenBalancedString_whenCheckingForBalance_shouldReturnTrue() {
boolean result = balancedBracketsUsingString.isBalanced("{{[[(())]]}}");
assertThat(result).isTrue();
}
@Test
public void givenAnotherBalancedString_whenCheckingForBalance_shouldReturnTrue() {
void givenAnotherBalancedString_whenCheckingForBalance_shouldReturnTrue() {
boolean result = balancedBracketsUsingString.isBalanced("{{([])}}");
assertThat(result).isTrue();
}
@Test
public void givenUnBalancedString_whenCheckingForBalance_shouldReturnFalse() {
void givenUnBalancedString_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingString.isBalanced("{{)[](}}");
assertThat(result).isFalse();
}

View File

@@ -2,17 +2,17 @@ package com.baeldung.algorithms.boruvka;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.google.common.graph.MutableValueGraph;
import com.google.common.graph.ValueGraphBuilder;
public class BoruvkaUnitTest {
class BoruvkaUnitTest {
private MutableValueGraph<Integer, Integer> graph;
@Before
@BeforeEach
public void setup() {
graph = ValueGraphBuilder.undirected()
.build();
@@ -26,7 +26,7 @@ public class BoruvkaUnitTest {
}
@Test
public void givenInputGraph_whenBoruvkaPerformed_thenMinimumSpanningTree() {
void givenInputGraph_whenBoruvkaPerformed_thenMinimumSpanningTree() {
BoruvkaMST boruvkaMST = new BoruvkaMST(graph);
MutableValueGraph<Integer, Integer> mst = boruvkaMST.getMST();

View File

@@ -1,15 +1,15 @@
package com.baeldung.algorithms.gradientdescent;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.function.Function;
import org.junit.Test;
import org.junit.jupiter.api.Test;
public class GradientDescentUnitTest {
class GradientDescentUnitTest {
@Test
public void givenFunction_whenStartingPointIsOne_thenLocalMinimumIsFound() {
void givenFunction_whenStartingPointIsOne_thenLocalMinimumIsFound() {
Function<Double, Double> df = x ->
StrictMath.abs(StrictMath.pow(x, 3)) - (3 * StrictMath.pow(x, 2)) + x;
GradientDescent gd = new GradientDescent();

View File

@@ -1,13 +1,14 @@
package com.baeldung.algorithms.greedy;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class GreedyAlgorithmUnitTest {
class GreedyAlgorithmUnitTest {
private SocialConnector prepareNetwork() {
SocialConnector sc = new SocialConnector();
@@ -35,21 +36,21 @@ public class GreedyAlgorithmUnitTest {
}
@Test
public void greedyAlgorithmTest() {
void greedyAlgorithmTest() {
GreedyAlgorithm ga = new GreedyAlgorithm(prepareNetwork());
assertEquals(ga.findMostFollowersPath("root"), 5);
}
@Test
public void nongreedyAlgorithmTest() {
void nongreedyAlgorithmTest() {
NonGreedyAlgorithm nga = new NonGreedyAlgorithm(prepareNetwork(), 0);
Assertions.assertThrows(IllegalStateException.class, () -> {
assertThrows(IllegalStateException.class, () -> {
nga.findMostFollowersPath("root");
});
}
@Test
public void nongreedyAlgorithmUnboundedTest() {
void nongreedyAlgorithmUnboundedTest() {
SocialConnector sc = prepareNetwork();
sc.switchCounter();
NonGreedyAlgorithm nga = new NonGreedyAlgorithm(sc, 0);

View File

@@ -1,21 +1,23 @@
package com.baeldung.algorithms.kruskal;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.google.common.graph.MutableValueGraph;
import com.google.common.graph.ValueGraph;
import com.google.common.graph.ValueGraphBuilder;
import com.baeldung.algorithms.kruskal.Kruskal;
public class KruskalUnitTest {
class KruskalUnitTest {
private MutableValueGraph<Integer, Double> graph;
@Before
@BeforeEach
public void setup() {
graph = ValueGraphBuilder.undirected().build();
graph.putEdgeValue(0, 1, 8.0);
@@ -28,7 +30,7 @@ public class KruskalUnitTest {
}
@Test
public void givenGraph_whenMinimumSpanningTree_thenOutputCorrectResult() {
void givenGraph_whenMinimumSpanningTree_thenOutputCorrectResult() {
final Kruskal kruskal = new Kruskal();
ValueGraph<Integer, Double> spanningTree = kruskal.minSpanningTree(graph);
@@ -47,7 +49,7 @@ public class KruskalUnitTest {
}
@Test
public void givenGraph_whenMaximumSpanningTree_thenOutputCorrectResult() {
void givenGraph_whenMaximumSpanningTree_thenOutputCorrectResult() {
final Kruskal kruskal = new Kruskal();
ValueGraph<Integer, Double> spanningTree = kruskal.maxSpanningTree(graph);

View File

@@ -1,13 +1,13 @@
package com.baeldung.algorithms.linkedlist;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.jupiter.api.Test;
public class LinkedListReversalUnitTest {
class LinkedListReversalUnitTest {
@Test
public void givenLinkedList_whenIterativeReverse_thenOutputCorrectResult() {
void givenLinkedList_whenIterativeReverse_thenOutputCorrectResult() {
ListNode head = constructLinkedList();
ListNode node = head;
for (int i = 1; i <= 5; i++) {
@@ -25,7 +25,7 @@ public class LinkedListReversalUnitTest {
}
@Test
public void givenLinkedList_whenRecursiveReverse_thenOutputCorrectResult() {
void givenLinkedList_whenRecursiveReverse_thenOutputCorrectResult() {
ListNode head = constructLinkedList();
ListNode node = head;
for (int i = 1; i <= 5; i++) {

View File

@@ -2,17 +2,17 @@ package com.baeldung.algorithms.minheapmerge;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Test;
import org.junit.jupiter.api.Test;
public class MinHeapUnitTest {
class MinHeapUnitTest {
private final int[][] inputArray = { { 0, 6 }, { 1, 5, 10, 100 }, { 2, 4, 200, 650 } };
private final int[] expectedArray = { 0, 1, 2, 4, 5, 6, 10, 100, 200, 650 };
@Test
public void givenSortedArrays_whenMerged_thenShouldReturnASingleSortedarray() {
void givenSortedArrays_whenMerged_thenShouldReturnASingleSortedarray() {
int[] resultArray = MinHeap.merge(inputArray);
assertThat(resultArray.length, is(equalTo(10)));

View File

@@ -1,13 +1,13 @@
package com.baeldung.algorithms.topkelements;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import static org.assertj.core.api.Java6Assertions.assertThat;
public class TopKElementsFinderUnitTest {
import org.junit.jupiter.api.Test;
class TopKElementsFinderUnitTest {
private final TopKElementsFinder<Integer> bruteForceFinder = new BruteForceTopKElementsFinder();
private final TopKElementsFinder<Integer> maxHeapFinder = new MaxHeapTopKElementsFinder();
private final TopKElementsFinder<Integer> treeSetFinder = new TreeSetTopKElementsFinder();
@@ -20,27 +20,27 @@ public class TopKElementsFinderUnitTest {
@Test
public void givenArrayDistinctIntegers_whenBruteForceFindTopK_thenReturnKLargest() {
void givenArrayDistinctIntegers_whenBruteForceFindTopK_thenReturnKLargest() {
assertThat(bruteForceFinder.findTopK(distinctIntegers, k)).containsOnlyElementsOf(distinctIntegersTopK);
}
@Test
public void givenArrayDistinctIntegers_whenMaxHeapFindTopK_thenReturnKLargest() {
void givenArrayDistinctIntegers_whenMaxHeapFindTopK_thenReturnKLargest() {
assertThat(maxHeapFinder.findTopK(distinctIntegers, k)).containsOnlyElementsOf(distinctIntegersTopK);
}
@Test
public void givenArrayDistinctIntegers_whenTreeSetFindTopK_thenReturnKLargest() {
void givenArrayDistinctIntegers_whenTreeSetFindTopK_thenReturnKLargest() {
assertThat(treeSetFinder.findTopK(distinctIntegers, k)).containsOnlyElementsOf(distinctIntegersTopK);
}
@Test
public void givenArrayNonDistinctIntegers_whenBruteForceFindTopK_thenReturnKLargest() {
void givenArrayNonDistinctIntegers_whenBruteForceFindTopK_thenReturnKLargest() {
assertThat(bruteForceFinder.findTopK(nonDistinctIntegers, k)).containsOnlyElementsOf(nonDistinctIntegersTopK);
}
@Test
public void givenArrayNonDistinctIntegers_whenMaxHeapFindTopK_thenReturnKLargest() {
void givenArrayNonDistinctIntegers_whenMaxHeapFindTopK_thenReturnKLargest() {
assertThat(maxHeapFinder.findTopK(nonDistinctIntegers, k)).containsOnlyElementsOf(nonDistinctIntegersTopK);
}
}