Merge branch 'eugenp:master' into master

This commit is contained in:
ciphx
2023-03-24 15:20:39 +05:30
committed by GitHub
3394 changed files with 88253 additions and 11713 deletions
+13 -1
View File
@@ -65,6 +65,11 @@ core-java-io/target_link.txt
core-java/src/main/java/com/baeldung/manifest/MANIFEST.MF
ethereum/logs/
jmeter/src/main/resources/*-JMeter.csv
jmeter/src/main/resources/*-Basic*.csv
jmeter/src/main/resources/*-JMeter*.csv
jmeter/src/main/resources/*ReportsDashboard*.csv
jmeter/src/main/resources/dashboard/*ReportsDashboard*.csv
ninja/devDb.mv.db
**/node_modules/
@@ -102,4 +107,11 @@ spring-boot-modules/spring-boot-properties-3/*.log
.sdkmanrc
# Localstack
**/.localstack
**/.localstack
#libraries-2
libraries-2/employee*
libraries-2/src/test/resources/crawler4j/**
#web-modules/ninja
devDb*.db
+16 -2
View File
@@ -31,9 +31,11 @@ The projects are broadly divided into 3 lists: first, second and heavy.
Next, they are segregated further on the basis of the tests that we want to execute.
Additionally, there are 2 profiles dedicated for JDK9 and above builds.
Additionally, there are 2 profiles dedicated for JDK9 and above builds - **which require JDK 17**.
Therefore, we have a total of 8 profiles:
We also have a parents profile to build only parent modules.
Therefore, we have a total of 9 profiles:
| Profile | Includes | Type of test enabled |
| -------------------------- | --------------------------- | -------------------- |
@@ -45,6 +47,7 @@ Therefore, we have a total of 8 profiles:
| integration-heavy | Heavy/long running projects | *IntegrationTest |
| default-jdk9-and-above | JDK9 and above projects | *UnitTest |
| integration-jdk9-and-above | JDK9 and above projects | *IntegrationTest |
| parents | Set of parent modules | None |
Building the project
====================
@@ -71,6 +74,17 @@ Building a single module
====================
To build a specific module, run the command: `mvn clean install` in the module directory.
It can happen that your module is part of a parent module e.g. `parent-boot-1`,`parent-spring-5` etc, then you will need to build the parent module first so that you can build your module.
We have created a `parents` profile that you can use to build just the parent modules, just run the profile as:
`mvn clean install -Pparents`
Building modules from the root of the repository
====================
To build specific modules from the root of the repository, run the command: `mvn clean install --pl asm,atomikos -Pdefault-first` in the root directory.
Here `asm` and `atomikos` are the modules that we want to build and `default-first` is the maven profile in which these modules are present.
Running a Spring Boot module
====================
+1
View File
@@ -36,6 +36,7 @@
</dependency>
</dependencies>
<properties>
<akka.http.version>10.0.11</akka.http.version>
<akka.stream.version>2.5.11</akka.stream.version>
+1 -1
View File
@@ -43,7 +43,7 @@
</dependencyManagement>
<properties>
<spring.version>4.3.4.RELEASE</spring.version>
<spring.version>5.3.25</spring.version>
<akka.version>2.4.14</akka.version>
</properties>
@@ -1,22 +1,24 @@
package com.baeldung.algorithms;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Test;
import com.baeldung.algorithms.ga.ant_colony.AntColonyOptimization;
public class AntColonyOptimizationLongRunningUnitTest {
class AntColonyOptimizationLongRunningUnitTest {
@Test
public void testGenerateRandomMatrix() {
void testGenerateRandomMatrix() {
AntColonyOptimization antTSP = new AntColonyOptimization(5);
Assert.assertNotNull(antTSP.generateRandomMatrix(5));
assertNotNull(antTSP.generateRandomMatrix(5));
}
@Test
public void testStartAntOptimization() {
void testStartAntOptimization() {
AntColonyOptimization antTSP = new AntColonyOptimization(5);
Assert.assertNotNull(antTSP.solve());
assertNotNull(antTSP.solve());
}
}
@@ -1,16 +1,19 @@
package com.baeldung.algorithms;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import com.baeldung.algorithms.ga.binary.SimpleGeneticAlgorithm;
public class BinaryGeneticAlgorithmLongRunningUnitTest {
class BinaryGeneticAlgorithmLongRunningUnitTest {
@Test
public void testGA() {
void testGA() {
SimpleGeneticAlgorithm ga = new SimpleGeneticAlgorithm();
Assert.assertTrue(ga.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111"));
assertTrue(ga.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111"));
}
}
@@ -1,15 +1,17 @@
package com.baeldung.algorithms;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import com.baeldung.algorithms.ga.annealing.SimulatedAnnealing;
public class SimulatedAnnealingLongRunningUnitTest {
class SimulatedAnnealingLongRunningUnitTest {
@Test
public void testSimulateAnnealing() {
Assert.assertTrue(SimulatedAnnealing.simulateAnnealing(10, 1000, 0.9) > 0);
void testSimulateAnnealing() {
assertTrue(SimulatedAnnealing.simulateAnnealing(10, 1000, 0.9) > 0);
}
}
@@ -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);
@@ -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)));
}
});
}
/**
@@ -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);
@@ -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);
@@ -1,6 +1,6 @@
package com.baeldung.algorithms;
import org.junit.Test;
import com.baeldung.algorithms.ga.dijkstra.Dijkstra;
import com.baeldung.algorithms.ga.dijkstra.Graph;
@@ -11,7 +11,9 @@ import java.util.List;
import static org.junit.Assert.assertTrue;
public class DijkstraAlgorithmLongRunningUnitTest {
import org.junit.jupiter.api.Test;
class DijkstraAlgorithmLongRunningUnitTest {
@Test
public void whenSPPSolved_thenCorrect() {
@@ -1,5 +1,6 @@
package com.baeldung.algorithms.astar.underground;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.HashMap;
@@ -10,22 +11,22 @@ import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.baeldung.algorithms.astar.Graph;
import com.baeldung.algorithms.astar.RouteFinder;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
@Slf4j
public class RouteFinderIntegrationTest {
class RouteFinderIntegrationTest {
private Graph<Station> underground;
private RouteFinder<Station> routeFinder;
@Before
@BeforeEach
public void setUp() throws Exception {
Set<Station> stations = new HashSet<>();
Map<String, Set<String>> connections = new HashMap<>();
@@ -641,7 +642,7 @@ public class RouteFinderIntegrationTest {
}
@Test
public void findRoute() {
void findRoute() {
List<Station> route = routeFinder.findRoute(underground.getNode("74"), underground.getNode("7"));
assertThat(route).size().isPositive();
@@ -1,21 +0,0 @@
package com.baeldung.algorithms.editdistance;
import org.junit.runners.Parameterized.Parameters;
import java.util.Arrays;
import java.util.Collection;
public class EditDistanceDataProvider {
@Parameters
public static Collection<Object[]> getLists() {
return Arrays.asList(new Object[][] {
{ "", "", 0 },
{ "ago", "", 3 },
{ "", "do", 2 },
{ "abc", "adc", 1 },
{ "peek", "pesek", 1 },
{ "sunday", "saturday", 3 }
});
}
}
@@ -1,32 +1,39 @@
package com.baeldung.algorithms.editdistance;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
@RunWith(Parameterized.class)
public class EditDistanceUnitTest extends EditDistanceDataProvider {
import java.util.stream.Stream;
private String x;
private String y;
private int result;
public EditDistanceUnitTest(String a, String b, int res) {
super();
x = a;
y = b;
result = res;
}
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
@Test
public void testEditDistance_RecursiveImplementation() {
import org.junit.jupiter.params.provider.MethodSource;
class EditDistanceUnitTest {
@ParameterizedTest
@MethodSource("provideArguments")
void testEditDistance_RecursiveImplementation(String x, String y, int result) {
assertEquals(result, EditDistanceRecursive.calculate(x, y));
}
@Test
public void testEditDistance_givenDynamicProgrammingImplementation() {
@ParameterizedTest
@MethodSource("provideArguments")
void testEditDistance_givenDynamicProgrammingImplementation(String x, String y, int result) {
assertEquals(result, EditDistanceDynamicProgramming.calculate(x, y));
}
static Stream<? extends Arguments> provideArguments() {
return Stream.of(new Object[][] {
{ "", "", 0 },
{ "ago", "", 3 },
{ "", "do", 2 },
{ "abc", "adc", 1 },
{ "peek", "pesek", 1 },
{ "sunday", "saturday", 3 }
}).map(Arguments::of);
}
}
@@ -1,23 +1,16 @@
package com.baeldung.algorithms.linkedlist;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import static org.junit.jupiter.api.Assertions.assertEquals;
@RunWith(value = Parameterized.class)
public class CycleDetectionBruteForceUnitTest extends CycleDetectionTestBase {
boolean cycleExists;
Node<Integer> head;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
public CycleDetectionBruteForceUnitTest(Node<Integer> head, boolean cycleExists) {
super();
this.cycleExists = cycleExists;
this.head = head;
}
class CycleDetectionBruteForceUnitTest extends CycleDetectionTestBase {
@Test
public void givenList_detectLoop() {
Assert.assertEquals(cycleExists, CycleDetectionBruteForce.detectCycle(head).cycleExists);
@ParameterizedTest
@MethodSource("getLists")
void givenList_detectLoop(Node<Integer> head, boolean cycleExists) {
assertEquals(cycleExists, CycleDetectionBruteForce.detectCycle(head).cycleExists);
}
}
@@ -1,23 +1,16 @@
package com.baeldung.algorithms.linkedlist;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(value = Parameterized.class)
public class CycleDetectionByFastAndSlowIteratorsUnitTest extends CycleDetectionTestBase {
boolean cycleExists;
Node<Integer> head;
import static org.junit.jupiter.api.Assertions.assertEquals;
public CycleDetectionByFastAndSlowIteratorsUnitTest(Node<Integer> head, boolean cycleExists) {
super();
this.cycleExists = cycleExists;
this.head = head;
}
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
@Test
public void givenList_detectLoop() {
Assert.assertEquals(cycleExists, CycleDetectionByFastAndSlowIterators.detectCycle(head).cycleExists);
class CycleDetectionByFastAndSlowIteratorsUnitTest extends CycleDetectionTestBase {
@ParameterizedTest
@MethodSource("getLists")
void givenList_detectLoop(Node<Integer> head, boolean cycleExists) {
assertEquals(cycleExists, CycleDetectionByFastAndSlowIterators.detectCycle(head).cycleExists);
}
}
@@ -1,23 +1,17 @@
package com.baeldung.algorithms.linkedlist;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(value = Parameterized.class)
public class CycleDetectionByHashingUnitTest extends CycleDetectionTestBase {
boolean cycleExists;
Node<Integer> head;
import static org.junit.jupiter.api.Assertions.assertEquals;
public CycleDetectionByHashingUnitTest(Node<Integer> head, boolean cycleExists) {
super();
this.cycleExists = cycleExists;
this.head = head;
}
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
@Test
public void givenList_detectLoop() {
Assert.assertEquals(cycleExists, CycleDetectionByHashing.detectCycle(head).cycleExists);
class CycleDetectionByHashingUnitTest extends CycleDetectionTestBase {
@ParameterizedTest
@MethodSource("getLists")
void givenList_detectLoop(Node<Integer> head, boolean cycleExists) {
assertEquals(cycleExists, CycleDetectionByHashing.detectCycle(head).cycleExists);
}
}
@@ -7,7 +7,7 @@ import org.junit.runners.Parameterized.Parameters;
public class CycleDetectionTestBase {
@Parameters
public static Collection<Object[]> getLists() {
return Arrays.asList(new Object[][] {
{ createList(), false },
@@ -1,24 +1,19 @@
package com.baeldung.algorithms.linkedlist;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(value = Parameterized.class)
public class CycleRemovalBruteForceUnitTest extends CycleDetectionTestBase {
boolean cycleExists;
Node<Integer> head;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
public CycleRemovalBruteForceUnitTest(Node<Integer> head, boolean cycleExists) {
super();
this.cycleExists = cycleExists;
this.head = head;
}
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
@Test
public void givenList_ifLoopExists_thenDetectAndRemoveLoop() {
Assert.assertEquals(cycleExists, CycleRemovalBruteForce.detectAndRemoveCycle(head));
Assert.assertFalse(CycleDetectionByFastAndSlowIterators.detectCycle(head).cycleExists);
class CycleRemovalBruteForceUnitTest extends CycleDetectionTestBase {
@ParameterizedTest
@MethodSource("getLists")
void givenList_ifLoopExists_thenDetectAndRemoveLoop(Node<Integer> head, boolean cycleExists) {
assertEquals(cycleExists, CycleRemovalBruteForce.detectAndRemoveCycle(head));
assertFalse(CycleDetectionByFastAndSlowIterators.detectCycle(head).cycleExists);
}
}
@@ -1,24 +1,17 @@
package com.baeldung.algorithms.linkedlist;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
@RunWith(value = Parameterized.class)
public class CycleRemovalByCountingLoopNodesUnitTest extends CycleDetectionTestBase {
boolean cycleExists;
Node<Integer> head;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
public CycleRemovalByCountingLoopNodesUnitTest(Node<Integer> head, boolean cycleExists) {
super();
this.cycleExists = cycleExists;
this.head = head;
}
class CycleRemovalByCountingLoopNodesUnitTest extends CycleDetectionTestBase {
@Test
public void givenList_ifLoopExists_thenDetectAndRemoveLoop() {
Assert.assertEquals(cycleExists, CycleRemovalByCountingLoopNodes.detectAndRemoveCycle(head));
Assert.assertFalse(CycleDetectionByFastAndSlowIterators.detectCycle(head).cycleExists);
@ParameterizedTest
@MethodSource("getLists")
void givenList_ifLoopExists_thenDetectAndRemoveLoop(Node<Integer> head, boolean cycleExists) {
assertEquals(cycleExists, CycleRemovalByCountingLoopNodes.detectAndRemoveCycle(head));
assertFalse(CycleDetectionByFastAndSlowIterators.detectCycle(head).cycleExists);
}
}
@@ -1,24 +1,19 @@
package com.baeldung.algorithms.linkedlist;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(value = Parameterized.class)
public class CycleRemovalWithoutCountingLoopNodesUnitTest extends CycleDetectionTestBase {
boolean cycleExists;
Node<Integer> head;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
public CycleRemovalWithoutCountingLoopNodesUnitTest(Node<Integer> head, boolean cycleExists) {
super();
this.cycleExists = cycleExists;
this.head = head;
}
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
@Test
public void givenList_ifLoopExists_thenDetectAndRemoveLoop() {
Assert.assertEquals(cycleExists, CycleRemovalWithoutCountingLoopNodes.detectAndRemoveCycle(head));
Assert.assertFalse(CycleDetectionByFastAndSlowIterators.detectCycle(head).cycleExists);
class CycleRemovalWithoutCountingLoopNodesUnitTest extends CycleDetectionTestBase {
@ParameterizedTest
@MethodSource("getLists")
void givenList_ifLoopExists_thenDetectAndRemoveLoop(Node<Integer> head, boolean cycleExists) {
assertEquals(cycleExists, CycleRemovalWithoutCountingLoopNodes.detectAndRemoveCycle(head));
assertFalse(CycleDetectionByFastAndSlowIterators.detectCycle(head).cycleExists);
}
}
@@ -1,84 +1,84 @@
package com.baeldung.algorithms.moneywords;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import com.baeldung.algorithms.numberwordconverter.NumberWordConverter;
public class NumberWordConverterUnitTest {
class NumberWordConverterUnitTest {
@Test
public void whenMoneyNegative_thenReturnInvalidInput() {
void whenMoneyNegative_thenReturnInvalidInput() {
assertEquals(NumberWordConverter.INVALID_INPUT_GIVEN, NumberWordConverter.getMoneyIntoWords(-13));
}
@Test
public void whenZeroDollarsGiven_thenReturnEmptyString() {
void whenZeroDollarsGiven_thenReturnEmptyString() {
assertEquals("", NumberWordConverter.getMoneyIntoWords(0));
}
@Test
public void whenOnlyDollarsGiven_thenReturnWords() {
void whenOnlyDollarsGiven_thenReturnWords() {
assertEquals("one dollar", NumberWordConverter.getMoneyIntoWords(1));
}
@Test
public void whenOnlyCentsGiven_thenReturnWords() {
void whenOnlyCentsGiven_thenReturnWords() {
assertEquals("sixty cents", NumberWordConverter.getMoneyIntoWords(0.6));
}
@Test
public void whenAlmostAMillioDollarsGiven_thenReturnWords() {
void whenAlmostAMillioDollarsGiven_thenReturnWords() {
String expectedResult = "nine hundred ninety nine thousand nine hundred ninety nine dollars";
assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(999_999));
}
@Test
public void whenThirtyMillionDollarsGiven_thenReturnWords() {
void whenThirtyMillionDollarsGiven_thenReturnWords() {
String expectedResult = "thirty three million three hundred forty eight thousand nine hundred seventy eight dollars";
assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(33_348_978));
}
@Test
public void whenTwoBillionDollarsGiven_thenReturnWords() {
void whenTwoBillionDollarsGiven_thenReturnWords() {
String expectedResult = "two billion one hundred thirty three million two hundred forty seven thousand eight hundred ten dollars";
assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(2_133_247_810));
}
@Test
public void whenGivenDollarsAndCents_thenReturnWords() {
void whenGivenDollarsAndCents_thenReturnWords() {
String expectedResult = "nine hundred twenty four dollars and sixty cents";
assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(924.6));
}
@Test
public void whenOneDollarAndNoCents_thenReturnDollarSingular() {
void whenOneDollarAndNoCents_thenReturnDollarSingular() {
assertEquals("one dollar", NumberWordConverter.getMoneyIntoWords(1));
}
@Test
public void whenNoDollarsAndOneCent_thenReturnCentSingular() {
void whenNoDollarsAndOneCent_thenReturnCentSingular() {
assertEquals("one cent", NumberWordConverter.getMoneyIntoWords(0.01));
}
@Test
public void whenNoDollarsAndTwoCents_thenReturnCentsPlural() {
void whenNoDollarsAndTwoCents_thenReturnCentsPlural() {
assertEquals("two cents", NumberWordConverter.getMoneyIntoWords(0.02));
}
@Test
public void whenNoDollarsAndNinetyNineCents_thenReturnWords() {
void whenNoDollarsAndNinetyNineCents_thenReturnWords() {
assertEquals("ninety nine cents", NumberWordConverter.getMoneyIntoWords(0.99));
}
@Test
public void whenNoDollarsAndNineFiveNineCents_thenCorrectRounding() {
void whenNoDollarsAndNineFiveNineCents_thenCorrectRounding() {
assertEquals("ninety six cents", NumberWordConverter.getMoneyIntoWords(0.959));
}
@Test
public void whenGivenDollarsAndCents_thenReturnWordsVersionTwo() {
void whenGivenDollarsAndCents_thenReturnWordsVersionTwo() {
assertEquals("three hundred ten £ 00/100", NumberWordConverter.getMoneyIntoWords("310"));
}
}
@@ -1,6 +1,8 @@
package com.baeldung.jgrapht;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
@@ -9,15 +11,15 @@ import org.jgrapht.alg.HamiltonianCycle;
import org.jgrapht.generate.CompleteGraphGenerator;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.SimpleWeightedGraph;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class CompleteGraphUnitTest {
class CompleteGraphUnitTest {
static SimpleWeightedGraph<String, DefaultEdge> completeGraph;
static int size = 10;
@Before
@BeforeEach
public void createCompleteGraph() {
completeGraph = new SimpleWeightedGraph<>(DefaultEdge.class);
CompleteGraphGenerator<String, DefaultEdge> completeGenerator = new CompleteGraphGenerator<String, DefaultEdge>(size);
@@ -1,7 +1,9 @@
package com.baeldung.jgrapht;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.List;
@@ -21,13 +23,13 @@ import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.DirectedSubgraph;
import org.jgrapht.traverse.BreadthFirstIterator;
import org.jgrapht.traverse.DepthFirstIterator;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class DirectedGraphUnitTest {
class DirectedGraphUnitTest {
DirectedGraph<String, DefaultEdge> directedGraph;
@Before
@BeforeEach
public void createDirectedGraph() {
directedGraph = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
IntStream.range(1, 10).forEach(i -> {
@@ -46,7 +48,7 @@ public class DirectedGraphUnitTest {
}
@Test
public void givenDirectedGraph_whenGetStronglyConnectedSubgraphs_thenPathExistsBetweenStronglyconnectedVertices() {
void givenDirectedGraph_whenGetStronglyConnectedSubgraphs_thenPathExistsBetweenStronglyconnectedVertices() {
StrongConnectivityAlgorithm<String, DefaultEdge> scAlg = new KosarajuStrongConnectivityInspector<>(directedGraph);
List<DirectedSubgraph<String, DefaultEdge>> stronglyConnectedSubgraphs = scAlg.stronglyConnectedSubgraphs();
List<String> stronglyConnectedVertices = new ArrayList<>(stronglyConnectedSubgraphs.get(3).vertexSet());
@@ -60,7 +62,7 @@ public class DirectedGraphUnitTest {
}
@Test
public void givenDirectedGraphWithCycle_whenCheckCycles_thenDetectCycles() {
void givenDirectedGraphWithCycle_whenCheckCycles_thenDetectCycles() {
CycleDetector<String, DefaultEdge> cycleDetector = new CycleDetector<String, DefaultEdge>(directedGraph);
assertTrue(cycleDetector.detectCycles());
Set<String> cycleVertices = cycleDetector.findCycles();
@@ -68,26 +70,26 @@ public class DirectedGraphUnitTest {
}
@Test
public void givenDirectedGraph_whenCreateInstanceDepthFirstIterator_thenGetIterator() {
void givenDirectedGraph_whenCreateInstanceDepthFirstIterator_thenGetIterator() {
DepthFirstIterator depthFirstIterator = new DepthFirstIterator<>(directedGraph);
assertNotNull(depthFirstIterator);
}
@Test
public void givenDirectedGraph_whenCreateInstanceBreadthFirstIterator_thenGetIterator() {
void givenDirectedGraph_whenCreateInstanceBreadthFirstIterator_thenGetIterator() {
BreadthFirstIterator breadthFirstIterator = new BreadthFirstIterator<>(directedGraph);
assertNotNull(breadthFirstIterator);
}
@Test
public void givenDirectedGraph_whenGetDijkstraShortestPath_thenGetNotNullPath() {
void givenDirectedGraph_whenGetDijkstraShortestPath_thenGetNotNullPath() {
DijkstraShortestPath dijkstraShortestPath = new DijkstraShortestPath(directedGraph);
List<String> shortestPath = dijkstraShortestPath.getPath("v1", "v4").getVertexList();
assertNotNull(shortestPath);
}
@Test
public void givenDirectedGraph_whenGetBellmanFordShortestPath_thenGetNotNullPath() {
void givenDirectedGraph_whenGetBellmanFordShortestPath_thenGetNotNullPath() {
BellmanFordShortestPath bellmanFordShortestPath = new BellmanFordShortestPath(directedGraph);
List<String> shortestPath = bellmanFordShortestPath.getPath("v1", "v4").getVertexList();
assertNotNull(shortestPath);
@@ -1,7 +1,7 @@
package com.baeldung.jgrapht;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.stream.IntStream;
@@ -9,13 +9,13 @@ import org.jgrapht.GraphPath;
import org.jgrapht.alg.cycle.HierholzerEulerianCycle;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.SimpleWeightedGraph;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class EulerianCircuitUnitTest {
class EulerianCircuitUnitTest {
SimpleWeightedGraph<String, DefaultEdge> simpleGraph;
@Before
@BeforeEach
public void createGraphWithEulerianCircuit() {
simpleGraph = new SimpleWeightedGraph<>(DefaultEdge.class);
IntStream.range(1, 6).forEach(i -> {
@@ -1,6 +1,8 @@
package com.baeldung.jgrapht;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.awt.Color;
import java.awt.image.BufferedImage;
@@ -12,18 +14,18 @@ import javax.imageio.ImageIO;
import org.jgrapht.ext.JGraphXAdapter;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.mxgraph.layout.mxCircleLayout;
import com.mxgraph.layout.mxIGraphLayout;
import com.mxgraph.util.mxCellRenderer;
public class GraphImageGenerationUnitTest {
class GraphImageGenerationUnitTest {
static DefaultDirectedGraph<String, DefaultEdge> g;
@Before
@BeforeEach
public void createGraph() throws IOException {
File imgFile = new File("src/test/resources/graph1.png");
imgFile.createNewFile();
@@ -39,14 +41,14 @@ public class GraphImageGenerationUnitTest {
g.addEdge(x3, x1);
}
@After
@AfterEach
public void cleanup() {
File imgFile = new File("src/test/resources/graph1.png");
imgFile.deleteOnExit();
}
@Test
public void givenAdaptedGraph_whenWriteBufferedImage_ThenFileShouldExist() throws IOException {
void givenAdaptedGraph_whenWriteBufferedImage_ThenFileShouldExist() throws IOException {
JGraphXAdapter<String, DefaultEdge> graphAdapter = new JGraphXAdapter<String, DefaultEdge>(g);
mxIGraphLayout layout = new mxCircleLayout(graphAdapter);
layout.execute(graphAdapter.getDefaultParent());
@@ -0,0 +1,58 @@
package com.baeldung.algorithms.checktargetsum;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class CheckTargetSum {
public boolean isTargetSumExistNaive(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
return true;
}
}
}
return false;
}
public boolean isTargetSumExistSorted(int[] nums, int target) {
Arrays.sort(nums);
int start = 0;
int end = nums.length - 1;
while (start < end) {
int sum = nums[start] + nums[end];
if (sum == target) {
return true;
}
if (sum < target) {
start++;
} else {
end--;
}
}
return false;
}
public boolean isTargetSumExistHashSet(int[] nums, int target) {
Set<Integer> hashSet = new HashSet<>();
for (int num : nums) {
int diff = target - num;
if (hashSet.contains(diff)) {
return true;
}
hashSet.add(num);
}
return false;
}
}
@@ -1,14 +1,14 @@
package com.baeldung.algorithms.analysis;
import org.junit.Test;
import org.junit.jupiter.api.Test;
public class AnalysisRunnerLiveTest {
class AnalysisRunnerLiveTest {
int n = 10;
int total = 0;
@Test
public void whenConstantComplexity_thenConstantRuntime() {
void whenConstantComplexity_thenConstantRuntime() {
System.out.println("**** n = " + n + " ****");
System.out.println();
@@ -22,7 +22,7 @@ public class AnalysisRunnerLiveTest {
}
@Test
public void whenLogarithmicComplexity_thenLogarithmicRuntime() {
void whenLogarithmicComplexity_thenLogarithmicRuntime() {
// Logarithmic Time
System.out.println("**** Logarithmic Time ****");
for (int i = 1; i < n; i = i * 2) {
@@ -34,7 +34,7 @@ public class AnalysisRunnerLiveTest {
}
@Test
public void whenLinearComplexity_thenLinearRuntime() {
void whenLinearComplexity_thenLinearRuntime() {
// Linear Time
System.out.println("**** Linear Time ****");
for (int i = 0; i < n; i++) {
@@ -47,7 +47,7 @@ public class AnalysisRunnerLiveTest {
}
@Test
public void whenNLogNComplexity_thenNLogNRuntime() {
void whenNLogNComplexity_thenNLogNRuntime() {
// N Log N Time
System.out.println("**** nlogn Time ****");
total = 0;
@@ -64,7 +64,7 @@ public class AnalysisRunnerLiveTest {
}
@Test
public void whenQuadraticComplexity_thenQuadraticRuntime() {
void whenQuadraticComplexity_thenQuadraticRuntime() {
// Quadratic Time
System.out.println("**** Quadratic Time ****");
total = 0;
@@ -81,7 +81,7 @@ public class AnalysisRunnerLiveTest {
}
@Test
public void whenCubicComplexity_thenCubicRuntime() {
void whenCubicComplexity_thenCubicRuntime() {
// Cubic Time
System.out.println("**** Cubic Time ****");
total = 0;
@@ -100,7 +100,7 @@ public class AnalysisRunnerLiveTest {
}
@Test
public void whenExponentialComplexity_thenExponentialRuntime() {
void whenExponentialComplexity_thenExponentialRuntime() {
// Exponential Time
System.out.println("**** Exponential Time ****");
total = 0;
@@ -115,7 +115,7 @@ public class AnalysisRunnerLiveTest {
}
@Test
public void whenFactorialComplexity_thenFactorialRuntime() {
void whenFactorialComplexity_thenFactorialRuntime() {
// Factorial Time
System.out.println("**** Factorial Time ****");
total = 0;
@@ -11,10 +11,10 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class SortedListCheckerUnitTest {
class SortedListCheckerUnitTest {
private List<String> sortedListOfString;
private List<String> unsortedListOfString;
@@ -23,7 +23,7 @@ public class SortedListCheckerUnitTest {
private List<Employee> employeesSortedByName;
private List<Employee> employeesNotSortedByName;
@Before
@BeforeEach
public void setUp() {
sortedListOfString = asList("Canada", "HK", "LA", "NJ", "NY");
unsortedListOfString = asList("LA", "HK", "NJ", "NY", "Canada");
@@ -34,72 +34,72 @@ public class SortedListCheckerUnitTest {
}
@Test
public void givenSortedList_whenUsingIterativeApproach_thenReturnTrue() {
void givenSortedList_whenUsingIterativeApproach_thenReturnTrue() {
assertThat(checkIfSortedUsingIterativeApproach(sortedListOfString)).isTrue();
}
@Test
public void givenSingleElementList_whenUsingIterativeApproach_thenReturnTrue() {
void givenSingleElementList_whenUsingIterativeApproach_thenReturnTrue() {
assertThat(checkIfSortedUsingIterativeApproach(singletonList)).isTrue();
}
@Test
public void givenUnsortedList_whenUsingIterativeApproach_thenReturnFalse() {
void givenUnsortedList_whenUsingIterativeApproach_thenReturnFalse() {
assertThat(checkIfSortedUsingIterativeApproach(unsortedListOfString)).isFalse();
}
@Test
public void givenSortedListOfEmployees_whenUsingIterativeApproach_thenReturnTrue() {
void givenSortedListOfEmployees_whenUsingIterativeApproach_thenReturnTrue() {
assertThat(checkIfSortedUsingIterativeApproach(employeesSortedByName, Comparator.comparing(Employee::getName))).isTrue();
}
@Test
public void givenUnsortedListOfEmployees_whenUsingIterativeApproach_thenReturnFalse() {
void givenUnsortedListOfEmployees_whenUsingIterativeApproach_thenReturnFalse() {
assertThat(checkIfSortedUsingIterativeApproach(employeesNotSortedByName, Comparator.comparing(Employee::getName))).isFalse();
}
@Test
public void givenSortedList_whenUsingRecursion_thenReturnTrue() {
void givenSortedList_whenUsingRecursion_thenReturnTrue() {
assertThat(checkIfSortedUsingRecursion(sortedListOfString)).isTrue();
}
@Test
public void givenSingleElementList_whenUsingRecursion_thenReturnTrue() {
void givenSingleElementList_whenUsingRecursion_thenReturnTrue() {
assertThat(checkIfSortedUsingRecursion(singletonList)).isTrue();
}
@Test
public void givenUnsortedList_whenUsingRecursion_thenReturnFalse() {
void givenUnsortedList_whenUsingRecursion_thenReturnFalse() {
assertThat(checkIfSortedUsingRecursion(unsortedListOfString)).isFalse();
}
@Test
public void givenSortedList_whenUsingGuavaOrdering_thenReturnTrue() {
void givenSortedList_whenUsingGuavaOrdering_thenReturnTrue() {
assertThat(checkIfSortedUsingOrderingClass(sortedListOfString)).isTrue();
}
@Test
public void givenUnsortedList_whenUsingGuavaOrdering_thenReturnFalse() {
void givenUnsortedList_whenUsingGuavaOrdering_thenReturnFalse() {
assertThat(checkIfSortedUsingOrderingClass(unsortedListOfString)).isFalse();
}
@Test
public void givenSortedListOfEmployees_whenUsingGuavaOrdering_thenReturnTrue() {
void givenSortedListOfEmployees_whenUsingGuavaOrdering_thenReturnTrue() {
assertThat(checkIfSortedUsingOrderingClass(employeesSortedByName, Comparator.comparing(Employee::getName))).isTrue();
}
@Test
public void givenUnsortedListOfEmployees_whenUsingGuavaOrdering_thenReturnFalse() {
void givenUnsortedListOfEmployees_whenUsingGuavaOrdering_thenReturnFalse() {
assertThat(checkIfSortedUsingOrderingClass(employeesNotSortedByName, Comparator.comparing(Employee::getName))).isFalse();
}
@Test
public void givenSortedList_whenUsingGuavaComparators_thenReturnTrue() {
void givenSortedList_whenUsingGuavaComparators_thenReturnTrue() {
assertThat(checkIfSortedUsingComparators(sortedListOfString)).isTrue();
}
@Test
public void givenUnsortedList_whenUsingGuavaComparators_thenReturnFalse() {
void givenUnsortedList_whenUsingGuavaComparators_thenReturnFalse() {
assertThat(checkIfSortedUsingComparators(unsortedListOfString)).isFalse();
}

Some files were not shown because too many files have changed in this diff Show More