f7
f7 is a spreadsheet formula execution library
git clone https://git.vogt.world/f7.git
Log | Files | README.md | LICENSE.md
← All files
name: src/test/js/formulas/math/LOGTest.ts
-rw-r--r--
3173
 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 { LOG } from "../../../../main/js/formulas/math/LOG";
 8import { Grid } from "../../../../main/js/models/common/Grid";
 9import { CollateralLookupFunction, LookupFunction } from "../../../../main/js/models/common/Types";
10import { Converters } from "../../../../main/js/utils/Converters";
11import { CommonModels } from "../../testutils/CommonModels";
12
13describe("LOG", function () {
14  it("should work with numbers", function () {
15    assert.deepEqual(LOG.SELF.run(null, 128, 2), 7);
16    assert.deepEqual(LOG.SELF.run(null, 128, 3), 4.4165082750002025);
17    assert.deepEqual(LOG.SELF.run(null, 1, 2), 0);
18    assert.deepEqual(LOG.SELF.run(null, 2, 2), 1);
19    assert.deepEqual(LOG.SELF.run(null, 3, 3), 1);
20    assert.deepEqual(LOG.SELF.run(null, 2, 3), 0.6309297535714575);
21  });
22
23  it("should work with default base", function () {
24    assert.deepEqual(LOG.SELF.run(null, 1), 0);
25    assert.deepEqual(LOG.SELF.run(null, 10), 1);
26    assert.deepEqual(LOG.SELF.run(null, 100), 2);
27    assert.deepEqual(LOG.SELF.run(null, 1000), 3);
28    assert.deepEqual(LOG.SELF.run(null, 10000), 4);
29    assert.deepEqual(LOG.SELF.run(null, 100000), 5);
30  });
31
32  it("should return errors when parameters are out of bounds", function () {
33    assert.deepEqual(
34      Converters.castAsF7Exception(LOG.SELF.run(null, 128, 1)).name,
35      F7ExceptionName.DIV
36    );
37    assert.deepEqual(
38      Converters.castAsF7Exception(LOG.SELF.run(null, 128, 0)).name,
39      F7ExceptionName.NUM
40    );
41    assert.deepEqual(
42      Converters.castAsF7Exception(LOG.SELF.run(null, 0, 1)).name,
43      F7ExceptionName.NUM
44    );
45  });
46
47  it("should work with strings", function () {
48    assert.deepEqual(LOG.SELF.run(null, "100000"), 5);
49  });
50
51  it("should do pass-through errors", function () {
52    assert.deepEqual(LOG.SELF.run(null, new ValueException()), new ValueException());
53  });
54
55  it("should use lookup", function () {
56    const lookup = stub();
57    const collateralLookup = stub();
58    const F = new LOG(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
59    collateralLookup.withArgs(CommonModels.A1, CommonModels.M22_RANGE).returns(10000);
60    assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE), 4);
61    assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.M22_RANGE));
62    assert.equal(collateralLookup.callCount, 1);
63    assert.isTrue(lookup.notCalled);
64  });
65
66  it("should handle grids", function () {
67    const one = Grid.builder().add(0, 0, 10000).add(0, 1, "A").build();
68    assert.deepEqual(LOG.SELF.run(null, one), 4);
69  });
70
71  it("should return error when argument lengths are wrong", function () {
72    assert.deepEqual((LOG.SELF.run(null) as NAException).name, F7ExceptionName.NA);
73    assert.deepEqual((LOG.SELF.run(null, "A", "B", "C") as NAException).name, F7ExceptionName.NA);
74  });
75});