|
| 1 | +import { kosajaru } from "../kosajaru"; |
| 2 | + |
| 3 | +describe("kosajaru", () => { |
| 4 | + |
| 5 | + it("it should return no sccs for empty graph", () => { |
| 6 | + expect(kosajaru([])).toStrictEqual([]); |
| 7 | + }); |
| 8 | + |
| 9 | + it("it should return one scc for graph with one element", () => { |
| 10 | + expect(kosajaru([[]])).toStrictEqual([[0]]); |
| 11 | + }); |
| 12 | + |
| 13 | + it("it should return one scc for graph with element that points to itself", () => { |
| 14 | + expect(kosajaru([[0]])).toStrictEqual([[0]]); |
| 15 | + }); |
| 16 | + |
| 17 | + it("it should return one scc for two element graph with cycle", () => { |
| 18 | + expect(kosajaru([[1], [0]])).toStrictEqual([[0, 1]]); |
| 19 | + }); |
| 20 | + |
| 21 | + it("should return one scc for each element for straight line", () => { |
| 22 | + expect(kosajaru([[1], [2], [3], []])).toStrictEqual([[0], [1], [2], [3]]); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should return sccs for straight line with backedge in middle", () => { |
| 26 | + expect(kosajaru([[1], [2], [3, 0], []])).toStrictEqual([[0, 2, 1], [3]]); |
| 27 | + }); |
| 28 | + |
| 29 | + it("should return sccs for straight line with backedge from end to middle", () => { |
| 30 | + expect(kosajaru([[1], [2], [3], [1]])).toStrictEqual([[0], [1, 3, 2]]); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should return scc for each element for graph with no edges", () => { |
| 34 | + expect(kosajaru([[], [], [], []])).toStrictEqual([[3], [2], [1], [0]]); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should return sccs disconnected graph", () => { |
| 38 | + expect(kosajaru([[1, 2], [0, 2], [0, 1], []])).toStrictEqual([[3], [0, 1, 2]]); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should return sccs disconnected graph", () => { |
| 42 | + expect(kosajaru([[1, 2], [0, 2], [0, 1], [4], [5], [3]])).toStrictEqual([[3, 5, 4], [0, 1, 2]]); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should return single scc", () => { |
| 46 | + expect(kosajaru([[1], [2], [3], [0, 4], [3]])).toStrictEqual([[0, 3, 2, 1, 4]]); |
| 47 | + }); |
| 48 | + |
| 49 | + it("should return one scc for complete connected graph", () => { |
| 50 | + const input = [[1, 2, 3, 4], [0, 2, 3, 4], [0, 1, 3, 4], [0, 1, 2, 4], [0, 1, 2, 3]]; |
| 51 | + expect(kosajaru(input)).toStrictEqual([[0, 1, 2, 3, 4]]); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should return sccs", () => { |
| 55 | + const input = [[1], [2], [0, 3], [4], []]; |
| 56 | + expect(kosajaru(input)).toStrictEqual([[0, 2, 1], [3], [4]]); |
| 57 | + }); |
| 58 | + |
| 59 | + it("should return sccs", () => { |
| 60 | + const input = [[1], [2], [0, 3, 4], [0], [5], [6, 7], [2, 4], [8], [5, 9], [5]]; |
| 61 | + const expected = [[0, 2, 1, 6, 5, 4, 8, 7, 9, 3]]; |
| 62 | + expect(kosajaru(input)).toStrictEqual(expected); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should return sccs", () => { |
| 66 | + const input = [[1], [0, 2], [0, 3], [4], [5, 7], [6], [4, 7], []]; |
| 67 | + const expected = [[0, 1, 2], [3], [4, 6, 5], [7]]; |
| 68 | + expect(kosajaru(input)).toStrictEqual(expected); |
| 69 | + }); |
| 70 | + |
| 71 | +}) |
| 72 | + |
0 commit comments