From 3b78f224df9896c618b1bbeabe8927d19135ebbe Mon Sep 17 00:00:00 2001 From: Oliver Akins Date: Wed, 22 Jun 2022 00:34:13 -0600 Subject: [PATCH] Add tests for the movementDirection function --- .../src/algorithms/movementDirection.spec.ts | 136 +++++++++++++++++- 1 file changed, 135 insertions(+), 1 deletion(-) diff --git a/common/src/algorithms/movementDirection.spec.ts b/common/src/algorithms/movementDirection.spec.ts index a51dd2b..9d0843b 100644 --- a/common/src/algorithms/movementDirection.spec.ts +++ b/common/src/algorithms/movementDirection.spec.ts @@ -1,4 +1,4 @@ -import { countShips } from "./movementDirection" +import { countShips, determineDirection } from "./movementDirection" import { expect } from "chai"; import "mocha"; @@ -44,4 +44,138 @@ describe("The countShips function", () => { throw "Function didn't throw an error"; } catch (_) {} }); +}); + +describe("The movementDirection function", () => { + + it("should return positive when no other ships present (0-index)", () => { + let b = [ + "p1", null, null, null, null, + null, null, null, null, null, + null, null, null, null, null, + ]; + let p = b.findIndex(x => x == "p1"); + expect(determineDirection(b, p)).to.be.greaterThan(0); + }); + + it("should return positive when no other ships present (low-index)", () => { + let b = [ + null, null, "p1", null, null, + null, null, null, null, null, + null, null, null, null, null, + ]; + let p = b.findIndex(x => x == "p1"); + expect(determineDirection(b, p)).to.be.greaterThan(0); + }); + + it("should return positive when no other ships present (mid-index)", () => { + let b = [ + null, null, null, null, null, + null, null, "p1", null, null, + null, null, null, null, null, + ]; + let p = b.findIndex(x => x == "p1"); + expect(determineDirection(b, p)).to.be.greaterThan(0); + }); + + it("should return positive when no other ships present (high-index)", () => { + let b = [ + null, null, null, null, null, + null, null, null, null, null, + null, null, "p1", null, null, + ]; + let p = b.findIndex(x => x == "p1"); + expect(determineDirection(b, p)).to.be.greaterThan(0); + }); + + it("should return positive when no other ships present (max-index)", () => { + let b = [ + null, null, null, null, null, + null, null, null, null, null, + null, null, null, null, "p1", + ]; + let p = b.findIndex(x => x == "p1"); + expect(determineDirection(b, p)).to.be.greaterThan(0); + }); + + it("should return negative when the only other ship is at a lower index", () => { + let b = [ + null, "p2", null, null, null, + null, null, "p1", null, null, + null, null, null, null, null, + ]; + let p = b.findIndex(x => x == "p1"); + expect(determineDirection(b, p)).to.be.lessThan(0); + }); + + it("should return positive when the only other ship is at a higher index", () => { + let b = [ + null, null, null, null, null, + null, null, "p1", null, null, + null, null, null, "p2", null, + ]; + let p = b.findIndex(x => x == "p1"); + expect(determineDirection(b, p)).to.be.greaterThan(0); + }); + + + it("should return positive when the closest ship is at a higher index", () => { + let b = [ + null, "p3", null, null, null, + null, null, "p1", null, null, + null, null, "p2", null, null, + ]; + let p = b.findIndex(x => x == "p1"); + expect(determineDirection(b, p)).to.be.greaterThan(0); + }); + + it("should return negative when the closest ship is at a lower index", () => { + let b = [ + null, null, "p3", null, null, + null, null, "p1", null, null, + null, null, null, "p2", null, + ]; + let p = b.findIndex(x => x == "p1"); + expect(determineDirection(b, p)).to.be.lessThan(0); + }); + + it("should return 0 when there is a tie between the closest ships (no extra ships)", () => { + let b = [ + null, null, "p3", null, null, + null, null, "p1", null, null, + null, null, "p2", null, null, + ]; + let p = b.findIndex(x => x == "p1"); + expect(determineDirection(b, p)).to.equal(0); + }); + + it("should return negative when there is a tie between the closest ships (extra ships in negative direction)", () => { + let b = [ + null, "p4", "p3", null, null, + null, null, "p1", null, null, + null, null, "p2", null, null, + ]; + let p = b.findIndex(x => x == "p1"); + expect(determineDirection(b, p)).to.be.lessThan(0); + }); + + it("should return positive when there is a tie between the closest ships (extra ships in positive direction)", () => { + let b = [ + null, null, "p3", null, null, + null, null, "p1", null, null, + null, null, "p2", "p4", null, + ]; + let p = b.findIndex(x => x == "p1"); + expect(determineDirection(b, p)).to.be.greaterThan(0); + }); + + it("should return positive when there is a tie between the closest ships (extra ships tied)", () => { + let b = [ + null, "p5", "p3", null, null, + null, null, "p1", null, null, + null, null, "p2", "p4", null, + ]; + let p = b.findIndex(x => x == "p1"); + expect(determineDirection(b, p)).to.equal(0); + }); }); \ No newline at end of file