]> Lady’s Gitweb - Pisces/blob - numeric.test.js
Add ::flags override to Matcher
[Pisces] / numeric.test.js
1 // ♓🌟 Piscēs ∷ numeric.test.js
2 // ====================================================================
3 //
4 // Copyright © 2022 Lady [@ Lady’s Computer].
5 //
6 // This Source Code Form is subject to the terms of the Mozilla Public
7 // License, v. 2.0. If a copy of the MPL was not distributed with this
8 // file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
9
10 import {
11 assertStrictEquals,
12 assertThrows,
13 describe,
14 it,
15 } from "./dev-deps.js";
16 import {
17 abs,
18 atan2,
19 clz32,
20 max,
21 min,
22 NEGATIVE_ZERO,
23 POSITIVE_ZERO,
24 sgn,
25 toBigInt,
26 toExponentialNotation,
27 toFixedDecimalNotation,
28 toFloat32,
29 toIntegralNumber,
30 toIntegralNumberOrInfinity,
31 toIntN,
32 toNumber,
33 toNumeric,
34 toUintN,
35 } from "./numeric.js";
36
37 describe("NEGATIVE_ZERO", () => {
38 it("[[Get]] is negative zero", () => {
39 assertStrictEquals(NEGATIVE_ZERO, -0);
40 });
41 });
42
43 describe("POSITIVE_ZERO", () => {
44 it("[[Get]] is positive zero", () => {
45 assertStrictEquals(POSITIVE_ZERO, 0);
46 });
47 });
48
49 describe("abs", () => {
50 it("[[Call]] returns the absolute value", () => {
51 assertStrictEquals(abs(-1), 1);
52 });
53
54 it("[[Call]] works with big·ints", () => {
55 const bn = BigInt(Number.MAX_SAFE_INTEGER) + 2n;
56 assertStrictEquals(abs(-bn), bn);
57 });
58 });
59
60 describe("atan2", () => {
61 it("[[Call]] returns the atan2", () => {
62 assertStrictEquals(atan2(6, 9), Math.atan2(6, 9));
63 });
64
65 it("[[Call]] works with big·ints", () => {
66 assertStrictEquals(atan2(6n, 9n), Math.atan2(6, 9));
67 });
68 });
69
70 describe("clz32", () => {
71 it("[[Call]] returns the clz32", () => {
72 assertStrictEquals(clz32(1 << 28), 3);
73 });
74
75 it("[[Call]] works with big·ints", () => {
76 assertStrictEquals(clz32(1n << 28n), 3);
77 });
78 });
79
80 describe("max", () => {
81 it("[[Call]] returns the largest number", () => {
82 assertStrictEquals(max(1, -6, 92, -Infinity, 0), 92);
83 });
84
85 it("[[Call]] returns the largest big·int", () => {
86 assertStrictEquals(max(1n, -6n, 92n, 0n), 92n);
87 });
88
89 it("[[Call]] returns nan if any argument is nan", () => {
90 assertStrictEquals(max(0, NaN, 1), NaN);
91 });
92
93 it("[[Call]] returns -Infinity when called with no arguments", () => {
94 assertStrictEquals(max(), -Infinity);
95 });
96
97 it("[[Call]] throws if both big·int and number arguments are provided", () => {
98 assertThrows(() => max(-Infinity, 0n));
99 });
100 });
101
102 describe("min", () => {
103 it("[[Call]] returns the largest number", () => {
104 assertStrictEquals(min(1, -6, 92, Infinity, 0), -6);
105 });
106
107 it("[[Call]] returns the largest big·int", () => {
108 assertStrictEquals(min(1n, -6n, 92n, 0n), -6n);
109 });
110
111 it("[[Call]] returns nan if any argument is nan", () => {
112 assertStrictEquals(min(0, NaN, 1), NaN);
113 });
114
115 it("[[Call]] returns Infinity when called with no arguments", () => {
116 assertStrictEquals(min(), Infinity);
117 });
118
119 it("[[Call]] throws if both big·int and number arguments are provided", () => {
120 assertThrows(() => min(Infinity, 0n));
121 });
122 });
123
124 describe("sgn", () => {
125 it("[[Call]] returns the sign", () => {
126 assertStrictEquals(sgn(Infinity), 1);
127 assertStrictEquals(sgn(-Infinity), -1);
128 assertStrictEquals(sgn(0), 0);
129 assertStrictEquals(sgn(-0), -0);
130 assertStrictEquals(sgn(NaN), NaN);
131 });
132
133 it("[[Call]] works with big·ints", () => {
134 assertStrictEquals(sgn(0n), 0n);
135 assertStrictEquals(sgn(92n), 1n);
136 assertStrictEquals(sgn(-92n), -1n);
137 });
138 });
139
140 describe("toBigInt", () => {
141 it("[[Call]] converts to a big·int", () => {
142 assertStrictEquals(toBigInt(2), 2n);
143 });
144 });
145
146 describe("toExponentialNotation", () => {
147 it("[[Call]] converts to exponential notation", () => {
148 assertStrictEquals(toExponentialNotation(231), "2.31e+2");
149 });
150
151 it("[[Call]] works with big·ints", () => {
152 assertStrictEquals(
153 toExponentialNotation(9007199254740993n),
154 "9.007199254740993e+15",
155 );
156 });
157
158 it("[[Call]] respects the specified number of fractional digits", () => {
159 assertStrictEquals(
160 toExponentialNotation(.00000000642, 3),
161 "6.420e-9",
162 );
163 assertStrictEquals(toExponentialNotation(.00691, 1), "6.9e-3");
164 assertStrictEquals(toExponentialNotation(.00685, 1), "6.9e-3");
165 assertStrictEquals(toExponentialNotation(.004199, 2), "4.20e-3");
166 assertStrictEquals(
167 toExponentialNotation(6420000000n, 3),
168 "6.420e+9",
169 );
170 assertStrictEquals(toExponentialNotation(6910n, 1), "6.9e+3");
171 assertStrictEquals(toExponentialNotation(6850n, 1), "6.9e+3");
172 assertStrictEquals(toExponentialNotation(4199n, 2), "4.20e+3");
173 });
174 });
175
176 describe("toFixedDecimalNotation", () => {
177 it("[[Call]] converts to fixed decimal notation", () => {
178 assertStrictEquals(toFixedDecimalNotation(69.4199, 3), "69.420");
179 });
180
181 it("[[Call]] works with big·ints", () => {
182 assertStrictEquals(
183 toFixedDecimalNotation(9007199254740993n),
184 "9007199254740993",
185 );
186 assertStrictEquals(
187 toFixedDecimalNotation(9007199254740993n, 2),
188 "9007199254740993.00",
189 );
190 });
191 });
192
193 describe("toFloat32", () => {
194 it("[[Call]] returns the 32‐bit floating‐point representation", () => {
195 assertStrictEquals(
196 toFloat32(562949953421313),
197 Math.fround(562949953421313),
198 );
199 });
200
201 it("[[Call]] works with big·ints", () => {
202 assertStrictEquals(
203 toFloat32(562949953421313n),
204 Math.fround(562949953421313),
205 );
206 });
207 });
208
209 describe("toIntN", () => {
210 it("[[Call]] converts to an int·n", () => {
211 assertStrictEquals(toIntN(2, 7n), -1n);
212 });
213
214 it("[[Call]] works with numbers", () => {
215 assertStrictEquals(toIntN(2, 7), -1);
216 });
217
218 it("[[Call]] works with non‐integers", () => {
219 assertStrictEquals(toIntN(2, 7.21), -1);
220 assertStrictEquals(toIntN(2, Infinity), 0);
221 });
222 });
223
224 describe("toIntegralNumber", () => {
225 it("[[Call]] converts nan to zero", () => {
226 assertStrictEquals(toIntegralNumber(NaN), 0);
227 });
228
229 it("[[Call]] converts negative zero to positive zero", () => {
230 assertStrictEquals(toIntegralNumber(-0), 0);
231 });
232
233 it("[[Call]] drops the fractional part of negative numbers", () => {
234 assertStrictEquals(toIntegralNumber(-1.79), -1);
235 });
236
237 it("[[Call]] returns zero for infinity", () => {
238 assertStrictEquals(toIntegralNumber(Infinity), 0);
239 });
240
241 it("[[Call]] returns zero for negative infinity", () => {
242 assertStrictEquals(toIntegralNumber(-Infinity), 0);
243 });
244
245 it("[[Call]] works with big·ints", () => {
246 assertStrictEquals(toIntegralNumber(2n), 2);
247 });
248 });
249
250 describe("toIntegralNumberOrInfinity", () => {
251 it("[[Call]] converts nan to zero", () => {
252 assertStrictEquals(toIntegralNumberOrInfinity(NaN), 0);
253 });
254
255 it("[[Call]] converts negative zero to positive zero", () => {
256 assertStrictEquals(toIntegralNumberOrInfinity(-0), 0);
257 });
258
259 it("[[Call]] drops the fractional part of negative numbers", () => {
260 assertStrictEquals(toIntegralNumberOrInfinity(-1.79), -1);
261 });
262
263 it("[[Call]] returns infinity for infinity", () => {
264 assertStrictEquals(toIntegralNumberOrInfinity(Infinity), Infinity);
265 });
266
267 it("[[Call]] returns negative infinity for negative infinity", () => {
268 assertStrictEquals(
269 toIntegralNumberOrInfinity(-Infinity),
270 -Infinity,
271 );
272 });
273
274 it("[[Call]] works with big·ints", () => {
275 assertStrictEquals(toIntegralNumberOrInfinity(2n), 2);
276 });
277 });
278
279 describe("toNumber", () => {
280 it("[[Call]] converts to a number", () => {
281 assertStrictEquals(toNumber(2n), 2);
282 });
283 });
284
285 describe("toNumeric", () => {
286 it("[[Call]] returns a big·int argument", () => {
287 assertStrictEquals(toNumeric(231n), 231n);
288 });
289
290 it("[[Call]] converts to a numeric", () => {
291 assertStrictEquals(toNumeric("231"), 231);
292 });
293
294 it("[[Call]] prefers `valueOf`", () => {
295 assertStrictEquals(
296 toNumeric({
297 toString() {
298 return 0;
299 },
300 valueOf() {
301 return 231n;
302 },
303 }),
304 231n,
305 );
306 });
307 });
308
309 describe("toUintN", () => {
310 it("[[Call]] converts to an int·n", () => {
311 assertStrictEquals(toUintN(2, 7n), 3n);
312 });
313
314 it("[[Call]] works with numbers", () => {
315 assertStrictEquals(toUintN(2, 7), 3);
316 });
317
318 it("[[Call]] works with non‐integers", () => {
319 assertStrictEquals(toUintN(2, 7.21), 3);
320 assertStrictEquals(toUintN(2, Infinity), 0);
321 });
322 });
This page took 0.094869 seconds and 5 git commands to generate.