Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/test/common/numbers.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { ensureNoDisposablesAreLeakedInTestSuite } from './utils.js';
8
import { isPointWithinTriangle, randomInt } from '../../common/numbers.js';
9
10
suite('isPointWithinTriangle', () => {
11
ensureNoDisposablesAreLeakedInTestSuite();
12
13
test('should return true if the point is within the triangle', () => {
14
const result = isPointWithinTriangle(0.25, 0.25, 0, 0, 1, 0, 0, 1);
15
assert.ok(result);
16
});
17
18
test('should return false if the point is outside the triangle', () => {
19
const result = isPointWithinTriangle(2, 2, 0, 0, 1, 0, 0, 1);
20
assert.ok(!result);
21
});
22
23
test('should return true if the point is on the edge of the triangle', () => {
24
const result = isPointWithinTriangle(0.5, 0, 0, 0, 1, 0, 0, 1);
25
assert.ok(result);
26
});
27
});
28
29
suite('randomInt', () => {
30
ensureNoDisposablesAreLeakedInTestSuite();
31
32
/**
33
* Test helper that allows to run a test on the `randomInt()`
34
* utility with specified `max` and `min` values.
35
*/
36
const testRandomIntUtil = (max: number, min: number | undefined, testName: string) => {
37
suite(testName, () => {
38
let i = 0;
39
while (++i < 5) {
40
test(`should generate random boolean attempt#${i}`, async () => {
41
let iterations = 100;
42
while (iterations-- > 0) {
43
const int = randomInt(max, min);
44
45
assert(
46
int <= max,
47
`Expected ${int} to be less than or equal to ${max}.`
48
);
49
assert(
50
int >= (min ?? 0),
51
`Expected ${int} to be greater than or equal to ${min ?? 0}.`,
52
);
53
}
54
});
55
}
56
57
test('should include min and max', async () => {
58
let iterations = 125;
59
const results = [];
60
while (iterations-- > 0) {
61
results.push(randomInt(max, min));
62
}
63
64
assert(
65
results.includes(max),
66
`Expected ${results} to include ${max}.`,
67
);
68
assert(
69
results.includes(min ?? 0),
70
`Expected ${results} to include ${min ?? 0}.`,
71
);
72
});
73
});
74
};
75
76
suite('positive numbers', () => {
77
testRandomIntUtil(4, 2, 'max: 4, min: 2');
78
testRandomIntUtil(4, 0, 'max: 4, min: 0');
79
testRandomIntUtil(4, undefined, 'max: 4, min: undefined');
80
testRandomIntUtil(1, 0, 'max: 0, min: 0');
81
});
82
83
suite('negative numbers', () => {
84
testRandomIntUtil(-2, -5, 'max: -2, min: -5');
85
testRandomIntUtil(0, -5, 'max: 0, min: -5');
86
testRandomIntUtil(0, -1, 'max: 0, min: -1');
87
});
88
89
suite('split numbers', () => {
90
testRandomIntUtil(3, -1, 'max: 3, min: -1');
91
testRandomIntUtil(2, -2, 'max: 2, min: -2');
92
testRandomIntUtil(1, -3, 'max: 2, min: -2');
93
});
94
95
suite('errors', () => {
96
test('should throw if "min" is == "max" #1', () => {
97
assert.throws(() => {
98
randomInt(200, 200);
99
}, `"max"(200) param should be greater than "min"(200)."`);
100
});
101
102
test('should throw if "min" is == "max" #2', () => {
103
assert.throws(() => {
104
randomInt(2, 2);
105
}, `"max"(2) param should be greater than "min"(2)."`);
106
});
107
108
test('should throw if "min" is == "max" #3', () => {
109
assert.throws(() => {
110
randomInt(0);
111
}, `"max"(0) param should be greater than "min"(0)."`);
112
});
113
114
test('should throw if "min" is > "max" #1', () => {
115
assert.throws(() => {
116
randomInt(2, 3);
117
}, `"max"(2) param should be greater than "min"(3)."`);
118
});
119
120
test('should throw if "min" is > "max" #2', () => {
121
assert.throws(() => {
122
randomInt(999, 2000);
123
}, `"max"(999) param should be greater than "min"(2000)."`);
124
});
125
126
test('should throw if "min" is > "max" #3', () => {
127
assert.throws(() => {
128
randomInt(0, 1);
129
}, `"max"(0) param should be greater than "min"(1)."`);
130
});
131
132
test('should throw if "min" is > "max" #4', () => {
133
assert.throws(() => {
134
randomInt(-5, 2);
135
}, `"max"(-5) param should be greater than "min"(2)."`);
136
});
137
138
test('should throw if "min" is > "max" #5', () => {
139
assert.throws(() => {
140
randomInt(-4, 0);
141
}, `"max"(-4) param should be greater than "min"(0)."`);
142
});
143
144
test('should throw if "min" is > "max" #6', () => {
145
assert.throws(() => {
146
randomInt(-4);
147
}, `"max"(-4) param should be greater than "min"(0)."`);
148
});
149
150
test('should throw if "max" is `NaN`', () => {
151
assert.throws(() => {
152
randomInt(NaN);
153
}, `"max" param is not a number."`);
154
});
155
156
test('should throw if "min" is `NaN`', () => {
157
assert.throws(() => {
158
randomInt(4, NaN);
159
}, `"min" param is not a number."`);
160
});
161
162
suite('infinite arguments', () => {
163
test('should throw if "max" is infinite [Infinity]', () => {
164
assert.throws(() => {
165
randomInt(Infinity);
166
}, `"max" param is not finite."`);
167
});
168
169
test('should throw if "max" is infinite [-Infinity]', () => {
170
assert.throws(() => {
171
randomInt(-Infinity);
172
}, `"max" param is not finite."`);
173
});
174
175
test('should throw if "max" is infinite [+Infinity]', () => {
176
assert.throws(() => {
177
randomInt(+Infinity);
178
}, `"max" param is not finite."`);
179
});
180
181
test('should throw if "min" is infinite [Infinity]', () => {
182
assert.throws(() => {
183
randomInt(Infinity, Infinity);
184
}, `"max" param is not finite."`);
185
});
186
187
test('should throw if "min" is infinite [-Infinity]', () => {
188
assert.throws(() => {
189
randomInt(Infinity, -Infinity);
190
}, `"max" param is not finite."`);
191
});
192
193
test('should throw if "min" is infinite [+Infinity]', () => {
194
assert.throws(() => {
195
randomInt(Infinity, +Infinity);
196
}, `"max" param is not finite."`);
197
});
198
});
199
});
200
});
201
202