Add a tests for countShips
This commit is contained in:
parent
18f58a6221
commit
762e2bdb1b
1 changed files with 47 additions and 0 deletions
47
common/src/algorithms/movementDirection.spec.ts
Normal file
47
common/src/algorithms/movementDirection.spec.ts
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
import { countShips } from "./movementDirection"
|
||||||
|
import { expect } from "chai";
|
||||||
|
import "mocha";
|
||||||
|
|
||||||
|
describe("The countShips function", () => {
|
||||||
|
it("should count the ships correctly when unbalanced", () => {
|
||||||
|
let r = countShips(["p3", null, "p1", "p2", null, "p4"], 2);
|
||||||
|
expect(r).to.have.keys(`left`, `right`);
|
||||||
|
expect(r.left).to.equal(1);
|
||||||
|
expect(r.right).to.equal(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should count the ships correctly when balanced", () => {
|
||||||
|
let r = countShips(["p3", null, "p1", "p2", null], 2);
|
||||||
|
expect(r).to.have.keys(`left`, `right`);
|
||||||
|
expect(r.left).to.equal(1);
|
||||||
|
expect(r.right).to.equal(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should count the ships correctly when shipLocation is at the start of the array", () => {
|
||||||
|
let r = countShips(["p3", null, "p1", "p2", null], 0);
|
||||||
|
expect(r).to.have.keys(`left`, `right`);
|
||||||
|
expect(r.left).to.equal(0);
|
||||||
|
expect(r.right).to.equal(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should count the ships correctly when shipLocation is at the end of the array", () => {
|
||||||
|
let r = countShips(["p3", null, "p1", "p2", null, "p4"], 5);
|
||||||
|
expect(r).to.have.keys(`left`, `right`);
|
||||||
|
expect(r.left).to.equal(3);
|
||||||
|
expect(r.right).to.equal(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw an error when shipLocation is larger than length", () => {
|
||||||
|
try {
|
||||||
|
countShips(["p3", null, "p1", "p2", null, "p4"], 8);
|
||||||
|
throw "Function didn't throw an error";
|
||||||
|
} catch (_) {}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw an error when shipLocation is less than 0", () => {
|
||||||
|
try {
|
||||||
|
countShips(["p3", null, "p1", "p2", null, "p4"], -3);
|
||||||
|
throw "Function didn't throw an error";
|
||||||
|
} catch (_) {}
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue