forked from janjakubnanista/downsample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.spec.ts
More file actions
98 lines (82 loc) · 3.19 KB
/
Copy pathutils.spec.ts
File metadata and controls
98 lines (82 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import "jest";
import { NormalizedDataPoint } from "./types";
import { calculateTriangleArea, calculateAverageDataPoint, splitIntoBuckets } from "./utils";
describe("utils", () => {
describe("calculateTriangleArea", () => {
it("should return 0 for a collapsed triangle", () => {
const pointA: NormalizedDataPoint = [1, 2];
const pointB: NormalizedDataPoint = [2, 2];
const pointC: NormalizedDataPoint = [3, 2];
expect(calculateTriangleArea(pointA, pointB, pointC)).toBe(0);
});
it("should return correct area for a triangle aligned with an axis", () => {
const pointA: NormalizedDataPoint = [1, 0];
const pointB: NormalizedDataPoint = [2, 5];
const pointC: NormalizedDataPoint = [3, 0];
expect(calculateTriangleArea(pointA, pointB, pointC)).toBe(5);
});
it("should return correct area for a triangle not aligned with an axis", () => {
const pointA: NormalizedDataPoint = [1, 2];
const pointB: NormalizedDataPoint = [2, 5];
const pointC: NormalizedDataPoint = [3, 5];
expect(calculateTriangleArea(pointA, pointB, pointC)).toBe(1.5);
});
it("should return correct area for a triangle covering negative quadrants", () => {
const pointA: NormalizedDataPoint = [-1, -1];
const pointB: NormalizedDataPoint = [0, 1];
const pointC: NormalizedDataPoint = [1, -1];
expect(calculateTriangleArea(pointA, pointB, pointC)).toBe(2);
});
});
describe("calculateAverageDataPoint", () => {
it("should return undefined when passed no data points", () => {
expect(calculateAverageDataPoint()).toBe(undefined);
});
it("should return the same point when passed one data point", () => {
expect(calculateAverageDataPoint([0, 1])).toEqual([0, 1]);
});
it("should return correct point average", () => {
const pointA: NormalizedDataPoint = [-1, -1];
const pointB: NormalizedDataPoint = [0, 2];
const pointC: NormalizedDataPoint = [1, -1];
expect(calculateAverageDataPoint(pointA, pointB, pointC)).toEqual([0, 0]);
});
});
describe("spliIntoBuckets", () => {
const data: [number, number][] = [
[0, 2],
[1, -1],
[2, 2],
[3, -1],
[4, 2],
[5, -1],
[6, 2],
[7, -1]
];
it("should return two buckets with one data point each when passed two data points", () => {
expect(splitIntoBuckets([
[0, 2],
[1, -1]
], 2)).toEqual([
[
[0, 2]
],
[
[1, -1]
]
]);
});
it("should return an array of desired length", () => {
expect(splitIntoBuckets(data, 2)).toHaveLength(2);
expect(splitIntoBuckets(data, 3)).toHaveLength(3);
expect(splitIntoBuckets(data, 4)).toHaveLength(4);
expect(splitIntoBuckets(data, 5)).toHaveLength(5);
});
it("should return an array with the first and the last bucket containing the first and the last data points", () => {
expect(splitIntoBuckets(data, 3)[0]).toHaveLength(1);
expect(splitIntoBuckets(data, 3)[0]).toEqual([[0, 2]]);
expect(splitIntoBuckets(data, 3)[2]).toHaveLength(1);
expect(splitIntoBuckets(data, 3)[2]).toEqual([[7, -1]]);
});
});
});