summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorHaidong Ji2020-07-10 20:23:19 -0500
committerHaidong Ji2020-07-10 20:23:19 -0500
commit4008cc6f7eb547d7e93808b9ba9a87fba9acf0e6 (patch)
treeaf230cfd45081cb39c5f6515a578a4cfd00f3732 /src/test
parent664a4e9667c5cc25b0ea1344b74cb0cbd6f33707 (diff)
Checking in implementation with stack. Didn't pass the grader. I'm checking in first and then start from scratch.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/ToposortTest.java108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/test/ToposortTest.java b/src/test/ToposortTest.java
new file mode 100644
index 0000000..e38d3bc
--- /dev/null
+++ b/src/test/ToposortTest.java
@@ -0,0 +1,108 @@
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class ToposortTest {
+
+ @Test
+ void test() {
+ ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
+ for (int i = 0; i < 4; i++) {
+ adj.add(new ArrayList<>());
+ }
+
+ adj.get(0).add(1);
+ adj.get(2).add(0);
+ adj.get(3).add(0);
+
+ ArrayList<Integer> results = new ArrayList<Integer>(Arrays.asList(4, 3, 6, 8));
+ assertEquals(results, Toposort.postOrder(adj));
+ }
+
+ @Test
+ void test1() {
+ ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
+ for (int i = 0; i < 5; i++) {
+ adj.add(new ArrayList<>());
+ }
+
+ adj.get(1).add(0);
+ adj.get(1).add(2);
+ adj.get(1).add(3);
+ adj.get(1).add(4);
+
+ adj.get(2).add(0);
+ adj.get(2).add(3);
+ adj.get(2).add(4);
+
+ adj.get(3).add(0);
+ adj.get(3).add(4);
+
+ adj.get(4).add(0);
+
+ ArrayList<Integer> results = new ArrayList<Integer>(Arrays.asList(2, 7, 6, 5, 4));
+ assertEquals(results, Toposort.postOrder(adj));
+ }
+
+ @Test
+ void test2() {
+ ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
+ for (int i = 0; i < 5; i++) {
+ adj.add(new ArrayList<>());
+ }
+
+ adj.get(1).add(0);
+ adj.get(1).add(2);
+ adj.get(1).add(4);
+ adj.get(1).add(3);
+
+ adj.get(2).add(0);
+ adj.get(2).add(3);
+ adj.get(2).add(4);
+
+ adj.get(3).add(4);
+ adj.get(3).add(0);
+
+ adj.get(4).add(0);
+
+ ArrayList<Integer> results = new ArrayList<Integer>(Arrays.asList(2, 7, 6, 5, 4));
+ assertEquals(results, Toposort.postOrder(adj));
+ }
+
+ @Test
+ void test10() {
+ ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
+ for (int i = 0; i < 10; i++) {
+ adj.add(new ArrayList<>());
+ }
+
+ adj.get(9).add(1);
+ adj.get(9).add(7);
+ adj.get(4).add(3);
+ adj.get(2).add(1);
+ adj.get(7).add(3);
+ adj.get(9).add(3);
+ adj.get(0).add(1);
+ adj.get(8).add(6);
+ adj.get(1).add(7);
+ adj.get(2).add(9);
+ adj.get(6).add(5);
+ adj.get(0).add(4);
+ adj.get(0).add(7);
+ adj.get(0).add(5);
+ adj.get(0).add(8);
+ adj.get(2).add(5);
+ adj.get(6).add(9);
+ adj.get(8).add(3);
+ adj.get(8).add(4);
+ adj.get(4).add(2);
+
+ ArrayList<Integer> results = new ArrayList<Integer>(Arrays.asList(13, 7, 9, 5, 10, 3, 11, 6, 12, 8));
+ assertEquals(results, Toposort.postOrder(adj));
+ }
+
+} \ No newline at end of file