Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 27af329

Browse files
committed
Add number utility tests
1 parent 6124a83 commit 27af329

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed

test/numbers-test.ts

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
Copyright 2021 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import {clamp, defaultNumber, percentageOf, percentageWithin, sum} from "../src/utils/numbers";
18+
19+
describe('numbers', () => {
20+
describe('defaultNumber', () => {
21+
it('should use the default when the input is not a number', () => {
22+
const def = 42;
23+
24+
let result = defaultNumber(null, def);
25+
expect(result).toBe(def);
26+
27+
result = defaultNumber(undefined, def);
28+
expect(result).toBe(def);
29+
30+
result = defaultNumber(Number.NaN, def);
31+
expect(result).toBe(def);
32+
});
33+
34+
it('should use the number when it is a number', () => {
35+
const input = 24;
36+
const def = 42;
37+
const result = defaultNumber(input, def);
38+
expect(result).toBe(input);
39+
});
40+
});
41+
42+
describe('clamp', () => {
43+
it('should clamp high numbers', () => {
44+
const input = 101;
45+
const min = 0;
46+
const max = 100;
47+
const result = clamp(input, min, max);
48+
expect(result).toBe(max);
49+
});
50+
51+
it('should clamp low numbers', () => {
52+
const input = -1;
53+
const min = 0;
54+
const max = 100;
55+
const result = clamp(input, min, max);
56+
expect(result).toBe(min);
57+
});
58+
59+
it('should not clamp numbers in range', () => {
60+
const input = 50;
61+
const min = 0;
62+
const max = 100;
63+
const result = clamp(input, min, max);
64+
expect(result).toBe(input);
65+
});
66+
67+
it('should clamp floats', () => {
68+
const min = -0.10;
69+
const max = +0.10;
70+
71+
let result = clamp(-1.2, min, max);
72+
expect(result).toBe(min);
73+
74+
result = clamp(1.2, min, max);
75+
expect(result).toBe(max);
76+
77+
result = clamp(0.02, min, max);
78+
expect(result).toBe(0.02);
79+
});
80+
});
81+
82+
describe('sum', () => {
83+
it('should sum', () => { // duh
84+
const result = sum(1, 2, 1, 4);
85+
expect(result).toBe(8);
86+
});
87+
});
88+
89+
describe('percentageWithin', () => {
90+
it('should work within 0-100', () => {
91+
const result = percentageWithin(0.4, 0, 100);
92+
expect(result).toBe(40);
93+
});
94+
95+
it('should work within 0-100 when pct > 1', () => {
96+
const result = percentageWithin(1.4, 0, 100);
97+
expect(result).toBe(140);
98+
});
99+
100+
it('should work within 0-100 when pct < 0', () => {
101+
const result = percentageWithin(-1.4, 0, 100);
102+
expect(result).toBe(-140);
103+
});
104+
105+
it('should work with ranges other than 0-100', () => {
106+
const result = percentageWithin(0.4, 10, 20);
107+
expect(result).toBe(14);
108+
});
109+
110+
it('should work with ranges other than 0-100 when pct > 1', () => {
111+
const result = percentageWithin(1.4, 10, 20);
112+
expect(result).toBe(24);
113+
});
114+
115+
it('should work with ranges other than 0-100 when pct < 0', () => {
116+
const result = percentageWithin(-1.4, 10, 20);
117+
expect(result).toBe(-4);
118+
});
119+
120+
it('should work with floats', () => {
121+
const result = percentageWithin(0.4, 10.2, 20.4);
122+
expect(result).toBe(14.28);
123+
});
124+
});
125+
126+
// These are the inverse of percentageWithin
127+
describe('percentageOf', () => {
128+
it('should work within 0-100', () => {
129+
const result = percentageOf(40, 0, 100);
130+
expect(result).toBe(0.4);
131+
});
132+
133+
it('should work within 0-100 when val > 100', () => {
134+
const result = percentageOf(140, 0, 100);
135+
expect(result).toBe(1.40);
136+
});
137+
138+
it('should work within 0-100 when val < 0', () => {
139+
const result = percentageOf(-140, 0, 100);
140+
expect(result).toBe(-1.40);
141+
});
142+
143+
it('should work with ranges other than 0-100', () => {
144+
const result = percentageOf(14, 10, 20);
145+
expect(result).toBe(0.4);
146+
});
147+
148+
it('should work with ranges other than 0-100 when val > 100', () => {
149+
const result = percentageOf(24, 10, 20);
150+
expect(result).toBe(1.4);
151+
});
152+
153+
it('should work with ranges other than 0-100 when val < 0', () => {
154+
const result = percentageOf(-4, 10, 20);
155+
expect(result).toBe(-1.4);
156+
});
157+
158+
it('should work with floats', () => {
159+
const result = percentageOf(14.28, 10.2, 20.4);
160+
expect(result).toBe(0.4);
161+
});
162+
});
163+
});

0 commit comments

Comments
 (0)