name:
src/test/js/formulas/math/LNTest.ts
-rw-r--r--
2418
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 { LN } from "../../../../main/js/formulas/math/LN";
8import { Grid } from "../../../../main/js/models/common/Grid";
9import { CollateralLookupFunction, LookupFunction } from "../../../../main/js/models/common/Types";
10import { CommonModels } from "../../testutils/CommonModels";
11
12describe("LN", function () {
13 it("should work with numbers", function () {
14 assert.deepEqual(LN.SELF.run(null, 128), Math.log(128));
15 assert.deepEqual(LN.SELF.run(null, 1), Math.log(1));
16 assert.deepEqual(LN.SELF.run(null, 2), Math.log(2));
17 assert.deepEqual(LN.SELF.run(null, 3), Math.log(3));
18 assert.deepEqual(LN.SELF.run(null, 0.1), Math.log(0.1));
19 });
20
21 it("should require parameter 1 to be greater than 0", function () {
22 assert.deepEqual((LN.SELF.run(null, 0) as NAException).name, F7ExceptionName.NUM);
23 });
24
25 it("should work with strings", function () {
26 assert.deepEqual(LN.SELF.run(null, "128"), Math.log(128));
27 });
28
29 it("should do pass-through errors", function () {
30 assert.deepEqual(LN.SELF.run(null, new ValueException()), new ValueException());
31 });
32
33 it("should use lookup", function () {
34 const lookup = stub();
35 const collateralLookup = stub();
36 const F = new LN(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
37 collateralLookup.withArgs(CommonModels.A1, CommonModels.M22_RANGE).returns(128);
38 assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE), Math.log(128));
39 assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.M22_RANGE));
40 assert.equal(collateralLookup.callCount, 1);
41 assert.isTrue(lookup.notCalled);
42 });
43
44 it("should handle grids", function () {
45 const one = Grid.builder().add(0, 0, 128).add(0, 1, "A").build();
46 assert.deepEqual(LN.SELF.run(null, one), Math.log(128));
47 });
48
49 it("should return error when argument lengths are wrong", function () {
50 assert.deepEqual((LN.SELF.run(null) as NAException).name, F7ExceptionName.NA);
51 assert.deepEqual((LN.SELF.run(null, "A", "B") as NAException).name, F7ExceptionName.NA);
52 });
53});