Skip to content

Commit e1aad48

Browse files
tech(time): add unit tests for math utils (#5343)
## Description Adds unit tests for roundToNearest, floorToNearest, ceilToNearest and circularize in the time entrypoint. ----- -----
1 parent 50076f6 commit e1aad48

1 file changed

Lines changed: 147 additions & 0 deletions

File tree

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import { ceilToNearest, circularize, floorToNearest, roundToNearest } from './math.utils';
2+
3+
describe('MathUtils', () => {
4+
describe('roundToNearest', () => {
5+
it('should return the value itself when it is already a multiple of the step', () => {
6+
// Act
7+
const result = roundToNearest(30, 15);
8+
9+
// Assert
10+
expect(result).toBe(30);
11+
});
12+
13+
it('should round down when the value is below the half step', () => {
14+
// Act
15+
const result = roundToNearest(37, 15);
16+
17+
// Assert
18+
expect(result).toBe(30);
19+
});
20+
21+
it('should round up when the value is above the half step', () => {
22+
// Act
23+
const result = roundToNearest(38, 15);
24+
25+
// Assert
26+
expect(result).toBe(45);
27+
});
28+
29+
it('should round negative values', () => {
30+
// Act
31+
const result = roundToNearest(-23, 5);
32+
33+
// Assert
34+
expect(result).toBe(-25);
35+
});
36+
});
37+
38+
describe('floorToNearest', () => {
39+
it('should return the value itself when it is already a multiple of the step', () => {
40+
// Act
41+
const result = floorToNearest(30, 15);
42+
43+
// Assert
44+
expect(result).toBe(30);
45+
});
46+
47+
it('should round down even when the value is close to the next step', () => {
48+
// Act
49+
const result = floorToNearest(44, 15);
50+
51+
// Assert
52+
expect(result).toBe(30);
53+
});
54+
55+
it('should round negative values towards minus infinity', () => {
56+
// Act
57+
const result = floorToNearest(-23, 5);
58+
59+
// Assert
60+
expect(result).toBe(-25);
61+
});
62+
});
63+
64+
describe('ceilToNearest', () => {
65+
it('should return the value itself when it is already a multiple of the step', () => {
66+
// Act
67+
const result = ceilToNearest(30, 15);
68+
69+
// Assert
70+
expect(result).toBe(30);
71+
});
72+
73+
it('should round up even when the value is close to the previous step', () => {
74+
// Act
75+
const result = ceilToNearest(31, 15);
76+
77+
// Assert
78+
expect(result).toBe(45);
79+
});
80+
81+
it('should round negative values towards plus infinity', () => {
82+
// Act
83+
const result = ceilToNearest(-23, 5);
84+
85+
// Assert
86+
expect(result).toBe(-20);
87+
});
88+
});
89+
90+
describe('circularize', () => {
91+
it('should return 0 for 0', () => {
92+
// Act
93+
const result = circularize(0, 10);
94+
95+
// Assert
96+
expect(result).toBe(0);
97+
});
98+
99+
it('should return max instead of 0 when the value is max', () => {
100+
// Act
101+
const result = circularize(10, 10);
102+
103+
// Assert
104+
expect(result).toBe(10);
105+
});
106+
107+
it('should return max instead of 0 when the value is a multiple of max', () => {
108+
// Act
109+
const result = circularize(30, 10);
110+
111+
// Assert
112+
expect(result).toBe(10);
113+
});
114+
115+
it('should keep values already inside the range unchanged', () => {
116+
// Act
117+
const result = circularize(7, 10);
118+
119+
// Assert
120+
expect(result).toBe(7);
121+
});
122+
123+
it('should wrap values above max', () => {
124+
// Act
125+
const result = circularize(11, 10);
126+
127+
// Assert
128+
expect(result).toBe(1);
129+
});
130+
131+
it('should wrap negative values', () => {
132+
// Act
133+
const result = circularize(-1, 10);
134+
135+
// Assert
136+
expect(result).toBe(9);
137+
});
138+
139+
it('should wrap values several turns above max', () => {
140+
// Act
141+
const result = circularize(25, 10);
142+
143+
// Assert
144+
expect(result).toBe(5);
145+
});
146+
});
147+
});

0 commit comments

Comments
 (0)