[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,11 +1,13 @@
package com.baeldung.algorithms.binarysearch;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
public class BinarySearchUnitTest {
import org.junit.jupiter.api.Test;
class BinarySearchUnitTest {
int[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
int key = 6;
@@ -15,27 +17,27 @@ public class BinarySearchUnitTest {
List<Integer> sortedList = Arrays.asList(0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9);
@Test
public void givenASortedArrayOfIntegers_whenBinarySearchRunIterativelyForANumber_thenGetIndexOfTheNumber() {
void givenASortedArrayOfIntegers_whenBinarySearchRunIterativelyForANumber_thenGetIndexOfTheNumber() {
BinarySearch binSearch = new BinarySearch();
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchIteratively(sortedArray, key, low, high));
assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchIteratively(sortedArray, key, low, high));
}
@Test
public void givenASortedArrayOfIntegers_whenBinarySearchRunRecursivelyForANumber_thenGetIndexOfTheNumber() {
void givenASortedArrayOfIntegers_whenBinarySearchRunRecursivelyForANumber_thenGetIndexOfTheNumber() {
BinarySearch binSearch = new BinarySearch();
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchRecursively(sortedArray, key, low, high));
assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchRecursively(sortedArray, key, low, high));
}
@Test
public void givenASortedArrayOfIntegers_whenBinarySearchRunUsingArraysClassStaticMethodForANumber_thenGetIndexOfTheNumber() {
void givenASortedArrayOfIntegers_whenBinarySearchRunUsingArraysClassStaticMethodForANumber_thenGetIndexOfTheNumber() {
BinarySearch binSearch = new BinarySearch();
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchUsingJavaArrays(sortedArray, key));
assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchUsingJavaArrays(sortedArray, key));
}
@Test
public void givenASortedListOfIntegers_whenBinarySearchRunUsingCollectionsClassStaticMethodForANumber_thenGetIndexOfTheNumber() {
void givenASortedListOfIntegers_whenBinarySearchRunUsingCollectionsClassStaticMethodForANumber_thenGetIndexOfTheNumber() {
BinarySearch binSearch = new BinarySearch();
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchUsingJavaCollections(sortedList, key));
assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchUsingJavaCollections(sortedList, key));
}
}

View File

