UNPKG

rulyotano.math.geometry

Version:
77 lines 3.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const rulyotano_math_1 = require("rulyotano.math"); const Helpers_1 = require("../Helpers"); const Point_1 = require("../Point"); describe('src > helpers', () => { beforeEach(() => { }); afterEach(() => { }); describe('radianToDegree()', () => { [3, 1, 0.6, 0.2].forEach(testCase => { test(`Should convert according to formula (180*radian)/PI. Current ${testCase}`, () => { const expected = (testCase * 180) / Math.PI; expect(rulyotano_math_1.Numeric.numericEqual(expected, Helpers_1.default.radianToDegree(testCase))).toBeTruthy(); }); }); }); describe('degreeToRadian()', () => { [180, 90, 90, 30, 0.5, 330].forEach(testCase => { test(`Should convert according to formula (degree*PI)/180. Current ${testCase}`, () => { const expected = (testCase * Math.PI) / 180; expect(rulyotano_math_1.Numeric.numericEqual(expected, Helpers_1.default.degreeToRadian(testCase))).toBeTruthy(); }); }); }); describe('euclideanDistance()', () => { [ [0, 0, 3, 3], [1, 7, 1, 7], [1, 0.9, 20, 7], [0, 0, 10, 0], [-10, 5, 5, 8], [3, 0.54544, 1, 9], ].forEach(testCase => { test(`Should return according to euclidean distance formula Sqrt(dx^2 + dy^2). p1=(${testCase[0]},${testCase[1]}) p2=(${testCase[2]},${testCase[3]})`, () => { const p1 = new Point_1.default(testCase[0], testCase[1]); const p2 = new Point_1.default(testCase[2], testCase[3]); const expected = Math.sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y)); expect(rulyotano_math_1.Numeric.numericEqual(expected, Helpers_1.default.euclideanDistance(p1.x, p1.y, p2.x, p2.y))).toBeTruthy(); expect(rulyotano_math_1.Numeric.numericEqual(expected, Helpers_1.default.euclideanDistanceBetweenPoints(p1, p2))).toBeTruthy(); }); }); }); describe('bestPlaceToInsertPoint', () => { test('When points are empty should insert at the beginning', () => { const newPoint = new Point_1.default(3, 4); const list = []; expect(Helpers_1.default.bestPlaceToInsertPoint(newPoint, list)).toBe(0); }); test('When only one point should insert at the end', () => { const newPoint = new Point_1.default(3, 4); const list = [new Point_1.default(10, 5)]; expect(Helpers_1.default.bestPlaceToInsertPoint(newPoint, list)).toBe(1); }); test('When 2 points and is near to the first one should insert at the beginning', () => { const newPoint = new Point_1.default(3, 0); const list = [new Point_1.default(10, 0), new Point_1.default(50, 0)]; expect(Helpers_1.default.bestPlaceToInsertPoint(newPoint, list)).toBe(0); }); test('When 2 points and is near to the last one should insert at the end', () => { const newPoint = new Point_1.default(70, 0); const list = [new Point_1.default(40, 0), new Point_1.default(50, 0)]; expect(Helpers_1.default.bestPlaceToInsertPoint(newPoint, list)).toBe(list.length); }); test('When 4 points should insert in the index with lower distance', () => { const expectedIndex = 3; const newPoint = new Point_1.default(45, 0); const list = [ new Point_1.default(38, 0), new Point_1.default(39, 0), new Point_1.default(40, 0), new Point_1.default(50, 0), ]; expect(Helpers_1.default.bestPlaceToInsertPoint(newPoint, list)).toBe(expectedIndex); }); }); }); //# sourceMappingURL=helpers.test.js.map