[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,23 +1,25 @@
package com.baeldung.algorithms;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.baeldung.algorithms.hillclimbing.HillClimbing;
import com.baeldung.algorithms.hillclimbing.State;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class HillClimbingAlgorithmUnitTest {
class HillClimbingAlgorithmUnitTest {
private Stack<String> initStack;
private Stack<String> goalStack;
@Before
@BeforeEach
public void initStacks() {
String blockArr[] = { "B", "C", "D", "A" };
String goalBlockArr[] = { "A", "B", "C", "D" };
@@ -30,7 +32,7 @@ public class HillClimbingAlgorithmUnitTest {
}
@Test
public void givenInitAndGoalState_whenGetPathWithHillClimbing_thenPathFound() {
void givenInitAndGoalState_whenGetPathWithHillClimbing_thenPathFound() {
HillClimbing hillClimbing = new HillClimbing();
List<State> path;
@@ -46,7 +48,7 @@ public class HillClimbingAlgorithmUnitTest {
}
@Test
public void givenCurrentState_whenFindNextState_thenBetterHeuristics() {
void givenCurrentState_whenFindNextState_thenBetterHeuristics() {
HillClimbing hillClimbing = new HillClimbing();
List<Stack<String>> initList = new ArrayList<>();
initList.add(initStack);

View File

@@ -1,14 +1,16 @@
package com.baeldung.algorithms;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import com.baeldung.algorithms.automata.*;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public final class RtFiniteStateMachineLongRunningUnitTest {
class RtFiniteStateMachineLongRunningUnitTest {
@Test
public void acceptsSimplePair() {
void acceptsSimplePair() {
String json = "{\"key\":\"value\"}";
FiniteStateMachine machine = this.buildJsonStateMachine();
for (int i = 0; i < json.length(); i++) {
@@ -18,7 +20,7 @@ public final class RtFiniteStateMachineLongRunningUnitTest {
}
@Test
public void acceptsMorePairs() {
void acceptsMorePairs() {
String json = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
FiniteStateMachine machine = this.buildJsonStateMachine();
for (int i = 0; i < json.length(); i++) {
@@ -27,13 +29,15 @@ public final class RtFiniteStateMachineLongRunningUnitTest {
assertTrue(machine.canStop());
}
@Test(expected = IllegalArgumentException.class)
public void missingColon() {
@Test
void missingColon() {
String json = "{\"key\"\"value\"}";
FiniteStateMachine machine = this.buildJsonStateMachine();
for (int i = 0; i < json.length(); i++) {
machine = machine.switchState(String.valueOf(json.charAt(i)));
}
assertThrows(IllegalArgumentException.class, () -> {
FiniteStateMachine machine = this.buildJsonStateMachine();
for (int i = 0; i < json.length(); i++) {
machine = machine.switchState(String.valueOf(json.charAt(i)));
}
});
}
/**

View File

@@ -1,54 +1,54 @@
package com.baeldung.algorithms.kthlargest;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class FindKthLargestUnitTest {
class FindKthLargestUnitTest {
private FindKthLargest findKthLargest;
private Integer[] arr = { 3, 7, 1, 2, 8, 10, 4, 5, 6, 9 };
@Before
@BeforeEach
public void setup() {
findKthLargest = new FindKthLargest();
}
@Test
public void givenIntArray_whenFindKthLargestBySorting_thenGetResult() {
void givenIntArray_whenFindKthLargestBySorting_thenGetResult() {
int k = 3;
assertThat(findKthLargest.findKthLargestBySorting(arr, k)).isEqualTo(8);
}
@Test
public void givenIntArray_whenFindKthLargestBySortingDesc_thenGetResult() {
void givenIntArray_whenFindKthLargestBySortingDesc_thenGetResult() {
int k = 3;
assertThat(findKthLargest.findKthLargestBySortingDesc(arr, k)).isEqualTo(8);
}
@Test
public void givenIntArray_whenFindKthLargestByQuickSelect_thenGetResult() {
void givenIntArray_whenFindKthLargestByQuickSelect_thenGetResult() {
int k = 3;
int kthLargest = arr.length - k;
assertThat(findKthLargest.findKthElementByQuickSelect(arr, 0, arr.length - 1, kthLargest)).isEqualTo(8);
}
@Test
public void givenIntArray_whenFindKthElementByQuickSelectIterative_thenGetResult() {
void givenIntArray_whenFindKthElementByQuickSelectIterative_thenGetResult() {
int k = 3;
int kthLargest = arr.length - k;
assertThat(findKthLargest.findKthElementByQuickSelectWithIterativePartition(arr, 0, arr.length - 1, kthLargest)).isEqualTo(8);
}
@Test
public void givenIntArray_whenFindKthSmallestByQuickSelect_thenGetResult() {
void givenIntArray_whenFindKthSmallestByQuickSelect_thenGetResult() {
int k = 3;
assertThat(findKthLargest.findKthElementByQuickSelect(arr, 0, arr.length - 1, k - 1)).isEqualTo(3);
}
@Test
public void givenIntArray_whenFindKthLargestByRandomizedQuickSelect_thenGetResult() {
void givenIntArray_whenFindKthLargestByRandomizedQuickSelect_thenGetResult() {
int k = 3;
int kthLargest = arr.length - k;
assertThat(findKthLargest.findKthElementByRandomizedQuickSelect(arr, 0, arr.length - 1, kthLargest)).isEqualTo(8);

View File

@@ -1,23 +1,24 @@
package com.baeldung.algorithms.minimax;
import org.junit.Before;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.*;
import com.baeldung.algorithms.minimax.MiniMax;
import com.baeldung.algorithms.minimax.Tree;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class MinimaxUnitTest {
class MinimaxUnitTest {
private Tree gameTree;
private MiniMax miniMax;
@Before
@BeforeEach
public void initMiniMaxUtility() {
miniMax = new MiniMax();
}
@Test
public void givenMiniMax_whenConstructTree_thenNotNullTree() {
void givenMiniMax_whenConstructTree_thenNotNullTree() {
assertNull(gameTree);
miniMax.constructTree(6);
gameTree = miniMax.getTree();
@@ -25,7 +26,7 @@ public class MinimaxUnitTest {
}
@Test
public void givenMiniMax_whenCheckWin_thenComputeOptimal() {
void givenMiniMax_whenCheckWin_thenComputeOptimal() {
miniMax.constructTree(6);
boolean result = miniMax.checkWin();
assertTrue(result);