-
Notifications
You must be signed in to change notification settings - Fork 0
/
numbers.test.js
85 lines (83 loc) · 2.97 KB
/
numbers.test.js
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
import Numbers from './numbers.js';
describe('.roundEnergy', () => {
it('handles nulls.', () => {
expect(Numbers.roundEnergy(null)).toBeNull();
});
it('non numbers return NaN if not parsable.', () => {
expect(Numbers.roundEnergy('hi')).toBeNaN();
expect(Numbers.roundEnergy(true)).toBeNaN();
expect(Numbers.roundEnergy(false)).toBeNaN();
expect(Numbers.roundEnergy(new Date())).toBeNaN();
});
it('properly rounds floating point values.', () => {
for (let i = -100; i < 100; i += 0.12345) {
let intv = Math.trunc(i);
let decr = i - intv;
let decv = Math.round(decr * 4) / 4;
expect(Numbers.roundEnergy(i)).toBe(intv + decv);
}
for (let i = -100; i < 100; i += 0.25) { //should always be the same
expect(Numbers.roundEnergy(i)).toBe(i);
}
//spot checks
expect(Numbers.roundEnergy(12.12)).toBe(12);
expect(Numbers.roundEnergy(59.125)).toBe(59.25);
expect(Numbers.roundEnergy(12.37)).toBe(12.25);
expect(Numbers.roundEnergy(59.375)).toBe(59.5);
expect(Numbers.roundEnergy(12.62)).toBe(12.5);
expect(Numbers.roundEnergy(59.625)).toBe(59.75);
expect(Numbers.roundEnergy(12.87)).toBe(12.75);
expect(Numbers.roundEnergy(59.875)).toBe(60);
});
it('does not affect integer values.', () => {
for (let i = -1000; i < 10000; i++) {
expect(Numbers.roundEnergy(i)).toBe(i);
}
});
});
describe('.sign', () => {
it('handles nulls.', () => {
expect(Numbers.sign(null)).toBe('');
});
it('handles 0s.', () => {
expect(Numbers.sign(0)).toBe('');
});
it('handles frameworks.', () => {
let numbertype = {
toNumber: () => 5
};
expect(Numbers.sign(numbertype)).toBe('+');
});
it('handles booleans.', () => {
expect(Numbers.sign(true)).toBe('+');
expect(Numbers.sign(false)).toBe('');
});
it('handles number-like strings.', () => {
expect(Numbers.sign('10')).toBe('+');
expect(Numbers.sign('10.44')).toBe('+');
expect(Numbers.sign('0')).toBe('');
expect(Numbers.sign('-10')).toBe('-');
expect(Numbers.sign('-10.44')).toBe('-');
});
it('handles bad number types.', () => {
expect(Numbers.sign(undefined)).toBeNaN();
expect(Numbers.sign('whee')).toBeNaN();
expect(Numbers.sign(new Date())).toBeNaN();
});
it('handles positive numbers.', () => {
for (let x = 1; x < 10000; x++) {
expect(Numbers.sign(x)).toBe('+');
}
for (let x = 1; x < 1000; x += 0.123) {
expect(Numbers.sign(x)).toBe('+');
}
});
it('handles negative numbers.', () => {
for (let x = -1; x > -10000; x--) {
expect(Numbers.sign(x)).toBe('-');
}
for (let x = -1; x > -1000; x -= 0.123) {
expect(Numbers.sign(x)).toBe('-');
}
});
});