// (C) 2007-2019 GoodData Corporation import { cartesianProduct, rotatedCartesianProduct } from "../cartesianProduct"; describe("cartesianProduct", () => { it("generates cartesian product of arrays", () => { const arr = [[1, 2, 3], [4, 5]]; const expected = [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]]; expect(cartesianProduct(arr)).toEqual(expected); }); it("works with arrays of different types", () => { const arr = [[{ a: 1 }, { b: 2 }], ["a", "b", "c"]]; const expected = [ [{ a: 1 }, "a"], [{ a: 1 }, "b"], [{ a: 1 }, "c"], [{ b: 2 }, "a"], [{ b: 2 }, "b"], [{ b: 2 }, "c"], ]; expect(cartesianProduct(arr)).toEqual(expected); }); it("computes more complex cartesian product", () => { const arr = [[1, 2, 3], ["a", "b", "c", "d"], ["type1", "type2"]]; const expected = [ [1, "a", "type1"], [1, "a", "type2"], [1, "b", "type1"], [1, "b", "type2"], [1, "c", "type1"], [1, "c", "type2"], [1, "d", "type1"], [1, "d", "type2"], [2, "a", "type1"], [2, "a", "type2"], [2, "b", "type1"], [2, "b", "type2"], [2, "c", "type1"], [2, "c", "type2"], [2, "d", "type1"], [2, "d", "type2"], [3, "a", "type1"], [3, "a", "type2"], [3, "b", "type1"], [3, "b", "type2"], [3, "c", "type1"], [3, "c", "type2"], [3, "d", "type1"], [3, "d", "type2"], ]; expect(cartesianProduct(arr)).toEqual(expected); }); it("computes cartesian product and rotates", () => { const arr = [[1, 2, 3], ["a", "b", "c", "d"], ["type1", "type2"]]; const expected = [ [1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3], [ "a", "a", "b", "b", "c", "c", "d", "d", "a", "a", "b", "b", "c", "c", "d", "d", "a", "a", "b", "b", "c", "c", "d", "d", ], [ "type1", "type2", "type1", "type2", "type1", "type2", "type1", "type2", "type1", "type2", "type1", "type2", "type1", "type2", "type1", "type2", "type1", "type2", "type1", "type2", "type1", "type2", "type1", "type2", ], ]; expect(rotatedCartesianProduct(arr)).toEqual(expected); }); });