mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
Add Unit Tests for Empty and Single-Node Graphs in TopologicalSort (#6263)
This commit is contained in:
@@ -3,6 +3,7 @@ package com.thealgorithms.sorts;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import com.thealgorithms.sorts.TopologicalSort.Graph;
|
||||
import java.util.LinkedList;
|
||||
@@ -59,4 +60,18 @@ class TopologicalSortTest {
|
||||
+ "Back edge: 6 -> 2";
|
||||
assertEquals(exception.getMessage(), expected);
|
||||
}
|
||||
@Test
|
||||
void testEmptyGraph() {
|
||||
Graph graph = new Graph();
|
||||
LinkedList<String> sorted = TopologicalSort.sort(graph);
|
||||
assertTrue(sorted.isEmpty());
|
||||
}
|
||||
@Test
|
||||
void testSingleNode() {
|
||||
Graph graph = new Graph();
|
||||
graph.addEdge("A", "");
|
||||
LinkedList<String> sorted = TopologicalSort.sort(graph);
|
||||
assertEquals(1, sorted.size());
|
||||
assertEquals("A", sorted.getFirst());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user