name:
src/test/js/formulas/math/ATAN2Test.ts
-rw-r--r--
3027
1import { assert } from "chai";
2import { it, describe } from "../../testutils/TestUtils";
3import { stub } from "sinon";
4import { F7ExceptionName } from "../../../../main/js/errors/F7ExceptionName";
5import { NAException } from "../../../../main/js/errors/NAException";
6import { ValueException } from "../../../../main/js/errors/ValueException";
7import { ATAN2 } from "../../../../main/js/formulas/math/ATAN2";
8import { Grid } from "../../../../main/js/models/common/Grid";
9import { CollateralLookupFunction, LookupFunction } from "../../../../main/js/models/common/Types";
10import { CommonModels } from "../../testutils/CommonModels";
11
12describe("ATAN2", function () {
13 it("should work with valid numbers", function () {
14 assert.deepEqual(ATAN2.SELF.run(null, 10.0, 4.0), Math.atan2(10.0, 4.0));
15 assert.deepEqual(ATAN2.SELF.run(null, 128731.2, 4.0), Math.atan2(128731.2, 4.0));
16 assert.deepEqual(ATAN2.SELF.run(null, 11.11, 4.0), Math.atan2(11.11, 4.0));
17 assert.deepEqual(ATAN2.SELF.run(null, 0.0, 4.0), Math.atan2(0.0, 4.0));
18 assert.deepEqual(ATAN2.SELF.run(null, 88281.0, 4.0), Math.atan2(88281, 4.0));
19 assert.deepEqual(ATAN2.SELF.run(null, 2.0, 4.0), Math.atan2(2.0, 4.0));
20 assert.deepEqual(ATAN2.SELF.run(null, 4.0, 4.0), Math.atan2(4.0, 4.0));
21 assert.deepEqual(ATAN2.SELF.run(null, -4.0, 4.0), Math.atan2(-4.0, 4.0));
22 assert.deepEqual(ATAN2.SELF.run(null, -10124.0, 4.0), Math.atan2(-10124.0, 4.0));
23 });
24
25 it("should work with strings", function () {
26 assert.equal(ATAN2.SELF.run(null, "10", "4"), Math.atan2(10.0, 4.0));
27 });
28
29 it("should do pass-through errors", function () {
30 assert.deepEqual(ATAN2.SELF.run(null, new ValueException(), 10), new ValueException());
31 });
32
33 it("should use lookup", function () {
34 const lookup = stub();
35 const collateralLookup = stub();
36 const F = new ATAN2(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
37 collateralLookup.withArgs(CommonModels.A1, CommonModels.M22_RANGE).returns(10);
38 collateralLookup.withArgs(CommonModels.A1, CommonModels.G19_RANGE).returns(4);
39 assert.deepEqual(
40 F.run(CommonModels.A1, CommonModels.M22_RANGE, CommonModels.G19_RANGE),
41 Math.atan2(10, 4)
42 );
43 assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.M22_RANGE));
44 assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.G19_RANGE));
45 assert.equal(collateralLookup.callCount, 2);
46 assert.isTrue(lookup.notCalled);
47 });
48
49 it("should handle grids", function () {
50 const one = Grid.builder().add(0, 0, 10).add(0, 1, "A").build();
51 const two = Grid.builder().add(0, 0, 4).add(0, 1, "B").build();
52 assert.deepEqual(ATAN2.SELF.run(null, one, two), Math.atan2(10, 4));
53 });
54
55 it("should return error when argument lengths are wrong", function () {
56 assert.deepEqual((ATAN2.SELF.run(null, "A") as NAException).name, F7ExceptionName.NA);
57 assert.deepEqual((ATAN2.SELF.run(null, "A", "B", "C") as NAException).name, F7ExceptionName.NA);
58 });
59});