-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.spec.ts
172 lines (155 loc) · 5.25 KB
/
math.spec.ts
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import * as math from '@src/math';
describe('zeroFill test', () => {
test('ZEROFILL: invalid param that will return itself', () => {
const param1 = -Infinity;
const param2 = Infinity;
const param3 = '123';
expect(math.zeroFill(param1)).toBe(String(param1));
expect(math.zeroFill(param2)).toBe(String(param2));
expect(math.zeroFill(param3)).toBe(param3);
});
test('ZEROFILL: param le zero', () => {
const param1 = -9;
const param2 = -10;
expect(math.zeroFill(param1)).toBe('-09');
expect(math.zeroFill(param2)).toBe('-10');
});
test('ZEROFILL: param ge zero', () => {
const param1 = 0;
const param2 = 1;
const param3 = 10;
expect(math.zeroFill(param1)).toBe('00');
expect(math.zeroFill(param2)).toBe('01');
expect(math.zeroFill(param3)).toBe('10');
});
test('ZEROFILL: param is number-like string', () => {
const parma1 = '1';
const param2 = '10';
const param3 = '-1.1';
const param4 = 'num';
expect(math.zeroFill(parma1)).toBe('01');
expect(math.zeroFill(param2)).toBe('10');
expect(math.zeroFill(param3)).toBe('-01.1');
expect(math.zeroFill(param4)).toBe('num');
});
});
describe('max test', () => {
test('MAX: input is a number array', () => {
const param = [1, 2, 3];
expect(math.max(param)).toBe(3);
});
test('MAX: input is a string array', () => {
const param = ['1', '2', '3'];
expect(() => math.max(param as unknown as number[])).toThrow();
});
});
describe('min test', () => {
test('MIN: input is a number array', () => {
const param = [1, 2, 3];
expect(math.min(param)).toBe(1);
});
test('MIN: input is a number-like string array', () => {
const param = ['1', '2', '3'];
expect(() => math.min(param as unknown as number[])).toThrow();
});
});
describe('intersection test', () => {
test('INTERSECTION: two arries has intersection', () => {
const result = math.intersection([1, 2, 3], [1, 2]);
expect(result).toEqual([1, 2]);
});
test('INTERSECTION: two arraies has no interaction by different type', () => {
const result = math.intersection([1, 2], ['1', '2'] as unknown as number[]);
expect(result).toEqual([]);
});
});
describe('union test', () => {
test('UNION: return the union of two arries with same type', () => {
const result = math.union([1, 2], [1, 3]);
expect(result).toEqual([1, 2, 3]);
});
test('UNION: return the union of two arries with different types', () => {
const result = math.union([1, 2], ['1', '2']);
expect(result).toEqual([1, 2, '1', '2']);
});
});
describe('pickUniqueNumbers', () => {
it('should pick n unique elements from array', () => {
const arr = [1, 2, 2, 3];
const result = math.pickUniqueNumber(arr, 3);
expect(new Set(result).size).toBe(3);
expect(result.length).toBe(3);
});
it('should throw if n > array length', () => {
const arr = [1, 2];
expect(() => {
math.pickUniqueNumber(arr, 3);
}).toThrow();
});
it('should handle duplicate elements', () => {
const arr = [1, 2, 2, 3];
const result = math.pickUniqueNumber(arr, 3);
expect(new Set(result).size).toBe(3);
});
});
describe('shuffle', () => {
it('should contain the same elements', () => {
const array = [1, 2, 3];
math.shuffle(array);
expect(array).toEqual(expect.arrayContaining(array));
});
it('param is not an array, should throw error', () => {
expect(() => math.shuffle(123 as unknown as any[])).toThrow();
});
});
describe('randomInt', () => {
it('should return the random int correctly', () => {
const result = math.randomInt(1, 10);
expect(result).toBeGreaterThanOrEqual(1);
expect(result).toBeLessThanOrEqual(10);
});
it('should throw error if the param is not an integer', () => {
expect(() => math.randomInt('1' as unknown as number, 10)).toThrow();
});
it('only one param', () => {
const result = math.randomInt(10);
expect(result).toBeGreaterThanOrEqual(0);
expect(result).toBeLessThanOrEqual(10);
});
});
describe('mean', () => {
it('should return mean value of the array correctly', () => {
const result = math.mean([1, 2, 3]);
expect(result).toBe(2);
});
it('array is not an array, should throw error', () => {
expect(() => math.mean(123 as unknown as any[])).toThrow();
});
it('elements in array is not number, should throw error', () => {
const array = [1, 2, '3'];
expect(() => math.mean(array as number[])).toThrow();
});
});
describe('sum', () => {
it('should return the sum value correctly', () => {
const result = math.sum([1, 2, 3]);
expect(result).toBe(6);
});
it('param is not an array, should throw error', () => {
expect(() => math.sum(123 as unknown as any[])).toThrow();
});
it('element in param is not array type, should throw error', () => {
expect(() => math.sum([1, 2, '3'] as number[])).toThrow();
});
});
describe('stdDev', () => {
it('should return standard deviation correctly', () => {
expect(math.stdDev([1, 2, 3, 4, 5])).toBeCloseTo(1.41);
});
it('param is not array, should throw error', () => {
expect(() => math.stdDev(123 as unknown as any[])).toThrow();
});
it('element in param is not number, should throw error', () => {
expect(() => math.stdDev([1, 2, '3'] as number[])).toThrow();
});
});