Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import org.junit.Test;
import static org.junit.Assert.*;

public class TSPTest {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to use @ParameterizedTest. Also, it is better to test both the path and minimum cost for all cases.


@Test
public void testTSPWithSmallGraph() {
int[][] distanceMatrix = {
{ 0, 10, 15, 20 },
{ 10, 0, 35, 25 },
{ 15, 35, 0, 30 },
{ 20, 25, 30, 0 }
};

TSP.initialize(distanceMatrix);

int minCost = TSP.tsp(0, 1);

assertEquals(80, minCost);
}

@Test
public void testOptimalPath() {
int[][] distanceMatrix = {
{ 0, 10, 15, 20 },
{ 10, 0, 35, 25 },
{ 15, 35, 0, 30 },
{ 20, 25, 30, 0 }
};

TSP.initialize(distanceMatrix);

TSP.tsp(0, 1);

String expectedPath = "0 → 1 → 3 → 2 → 0";
String actualPath = TSP.printPath(0, 1);

assertEquals(expectedPath, actualPath);
}

@Test
public void testDifferentGraph() {
int[][] distanceMatrix = {
{ 0, 29, 20, 21 },
{ 29, 0, 15, 17 },
{ 20, 15, 0, 28 },
{ 21, 17, 28, 0 }
};

TSP.initialize(distanceMatrix);

int minCost = TSP.tsp(0, 1);

assertEquals(75, minCost);

String expectedPath = "0 → 2 → 1 → 3 → 0";
String actualPath = TSP.printPath(0, 1);

assertEquals(expectedPath, actualPath);
}
}
Loading