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/FLOORTest.ts
-rw-r--r--
5140
  1import { assert } from "chai";
  2import { it, describe } from "../../testutils/TestUtils";
  3import { stub } from "sinon";
  4import { F7ExceptionName } from "../../../../main/js/errors/F7ExceptionName";
  5import { ValueException } from "../../../../main/js/errors/ValueException";
  6import { FLOOR } from "../../../../main/js/formulas/math/FLOOR";
  7import { Grid } from "../../../../main/js/models/common/Grid";
  8import { CollateralLookupFunction, LookupFunction } from "../../../../main/js/models/common/Types";
  9import { CommonModels } from "../../testutils/CommonModels";
 10import { assertF7ExceptionByName } from "../../testutils/TestUtils";
 11
 12describe("FLOOR", function () {
 13  it("should work with whole numbers", function () {
 14    assert.deepEqual(FLOOR.SELF.run(null, 10), 10);
 15    assert.deepEqual(FLOOR.SELF.run(null, -10), -10);
 16    assert.deepEqual(FLOOR.SELF.run(null, 0), 0);
 17  });
 18
 19  it("should floor decimals to full integers", function () {
 20    assert.deepEqual(FLOOR.SELF.run(null, 3.218639128), 3);
 21    assert.deepEqual(FLOOR.SELF.run(null, 7.99), 7);
 22    assert.deepEqual(FLOOR.SELF.run(null, 7.0000000001), 7);
 23    assert.deepEqual(FLOOR.SELF.run(null, -4.444444), -5);
 24  });
 25
 26  it("should floor to place", function () {
 27    assert.deepEqual(FLOOR.SELF.run(null, 3.218639128, 4), 0);
 28    assert.deepEqual(FLOOR.SELF.run(null, 3.218639128, 5), 0);
 29    assert.deepEqual(FLOOR.SELF.run(null, 9.12, 1), 9);
 30    assert.deepEqual(FLOOR.SELF.run(null, 9.12, 2), 8);
 31    assert.deepEqual(FLOOR.SELF.run(null, 9.12, 3), 9);
 32    assert.deepEqual(FLOOR.SELF.run(null, 9.12, 8), 8);
 33  });
 34
 35  it("should return DIV error when second param is zero", function () {
 36    assertF7ExceptionByName(FLOOR.SELF.run(null, 3341.218639128, 0), F7ExceptionName.DIV);
 37    assertF7ExceptionByName(FLOOR.SELF.run(null, 9.12, 0), F7ExceptionName.DIV);
 38  });
 39
 40  it("should return NUM error when params are not of the same sign (both pos. or both neg.)", function () {
 41    assertF7ExceptionByName(FLOOR.SELF.run(null, 3341.218639128, -1), F7ExceptionName.NUM);
 42    assertF7ExceptionByName(FLOOR.SELF.run(null, 3341.218639128, -2), F7ExceptionName.NUM);
 43    assertF7ExceptionByName(FLOOR.SELF.run(null, 3341.218639128, -3), F7ExceptionName.NUM);
 44    assertF7ExceptionByName(FLOOR.SELF.run(null, 3341.218639128, -4), F7ExceptionName.NUM);
 45    assertF7ExceptionByName(FLOOR.SELF.run(null, 3341.218639128, -4.99), F7ExceptionName.NUM);
 46    assertF7ExceptionByName(FLOOR.SELF.run(null, 3341.218639128, -3.999999), F7ExceptionName.NUM);
 47    assertF7ExceptionByName(FLOOR.SELF.run(null, 3341.218639128, -2.1), F7ExceptionName.NUM);
 48    assertF7ExceptionByName(
 49      FLOOR.SELF.run(null, 3341.218639128, -1.1982371982),
 50      F7ExceptionName.NUM
 51    );
 52  });
 53
 54  it("should floor to the negative place", function () {
 55    assert.deepEqual(FLOOR.SELF.run(null, -3341.839749239, -1), -3341);
 56    assert.deepEqual(FLOOR.SELF.run(null, -3341.839749239, -2), -3340);
 57    assert.deepEqual(FLOOR.SELF.run(null, -3341.839749239, -3), -3339);
 58    assert.deepEqual(FLOOR.SELF.run(null, -3341.839749239, -4), -3340);
 59    assert.deepEqual(FLOOR.SELF.run(null, -3341.839749239, -5), -3340);
 60    assert.deepEqual(FLOOR.SELF.run(null, -3341.839749239, -6), -3336);
 61    assert.deepEqual(FLOOR.SELF.run(null, -3341.839749239, -7), -3339);
 62    assert.deepEqual(FLOOR.SELF.run(null, -3341.839749239, -8), -3336);
 63  });
 64
 65  it("should round to non-integer places", function () {
 66    assert.deepEqual(FLOOR.SELF.run(null, 3341.218639128, 4.99), 3338.31);
 67    assert.deepEqual(FLOOR.SELF.run(null, 3341.218639128, 3.999999), 3339.9991649999997);
 68    assert.deepEqual(FLOOR.SELF.run(null, 3341.218639128, 2.1), 3341.1000000000004);
 69    assert.deepEqual(FLOOR.SELF.run(null, 3341.218639128, 1.1982371982), 3340.6853085816);
 70    assert.deepEqual(FLOOR.SELF.run(null, 3341.218639128, 0.999), 3340.656);
 71    assert.deepEqual(FLOOR.SELF.run(null, 3341.218639128, 1.111), 3340.777);
 72  });
 73
 74  it("should work with strings", function () {
 75    assert.deepEqual(FLOOR.SELF.run(null, "9.1"), 9);
 76  });
 77
 78  it("should do pass-through errors", function () {
 79    assertF7ExceptionByName(FLOOR.SELF.run(null, new ValueException()), F7ExceptionName.VALUE);
 80  });
 81
 82  it("should use lookup", function () {
 83    const lookup = stub();
 84    const collateralLookup = stub();
 85    const F = new FLOOR(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
 86    collateralLookup.withArgs(CommonModels.A1, CommonModels.M22_RANGE).returns(1.1);
 87    assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE), 1);
 88    assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.M22_RANGE));
 89    assert.equal(collateralLookup.callCount, 1);
 90    assert.isTrue(lookup.notCalled);
 91  });
 92
 93  it("should handle grids", function () {
 94    const one = Grid.builder().add(0, 0, 1.1).add(0, 1, "A").build();
 95    assert.deepEqual(FLOOR.SELF.run(null, one), 1);
 96  });
 97
 98  it("should return error when argument lengths are wrong", function () {
 99    assertF7ExceptionByName(FLOOR.SELF.run(null), F7ExceptionName.NA);
100    assertF7ExceptionByName(FLOOR.SELF.run(null, "A", "B", "C"), F7ExceptionName.NA);
101  });
102});