@@ -1,15 +1,15 @@
package com.baeldung.algorithms.dfs;
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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.jupiter.api.Test;
public class BinaryTreeUnitTest {
class BinaryTreeUnitTest {
@Test
public void givenABinaryTree_WhenAddingElements_ThenTreeNotEmpty() {
void givenABinaryTree_WhenAddingElements_ThenTreeNotEmpty() {
BinaryTree bt = createBinaryTree();
@@ -17,7 +17,7 @@ public class BinaryTreeUnitTest {
}
@Test
public void givenABinaryTree_WhenAddingElements_ThenTreeContainsThoseElements() {
void givenABinaryTree_WhenAddingElements_ThenTreeContainsThoseElements() {
BinaryTree bt = createBinaryTree();
@@ -28,7 +28,7 @@ public class BinaryTreeUnitTest {
}
@Test
public void givenABinaryTree_WhenAddingExistingElement_ThenElementIsNotAdded() {
void givenABinaryTree_WhenAddingExistingElement_ThenElementIsNotAdded() {
BinaryTree bt = createBinaryTree();
@@ -40,7 +40,7 @@ public class BinaryTreeUnitTest {
}
@Test
public void givenABinaryTree_WhenLookingForNonExistingElement_ThenReturnsFalse() {
void givenABinaryTree_WhenLookingForNonExistingElement_ThenReturnsFalse() {
BinaryTree bt = createBinaryTree();
@@ -48,7 +48,7 @@ public class BinaryTreeUnitTest {
}
@Test
public void givenABinaryTree_WhenDeletingElements_ThenTreeDoesNotContainThoseElements() {
void givenABinaryTree_WhenDeletingElements_ThenTreeDoesNotContainThoseElements() {
BinaryTree bt = createBinaryTree();
@@ -58,7 +58,7 @@ public class BinaryTreeUnitTest {
}
@Test
public void givenABinaryTree_WhenDeletingNonExistingElement_ThenTreeDoesNotDelete() {
void givenABinaryTree_WhenDeletingNonExistingElement_ThenTreeDoesNotDelete() {
BinaryTree bt = createBinaryTree();
@@ -71,7 +71,7 @@ public class BinaryTreeUnitTest {
}
@Test
public void it_deletes_the_root() {
void it_deletes_the_root() {
int value = 12;
BinaryTree bt = new BinaryTree();
bt.add(value);
@@ -82,7 +82,7 @@ public class BinaryTreeUnitTest {
}
@Test
public void givenABinaryTree_WhenTraversingInOrder_ThenPrintValues() {
void givenABinaryTree_WhenTraversingInOrder_ThenPrintValues() {
BinaryTree bt = createBinaryTree();
@@ -92,7 +92,7 @@ public class BinaryTreeUnitTest {
}
@Test
public void givenABinaryTree_WhenTraversingPreOrder_ThenPrintValues() {
void givenABinaryTree_WhenTraversingPreOrder_ThenPrintValues() {
BinaryTree bt = createBinaryTree();
@@ -102,7 +102,7 @@ public class BinaryTreeUnitTest {
}
@Test
public void givenABinaryTree_WhenTraversingPostOrder_ThenPrintValues() {
void givenABinaryTree_WhenTraversingPostOrder_ThenPrintValues() {
BinaryTree bt = createBinaryTree();

View File

@@ -2,12 +2,12 @@ package com.baeldung.algorithms.dfs;
import java.util.List;
import org.junit.Test;
import org.junit.jupiter.api.Test;
public class GraphUnitTest {
class GraphUnitTest {
@Test
public void givenDirectedGraph_whenDFS_thenPrintAllValues() {
void givenDirectedGraph_whenDFS_thenPrintAllValues() {
Graph graph = createDirectedGraph();
graph.dfs(0);
System.out.println();
@@ -15,7 +15,7 @@ public class GraphUnitTest {
}
@Test
public void givenDirectedGraph_whenGetTopologicalSort_thenPrintValuesSorted() {
void givenDirectedGraph_whenGetTopologicalSort_thenPrintValuesSorted() {
Graph graph = createDirectedGraph();
List<Integer> list = graph.topologicalSort(0);
System.out.println(list);

View File

@@ -1,27 +1,28 @@
package com.baeldung.algorithms.interpolationsearch;
import org.junit.Before;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class InterpolationSearchUnitTest {
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class InterpolationSearchUnitTest {
private int[] myData;
@Before
@BeforeEach
public void setUp() {
myData = new int[]{13,21,34,55,69,73,84,101};
}
@Test
public void givenSortedArray_whenLookingFor84_thenReturn6() {
void givenSortedArray_whenLookingFor84_thenReturn6() {
int pos = InterpolationSearch.interpolationSearch(myData, 84);
assertEquals(6, pos);
}
@Test
public void givenSortedArray_whenLookingFor19_thenReturnMinusOne() {
void givenSortedArray_whenLookingFor19_thenReturnMinusOne() {
int pos = InterpolationSearch.interpolationSearch(myData, 19);
assertEquals(-1, pos);
}

View File

@@ -9,13 +9,13 @@ import java.util.*;
import static com.baeldung.algorithms.kthsmallest.KthSmallest.*;
import static org.junit.jupiter.api.Assertions.*;
public class KthSmallestUnitTest {
class KthSmallestUnitTest {
@Nested
class Exceptions {
@Test
public void when_at_least_one_list_is_null_then_an_exception_is_thrown() {
void when_at_least_one_list_is_null_then_an_exception_is_thrown() {
Executable executable1 = () -> findKthSmallestElement(1, null, null);
Executable executable2 = () -> findKthSmallestElement(1, new int[]{2}, null);
@@ -27,7 +27,7 @@ public class KthSmallestUnitTest {
}
@Test
public void when_at_least_one_list_is_empty_then_an_exception_is_thrown() {
void when_at_least_one_list_is_empty_then_an_exception_is_thrown() {
Executable executable1 = () -> findKthSmallestElement(1, new int[]{}, new int[]{2});
Executable executable2 = () -> findKthSmallestElement(1, new int[]{2}, new int[]{});
@@ -39,19 +39,19 @@ public class KthSmallestUnitTest {
}
@Test
public void when_k_is_smaller_than_0_then_an_exception_is_thrown() {
void when_k_is_smaller_than_0_then_an_exception_is_thrown() {
Executable executable1 = () -> findKthSmallestElement(-1, new int[]{2}, new int[]{2});
assertThrows(IllegalArgumentException.class, executable1);
}
@Test
public void when_k_is_smaller_than_1_then_an_exception_is_thrown() {
void when_k_is_smaller_than_1_then_an_exception_is_thrown() {
Executable executable1 = () -> findKthSmallestElement(0, new int[]{2}, new int[]{2});
assertThrows(IllegalArgumentException.class, executable1);
}
@Test
public void when_k_bigger_then_the_two_lists_then_an_exception_is_thrown() {
void when_k_bigger_then_the_two_lists_then_an_exception_is_thrown() {
Executable executable1 = () -> findKthSmallestElement(6, new int[]{1, 5, 6}, new int[]{2, 5});
assertThrows(NoSuchElementException.class, executable1);
}

View File

@@ -1,12 +1,13 @@
package com.baeldung.algorithms.mcts;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.baeldung.algorithms.mcts.montecarlo.MonteCarloTreeSearch;
import com.baeldung.algorithms.mcts.montecarlo.State;
@@ -15,31 +16,31 @@ import com.baeldung.algorithms.mcts.tictactoe.Board;
import com.baeldung.algorithms.mcts.tictactoe.Position;
import com.baeldung.algorithms.mcts.tree.Tree;
public class MCTSUnitTest {
class MCTSUnitTest {
private Tree gameTree;
private MonteCarloTreeSearch mcts;
@Before
@BeforeEach
public void initGameTree() {
gameTree = new Tree();
mcts = new MonteCarloTreeSearch();
}
@Test
public void givenStats_whenGetUCTForNode_thenUCTMatchesWithManualData() {
void givenStats_whenGetUCTForNode_thenUCTMatchesWithManualData() {
double uctValue = 15.79;
assertEquals(UCT.uctValue(600, 300, 20), uctValue, 0.01);
}
@Test
public void giveninitBoardState_whenGetAllPossibleStates_thenNonEmptyList() {
void giveninitBoardState_whenGetAllPossibleStates_thenNonEmptyList() {
State initState = gameTree.getRoot().getState();
List<State> possibleStates = initState.getAllPossibleStates();
assertTrue(possibleStates.size() > 0);
}
@Test
public void givenEmptyBoard_whenPerformMove_thenLessAvailablePossitions() {
void givenEmptyBoard_whenPerformMove_thenLessAvailablePossitions() {
Board board = new Board();
int initAvailablePositions = board.getEmptyPositions().size();
board.performMove(Board.P1, new Position(1, 1));
@@ -48,7 +49,7 @@ public class MCTSUnitTest {
}
@Test
public void givenEmptyBoard_whenSimulateInterAIPlay_thenGameDraw() {
void givenEmptyBoard_whenSimulateInterAIPlay_thenGameDraw() {
Board board = new Board();
int player = Board.P1;
@@ -61,11 +62,11 @@ public class MCTSUnitTest {
player = 3 - player;
}
int winStatus = board.checkStatus();
assertEquals(winStatus, Board.DRAW);
assertEquals(Board.DRAW, winStatus);
}
@Test
public void givenEmptyBoard_whenLevel1VsLevel3_thenLevel3WinsOrDraw() {
void givenEmptyBoard_whenLevel1VsLevel3_thenLevel3WinsOrDraw() {
Board board = new Board();
MonteCarloTreeSearch mcts1 = new MonteCarloTreeSearch();
mcts1.setLevel(1);

View File

@@ -1,21 +1,22 @@
package com.baeldung.algorithms.quadtree;
import org.junit.Assert;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class QuadTreeSearchUnitTest {
class QuadTreeSearchUnitTest {
private static final Logger LOGGER = LoggerFactory.getLogger(QuadTreeSearchUnitTest.class);
private static QuadTree quadTree;
@BeforeClass
@BeforeAll
public static void setUp() {
Region area = new Region(0, 0, 400, 400);
quadTree = new QuadTree(area);
@@ -32,30 +33,30 @@ public class QuadTreeSearchUnitTest {
}
@Test
public void givenQuadTree_whenSearchingForRange_thenReturn1MatchingItem() {
void givenQuadTree_whenSearchingForRange_thenReturn1MatchingItem() {
Region searchArea = new Region(200, 200, 250, 250);
List<Point> result = quadTree.search(searchArea, null, "");
LOGGER.debug(result.toString());
LOGGER.debug(quadTree.printSearchTraversePath());
Assert.assertEquals(1, result.size());
Assert.assertArrayEquals(new float[] { 245, 238 },
assertEquals(1, result.size());
assertArrayEquals(new float[] { 245, 238 },
new float[]{result.get(0).getX(), result.get(0).getY() }, 0);
}
@Test
public void givenQuadTree_whenSearchingForRange_thenReturn2MatchingItems() {
void givenQuadTree_whenSearchingForRange_thenReturn2MatchingItems() {
Region searchArea = new Region(0, 0, 100, 100);
List<Point> result = quadTree.search(searchArea, null, "");
LOGGER.debug(result.toString());
LOGGER.debug(quadTree.printSearchTraversePath());
Assert.assertEquals(2, result.size());
Assert.assertArrayEquals(new float[] { 21, 25 },
assertEquals(2, result.size());
assertArrayEquals(new float[] { 21, 25 },
new float[]{result.get(0).getX(), result.get(0).getY() }, 0);
Assert.assertArrayEquals(new float[] { 55, 53 },
assertArrayEquals(new float[] { 55, 53 },
new float[]{result.get(1).getX(), result.get(1).getY() }, 0);
}

View File

@@ -1,71 +1,72 @@
package com.baeldung.algorithms.suffixtree;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import java.util.List;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SuffixTreeUnitTest {
class SuffixTreeUnitTest {
private static final Logger LOGGER = LoggerFactory.getLogger(SuffixTreeUnitTest.class);
private static SuffixTree suffixTree;
@BeforeClass
@BeforeAll
public static void setUp() {
suffixTree = new SuffixTree("havanabanana");
printTree();
}
@Test
public void givenSuffixTree_whenSearchingForA_thenReturn6Matches() {
void givenSuffixTree_whenSearchingForA_thenReturn6Matches() {
List<String> matches = suffixTree.searchText("a");
matches.stream()
.forEach(m -> LOGGER.debug(m));
Assert.assertArrayEquals(new String[] { "h[a]vanabanana", "hav[a]nabanana", "havan[a]banana", "havanab[a]nana", "havanaban[a]na", "havanabanan[a]" }, matches.toArray());
assertArrayEquals(new String[] { "h[a]vanabanana", "hav[a]nabanana", "havan[a]banana", "havanab[a]nana", "havanaban[a]na", "havanabanan[a]" }, matches.toArray());
}
@Test
public void givenSuffixTree_whenSearchingForNab_thenReturn1Match() {
void givenSuffixTree_whenSearchingForNab_thenReturn1Match() {
List<String> matches = suffixTree.searchText("nab");
matches.stream()
.forEach(m -> LOGGER.debug(m));
Assert.assertArrayEquals(new String[] { "hava[nab]anana" }, matches.toArray());
assertArrayEquals(new String[] { "hava[nab]anana" }, matches.toArray());
}
@Test
public void givenSuffixTree_whenSearchingForNag_thenReturnNoMatches() {
void givenSuffixTree_whenSearchingForNag_thenReturnNoMatches() {
List<String> matches = suffixTree.searchText("nag");
matches.stream()
.forEach(m -> LOGGER.debug(m));
Assert.assertArrayEquals(new String[] {}, matches.toArray());
assertArrayEquals(new String[] {}, matches.toArray());
}
@Test
public void givenSuffixTree_whenSearchingForBanana_thenReturn2Matches() {
void givenSuffixTree_whenSearchingForBanana_thenReturn2Matches() {
List<String> matches = suffixTree.searchText("ana");
matches.stream()
.forEach(m -> LOGGER.debug(m));
Assert.assertArrayEquals(new String[] { "hav[ana]banana", "havanab[ana]na", "havanaban[ana]" }, matches.toArray());
assertArrayEquals(new String[] { "hav[ana]banana", "havanab[ana]na", "havanaban[ana]" }, matches.toArray());
}
@Test
public void givenSuffixTree_whenSearchingForNa_thenReturn4Matches() {
void givenSuffixTree_whenSearchingForNa_thenReturn4Matches() {
List<String> matches = suffixTree.searchText("na");
matches.stream()
.forEach(m -> LOGGER.debug(m));
Assert.assertArrayEquals(new String[] { "hava[na]banana", "havanaba[na]na", "havanabana[na]" }, matches.toArray());
assertArrayEquals(new String[] { "hava[na]banana", "havanaba[na]na", "havanabana[na]" }, matches.toArray());
}
@Test
public void givenSuffixTree_whenSearchingForX_thenReturnNoMatches() {
void givenSuffixTree_whenSearchingForX_thenReturnNoMatches() {
List<String> matches = suffixTree.searchText("x");
matches.stream()
.forEach(m -> LOGGER.debug(m));
Assert.assertArrayEquals(new String[] {}, matches.toArray());
assertArrayEquals(new String[] {}, matches.toArray());
}
private static void printTree() {

View File

@@ -1,23 +1,24 @@
package com.baeldung.algorithms.textsearch;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;
public class TextSearchAlgorithmsUnitTest {
class TextSearchAlgorithmsUnitTest {
@Test
public void testStringSearchAlgorithms() {
void testStringSearchAlgorithms() {
String text = "This is some nice text.";
String pattern = "some";
int realPosition = text.indexOf(pattern);
Assert.assertTrue(realPosition == TextSearchAlgorithms.simpleTextSearch(pattern.toCharArray(), text.toCharArray()));
Assert.assertTrue(realPosition == TextSearchAlgorithms.RabinKarpMethod(pattern.toCharArray(), text.toCharArray()));
Assert.assertTrue(realPosition == TextSearchAlgorithms.KnuthMorrisPrattSearch(pattern.toCharArray(), text.toCharArray()));
Assert.assertTrue(realPosition == TextSearchAlgorithms.BoyerMooreHorspoolSimpleSearch(pattern.toCharArray(), text.toCharArray()));
Assert.assertTrue(realPosition == TextSearchAlgorithms.BoyerMooreHorspoolSearch(pattern.toCharArray(), text.toCharArray()));
assertEquals(TextSearchAlgorithms.simpleTextSearch(pattern.toCharArray(), text.toCharArray()), realPosition);
assertEquals(TextSearchAlgorithms.RabinKarpMethod(pattern.toCharArray(), text.toCharArray()), realPosition);
assertEquals(TextSearchAlgorithms.KnuthMorrisPrattSearch(pattern.toCharArray(), text.toCharArray()) , realPosition);
assertEquals(TextSearchAlgorithms.BoyerMooreHorspoolSimpleSearch(pattern.toCharArray(), text.toCharArray()), realPosition);
assertEquals(TextSearchAlgorithms.BoyerMooreHorspoolSearch(pattern.toCharArray(), text.toCharArray()), realPosition);
}
}