|
| 1 | +import { assertEquals, assertThrows } from "jsr:@std/assert@1"; |
| 2 | +import { beforeEach, describe, it } from "@std/testing/bdd"; |
| 3 | +import { combine, getDistance, parseCityLine } from "./utils.ts"; |
| 4 | + |
| 5 | +describe("combine", () => { |
| 6 | + describe('for set {"A"}', () => { |
| 7 | + const uncombinedValues = new Set(["A"]); |
| 8 | + it('Should return [["A"]] for empty currentArrays', function () { |
| 9 | + assertEquals( |
| 10 | + combine([[]], uncombinedValues), |
| 11 | + [["A"]], |
| 12 | + ); |
| 13 | + }); |
| 14 | + it('Should return [["X", "A"]] for currentArrays [["X"]]', function () { |
| 15 | + assertEquals( |
| 16 | + combine([["X"]], uncombinedValues), |
| 17 | + [["X", "A"]], |
| 18 | + ); |
| 19 | + }); |
| 20 | + it('Should add ["A"] to every array in currentArrays', function () { |
| 21 | + assertEquals( |
| 22 | + combine([["X", "Y"], ["Y", "X"]], uncombinedValues), |
| 23 | + [["X", "Y", "A"], ["Y", "X", "A"]], |
| 24 | + ); |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('for set {"A", "B"}', function () { |
| 29 | + const uncombinedValues = new Set(["A", "B"]); |
| 30 | + it('Should return result with arrays ["A", "B"], ["B", "A"] for empty currentArrays', function () { |
| 31 | + assertEquals(combine([[]], uncombinedValues), [ |
| 32 | + ["A", "B"], |
| 33 | + ["B", "A"], |
| 34 | + ]); |
| 35 | + }); |
| 36 | + it('Should return result with arrays ["X", "A", "B"], ["X", "B", "A"] for currentArrays [["X"]]', function () { |
| 37 | + assertEquals(combine([["X"]], uncombinedValues), [ |
| 38 | + ["X", "A", "B"], |
| 39 | + ["X", "B", "A"], |
| 40 | + ]); |
| 41 | + }); |
| 42 | + |
| 43 | + it('Should add arrays ["A", "B"], ["B", "A"] to every array in currentArrays', function () { |
| 44 | + assertEquals(combine([["X", "Y"], ["Y", "X"]], uncombinedValues), [ |
| 45 | + ["X", "Y", "A", "B"], |
| 46 | + ["X", "Y", "B", "A"], |
| 47 | + ["Y", "X", "A", "B"], |
| 48 | + ["Y", "X", "B", "A"], |
| 49 | + ]); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('for set {"A", "B", "C"}', function () { |
| 54 | + const uncombinedValues = new Set(["A", "B", "C"]); |
| 55 | + it("Should return result with 6 arrays for empty currentArrays", function () { |
| 56 | + assertEquals(combine([[]], uncombinedValues), [ |
| 57 | + ["A", "B", "C"], |
| 58 | + ["A", "C", "B"], |
| 59 | + ["B", "A", "C"], |
| 60 | + ["B", "C", "A"], |
| 61 | + ["C", "A", "B"], |
| 62 | + ["C", "B", "A"], |
| 63 | + ]); |
| 64 | + }); |
| 65 | + it('Should return result with 6 arrays for currentArrays ["X"]', function () { |
| 66 | + assertEquals(combine([["X"]], uncombinedValues), [ |
| 67 | + ["X", "A", "B", "C"], |
| 68 | + ["X", "A", "C", "B"], |
| 69 | + ["X", "B", "A", "C"], |
| 70 | + ["X", "B", "C", "A"], |
| 71 | + ["X", "C", "A", "B"], |
| 72 | + ["X", "C", "B", "A"], |
| 73 | + ]); |
| 74 | + }); |
| 75 | + }); |
| 76 | +}); |
| 77 | + |
| 78 | +describe("getDistance", function () { |
| 79 | + const resolvedCombinations = new Map<string, number>(); |
| 80 | + beforeEach(() => { |
| 81 | + resolvedCombinations.clear(); |
| 82 | + resolvedCombinations.set("AB", 1); |
| 83 | + resolvedCombinations.set("BC", 2); |
| 84 | + resolvedCombinations.set("CD", 3); |
| 85 | + resolvedCombinations.set("DE", 4); |
| 86 | + }); |
| 87 | + it("Should throw error for unresolvable combination", function () { |
| 88 | + assertThrows(() => { |
| 89 | + getDistance("XY", resolvedCombinations); |
| 90 | + }); |
| 91 | + }); |
| 92 | + it("Should resolve AB combination", function () { |
| 93 | + assertEquals(getDistance("AB", resolvedCombinations), 1); |
| 94 | + }); |
| 95 | + it("Should resolve BA combination", function () { |
| 96 | + assertEquals(getDistance("BA", resolvedCombinations), 1); |
| 97 | + assertEquals(resolvedCombinations.get("BA"), 1); |
| 98 | + }); |
| 99 | + it("Should resolve ABC combination", function () { |
| 100 | + assertEquals(getDistance("ABC", resolvedCombinations), 3); |
| 101 | + assertEquals(resolvedCombinations.get("ABC"), 3); |
| 102 | + }); |
| 103 | + it("Should resolve CBA combination", function () { |
| 104 | + assertEquals(getDistance("CBA", resolvedCombinations), 3); |
| 105 | + assertEquals(resolvedCombinations.get("CB"), 2); |
| 106 | + assertEquals(resolvedCombinations.get("BA"), 1); |
| 107 | + assertEquals(resolvedCombinations.get("CBA"), 3); |
| 108 | + }); |
| 109 | + it("Should resolve ABCDE combination", function () { |
| 110 | + assertEquals(getDistance("ABCDE", resolvedCombinations), 10); |
| 111 | + assertEquals(resolvedCombinations.get("CDE"), 7); |
| 112 | + assertEquals(resolvedCombinations.get("BCDE"), 9); |
| 113 | + assertEquals(resolvedCombinations.get("ABCDE"), 10); |
| 114 | + }); |
| 115 | +}); |
| 116 | + |
| 117 | +describe("parseCityLine", function () { |
| 118 | + it("Should throw error for invalid city line", function () { |
| 119 | + assertThrows(() => { |
| 120 | + parseCityLine("ascsdf"); |
| 121 | + }); |
| 122 | + }); |
| 123 | + it("Should parse valid city line", function () { |
| 124 | + assertEquals(parseCityLine("London to Dublin = 464"), { |
| 125 | + cityALiteral: "A", |
| 126 | + cityBLiteral: "B", |
| 127 | + distance: 464, |
| 128 | + }); |
| 129 | + }); |
| 130 | +}); |
0 commit comments