Add Unit Tests for Empty and Single-Node Graphs in TopologicalSort (#6263)

This commit is contained in:
Saipriya Patnaik
2025-06-11 02:37:24 +05:30
committed by GitHub
parent ed4a724e33
commit e41c2b9456

View File

@@ -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());
}
}