malwoden
Version:
   
162 lines • 5.82 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var vector_1 = require("./vector");
describe("areEqual", function () {
it("returns true if two vectors are equal", function () {
var tests = [
[
{ x: 0, y: 0 },
{ x: 0, y: 0 },
],
[
{ x: 1, y: 0 },
{ x: 1, y: 0 },
],
[
{ x: -5, y: -5 },
{ x: -5, y: -5 },
],
];
for (var _i = 0, tests_1 = tests; _i < tests_1.length; _i++) {
var _a = tests_1[_i], v1 = _a[0], v2 = _a[1];
expect(vector_1.Vector.areEqual(v1, v2)).toBeTruthy();
}
});
it("returns false if two vectors are unequal", function () {
var tests = [
[
{ x: 0, y: 0 },
{ x: 1, y: 0 },
],
[
{ x: 1, y: 0 },
{ x: -1, y: 0 },
],
[
{ x: -5, y: 0 },
{ x: -5, y: 1 },
],
];
for (var _i = 0, tests_2 = tests; _i < tests_2.length; _i++) {
var _a = tests_2[_i], v1 = _a[0], v2 = _a[1];
expect(vector_1.Vector.areEqual(v1, v2)).toBeFalsy();
}
});
});
describe("getDistance", function () {
it("Can get the distance between two vectors", function () {
var tests = [
[{ x: 0, y: 0 }, { x: 0, y: 0 }, 0],
[{ x: 1, y: 0 }, { x: 0, y: 0 }, 1],
[{ x: 1, y: 1 }, { x: 0, y: 0 }, Math.sqrt(2)],
[{ x: 100, y: 100 }, { x: -100, y: -100 }, Math.sqrt(80000)],
];
for (var _i = 0, tests_3 = tests; _i < tests_3.length; _i++) {
var _a = tests_3[_i], v1 = _a[0], v2 = _a[1], d = _a[2];
expect(vector_1.Vector.getDistance(v1, v2)).toEqual(d);
}
});
it("Can get the distance between two vectors - 4", function () {
var tests = [
[{ x: 0, y: 0 }, { x: 0, y: 0 }, 0],
[{ x: 1, y: 0 }, { x: 0, y: 0 }, 1],
[{ x: 1, y: 1 }, { x: 0, y: 0 }, 2],
[{ x: 100, y: 100 }, { x: -100, y: -100 }, 400],
];
for (var _i = 0, tests_4 = tests; _i < tests_4.length; _i++) {
var _a = tests_4[_i], v1 = _a[0], v2 = _a[1], d = _a[2];
expect(vector_1.Vector.getDistance(v1, v2, "four")).toEqual(d);
}
});
it("Can get the distance between two vectors - 8", function () {
var tests = [
[{ x: 0, y: 0 }, { x: 0, y: 0 }, 0],
[{ x: 1, y: 0 }, { x: 0, y: 0 }, 1],
[{ x: 1, y: 1 }, { x: 0, y: 0 }, 1],
[{ x: 100, y: 100 }, { x: -100, y: -100 }, 200],
];
for (var _i = 0, tests_5 = tests; _i < tests_5.length; _i++) {
var _a = tests_5[_i], v1 = _a[0], v2 = _a[1], d = _a[2];
expect(vector_1.Vector.getDistance(v1, v2, "eight")).toEqual(d);
}
});
});
describe("getCenter", function () {
it("Can get a center point", function () {
expect(vector_1.Vector.getCenter([
{ x: 0, y: 0 },
{ x: 10, y: 10 },
])).toEqual({ x: 5, y: 5 });
expect(vector_1.Vector.getCenter([
{ x: 0, y: 0 },
{ x: 10, y: 10 },
{ x: 100, y: 100 },
])).toEqual({
x: 110 / 3,
y: 110 / 3,
});
});
it("Will throw an error if the area is empty", function () {
expect(function () { return vector_1.Vector.getCenter([]); }).toThrow("Error: Trying to find center of empty area");
});
});
describe("getClosest", function () {
it("Can get the closest vector - 4", function () {
var result = vector_1.Vector.getClosest([
{ x: 0, y: 0 },
{ x: 10, y: 10 },
{ x: 100, y: 100 },
{ x: 1000, y: 1000 },
], { x: 20, y: 20 });
expect(result).toEqual({ x: 10, y: 10 });
});
it("Can get the closest vector - 8", function () {
var result = vector_1.Vector.getClosest([
{ x: 0, y: 0 },
{ x: 10, y: 10 },
{ x: 100, y: 100 },
{ x: 1000, y: 1000 },
], { x: 20, y: 20 }, "eight");
expect(result).toEqual({ x: 10, y: 10 });
});
it("Will immediately return if an exact match is found", function () {
expect(vector_1.Vector.getClosest([
{ x: 0, y: 0 },
{ x: 5, y: 5 },
{ x: 10, y: 10 },
], { x: 5, y: 5 })).toEqual({ x: 5, y: 5 });
});
it("Will throw an error if the area is empty", function () {
expect(function () { return vector_1.Vector.getClosest([], { x: 0, y: 0 }); }).toThrow("Error: Trying to find closest position of an empty area");
});
});
describe("add", function () {
it("Can add vectors", function () {
expect(vector_1.Vector.add({ x: 0, y: 0 }, { x: 1, y: 2 })).toEqual({ x: 1, y: 2 });
expect(vector_1.Vector.add({ x: -1, y: -2 }, { x: 1, y: 2 })).toEqual({
x: 0,
y: 0,
});
expect(vector_1.Vector.add({ x: -1, y: -2 }, { x: -1, y: -2 })).toEqual({
x: -2,
y: -4,
});
});
});
describe("subtract", function () {
it("Can subtract vectors", function () {
expect(vector_1.Vector.subtract({ x: 0, y: 0 }, { x: 1, y: 2 })).toEqual({
x: -1,
y: -2,
});
expect(vector_1.Vector.subtract({ x: -1, y: -2 }, { x: 1, y: 2 })).toEqual({
x: -2,
y: -4,
});
expect(vector_1.Vector.subtract({ x: -1, y: -2 }, { x: -1, y: -2 })).toEqual({
x: 0,
y: 0,
});
});
});
//# sourceMappingURL=vector.spec.js.map