Skip to content

Instantly share code, notes, and snippets.

@shreydesai
Created December 5, 2016 21:56
Show Gist options
  • Save shreydesai/89f495cf87fde39a14f2daa0dfaf24fe to your computer and use it in GitHub Desktop.
Save shreydesai/89f495cf87fde39a14f2daa0dfaf24fe to your computer and use it in GitHub Desktop.
CS 314 Assignment 12 Tests
public class GraphAndRankTester {
public static void main(String[] args) {
testGraph1();
testGraph2();
testFootballRanker();
}
private static void testGraph1() {
String[][] edges;
String[] expectedPaths;
Object actual, expected;
Graph g;
edges = new String[][]{
{"A", "G", "30"},
{"G", "A", "30"},
{"G", "E", "10"},
{"E", "G", "10"},
{"A", "C", "25"},
{"C", "A", "25"},
{"G", "C", "25"},
{"C", "G", "25"},
{"G", "B", "10"},
{"B", "G", "10"},
{"E", "B", "5"},
{"B", "E", "5"},
{"B", "C", "1"},
{"E", "H", "120"},
{"C", "D", "60"},
{"D", "C", "60"},
{"B", "F", "50"},
{"B", "H", "25"},
{"F", "D", "5"},
{"H", "D", "15"}
};
g = new Graph();
for (String[] edge : edges) {
g.addEdge(edge[0], edge[1], Integer.parseInt(edge[2]));
}
g.dijkstra("A");
// Weighted - Test 1
actual = g.findPath("D").toString();
expected = "[A, G, B, H, D]";
test(expected, actual, "Weighted Graph 1 - Test 1");
// Weighted - Test 2
actual = g.findPath("F").toString();
expected = "[A, G, B, F]";
test(expected, actual, "Weighted Graph 1 - Test 2");
g.dijkstra("D");
// Weighted - Test 3
actual = g.findPath("H").toString();
expected = "[D, C, G, B, H]";
test(expected, actual, "Weighted Graph 1 - Test 3");
// Diameter - Test 4
g.findAllPaths(true);
actual = g.getDiameter();
expected = 5;
test(expected, actual, "Weighted Graph 1 - Test 4");
// Weighted Cost - Test 5
actual = g.costOfLongestShortestPath();
expected = 160.0;
test(expected, actual, "Weighted Graph 1 - Test 5");
// Weighted - Test 6-13
expectedPaths = new String[]{
"Name: B cost per path: 22.4286, num paths: 7",
"Name: E cost per path: 26.0000, num paths: 7",
"Name: G cost per path: 29.4286, num paths: 7",
"Name: C cost per path: 46.4286, num paths: 7",
"Name: A cost per path: 52.8571, num paths: 7",
"Name: F cost per path: 82.1429, num paths: 7",
"Name: H cost per path: 95.7143, num paths: 7",
"Name: D cost per path: 97.8571, num paths: 7"
};
int index = 0;
for (AllPathsInfo api : g.getAllPaths()) {
test(api.toString(), expectedPaths[index], "Weighted Graph 1 " +
"- Test " + (6 + index));
index++;
}
// Unweighted - Test 14
g.findAllPaths(false);
actual = g.costOfLongestShortestPath();
expected = 5.0;
test(expected, actual, "Unweighted Graph 1 - Test 14");
// Unweighted - Test 15-23
expectedPaths = new String[]{
"Name: B cost per path: 1.2857, num paths: 7",
"Name: G cost per path: 1.4286, num paths: 7",
"Name: E cost per path: 1.5714, num paths: 7",
"Name: C cost per path: 1.8571, num paths: 7",
"Name: A cost per path: 2.0000, num paths: 7",
"Name: D cost per path: 2.7143, num paths: 7",
"Name: F cost per path: 3.1429, num paths: 7",
"Name: H cost per path: 3.1429, num paths: 7"
};
index = 0;
for (AllPathsInfo api : g.getAllPaths()) {
test(api.toString(), expectedPaths[index], "Unweighted Graph 1 " +
"- Test " + (15 + index));
index++;
}
}
private static void testGraph2() {
String[][] edges;
String[] expectedPaths;
Object actual, expected;
Graph g;
edges = new String[][]{
{"C", "A", "5"},
{"A", "B", "10"},
{"B", "A", "10"},
{"A", "E", "40"},
{"B", "C", "30"},
{"E", "B", "20"},
{"D", "C", "5"},
{"B", "D", "20"},
{"E", "D", "5"},
};
g = new Graph();
for (String[] edge : edges) {
g.addEdge(edge[0], edge[1], Integer.parseInt(edge[2]));
}
g.dijkstra("A");
// Weighted Graph 2 - Test 1
expected = "[A, B, D, C]";
actual = g.findPath("C").toString();
test(expected, actual, "Weighted Graph 2 - Test 1");
// Weighted Graph 2 - Test 2
g.dijkstra("C");
expected = "[C, A, B, D]";
actual = g.findPath("D").toString();
test(expected, actual, "Weighted Graph 2 - Test 2");
// Diameter - Test 3
g.findAllPaths(true);
actual = g.getDiameter();
expected = 2;
test(expected, actual, "Weighted Graph 2 - Test 3");
// Weighted Cost - Test 4
actual = g.costOfLongestShortestPath();
expected = 50.0;
test(expected, actual, "Weighted Graph 2 - Test 4");
// Weighted - Test 5-10
expectedPaths = new String[]{
"Name: E cost per path: 12.5000, num paths: 4",
"Name: D cost per path: 21.2500, num paths: 4",
"Name: C cost per path: 25.0000, num paths: 4",
"Name: B cost per path: 26.2500, num paths: 4",
"Name: A cost per path: 28.7500, num paths: 4",
};
int index = 0;
for (AllPathsInfo api : g.getAllPaths()) {
test(api.toString(), expectedPaths[index], "Weighted Graph 2 " +
"- Test " + (5 + index));
index++;
}
}
private static void testFootballRanker() {
String actual;
String expected;
double result;
FootballRanker ranker;
// 2005 results
actual = "2014ap_poll.txt";
expected = "div12014.txt";
ranker = new FootballRanker(expected, actual);
result = ranker.doUnweighted(false);
test(result, 10.1, "FootballRanker - Test 1");
result = ranker.doWeighted(false);
test(result, 8.5, "FootballRanker - Test 2");
result = ranker.doWeightedAndWinPercentAdjusted(false);
test(result, 4.2, "FootballRanker - Test 3");
// 2008 results
actual = "2008ap_poll.txt";
expected = "div12008.txt";
ranker = new FootballRanker(expected, actual);
result = ranker.doUnweighted(false);
test(result, 13.7, "FootballRanker - Test 4");
result = ranker.doWeighted(false);
test(result, 12.6, "FootballRanker - Test 5");
result = ranker.doWeightedAndWinPercentAdjusted(false);
test(result, 6.3, "FootballRanker - Test 6");
// 2014 results
actual = "2005ap_poll.txt";
expected = "div12005.txt";
ranker = new FootballRanker(expected, actual);
result = ranker.doUnweighted(false);
test(result, 6.8, "FootballRanker - Test 4");
result = ranker.doWeighted(false);
test(result, 5.8, "FootballRanker - Test 5");
result = ranker.doWeightedAndWinPercentAdjusted(false);
test(result, 3.0, "FootballRanker - Test 6");
}
/**
* Runs tests on primitive data types
* pre: exp != null, actual != null, test != null
* post: output test failure or success
*/
private static boolean test(Object exp, Object actual, String test) {
String output = null;
if (exp.equals(actual)) {
output = "✔ Passed " + test;
System.out.println(output + "\n");
return true;
}
output = "***** FAILED ***** " + test + "\n" +
"Expected: " + exp + ", Actual: " + actual;
System.out.println(output + "\n");
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment