spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← All files
name: tests/Formulas/RangeTest.ts
-rw-r--r--
2757
 1import {
 2  FREQUENCY,
 3  GROWTH,
 4  LINEST
 5} from "../../src/Formulas/Range";
 6import {
 7  assertArrayEquals,
 8  catchAndAssertEquals,
 9  test
10} from "../Utils/Asserts";
11import * as ERRORS from "../../src/Errors";
12
13
14test("FREQUENCY", function(){
15  assertArrayEquals(FREQUENCY([10, 2, 3, 44, 1, 2], 22), [5, 1]);
16  assertArrayEquals(FREQUENCY([10, 2, 3, 44, 1, 2], [22]), [5, 1]);
17  assertArrayEquals(FREQUENCY([10, [2, 3, 44, 1], 2], [22]), [5, 1]);
18  assertArrayEquals(FREQUENCY([18, 30, 90, 91, 35, 27, 75, 28, 58], [25, 50, 75]), [1, 4, 2, 2]);
19  assertArrayEquals(FREQUENCY([18, 30, 90, 91, 35, 27, 75, 28, 58], [50, 25, 75]), [1, 4, 2, 2]);
20  catchAndAssertEquals(function() {
21    FREQUENCY.apply(this, [10, 10, 10]);
22  }, ERRORS.NA_ERROR);
23  catchAndAssertEquals(function() {
24    FREQUENCY.apply(this, [10]);
25  }, ERRORS.NA_ERROR);
26  catchAndAssertEquals(function() {
27    FREQUENCY([10, 2, 3, 44, 1, [], 2], 22);
28  }, ERRORS.REF_ERROR);
29});
30
31
32test("GROWTH", function(){
33  assertArrayEquals(GROWTH(
34    [15.53, 19.99, 20.43, 21.18, 25.93, 30.00, 30.00, 34.01, 36.47],
35    [1, 2, 3, 4, 5, 6, 7, 8, 9],
36    [10, 11, 12]
37  ), [41.74052172327583, 46.22712349335043, 51.19598074591968]);
38  assertArrayEquals(GROWTH(
39    [15.53, 19.99, 20.43, 21.18, 25.93, [30.00, 30.00, 34.01], 36.47],
40    [1, 2, 3, 4, 5, 6, 7, 8, 9],
41    [10, 11, 12]
42  ), [41.74052172327583, 46.22712349335043, 51.19598074591968]);
43  catchAndAssertEquals(function() {
44    GROWTH(
45      [15.53, 19.99, 20.43, 21.18, "25.93", 30.00, 30.00, 34.01, 36.47],
46      [1, 2, 3, 4, 5, 6, 7, 8, 9],
47      [10, 11, 12]
48    );
49  }, ERRORS.VALUE_ERROR);
50  catchAndAssertEquals(function() {
51    GROWTH(
52      [15.53, 19.99, 20.43, 21.18, [25.93, 30.00], [], 30.00, 34.01, 36.47],
53      [1, 2, 3, 4, 5, 6, 7, 8, 9],
54      [10, 11, 12]
55    );
56  }, ERRORS.REF_ERROR);
57});
58
59
60test("GROWTH", function(){
61  assertArrayEquals(LINEST(
62    [15.53, 19.99, 20.43, 21.18, 25.93, 30.00, 30.00, 34.01, 36.47],
63    [1, 2, 3, 4, 5, 6, 7, 8, 9]
64  ), [2.563, 13.13388888888889]);
65  assertArrayEquals(LINEST(
66    [15.53, 19.99, 20.43, 21.18, 25.93, 30],
67    [1, 2, 3, 4, 5, 6]
68  ), [2.5977142857142863,	13.08466666666666]);
69  catchAndAssertEquals(function() {
70    LINEST(
71      [15.53, 19.99, 20.43, 21.18, 25.93, "string", 30],
72      [1, 2, 3, 4, 5, 6]
73    );
74  }, ERRORS.VALUE_ERROR);
75  catchAndAssertEquals(function() {
76    LINEST(
77      [15.53],
78      [1]
79    );
80  }, ERRORS.NA_ERROR);
81  catchAndAssertEquals(function() {
82    LINEST.apply(
83      this,
84      [[15.53, 19.99, 20.43, 21.18, 25.93, 30],
85        [1, 2, 3, 4, 5, 6],
86        "another"]
87    );
88  }, ERRORS.NA_ERROR);
89  catchAndAssertEquals(function() {
90    LINEST.apply(
91      this,
92      [[15.53, 19.99, 20.43, 21.18, 25.93, 30]]
93    );
94  }, ERRORS.NA_ERROR);
95});