name:
src/test/js/formulas/math/ROUNDTest.ts
-rw-r--r--
4449
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 { ROUND } from "../../../../main/js/formulas/math/ROUND";
8import { Grid } from "../../../../main/js/models/common/Grid";
9import { CollateralLookupFunction, LookupFunction } from "../../../../main/js/models/common/Types";
10import { CommonModels } from "../../testutils/CommonModels";
11
12describe("ROUND", function () {
13 it("should work with whole numbers with places defaulting to 0", function () {
14 assert.deepEqual(ROUND.SELF.run(null, 10), 10);
15 assert.deepEqual(ROUND.SELF.run(null, -10), -10);
16 assert.deepEqual(ROUND.SELF.run(null, 0), 0);
17 });
18
19 it("should round down decimals to full with places defaulting to 0", function () {
20 assert.deepEqual(ROUND.SELF.run(null, 3.218639128), 3);
21 assert.deepEqual(ROUND.SELF.run(null, 7.99), 8);
22 assert.deepEqual(ROUND.SELF.run(null, 7.0000000001), 7);
23 assert.deepEqual(ROUND.SELF.run(null, -4.444444), -4);
24 });
25
26 it("should round up to place", function () {
27 assert.deepEqual(ROUND.SELF.run(null, 3.218639128, 4), 3.2186);
28 assert.deepEqual(ROUND.SELF.run(null, 3.218639128, 5), 3.21864);
29 assert.deepEqual(ROUND.SELF.run(null, 9.12, 0), 9);
30 assert.deepEqual(ROUND.SELF.run(null, 9.12, 1), 9.1);
31 assert.deepEqual(ROUND.SELF.run(null, 9.12, 2), 9.12);
32 assert.deepEqual(ROUND.SELF.run(null, 9.12, 3), 9.12);
33 assert.deepEqual(ROUND.SELF.run(null, 9.12, 8), 9.12);
34 });
35
36 it("should round up to the negative place", function () {
37 assert.deepEqual(ROUND.SELF.run(null, 3341.218639128, 0), 3341);
38 assert.deepEqual(ROUND.SELF.run(null, 3341.218639128, -1), 3340);
39 assert.deepEqual(ROUND.SELF.run(null, 3341.218639128, -2), 3300);
40 assert.deepEqual(ROUND.SELF.run(null, 3341.218639128, -3), 3000);
41 assert.deepEqual(ROUND.SELF.run(null, 3341.218639128, -4), 0);
42 assert.deepEqual(ROUND.SELF.run(null, -3341.839749239, 0), -3342);
43 assert.deepEqual(ROUND.SELF.run(null, -3341.839749239, -1), -3340);
44 assert.deepEqual(ROUND.SELF.run(null, -3341.839749239, -2), -3300);
45 assert.deepEqual(ROUND.SELF.run(null, -3341.839749239, -3), -3000);
46 assert.deepEqual(ROUND.SELF.run(null, -3341.839749239, -4), 0);
47 });
48
49 it("should round to non-integer places", function () {
50 assert.deepEqual(ROUND.SELF.run(null, 3341.218639128, -4.99), 0);
51 assert.deepEqual(ROUND.SELF.run(null, 3341.218639128, -3.999999), 3000);
52 assert.deepEqual(ROUND.SELF.run(null, 3341.218639128, -2.1), 3300);
53 assert.deepEqual(ROUND.SELF.run(null, 3341.218639128, -1.1982371982), 3340);
54 assert.deepEqual(ROUND.SELF.run(null, 3341.218639128, 0), 3341);
55 assert.deepEqual(ROUND.SELF.run(null, 3341.218639128, 0.999), 3341);
56 assert.deepEqual(ROUND.SELF.run(null, 3341.218639128, -0.999), 3341);
57 assert.deepEqual(ROUND.SELF.run(null, 3341.218639128, 1.111), 3341.2);
58 assert.deepEqual(ROUND.SELF.run(null, 3341.218639128, -1.111), 3340);
59 });
60
61 it("should work with strings", function () {
62 assert.deepEqual(ROUND.SELF.run(null, "9"), 9);
63 });
64
65 it("should do pass-through errors", function () {
66 assert.deepEqual(ROUND.SELF.run(null, new ValueException()), new ValueException());
67 });
68
69 it("should use lookup", function () {
70 const lookup = stub();
71 const collateralLookup = stub();
72 const F = new ROUND(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
73 collateralLookup.withArgs(CommonModels.A1, CommonModels.M22_RANGE).returns(1.1);
74 assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE), 1);
75 assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.M22_RANGE));
76 assert.equal(collateralLookup.callCount, 1);
77 assert.isTrue(lookup.notCalled);
78 });
79
80 it("should handle grids", function () {
81 const one = Grid.builder().add(0, 0, 1.1).add(0, 1, "A").build();
82 assert.deepEqual(ROUND.SELF.run(null, one), 1);
83 });
84
85 it("should return error when argument lengths are wrong", function () {
86 assert.deepEqual((ROUND.SELF.run(null) as NAException).name, F7ExceptionName.NA);
87 assert.deepEqual((ROUND.SELF.run(null, "A", "B", "C") as NAException).name, F7ExceptionName.NA);
88 });
89});