|
1 | 1 | import { expect } from "chai";
|
2 | 2 |
|
3 |
| -import { math } from "../../src/js/math"; |
| 3 | +import { math, RoundingMethodEnum } from "../../src/js/math"; |
4 | 4 |
|
5 | 5 | describe("Math Tests", () => {
|
6 |
| - it("should round according to specified method", () => { |
7 |
| - const [value1, value2, value3, value4] = [0.5, 1.5, -0.5, -1.5]; |
8 |
| - const [expectedBankers1, expectedBankers2, expectedBankers3, expectedBankers4] = [ |
9 |
| - 0, 2, 0, -2, |
10 |
| - ]; |
11 |
| - const [expectedAway1, expectedAway2, expectedAway3, expectedAway4] = [1, 2, -1, -2]; |
| 6 | + it("should roundCustom according to specified method", () => { |
| 7 | + const values = [0.5, 1.5, -0.5, -1.5]; |
| 8 | + const expectedBankers = [0, 2, 0, -2]; |
| 9 | + const expectedHalfAwayFromZero = [1, 2, -1, -2]; |
12 | 10 | const n = 0;
|
13 |
| - expect(math.roundCustom(value1, n, math.RoundingMethod.Bankers)).to.equal(expectedBankers1); |
14 |
| - expect(math.roundCustom(value2, n, math.RoundingMethod.Bankers)).to.equal(expectedBankers2); |
15 |
| - expect(math.roundCustom(value3, n, math.RoundingMethod.Bankers)).to.equal(expectedBankers3); |
16 |
| - expect(math.roundCustom(value4, n, math.RoundingMethod.Bankers)).to.equal(expectedBankers4); |
17 |
| - expect(math.roundCustom(value1, n, math.RoundingMethod.HalfAwayFromZero)).to.equal( |
18 |
| - expectedAway1, |
19 |
| - ); |
20 |
| - expect(math.roundCustom(value2, n, math.RoundingMethod.HalfAwayFromZero)).to.equal( |
21 |
| - expectedAway2, |
22 |
| - ); |
23 |
| - expect(math.roundCustom(value3, n, math.RoundingMethod.HalfAwayFromZero)).to.equal( |
24 |
| - expectedAway3, |
25 |
| - ); |
26 |
| - expect(math.roundCustom(value4, n, math.RoundingMethod.HalfAwayFromZero)).to.equal( |
27 |
| - expectedAway4, |
28 |
| - ); |
| 11 | + |
| 12 | + expectedBankers.forEach((expected, i) => { |
| 13 | + const result = math.roundCustom(values[i], n, RoundingMethodEnum.Bankers); |
| 14 | + expect(result).to.equal(expected); |
| 15 | + }); |
| 16 | + expectedHalfAwayFromZero.forEach((expected, i) => { |
| 17 | + const result = math.roundCustom(values[i], n, RoundingMethodEnum.HalfAwayFromZero); |
| 18 | + expect(result).to.equal(expected); |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + it("should calculate vector length", () => { |
| 23 | + const vector = [3, 4, 0]; |
| 24 | + const expectedLength = 5; |
| 25 | + expect(math.vlen(vector)).to.equal(expectedLength); |
| 26 | + }); |
| 27 | + |
| 28 | + it("should calculate angle between vectors", () => { |
| 29 | + const vectorA = [1, 0, 0]; |
| 30 | + const vectorB = [0, 1, 0]; |
| 31 | + const expectedAngle = 90; |
| 32 | + expect(math.angle(vectorA, vectorB, "deg")).to.equal(expectedAngle); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should calculate distance between vectors", () => { |
| 36 | + const vectorA = [1, 2, 3]; |
| 37 | + const vectorB = [4, 5, 6]; |
| 38 | + const expectedDistance = 5.196152422706632; |
| 39 | + expect(math.vDist(vectorA, vectorB)).to.equal(expectedDistance); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should check if vectors are equal within tolerance", () => { |
| 43 | + const vectorA = [1, 2, 3]; |
| 44 | + const vectorB = [1.0001, 2.0001, 3.0001]; |
| 45 | + const tolerance = 0.001; |
| 46 | + expect(math.vEqualWithTolerance(vectorA, vectorB, tolerance)).to.equal(true); |
| 47 | + }); |
| 48 | + |
| 49 | + it("should check if vectors are not equal within tolerance", () => { |
| 50 | + const vectorA = [1, 2, 3]; |
| 51 | + const vectorB = [1.1, 2.1, 3.1]; |
| 52 | + const tolerance = 0.001; |
| 53 | + expect(math.vEqualWithTolerance(vectorA, vectorB, tolerance)).to.equal(false); |
29 | 54 | });
|
30 | 55 | });
|
0 commit comments