|
| 1 | +import { floydWarshall } from "../floyd_warshall"; |
| 2 | + |
| 3 | +describe("floydWarshall", () => { |
| 4 | + it("should return the correct value for zero element graph", () => { |
| 5 | + expect(floydWarshall([])).toEqual([]); |
| 6 | + }); |
| 7 | + |
| 8 | + it("should return the correct value for one element graph", () => { |
| 9 | + expect(floydWarshall([[1]])).toStrictEqual([[1]]); |
| 10 | + }); |
| 11 | + |
| 12 | + it("should return the correct value for two element graph", () => { |
| 13 | + expect(floydWarshall([[10, 4], [3, 6]])).toStrictEqual([[7, 4], [3, 6]]); |
| 14 | + }); |
| 15 | + |
| 16 | + it("should return the correct value", () => { |
| 17 | + let graph = []; |
| 18 | + for (let i = 1; i <= 5; ++i) { |
| 19 | + let arr = []; |
| 20 | + for (let j = 1; j <= 5; ++j) { |
| 21 | + arr.push(i * j); |
| 22 | + } |
| 23 | + graph.push(arr); |
| 24 | + } |
| 25 | + |
| 26 | + let expected = [ |
| 27 | + [ 1, 2, 3, 4, 5 ], |
| 28 | + [ 2, 4, 5, 6, 7 ], |
| 29 | + [ 3, 5, 6, 7, 8 ], |
| 30 | + [ 4, 6, 7, 8, 9 ], |
| 31 | + [ 5, 7, 8, 9, 10 ] |
| 32 | + ]; |
| 33 | + expect(floydWarshall(graph)).toStrictEqual(expected); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should return the correct value", () => { |
| 37 | + let graph = [ |
| 38 | + [0, 4, Infinity, Infinity, Infinity, Infinity, Infinity, 8, Infinity], |
| 39 | + [4, 0, 8, Infinity, Infinity, Infinity, Infinity, 11, Infinity], |
| 40 | + [Infinity, 8, 0, 7, Infinity, 4, Infinity, Infinity, 2], |
| 41 | + [Infinity, Infinity, 7, 0, 9, 14, Infinity, Infinity, Infinity], |
| 42 | + [Infinity, Infinity, Infinity, 9, 0, 10, Infinity, Infinity, Infinity], |
| 43 | + [Infinity, Infinity, 4, 14, 10, 0, 2, Infinity, Infinity], |
| 44 | + [Infinity, Infinity, Infinity, Infinity, Infinity, 2, 0, 1, 6], |
| 45 | + [8, 11, Infinity, Infinity, Infinity, Infinity, 1, 0, 7], |
| 46 | + [Infinity, Infinity, 2, Infinity, Infinity, Infinity, 6, 7, 0] |
| 47 | + ]; |
| 48 | + |
| 49 | + let expected = [ |
| 50 | + [0, 4, 12, 19, 21, 11, 9, 8, 14], |
| 51 | + [4, 0, 8, 15, 22, 12, 12, 11, 10], |
| 52 | + [12, 8, 0, 7, 14, 4, 6, 7, 2], |
| 53 | + [19, 15, 7, 0, 9, 11, 13, 14, 9], |
| 54 | + [21, 22, 14, 9, 0, 10, 12, 13, 16], |
| 55 | + [11, 12, 4, 11, 10, 0, 2, 3, 6], |
| 56 | + [9, 12, 6, 13, 12, 2, 0, 1, 6], |
| 57 | + [8, 11, 7, 14, 13, 3, 1, 0, 7], |
| 58 | + [14, 10, 2, 9, 16, 6, 6, 7, 0] |
| 59 | + ] |
| 60 | + |
| 61 | + expect(floydWarshall(graph)).toStrictEqual(expected); |
| 62 | + }); |
| 63 | +}); |
0 commit comments