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);
}
public void dfsWithoutRecursion(int start) {
public boolean[] dfsWithoutRecursion(int start) {
Stack<Integer> stack = new Stack<Integer>();
boolean[] isVisited = new boolean[adjVertices.size()];
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()];
dfsRecursive(start, isVisited);
return dfsRecursive(start, isVisited);
}
private void dfsRecursive(int current, boolean[] isVisited) {
private boolean[] dfsRecursive(int current, boolean[] isVisited) {
isVisited[current] = true;
visit(current);
for (int dest : adjVertices.get(current)) {
if (!isVisited[dest])
dfsRecursive(dest, isVisited);
}
return isVisited;
}
public List<Integer> topologicalSort(int start) {

View File

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

View File

@ -16,7 +16,7 @@ class Voucher {
return true;
if (!(o instanceof Voucher))
return false;
Voucher other = (Voucher)o;
Voucher other = (Voucher) o;
boolean valueEquals = (this.value == null && other.value == null)
|| (this.value != null && this.value.equals(other.value));
boolean storeEquals = (this.store == null && other.store == null)

View File

@ -13,15 +13,25 @@ public class MoneyUnitTest {
Money expenses = new Money(55, "USD");
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
public void givenMoneyAndVoucherInstances_whenEquals_thenReturnValuesArentSymmetric() {
Money cash = new Money(42, "USD");
WrongVoucher voucher = new WrongVoucher(42, "USD", "Amazon");
Money money = new Money(42, "USD");
Voucher voucher = new Voucher(42, "USD", "Amazon");
assertFalse(voucher.equals(cash));
assertTrue(cash.equals(voucher));
assertFalse(voucher.equals(money));
assertFalse(money.equals(voucher));
}
}