Skip to content

Commit

Permalink
add more tests and validation
Browse files Browse the repository at this point in the history
  • Loading branch information
skedwards88 committed Mar 24, 2024
1 parent 5c49cb6 commit 64a1270
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 12 deletions.
17 changes: 17 additions & 0 deletions src/logic/centerIndexes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import {centerGrid} from "@skedwards88/word_logic";

export function centerIndexes(indexes, gridSize) {
if (!Array.isArray(indexes) || !indexes.every(Number.isInteger)) {
throw new TypeError("The `indexes` input must be an array of integers");
}

if (!Number.isInteger(gridSize) || gridSize <= 0) {
throw new TypeError("The `gridSize` input must be an integer > 0");
}

if (indexes.some((index) => index > gridSize * gridSize - 1 || index < 0)) {
throw new Error("One of more of the indexes exceeds the grid size");
}

// return early if the index list is empty
if (indexes.length === 0) {
return indexes;
}

// Arrange the flat indexes into a 2D grid of 0 and 1 where 1 represents an input index
let grid = Array.from({length: gridSize}, () => Array(gridSize).fill(0));
for (const index of indexes) {
Expand Down
74 changes: 62 additions & 12 deletions src/logic/centerIndexes.test.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,102 @@
import {centerIndexes} from "./centerIndexes";

test("Given indexes in a grid, returns the indexes of the centered input indexes", () => {
const indexes = [11,15,17,20,21]
const indexes = [11, 15, 17, 20, 21];

const centeredIndexes = centerIndexes(indexes, 5);

const expectedIndexes = [7,11,13,16,17]
const expectedIndexes = [7, 11, 13, 16, 17];

expect(centeredIndexes).toEqual(expectedIndexes);
});


test("If the indexes can't be perfectly centered, errs towards the top left", () => {
const expectedIndexes = [8,13,14,15,20]
const expectedIndexes = [8, 13, 14, 15, 20];

const indexesBottomLeft = [19,24,25,26,31]
const indexesBottomLeft = [19, 24, 25, 26, 31];

expect(centerIndexes(indexesBottomLeft, 6)).toEqual(expectedIndexes);

const indexesBottomRight = [22,27,28,29,34]
const indexesBottomRight = [22, 27, 28, 29, 34];

expect(centerIndexes(indexesBottomRight, 6)).toEqual(expectedIndexes);

const indexesTopLeft = [1,6,7,8,13]
const indexesTopLeft = [1, 6, 7, 8, 13];

expect(centerIndexes(indexesTopLeft, 6)).toEqual(expectedIndexes);

const indexesTopRight = [4,9,10,11,16]
const indexesTopRight = [4, 9, 10, 11, 16];

expect(centerIndexes(indexesTopRight, 6)).toEqual(expectedIndexes);
});

test("If the indexes are already centered up/down, will still center right/left", () => {
const indexes = [6,10,12,15,16]
const indexes = [6, 10, 12, 15, 16];

const centeredIndexes = centerIndexes(indexes, 5);

const expectedIndexes = [7,11,13,16,17]
const expectedIndexes = [7, 11, 13, 16, 17];

expect(centeredIndexes).toEqual(expectedIndexes);
});

test("If the indexes are already centered right/left, will still center up/down", () => {
const indexes = [12,16,18,21,22]
const indexes = [12, 16, 18, 21, 22];

const centeredIndexes = centerIndexes(indexes, 5);

const expectedIndexes = [7,11,13,16,17]
const expectedIndexes = [7, 11, 13, 16, 17];

expect(centeredIndexes).toEqual(expectedIndexes);
});

test("Works on empty indexes", () => {
const indexes = [];

const centeredIndexes = centerIndexes(indexes, 5);

const expectedIndexes = [];

expect(centeredIndexes).toEqual(expectedIndexes);
});

test("Errors if the indexes input is not a list of ints", () => {
expect(() => centerIndexes(["A", "B", "C"], 5)).toThrow(
"The `indexes` input must be an array of integers",
);

expect(() =>
centerIndexes(
[
[1, 0, 1],
[0, 1, 1],
[0, 0, 0],
],
5,
),
).toThrow("The `indexes` input must be an array of integers");
});

test("Errors if the gridSize input is not an int > 0", () => {
expect(() => centerIndexes([12, 16, 18, 21, 22], "6")).toThrow(
"The `gridSize` input must be an integer > 0",
);

expect(() => centerIndexes([12, 16, 18, 21, 22], 0)).toThrow(
"The `gridSize` input must be an integer > 0",
);
});

test("Errors if any of the indexes exceed the grid size", () => {
expect(() => centerIndexes([12, 16, 18, 21, 22], 4)).toThrow(
"One of more of the indexes exceeds the grid size",
);

expect(() => centerIndexes([-2, 12, 16, 18, 21, 22, 24], 6)).toThrow(
"One of more of the indexes exceeds the grid size",
);

expect(() => centerIndexes([2, 16, 18, 21, 22, 36], 6)).toThrow(
"One of more of the indexes exceeds the grid size",
);
});
173 changes: 173 additions & 0 deletions src/logic/shapesMatchQ.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import {shapesMatchQ} from "./shapesMatchQ";

test("Returns true if two sets of indexes form the same shape in a grid", () => {
expect(
shapesMatchQ({
indexes1: [11, 15, 17, 20, 21],
indexes2: [17, 11, 13, 16, 7],
gridSize: 5,
}),
).toBe(true);

expect(
shapesMatchQ({
indexes1: [7, 11, 13, 16, 17],
indexes2: [6, 10, 12, 15, 16],
gridSize: 5,
}),
).toBe(true);

expect(
shapesMatchQ({
indexes1: [12, 16, 18, 21, 22],
indexes2: [6, 15, 12, 10, 16],
gridSize: 5,
}),
).toBe(true);

expect(
shapesMatchQ({
indexes1: [8, 13, 14, 15, 20],
indexes2: [19, 24, 25, 26, 31],
gridSize: 6,
}),
).toBe(true);

expect(
shapesMatchQ({
indexes1: [22, 27, 28, 29, 34],
indexes2: [19, 24, 25, 26, 31],
gridSize: 6,
}),
).toBe(true);

expect(
shapesMatchQ({
indexes1: [1, 6, 7, 8, 13],
indexes2: [19, 24, 31, 26, 25],
gridSize: 6,
}),
).toBe(true);

expect(
shapesMatchQ({
indexes1: [8, 13, 14, 15, 20],
indexes2: [4, 9, 10, 11, 16],
gridSize: 6,
}),
).toBe(true);
});

test("Returns false if two sets of indexes do not form the same shape in a grid", () => {
expect(
shapesMatchQ({
indexes1: [11, 15, 17, 20, 21],
indexes2: [7, 11, 13, 16, 18],
gridSize: 5,
}),
).toBe(false);

expect(
shapesMatchQ({
indexes1: [7, 1, 13, 16, 17],
indexes2: [6, 10, 12, 15, 16],
gridSize: 5,
}),
).toBe(false);

expect(
shapesMatchQ({
indexes1: [11, 12, 16, 18, 21, 22],
indexes2: [6, 10, 12, 15, 16],
gridSize: 5,
}),
).toBe(false);

expect(
shapesMatchQ({
indexes1: [18, 13, 14, 15, 20],
indexes2: [19, 24, 25, 26, 31],
gridSize: 6,
}),
).toBe(false);

expect(
shapesMatchQ({
indexes1: [2, 27, 28, 29, 34],
indexes2: [19, 24, 25, 26, 31],
gridSize: 6,
}),
).toBe(false);
});

test("Returns works with empty arrays", () => {
expect(
shapesMatchQ({
indexes1: [],
indexes2: [],
gridSize: 5,
}),
).toBe(true);

expect(
shapesMatchQ({
indexes1: [],
indexes2: [5, 6, 3],
gridSize: 5,
}),
).toBe(false);
});

test("Errors if the indexes input is not a list of ints", () => {
expect(() =>
shapesMatchQ({
indexes1: ["7", "11", "13", 16, 17],
indexes2: [6, 10, 12, 15, 16],
gridSize: 5,
}),
).toThrow("The `indexes` input must be an array of integers");

expect(() =>
shapesMatchQ({
indexes1: [[7, 11, 13, 16, 17]],
indexes2: [6, 10, 12, 15, 16],
gridSize: 5,
}),
).toThrow("The `indexes` input must be an array of integers");
});

test("Errors if the gridSize input is not an int > 0", () => {
expect(() =>
shapesMatchQ({
indexes1: [7, 11, 13, 16, 17],
indexes2: [6, 10, 12, 15, 16],
gridSize: "5",
}),
).toThrow("The `gridSize` input must be an integer > 0");

expect(() =>
shapesMatchQ({
indexes1: [7, 11, 13, 16, 17],
indexes2: [6, 10, 12, 15, 16],
gridSize: 0,
}),
).toThrow("The `gridSize` input must be an integer > 0");
});

test("Errors if any of the indexes exceed the grid size", () => {
expect(() =>
shapesMatchQ({
indexes1: [7, 11, 13, 16, 25],
indexes2: [6, 10, 12, 15, 16],
gridSize: 5,
}),
).toThrow("One of more of the indexes exceeds the grid size");

expect(() =>
shapesMatchQ({
indexes1: [-1, 11, 13, 16, 17],
indexes2: [6, 10, 12, 15, 16],
gridSize: 5,
}),
).toThrow("One of more of the indexes exceeds the grid size");
});

0 comments on commit 64a1270

Please sign in to comment.