malwoden
Version:
   
177 lines • 7.18 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var table_1 = require("./table");
describe("table", function () {
it("is initialized to undefined values", function () {
var t = new table_1.Table(10, 10);
for (var x = 0; x < 10; x++) {
for (var y = 0; y < 10; y++) {
expect(t.get({ x: x, y: y })).toEqual(undefined);
}
}
});
it("can fill the entire table", function () {
var t = new table_1.Table(10, 10);
for (var i = 0; i < 10; i++) {
t.fill(i);
for (var x = 0; x < t.width; x++) {
for (var y = 0; y < t.height; y++) {
expect(t.get({ x: x, y: y })).toEqual(i);
}
}
}
});
it("will return undefined if retrieving a value out of bounds", function () {
var t = new table_1.Table(10, 10);
t.fill(10);
expect(t.get({ x: -10, y: 10 })).toEqual(undefined);
expect(t.get({ x: 10, y: -10 })).toEqual(undefined);
expect(t.get({ x: -10, y: -10 })).toEqual(undefined);
});
it("can set and unset values", function () {
var t = new table_1.Table(10, 10);
expect(t.get({ x: 0, y: 0 })).toEqual(undefined);
t.set({ x: 0, y: 0 }, 1);
expect(t.get({ x: 0, y: 0 })).toEqual(1);
expect(t.get({ x: 0, y: 0 })).not.toEqual(undefined);
t.set({ x: 0, y: 0 }, undefined);
expect(t.get({ x: 0, y: 0 })).toEqual(undefined);
});
it("will throw an error if setting out of bounds", function () {
var t = new table_1.Table(10, 10);
var tests = [
[-1, 0, "-1:0 is not in bounds"],
[0, -1, "0:-1 is not in bounds"],
[-1, -1, "-1:-1 is not in bounds"],
[10, 10, "10:10 is not in bounds"],
[100, 100, "100:100 is not in bounds"],
[Infinity, Infinity, "Infinity:Infinity is not in bounds"],
];
var _loop_1 = function (x, y, result) {
expect(function () { return t.set({ x: x, y: y }, 100); }).toThrowError(result);
};
for (var _i = 0, tests_1 = tests; _i < tests_1.length; _i++) {
var _a = tests_1[_i], x = _a[0], y = _a[1], result = _a[2];
_loop_1(x, y, result);
}
});
it("can get neighbors - topology 4", function () {
var t = new table_1.Table(10, 10);
// Corner Case
var cornerNeighbors = t.getNeighbors({ x: 0, y: 0 }, undefined, "four");
expect(cornerNeighbors).toHaveLength(2);
expect(cornerNeighbors).toContainEqual({ x: 1, y: 0 });
expect(cornerNeighbors).toContainEqual({ x: 0, y: 1 });
// General case
var neighbors = t.getNeighbors({ x: 5, y: 5 }, undefined, "four");
expect(neighbors).toHaveLength(4);
expect(neighbors).toContainEqual({ x: 6, y: 5 });
expect(neighbors).toContainEqual({ x: 4, y: 5 });
expect(neighbors).toContainEqual({ x: 5, y: 4 });
expect(neighbors).toContainEqual({ x: 5, y: 6 });
});
it("can get neighbors - topology 8", function () {
var t = new table_1.Table(10, 10);
// Corner Case
var cornerNeighbors = t.getNeighbors({ x: 0, y: 0 });
expect(cornerNeighbors).toHaveLength(3);
expect(cornerNeighbors).toContainEqual({ x: 1, y: 0 });
expect(cornerNeighbors).toContainEqual({ x: 0, y: 1 });
expect(cornerNeighbors).toContainEqual({ x: 1, y: 1 });
// General case
var neighbors = t.getNeighbors({ x: 5, y: 5 });
expect(neighbors).toHaveLength(8);
// Cardinal
expect(neighbors).toContainEqual({ x: 6, y: 5 });
expect(neighbors).toContainEqual({ x: 4, y: 5 });
expect(neighbors).toContainEqual({ x: 5, y: 4 });
expect(neighbors).toContainEqual({ x: 5, y: 6 });
// Diagonals
expect(neighbors).toContainEqual({ x: 6, y: 6 });
expect(neighbors).toContainEqual({ x: 4, y: 6 });
expect(neighbors).toContainEqual({ x: 4, y: 4 });
expect(neighbors).toContainEqual({ x: 6, y: 4 });
});
it("can get neighbords - predicate", function () {
var t = new table_1.Table(10, 10);
var n = t.getNeighbors({ x: 0, y: 0 }, function (pos, _) { return pos.x == 1; }, "eight");
expect(n).toEqual([
{ x: 1, y: 0 },
{ x: 1, y: 1 },
]);
});
it("can floodfillSelect", function () {
var t = new table_1.Table(10, 10);
var selection = t.floodFillSelect({ x: 0, y: 0 });
expect(selection).toHaveLength(100);
for (var x = 0; x < 10; x++) {
for (var y = 0; y < 10; y++) {
expect(selection).toContainEqual({ x: x, y: y });
}
}
t = new table_1.Table(10, 10);
// Test no value
selection = t.floodFillSelect({ x: 5, y: 5 }, 1);
expect(selection).toHaveLength(0);
expect(selection).toEqual([]);
// Test a single value
t.set({ x: 5, y: 5 }, 1);
selection = t.floodFillSelect({ x: 5, y: 5 });
expect(selection).toHaveLength(1);
expect(selection).toContainEqual({ x: 5, y: 5 });
// Throw some more in
var vs = [
{ x: 5, y: 5 },
{ x: 6, y: 5 },
{ x: 4, y: 5 },
{ x: 7, y: 5 },
{ x: 3, y: 5 },
{ x: 3, y: 4 },
{ x: 3, y: 3 },
{ x: 3, y: 2 },
{ x: 3, y: 1 },
];
for (var _i = 0, vs_1 = vs; _i < vs_1.length; _i++) {
var v = vs_1[_i];
t.set(v, 1);
}
selection = t.floodFillSelect({ x: 5, y: 5 });
expect(selection).toHaveLength(vs.length);
for (var _a = 0, vs_2 = vs; _a < vs_2.length; _a++) {
var v = vs_2[_a];
expect(selection).toContainEqual(v);
}
});
it("can clear a value", function () {
var t = new table_1.Table(20, 20);
var pos = { x: 10, y: 5 };
t.set(pos, 100);
expect(t.get(pos)).toEqual(100);
t.clear(pos);
expect(t.get(pos)).toEqual(undefined);
});
it("can filter vectors", function () {
var t = new table_1.Table(20, 20);
t.set({ x: 1, y: 5 }, 1);
var f = t.filter(function (x, v) { return v === 1; });
expect(f).toEqual([{ x: 1, y: 5 }]);
});
it("can clone a table", function () {
var t1 = new table_1.Table(10, 10);
var v = { x: 1, y: 5 };
t1.set(v, 5);
var t2 = t1.clone();
expect(t1.get(v)).toEqual(5);
expect(t2.get(v)).toEqual(5);
t1.set(v, 10);
expect(t1.get(v)).toEqual(10);
expect(t2.get(v)).toEqual(5);
});
it("can tell if it's the same size as another table", function () {
var t = new table_1.Table(10, 20);
expect(t.isSameSize(new table_1.Table(10, 20))).toEqual(true);
expect(t.isSameSize(new table_1.Table(20, 20))).toEqual(false);
expect(t.isSameSize(new table_1.Table(20, 10))).toEqual(false);
});
});
//# sourceMappingURL=table.spec.js.map