|
1 |
| -import { getTestAnswer } from "../../helperTests"; |
| 1 | +import { getTestAnswer, getTestAnswerArr } from "../../helperTests"; |
2 | 2 | import Graph from "./graph";
|
3 | 3 |
|
4 | 4 | describe("Graph class test", () => {
|
@@ -61,4 +61,40 @@ describe("Graph class test", () => {
|
61 | 61 | expect(labeledOrder.get("w")).toBeGreaterThan(1);
|
62 | 62 | expect(labeledOrder.get("w")).toBeLessThan(4);
|
63 | 63 | });
|
| 64 | + |
| 65 | + test("Kosaraju's Algorithm, using test cases from stanford-algs repo", () => { |
| 66 | + const TEST48 = `mostlyCycles_48_12800.txt`; |
| 67 | + const TEST52 = `mostlyCycles_52_20000.txt`; |
| 68 | + const TEST56 = `mostlyCycles_56_40000.txt`; |
| 69 | + const TEST60 = `mostlyCycles_60_80000.txt`; |
| 70 | + const TEST64 = `mostlyCycles_64_160000.txt`; |
| 71 | + |
| 72 | + const largest5 = (m: Map<string, number> | boolean) => { |
| 73 | + if (typeof m === "boolean") return false; |
| 74 | + let arr = Array.from(m.values()); |
| 75 | + arr = arr.sort((a, b) => b - a); |
| 76 | + return arr.slice(0, 5); |
| 77 | + }; |
| 78 | + const test = (file: string) => { |
| 79 | + const g = Graph.createDirected( |
| 80 | + `${__dirname}/test-datasets/kosaraju/input_${file}` |
| 81 | + ); |
| 82 | + const ans = getTestAnswerArr( |
| 83 | + `${__dirname}/test-datasets/kosaraju/output_${file}` |
| 84 | + ); |
| 85 | + const m = g.kosaraju(); |
| 86 | + expect(m).not.toBeFalsy(); |
| 87 | + // if m is false, this test already failed |
| 88 | + const arr = largest5(m) as number[]; |
| 89 | + arr.forEach((n, i) => { |
| 90 | + expect(n).toBe(ans[i]); |
| 91 | + }); |
| 92 | + }; |
| 93 | + |
| 94 | + test(TEST48); |
| 95 | + test(TEST52); |
| 96 | + test(TEST56); |
| 97 | + test(TEST60); |
| 98 | + test(TEST64); |
| 99 | + }); |
64 | 100 | });
|
0 commit comments