Merge pull request #13810 from press0/master

pr: add return types for assertion; test WrongVoucher vs GoodVoucher
This commit is contained in:
Kasra Madadipouya 2023-05-25 20:04:24 +02:00 committed by GitHub
commit 605d45ab11
4 changed files with 31 additions and 12 deletions

View File

@ -23,7 +23,7 @@ public class Graph {
adjVertices.get(src).add(dest); adjVertices.get(src).add(dest);
} }
public void dfsWithoutRecursion(int start) { public boolean[] dfsWithoutRecursion(int start) {
Stack<Integer> stack = new Stack<Integer>(); Stack<Integer> stack = new Stack<Integer>();
boolean[] isVisited = new boolean[adjVertices.size()]; boolean[] isVisited = new boolean[adjVertices.size()];
stack.push(start); stack.push(start);
@ -38,20 +38,22 @@ public class Graph {
} }
} }
} }
return isVisited;
} }
public void dfs(int start) { public boolean[] dfs(int start) {
boolean[] isVisited = new boolean[adjVertices.size()]; boolean[] isVisited = new boolean[adjVertices.size()];
dfsRecursive(start, isVisited); return dfsRecursive(start, isVisited);
} }
private void dfsRecursive(int current, boolean[] isVisited) { private boolean[] dfsRecursive(int current, boolean[] isVisited) {
isVisited[current] = true; isVisited[current] = true;
visit(current); visit(current);
for (int dest : adjVertices.get(current)) { for (int dest : adjVertices.get(current)) {
if (!isVisited[dest]) if (!isVisited[dest])
dfsRecursive(dest, isVisited); dfsRecursive(dest, isVisited);
} }
return isVisited;
} }
public List<Integer> topologicalSort(int start) { public List<Integer> topologicalSort(int start) {

View File

@ -1,7 +1,9 @@
package com.baeldung.algorithms.dfs; package com.baeldung.algorithms.dfs;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.junit.Assert;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
class GraphUnitTest { class GraphUnitTest {
@ -9,9 +11,12 @@ class GraphUnitTest {
@Test @Test
void givenDirectedGraph_whenDFS_thenPrintAllValues() { void givenDirectedGraph_whenDFS_thenPrintAllValues() {
Graph graph = createDirectedGraph(); Graph graph = createDirectedGraph();
graph.dfs(0); boolean[] visited;
System.out.println(); visited = graph.dfs(0);
graph.dfsWithoutRecursion(0); boolean[] expected = new boolean[]{true, true, true, true, true, true};
Assert.assertArrayEquals(expected, visited);
visited = graph.dfsWithoutRecursion(0);
Assert.assertArrayEquals(expected, visited);
} }
@Test @Test
@ -19,6 +24,8 @@ class GraphUnitTest {
Graph graph = createDirectedGraph(); Graph graph = createDirectedGraph();
List<Integer> list = graph.topologicalSort(0); List<Integer> list = graph.topologicalSort(0);
System.out.println(list); System.out.println(list);
List<Integer> expected = Arrays.asList(0, 2, 1, 3, 4, 5);
Assert.assertEquals(expected, list);
} }
private Graph createDirectedGraph() { private Graph createDirectedGraph() {

View File

@ -13,15 +13,25 @@ public class MoneyUnitTest {
Money expenses = new Money(55, "USD"); Money expenses = new Money(55, "USD");
assertTrue(income.equals(expenses)); assertTrue(income.equals(expenses));
assertTrue(expenses.equals(income));
}
@Test
public void givenMoneyAndWrongVoucherInstances_whenEquals_thenReturnValuesArentSymmetric() {
Money money = new Money(42, "USD");
WrongVoucher voucher = new WrongVoucher(42, "USD", "Amazon");
assertFalse(voucher.equals(money));
assertTrue(money.equals(voucher));
} }
@Test @Test
public void givenMoneyAndVoucherInstances_whenEquals_thenReturnValuesArentSymmetric() { public void givenMoneyAndVoucherInstances_whenEquals_thenReturnValuesArentSymmetric() {
Money cash = new Money(42, "USD"); Money money = new Money(42, "USD");
WrongVoucher voucher = new WrongVoucher(42, "USD", "Amazon"); Voucher voucher = new Voucher(42, "USD", "Amazon");
assertFalse(voucher.equals(cash)); assertFalse(voucher.equals(money));
assertTrue(cash.equals(voucher)); assertFalse(money.equals(voucher));
} }
} }