]> Lady’s Gitweb - Pisces/blob - numeric.test.js
Minor refactors to numeric.js
[Pisces] / numeric.test.js
1 // SPDX-FileCopyrightText: 2022, 2023, 2025 Lady <https://www.ladys.computer/about/#lady>
2 // SPDX-License-Identifier: MPL-2.0
3 /**
4 * ⁌ ♓🧩 Piscēs ∷ numeric.test.js
5 *
6 * Copyright © 2022–2023, 2025 Lady [@ Ladys Computer].
7 *
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
11 */
12
13 import {
14 assert,
15 assertStrictEquals,
16 assertThrows,
17 describe,
18 it,
19 } from "./dev-deps.js";
20 import {
21 abs,
22 arccos,
23 arccosh,
24 arcsin,
25 arcsinh,
26 arctan,
27 arctan2,
28 arctanh,
29 cbrt,
30 ceil,
31 clz32,
32 cos,
33 cosh,
34 exp,
35 expm1,
36 floor,
37 hypot,
38 isFiniteNumber,
39 isIntegralNumber,
40 isNan,
41 isSafeIntegralNumber,
42 ln,
43 ln1p,
44 log10,
45 log2,
46 max,
47 min,
48 rand,
49 round,
50 sgn,
51 sin,
52 sinh,
53 sqrt,
54 tan,
55 tanh,
56 toBigInt,
57 toExponentialNotation,
58 toFixedDecimalNotation,
59 toFloat32,
60 toIntegralNumber,
61 toIntegralNumberOrInfinity,
62 toNumber,
63 toNumeric,
64 toSignedIntegralNumeric,
65 toUnsignedIntegralNumeric,
66 trunc,
67 } from "./numeric.js";
68
69 describe("abs", () => {
70 it("[[Call]] returns the absolute value", () => {
71 assertStrictEquals(abs(-1), 1);
72 });
73
74 it("[[Call]] works with big·ints", () => {
75 const bn = BigInt(Number.MAX_SAFE_INTEGER) + 2n;
76 assertStrictEquals(abs(-bn), bn);
77 });
78
79 it("[[Construct]] throws an error", () => {
80 assertThrows(() => new abs(0));
81 });
82
83 describe(".length", () => {
84 it("[[Get]] returns the correct length", () => {
85 assertStrictEquals(abs.length, 1);
86 });
87 });
88
89 describe(".name", () => {
90 it("[[Get]] returns the correct name", () => {
91 assertStrictEquals(abs.name, "abs");
92 });
93 });
94 });
95
96 describe("arccos", () => {
97 it("[[Call]] returns the arccos", () => {
98 assertStrictEquals(arccos(1), 0);
99 assertStrictEquals(arccos(0), Math.PI / 2);
100 });
101
102 it("[[Call]] throws with big·ints", () => {
103 assertThrows(() => arccos(1n));
104 });
105
106 it("[[Construct]] throws an error", () => {
107 assertThrows(() => new arccos(1));
108 });
109
110 describe(".length", () => {
111 it("[[Get]] returns the correct length", () => {
112 assertStrictEquals(arccos.length, 1);
113 });
114 });
115
116 describe(".name", () => {
117 it("[[Get]] returns the correct name", () => {
118 assertStrictEquals(arccos.name, "arccos");
119 });
120 });
121 });
122
123 describe("arccosh", () => {
124 it("[[Call]] returns the arccosh", () => {
125 assertStrictEquals(arccosh(1), 0);
126 assertStrictEquals(arccosh(0), NaN);
127 });
128
129 it("[[Call]] throws with big·ints", () => {
130 assertThrows(() => arccosh(1n));
131 });
132
133 it("[[Construct]] throws an error", () => {
134 assertThrows(() => new arccosh(1));
135 });
136
137 describe(".length", () => {
138 it("[[Get]] returns the correct length", () => {
139 assertStrictEquals(arccosh.length, 1);
140 });
141 });
142
143 describe(".name", () => {
144 it("[[Get]] returns the correct name", () => {
145 assertStrictEquals(arccosh.name, "arccosh");
146 });
147 });
148 });
149
150 describe("arcsin", () => {
151 it("[[Call]] returns the arcsin", () => {
152 assertStrictEquals(arcsin(1), Math.PI / 2);
153 assertStrictEquals(arcsin(0), 0);
154 });
155
156 it("[[Call]] throws with big·ints", () => {
157 assertThrows(() => arcsin(1n));
158 });
159
160 it("[[Construct]] throws an error", () => {
161 assertThrows(() => new arcsin(1));
162 });
163
164 describe(".length", () => {
165 it("[[Get]] returns the correct length", () => {
166 assertStrictEquals(arcsin.length, 1);
167 });
168 });
169
170 describe(".name", () => {
171 it("[[Get]] returns the correct name", () => {
172 assertStrictEquals(arcsin.name, "arcsin");
173 });
174 });
175 });
176
177 describe("arcsinh", () => {
178 it("[[Call]] returns the arcsinh", () => {
179 assertStrictEquals(arcsinh(1), Math.asinh(1));
180 assertStrictEquals(arcsinh(0), 0);
181 });
182
183 it("[[Call]] throws with big·ints", () => {
184 assertThrows(() => arcsinh(1n));
185 });
186
187 it("[[Construct]] throws an error", () => {
188 assertThrows(() => new arcsinh(1));
189 });
190
191 describe(".length", () => {
192 it("[[Get]] returns the correct length", () => {
193 assertStrictEquals(arcsinh.length, 1);
194 });
195 });
196
197 describe(".name", () => {
198 it("[[Get]] returns the correct name", () => {
199 assertStrictEquals(arcsinh.name, "arcsinh");
200 });
201 });
202 });
203
204 describe("arctan", () => {
205 it("[[Call]] returns the arctan", () => {
206 assertStrictEquals(arctan(1), Math.PI / 4);
207 assertStrictEquals(arctan(0), 0);
208 });
209
210 it("[[Call]] throws with big·ints", () => {
211 assertThrows(() => arctan(1n));
212 });
213
214 it("[[Construct]] throws an error", () => {
215 assertThrows(() => new arctan(1));
216 });
217
218 describe(".length", () => {
219 it("[[Get]] returns the correct length", () => {
220 assertStrictEquals(arctan.length, 1);
221 });
222 });
223
224 describe(".name", () => {
225 it("[[Get]] returns the correct name", () => {
226 assertStrictEquals(arctan.name, "arctan");
227 });
228 });
229 });
230
231 describe("arctan2", () => {
232 it("[[Call]] returns the arctan2", () => {
233 assertStrictEquals(arctan2(6, 9), Math.atan2(6, 9));
234 });
235
236 it("[[Call]] works with big·ints", () => {
237 assertStrictEquals(arctan2(6n, 9n), Math.atan2(6, 9));
238 });
239
240 it("[[Construct]] throws an error", () => {
241 assertThrows(() => new arctan2(1, 0));
242 });
243
244 describe(".length", () => {
245 it("[[Get]] returns the correct length", () => {
246 assertStrictEquals(arctan2.length, 2);
247 });
248 });
249
250 describe(".name", () => {
251 it("[[Get]] returns the correct name", () => {
252 assertStrictEquals(arctan2.name, "arctan2");
253 });
254 });
255 });
256
257 describe("arctanh", () => {
258 it("[[Call]] returns the arctanh", () => {
259 assertStrictEquals(arctanh(1), Infinity);
260 assertStrictEquals(arctanh(0), 0);
261 });
262
263 it("[[Call]] throws with big·ints", () => {
264 assertThrows(() => arctanh(1n));
265 });
266
267 it("[[Construct]] throws an error", () => {
268 assertThrows(() => new arctanh(1));
269 });
270
271 describe(".length", () => {
272 it("[[Get]] returns the correct length", () => {
273 assertStrictEquals(arctanh.length, 1);
274 });
275 });
276
277 describe(".name", () => {
278 it("[[Get]] returns the correct name", () => {
279 assertStrictEquals(arctanh.name, "arctanh");
280 });
281 });
282 });
283
284 describe("cbrt", () => {
285 it("[[Call]] returns the cbrt", () => {
286 assertStrictEquals(cbrt(1), 1);
287 assertStrictEquals(cbrt(2), Math.cbrt(2));
288 });
289
290 it("[[Call]] throws with big·ints", () => {
291 assertThrows(() => cbrt(1n));
292 });
293
294 it("[[Construct]] throws an error", () => {
295 assertThrows(() => new cbrt(1));
296 });
297
298 describe(".length", () => {
299 it("[[Get]] returns the correct length", () => {
300 assertStrictEquals(cbrt.length, 1);
301 });
302 });
303
304 describe(".name", () => {
305 it("[[Get]] returns the correct name", () => {
306 assertStrictEquals(cbrt.name, "cbrt");
307 });
308 });
309 });
310
311 describe("ceil", () => {
312 it("[[Call]] returns the ceil", () => {
313 assertStrictEquals(ceil(1), 1);
314 assertStrictEquals(ceil(1.1), 2);
315 });
316
317 it("[[Call]] throws with big·ints", () => {
318 assertThrows(() => ceil(1n));
319 });
320
321 it("[[Construct]] throws an error", () => {
322 assertThrows(() => new ceil(1));
323 });
324
325 describe(".length", () => {
326 it("[[Get]] returns the correct length", () => {
327 assertStrictEquals(ceil.length, 1);
328 });
329 });
330
331 describe(".name", () => {
332 it("[[Get]] returns the correct name", () => {
333 assertStrictEquals(ceil.name, "ceil");
334 });
335 });
336 });
337
338 describe("clz32", () => {
339 it("[[Call]] returns the clz32", () => {
340 assertStrictEquals(clz32(1 << 28), 3);
341 });
342
343 it("[[Call]] works with big·ints", () => {
344 assertStrictEquals(clz32(1n << 28n), 3);
345 });
346
347 it("[[Construct]] throws an error", () => {
348 assertThrows(() => new clz32(1));
349 });
350
351 describe(".length", () => {
352 it("[[Get]] returns the correct length", () => {
353 assertStrictEquals(clz32.length, 1);
354 });
355 });
356
357 describe(".name", () => {
358 it("[[Get]] returns the correct name", () => {
359 assertStrictEquals(clz32.name, "clz32");
360 });
361 });
362 });
363
364 describe("cos", () => {
365 it("[[Call]] returns the cos", () => {
366 assertStrictEquals(cos(1), Math.cos(1));
367 assertStrictEquals(cos(0), 1);
368 });
369
370 it("[[Call]] throws with big·ints", () => {
371 assertThrows(() => cos(1n));
372 });
373
374 it("[[Construct]] throws an error", () => {
375 assertThrows(() => new cos(1));
376 });
377
378 describe(".length", () => {
379 it("[[Get]] returns the correct length", () => {
380 assertStrictEquals(cos.length, 1);
381 });
382 });
383
384 describe(".name", () => {
385 it("[[Get]] returns the correct name", () => {
386 assertStrictEquals(cos.name, "cos");
387 });
388 });
389 });
390
391 describe("cosh", () => {
392 it("[[Call]] returns the cosh", () => {
393 assertStrictEquals(cosh(1), Math.cosh(1));
394 assertStrictEquals(cosh(0), 1);
395 });
396
397 it("[[Call]] throws with big·ints", () => {
398 assertThrows(() => cosh(1n));
399 });
400
401 it("[[Construct]] throws an error", () => {
402 assertThrows(() => new cosh(1));
403 });
404
405 describe(".length", () => {
406 it("[[Get]] returns the correct length", () => {
407 assertStrictEquals(cosh.length, 1);
408 });
409 });
410
411 describe(".name", () => {
412 it("[[Get]] returns the correct name", () => {
413 assertStrictEquals(cosh.name, "cosh");
414 });
415 });
416 });
417
418 describe("exp", () => {
419 it("[[Call]] returns the exp", () => {
420 assertStrictEquals(exp(1), Math.E);
421 assertStrictEquals(exp(0), 1);
422 });
423
424 it("[[Call]] throws with big·ints", () => {
425 assertThrows(() => exp(1n));
426 });
427
428 it("[[Construct]] throws an error", () => {
429 assertThrows(() => new exp(1));
430 });
431
432 describe(".length", () => {
433 it("[[Get]] returns the correct length", () => {
434 assertStrictEquals(exp.length, 1);
435 });
436 });
437
438 describe(".name", () => {
439 it("[[Get]] returns the correct name", () => {
440 assertStrictEquals(exp.name, "exp");
441 });
442 });
443 });
444
445 describe("expm1", () => {
446 it("[[Call]] returns the expm1", () => {
447 assertStrictEquals(expm1(1), Math.E - 1);
448 assertStrictEquals(expm1(0), 0);
449 });
450
451 it("[[Call]] throws with big·ints", () => {
452 assertThrows(() => expm1(1n));
453 });
454
455 it("[[Construct]] throws an error", () => {
456 assertThrows(() => new expm1(1));
457 });
458
459 describe(".length", () => {
460 it("[[Get]] returns the correct length", () => {
461 assertStrictEquals(expm1.length, 1);
462 });
463 });
464
465 describe(".name", () => {
466 it("[[Get]] returns the correct name", () => {
467 assertStrictEquals(expm1.name, "expm1");
468 });
469 });
470 });
471
472 describe("floor", () => {
473 it("[[Call]] returns the floor", () => {
474 assertStrictEquals(floor(1), 1);
475 assertStrictEquals(floor(0.1), 0);
476 });
477
478 it("[[Call]] throws with big·ints", () => {
479 assertThrows(() => floor(1n));
480 });
481
482 it("[[Construct]] throws an error", () => {
483 assertThrows(() => new floor(1));
484 });
485
486 describe(".length", () => {
487 it("[[Get]] returns the correct length", () => {
488 assertStrictEquals(floor.length, 1);
489 });
490 });
491
492 describe(".name", () => {
493 it("[[Get]] returns the correct name", () => {
494 assertStrictEquals(floor.name, "floor");
495 });
496 });
497 });
498
499 describe("hypot", () => {
500 it("[[Call]] returns the floor", () => {
501 assertStrictEquals(hypot(1, 0), 1);
502 assertStrictEquals(hypot(3, 4), 5);
503 });
504
505 it("[[Call]] throws with big·ints", () => {
506 assertThrows(() => hypot(1n, 0n));
507 });
508
509 it("[[Construct]] throws an error", () => {
510 assertThrows(() => new hypot(1, 0));
511 });
512
513 describe(".length", () => {
514 it("[[Get]] returns the correct length", () => {
515 assertStrictEquals(hypot.length, 2);
516 });
517 });
518
519 describe(".name", () => {
520 it("[[Get]] returns the correct name", () => {
521 assertStrictEquals(hypot.name, "hypot");
522 });
523 });
524 });
525
526 describe("isFiniteNumber", () => {
527 it("[[Call]] returns true for finite numbers", () => {
528 assertStrictEquals(isFiniteNumber(1), true);
529 assertStrictEquals(isFiniteNumber(Number.MAX_VALUE), true);
530 assertStrictEquals(isFiniteNumber(Number.EPSILON), true);
531 });
532
533 it("[[Call]] returns false for nonfinite numbers", () => {
534 assertStrictEquals(isFiniteNumber(NaN), false);
535 assertStrictEquals(isFiniteNumber(Infinity), false);
536 });
537
538 it("[[Call]] returns false for nonnumbers", () => {
539 assertStrictEquals(isFiniteNumber(1n), false);
540 assertStrictEquals(isFiniteNumber("1"), false);
541 });
542
543 it("[[Construct]] throws an error", () => {
544 assertThrows(() => new isFiniteNumber(1));
545 });
546
547 describe(".length", () => {
548 it("[[Get]] returns the correct length", () => {
549 assertStrictEquals(isFiniteNumber.length, 1);
550 });
551 });
552
553 describe(".name", () => {
554 it("[[Get]] returns the correct name", () => {
555 assertStrictEquals(isFiniteNumber.name, "isFiniteNumber");
556 });
557 });
558 });
559
560 describe("isIntegralNumber", () => {
561 it("[[Call]] returns true for integral numbers", () => {
562 assertStrictEquals(isIntegralNumber(1), true);
563 assertStrictEquals(isIntegralNumber(Number.MAX_VALUE), true);
564 });
565
566 it("[[Call]] returns false for nonfinite numbers", () => {
567 assertStrictEquals(isIntegralNumber(NaN), false);
568 assertStrictEquals(isIntegralNumber(Infinity), false);
569 });
570
571 it("[[Call]] returns false for nonintegral numbers", () => {
572 assertStrictEquals(isIntegralNumber(.1), false);
573 assertStrictEquals(isIntegralNumber(Number.EPSILON), false);
574 });
575
576 it("[[Call]] returns false for nonnumbers", () => {
577 assertStrictEquals(isIntegralNumber(1n), false);
578 assertStrictEquals(isIntegralNumber("1"), false);
579 });
580
581 it("[[Construct]] throws an error", () => {
582 assertThrows(() => new isIntegralNumber(1));
583 });
584
585 describe(".length", () => {
586 it("[[Get]] returns the correct length", () => {
587 assertStrictEquals(isIntegralNumber.length, 1);
588 });
589 });
590
591 describe(".name", () => {
592 it("[[Get]] returns the correct name", () => {
593 assertStrictEquals(isIntegralNumber.name, "isIntegralNumber");
594 });
595 });
596 });
597
598 describe("isNan", () => {
599 it("[[Call]] returns true for nan", () => {
600 assertStrictEquals(isNan(NaN), true);
601 });
602
603 it("[[Call]] returns false for infinite numbers", () => {
604 assertStrictEquals(isNan(-Infinity), false);
605 assertStrictEquals(isNan(Infinity), false);
606 });
607
608 it("[[Call]] returns false for finite numbers", () => {
609 assertStrictEquals(isNan(1), false);
610 assertStrictEquals(isNan(Number.MAX_VALUE), false);
611 assertStrictEquals(isNan(.1), false);
612 assertStrictEquals(isNan(Number.EPSILON), false);
613 });
614
615 it("[[Call]] returns false for nonnumbers", () => {
616 assertStrictEquals(isNan(1n), false);
617 assertStrictEquals(isNan("NaN"), false);
618 assertStrictEquals(isNan({}), false);
619 assertStrictEquals(isNan(new Date(NaN)), false);
620 });
621
622 it("[[Construct]] throws an error", () => {
623 assertThrows(() => new isNan(1));
624 });
625
626 describe(".length", () => {
627 it("[[Get]] returns the correct length", () => {
628 assertStrictEquals(isNan.length, 1);
629 });
630 });
631
632 describe(".name", () => {
633 it("[[Get]] returns the correct name", () => {
634 assertStrictEquals(isNan.name, "isNan");
635 });
636 });
637 });
638
639 describe("isSafeIntegralNumber", () => {
640 it("[[Call]] returns true for safe integral numbers", () => {
641 assertStrictEquals(isSafeIntegralNumber(1), true);
642 assertStrictEquals(
643 isSafeIntegralNumber(Number.MAX_SAFE_INTEGER),
644 true,
645 );
646 });
647
648 it("[[Call]] returns false for nonfinite numbers", () => {
649 assertStrictEquals(isSafeIntegralNumber(NaN), false);
650 assertStrictEquals(isSafeIntegralNumber(Infinity), false);
651 });
652
653 it("[[Call]] returns false for nonintegral numbers", () => {
654 assertStrictEquals(isSafeIntegralNumber(.1), false);
655 assertStrictEquals(isSafeIntegralNumber(Number.EPSILON), false);
656 });
657 it("[[Call]] returns true for unsafe integral numbers", () => {
658 assertStrictEquals(isSafeIntegralNumber(Number.MAX_VALUE), false);
659 });
660
661 it("[[Call]] returns false for nonnumbers", () => {
662 assertStrictEquals(isSafeIntegralNumber(1n), false);
663 assertStrictEquals(isSafeIntegralNumber("1"), false);
664 });
665
666 it("[[Construct]] throws an error", () => {
667 assertThrows(() => new isSafeIntegralNumber(1));
668 });
669
670 describe(".length", () => {
671 it("[[Get]] returns the correct length", () => {
672 assertStrictEquals(isSafeIntegralNumber.length, 1);
673 });
674 });
675
676 describe(".name", () => {
677 it("[[Get]] returns the correct name", () => {
678 assertStrictEquals(
679 isSafeIntegralNumber.name,
680 "isSafeIntegralNumber",
681 );
682 });
683 });
684 });
685
686 describe("ln", () => {
687 it("[[Call]] returns the ln", () => {
688 assertStrictEquals(ln(1), 0);
689 assertStrictEquals(ln(Math.E), 1);
690 });
691
692 it("[[Call]] throws with big·ints", () => {
693 assertThrows(() => ln(1n));
694 });
695
696 it("[[Construct]] throws an error", () => {
697 assertThrows(() => new ln(1));
698 });
699
700 describe(".length", () => {
701 it("[[Get]] returns the correct length", () => {
702 assertStrictEquals(ln.length, 1);
703 });
704 });
705
706 describe(".name", () => {
707 it("[[Get]] returns the correct name", () => {
708 assertStrictEquals(ln.name, "ln");
709 });
710 });
711 });
712
713 describe("ln1p", () => {
714 it("[[Call]] returns the ln1p", () => {
715 assertStrictEquals(ln1p(1), Math.log1p(1));
716 assertStrictEquals(ln1p(Math.E - 1), 1);
717 });
718
719 it("[[Call]] throws with big·ints", () => {
720 assertThrows(() => ln1p(1n));
721 });
722
723 it("[[Construct]] throws an error", () => {
724 assertThrows(() => new ln1p(1));
725 });
726
727 describe(".length", () => {
728 it("[[Get]] returns the correct length", () => {
729 assertStrictEquals(ln1p.length, 1);
730 });
731 });
732
733 describe(".name", () => {
734 it("[[Get]] returns the correct name", () => {
735 assertStrictEquals(ln1p.name, "ln1p");
736 });
737 });
738 });
739
740 describe("log10", () => {
741 it("[[Call]] returns the log10", () => {
742 assertStrictEquals(log10(1), 0);
743 assertStrictEquals(log10(10), 1);
744 });
745
746 it("[[Call]] throws with big·ints", () => {
747 assertThrows(() => log10(1n));
748 });
749
750 it("[[Construct]] throws an error", () => {
751 assertThrows(() => new log10(1));
752 });
753
754 describe(".length", () => {
755 it("[[Get]] returns the correct length", () => {
756 assertStrictEquals(log10.length, 1);
757 });
758 });
759
760 describe(".name", () => {
761 it("[[Get]] returns the correct name", () => {
762 assertStrictEquals(log10.name, "log10");
763 });
764 });
765 });
766
767 describe("log2", () => {
768 it("[[Call]] returns the log2", () => {
769 assertStrictEquals(log2(1), 0);
770 assertStrictEquals(log2(2), 1);
771 });
772
773 it("[[Call]] throws with big·ints", () => {
774 assertThrows(() => log2(1n));
775 });
776
777 it("[[Construct]] throws an error", () => {
778 assertThrows(() => new log2(1));
779 });
780
781 describe(".length", () => {
782 it("[[Get]] returns the correct length", () => {
783 assertStrictEquals(log2.length, 1);
784 });
785 });
786
787 describe(".name", () => {
788 it("[[Get]] returns the correct name", () => {
789 assertStrictEquals(log2.name, "log2");
790 });
791 });
792 });
793
794 describe("max", () => {
795 it("[[Call]] returns the largest number", () => {
796 assertStrictEquals(max(1, -6, 92, -Infinity, 0), 92);
797 });
798
799 it("[[Call]] returns the largest big·int", () => {
800 assertStrictEquals(max(1n, -6n, 92n, 0n), 92n);
801 });
802
803 it("[[Call]] returns nan if any argument is nan", () => {
804 assertStrictEquals(max(0, NaN, 1), NaN);
805 });
806
807 it("[[Call]] returns -Infinity when called with no arguments", () => {
808 assertStrictEquals(max(), -Infinity);
809 });
810
811 it("[[Call]] throws if both big·int and number arguments are provided", () => {
812 assertThrows(() => max(-Infinity, 0n));
813 });
814
815 it("[[Construct]] throws an error", () => {
816 assertThrows(() => new max(1, 0));
817 });
818
819 describe(".length", () => {
820 it("[[Get]] returns the correct length", () => {
821 assertStrictEquals(max.length, 2);
822 });
823 });
824
825 describe(".name", () => {
826 it("[[Get]] returns the correct name", () => {
827 assertStrictEquals(max.name, "max");
828 });
829 });
830 });
831
832 describe("min", () => {
833 it("[[Call]] returns the largest number", () => {
834 assertStrictEquals(min(1, -6, 92, Infinity, 0), -6);
835 });
836
837 it("[[Call]] returns the largest big·int", () => {
838 assertStrictEquals(min(1n, -6n, 92n, 0n), -6n);
839 });
840
841 it("[[Call]] returns nan if any argument is nan", () => {
842 assertStrictEquals(min(0, NaN, 1), NaN);
843 });
844
845 it("[[Call]] returns Infinity when called with no arguments", () => {
846 assertStrictEquals(min(), Infinity);
847 });
848
849 it("[[Call]] throws if both big·int and number arguments are provided", () => {
850 assertThrows(() => min(Infinity, 0n));
851 });
852
853 it("[[Construct]] throws an error", () => {
854 assertThrows(() => new min(1, 0));
855 });
856
857 describe(".length", () => {
858 it("[[Get]] returns the correct length", () => {
859 assertStrictEquals(min.length, 2);
860 });
861 });
862
863 describe(".name", () => {
864 it("[[Get]] returns the correct name", () => {
865 assertStrictEquals(min.name, "min");
866 });
867 });
868 });
869
870 describe("rand", () => {
871 it("[[Call]] returns a random number between 0 and 1", () => {
872 // Not possible to fully test, obviously.
873 const r = rand();
874 assert(typeof r === "number");
875 assert(r >= 0 && r < 1);
876 });
877
878 it("[[Construct]] throws an error", () => {
879 assertThrows(() => new rand());
880 });
881
882 describe(".length", () => {
883 it("[[Get]] returns the correct length", () => {
884 assertStrictEquals(rand.length, 0);
885 });
886 });
887
888 describe(".name", () => {
889 it("[[Get]] returns the correct name", () => {
890 assertStrictEquals(rand.name, "rand");
891 });
892 });
893 });
894
895 describe("round", () => {
896 it("[[Call]] returns the round", () => {
897 assertStrictEquals(round(1), 1);
898 assertStrictEquals(round(0.5), 1);
899 assertStrictEquals(round(-0.5), -0);
900 });
901
902 it("[[Call]] throws with big·ints", () => {
903 assertThrows(() => round(1n));
904 });
905
906 it("[[Construct]] throws an error", () => {
907 assertThrows(() => new round(1));
908 });
909
910 describe(".length", () => {
911 it("[[Get]] returns the correct length", () => {
912 assertStrictEquals(round.length, 1);
913 });
914 });
915
916 describe(".name", () => {
917 it("[[Get]] returns the correct name", () => {
918 assertStrictEquals(round.name, "round");
919 });
920 });
921 });
922
923 describe("sgn", () => {
924 it("[[Call]] returns the sign", () => {
925 assertStrictEquals(sgn(Infinity), 1);
926 assertStrictEquals(sgn(-Infinity), -1);
927 assertStrictEquals(sgn(0), 0);
928 assertStrictEquals(sgn(-0), -0);
929 assertStrictEquals(sgn(NaN), NaN);
930 });
931
932 it("[[Call]] works with big·ints", () => {
933 assertStrictEquals(sgn(0n), 0n);
934 assertStrictEquals(sgn(92n), 1n);
935 assertStrictEquals(sgn(-92n), -1n);
936 });
937
938 it("[[Construct]] throws an error", () => {
939 assertThrows(() => new sgn(1));
940 });
941
942 describe(".length", () => {
943 it("[[Get]] returns the correct length", () => {
944 assertStrictEquals(sgn.length, 1);
945 });
946 });
947
948 describe(".name", () => {
949 it("[[Get]] returns the correct name", () => {
950 assertStrictEquals(sgn.name, "sgn");
951 });
952 });
953 });
954
955 describe("sin", () => {
956 it("[[Call]] returns the sin", () => {
957 assertStrictEquals(sin(1), Math.sin(1));
958 assertStrictEquals(sin(0), 0);
959 });
960
961 it("[[Call]] throws with big·ints", () => {
962 assertThrows(() => sin(1n));
963 });
964
965 it("[[Construct]] throws an error", () => {
966 assertThrows(() => new sin(1));
967 });
968
969 describe(".length", () => {
970 it("[[Get]] returns the correct length", () => {
971 assertStrictEquals(sin.length, 1);
972 });
973 });
974
975 describe(".name", () => {
976 it("[[Get]] returns the correct name", () => {
977 assertStrictEquals(sin.name, "sin");
978 });
979 });
980 });
981
982 describe("sinh", () => {
983 it("[[Call]] returns the sinh", () => {
984 assertStrictEquals(sinh(1), Math.sinh(1));
985 assertStrictEquals(sinh(0), 0);
986 });
987
988 it("[[Call]] throws with big·ints", () => {
989 assertThrows(() => sinh(1n));
990 });
991
992 it("[[Construct]] throws an error", () => {
993 assertThrows(() => new sinh(1));
994 });
995
996 describe(".length", () => {
997 it("[[Get]] returns the correct length", () => {
998 assertStrictEquals(sinh.length, 1);
999 });
1000 });
1001
1002 describe(".name", () => {
1003 it("[[Get]] returns the correct name", () => {
1004 assertStrictEquals(sinh.name, "sinh");
1005 });
1006 });
1007 });
1008
1009 describe("sqrt", () => {
1010 it("[[Call]] returns the sqrt", () => {
1011 assertStrictEquals(sqrt(1), 1);
1012 assertStrictEquals(sqrt(2), Math.SQRT2);
1013 });
1014
1015 it("[[Call]] throws with big·ints", () => {
1016 assertThrows(() => sqrt(1n));
1017 });
1018
1019 it("[[Construct]] throws an error", () => {
1020 assertThrows(() => new sqrt(1));
1021 });
1022
1023 describe(".length", () => {
1024 it("[[Get]] returns the correct length", () => {
1025 assertStrictEquals(sqrt.length, 1);
1026 });
1027 });
1028
1029 describe(".name", () => {
1030 it("[[Get]] returns the correct name", () => {
1031 assertStrictEquals(sqrt.name, "sqrt");
1032 });
1033 });
1034 });
1035
1036 describe("tan", () => {
1037 it("[[Call]] returns the tan", () => {
1038 assertStrictEquals(tan(1), Math.tan(1));
1039 assertStrictEquals(tan(0), 0);
1040 });
1041
1042 it("[[Call]] throws with big·ints", () => {
1043 assertThrows(() => tan(1n));
1044 });
1045
1046 it("[[Construct]] throws an error", () => {
1047 assertThrows(() => new tan(1));
1048 });
1049
1050 describe(".length", () => {
1051 it("[[Get]] returns the correct length", () => {
1052 assertStrictEquals(tan.length, 1);
1053 });
1054 });
1055
1056 describe(".name", () => {
1057 it("[[Get]] returns the correct name", () => {
1058 assertStrictEquals(tan.name, "tan");
1059 });
1060 });
1061 });
1062
1063 describe("tanh", () => {
1064 it("[[Call]] returns the tanh", () => {
1065 assertStrictEquals(tanh(1), Math.tanh(1));
1066 assertStrictEquals(tanh(0), 0);
1067 });
1068
1069 it("[[Call]] throws with big·ints", () => {
1070 assertThrows(() => tanh(1n));
1071 });
1072
1073 it("[[Construct]] throws an error", () => {
1074 assertThrows(() => new tanh(1));
1075 });
1076
1077 describe(".length", () => {
1078 it("[[Get]] returns the correct length", () => {
1079 assertStrictEquals(tanh.length, 1);
1080 });
1081 });
1082
1083 describe(".name", () => {
1084 it("[[Get]] returns the correct name", () => {
1085 assertStrictEquals(tanh.name, "tanh");
1086 });
1087 });
1088 });
1089
1090 describe("toBigInt", () => {
1091 it("[[Call]] converts to a big·int", () => {
1092 assertStrictEquals(toBigInt(2), 2n);
1093 });
1094
1095 it("[[Construct]] throws an error", () => {
1096 assertThrows(() => new toBigInt(1));
1097 });
1098
1099 describe(".length", () => {
1100 it("[[Get]] returns the correct length", () => {
1101 assertStrictEquals(toBigInt.length, 1);
1102 });
1103 });
1104
1105 describe(".name", () => {
1106 it("[[Get]] returns the correct name", () => {
1107 assertStrictEquals(toBigInt.name, "toBigInt");
1108 });
1109 });
1110 });
1111
1112 describe("toExponentialNotation", () => {
1113 it("[[Call]] converts to exponential notation", () => {
1114 assertStrictEquals(toExponentialNotation(231), "2.31e+2");
1115 });
1116
1117 it("[[Call]] works with big·ints", () => {
1118 assertStrictEquals(
1119 toExponentialNotation(9007199254740993n),
1120 "9.007199254740993e+15",
1121 );
1122 });
1123
1124 it("[[Call]] respects the specified number of fractional digits", () => {
1125 assertStrictEquals(
1126 toExponentialNotation(.00000000642, 3),
1127 "6.420e-9",
1128 );
1129 assertStrictEquals(toExponentialNotation(.00691, 1), "6.9e-3");
1130 assertStrictEquals(toExponentialNotation(.00685, 1), "6.9e-3");
1131 assertStrictEquals(toExponentialNotation(.004199, 2), "4.20e-3");
1132 assertStrictEquals(
1133 toExponentialNotation(6420000000n, 3),
1134 "6.420e+9",
1135 );
1136 assertStrictEquals(toExponentialNotation(6910n, 1), "6.9e+3");
1137 assertStrictEquals(toExponentialNotation(6850n, 1), "6.9e+3");
1138 assertStrictEquals(toExponentialNotation(4199n, 2), "4.20e+3");
1139 });
1140
1141 it("[[Construct]] throws an error", () => {
1142 assertThrows(() => new toExponentialNotation(1, 1));
1143 });
1144
1145 describe(".length", () => {
1146 it("[[Get]] returns the correct length", () => {
1147 assertStrictEquals(toExponentialNotation.length, 2);
1148 });
1149 });
1150
1151 describe(".name", () => {
1152 it("[[Get]] returns the correct name", () => {
1153 assertStrictEquals(
1154 toExponentialNotation.name,
1155 "toExponentialNotation",
1156 );
1157 });
1158 });
1159 });
1160
1161 describe("toFixedDecimalNotation", () => {
1162 it("[[Call]] converts to fixed decimal notation", () => {
1163 assertStrictEquals(toFixedDecimalNotation(69.4199, 3), "69.420");
1164 });
1165
1166 it("[[Call]] works with big·ints", () => {
1167 assertStrictEquals(
1168 toFixedDecimalNotation(9007199254740993n),
1169 "9007199254740993",
1170 );
1171 assertStrictEquals(
1172 toFixedDecimalNotation(9007199254740993n, 2),
1173 "9007199254740993.00",
1174 );
1175 });
1176
1177 it("[[Construct]] throws an error", () => {
1178 assertThrows(() => new toFixedDecimalNotation(1, 1));
1179 });
1180
1181 describe(".length", () => {
1182 it("[[Get]] returns the correct length", () => {
1183 assertStrictEquals(toFixedDecimalNotation.length, 2);
1184 });
1185 });
1186
1187 describe(".name", () => {
1188 it("[[Get]] returns the correct name", () => {
1189 assertStrictEquals(
1190 toFixedDecimalNotation.name,
1191 "toFixedDecimalNotation",
1192 );
1193 });
1194 });
1195 });
1196
1197 describe("toFloat32", () => {
1198 it("[[Call]] returns the 32‐bit floating‐point representation", () => {
1199 assertStrictEquals(
1200 toFloat32(562949953421313),
1201 Math.fround(562949953421313),
1202 );
1203 });
1204
1205 it("[[Call]] works with big·ints", () => {
1206 assertStrictEquals(
1207 toFloat32(562949953421313n),
1208 Math.fround(562949953421313),
1209 );
1210 });
1211
1212 it("[[Construct]] throws an error", () => {
1213 assertThrows(() => new toFloat32(1));
1214 });
1215
1216 describe(".length", () => {
1217 it("[[Get]] returns the correct length", () => {
1218 assertStrictEquals(toFloat32.length, 1);
1219 });
1220 });
1221
1222 describe(".name", () => {
1223 it("[[Get]] returns the correct name", () => {
1224 assertStrictEquals(toFloat32.name, "toFloat32");
1225 });
1226 });
1227 });
1228
1229 describe("toSignedIntegralNumeric", () => {
1230 it("[[Call]] converts to an int·n", () => {
1231 assertStrictEquals(toSignedIntegralNumeric(2, 7n), -1n);
1232 });
1233
1234 it("[[Call]] works with numbers", () => {
1235 assertStrictEquals(toSignedIntegralNumeric(2, 7), -1);
1236 });
1237
1238 it("[[Call]] works with non‐integers", () => {
1239 assertStrictEquals(toSignedIntegralNumeric(2, 7.21), -1);
1240 assertStrictEquals(toSignedIntegralNumeric(2, Infinity), 0);
1241 });
1242
1243 it("[[Construct]] throws an error", () => {
1244 assertThrows(() => new toSignedIntegralNumeric(1, 1));
1245 });
1246
1247 describe(".length", () => {
1248 it("[[Get]] returns the correct length", () => {
1249 assertStrictEquals(toSignedIntegralNumeric.length, 2);
1250 });
1251 });
1252
1253 describe(".name", () => {
1254 it("[[Get]] returns the correct name", () => {
1255 assertStrictEquals(
1256 toSignedIntegralNumeric.name,
1257 "toSignedIntegralNumeric",
1258 );
1259 });
1260 });
1261 });
1262
1263 describe("toIntegralNumber", () => {
1264 it("[[Call]] converts nan to zero", () => {
1265 assertStrictEquals(toIntegralNumber(NaN), 0);
1266 });
1267
1268 it("[[Call]] converts negative zero to positive zero", () => {
1269 assertStrictEquals(toIntegralNumber(-0), 0);
1270 });
1271
1272 it("[[Call]] drops the fractional part of negative numbers", () => {
1273 assertStrictEquals(toIntegralNumber(-1.79), -1);
1274 });
1275
1276 it("[[Call]] returns zero for infinity", () => {
1277 assertStrictEquals(toIntegralNumber(Infinity), 0);
1278 });
1279
1280 it("[[Call]] returns zero for negative infinity", () => {
1281 assertStrictEquals(toIntegralNumber(-Infinity), 0);
1282 });
1283
1284 it("[[Call]] works with big·ints", () => {
1285 assertStrictEquals(toIntegralNumber(2n), 2);
1286 });
1287
1288 it("[[Construct]] throws an error", () => {
1289 assertThrows(() => new toIntegralNumber(1));
1290 });
1291
1292 describe(".length", () => {
1293 it("[[Get]] returns the correct length", () => {
1294 assertStrictEquals(toIntegralNumber.length, 1);
1295 });
1296 });
1297
1298 describe(".name", () => {
1299 it("[[Get]] returns the correct name", () => {
1300 assertStrictEquals(toIntegralNumber.name, "toIntegralNumber");
1301 });
1302 });
1303 });
1304
1305 describe("toIntegralNumberOrInfinity", () => {
1306 it("[[Call]] converts nan to zero", () => {
1307 assertStrictEquals(toIntegralNumberOrInfinity(NaN), 0);
1308 });
1309
1310 it("[[Call]] converts negative zero to positive zero", () => {
1311 assertStrictEquals(toIntegralNumberOrInfinity(-0), 0);
1312 });
1313
1314 it("[[Call]] drops the fractional part of negative numbers", () => {
1315 assertStrictEquals(toIntegralNumberOrInfinity(-1.79), -1);
1316 });
1317
1318 it("[[Call]] returns infinity for infinity", () => {
1319 assertStrictEquals(toIntegralNumberOrInfinity(Infinity), Infinity);
1320 });
1321
1322 it("[[Call]] returns negative infinity for negative infinity", () => {
1323 assertStrictEquals(
1324 toIntegralNumberOrInfinity(-Infinity),
1325 -Infinity,
1326 );
1327 });
1328
1329 it("[[Call]] works with big·ints", () => {
1330 assertStrictEquals(toIntegralNumberOrInfinity(2n), 2);
1331 });
1332
1333 it("[[Construct]] throws an error", () => {
1334 assertThrows(() => new toIntegralNumberOrInfinity(1));
1335 });
1336
1337 describe(".length", () => {
1338 it("[[Get]] returns the correct length", () => {
1339 assertStrictEquals(toIntegralNumberOrInfinity.length, 1);
1340 });
1341 });
1342
1343 describe(".name", () => {
1344 it("[[Get]] returns the correct name", () => {
1345 assertStrictEquals(
1346 toIntegralNumberOrInfinity.name,
1347 "toIntegralNumberOrInfinity",
1348 );
1349 });
1350 });
1351 });
1352
1353 describe("toNumber", () => {
1354 it("[[Call]] converts to a number", () => {
1355 assertStrictEquals(toNumber(2n), 2);
1356 });
1357
1358 it("[[Construct]] throws an error", () => {
1359 assertThrows(() => new toNumber(1));
1360 });
1361
1362 describe(".length", () => {
1363 it("[[Get]] returns the correct length", () => {
1364 assertStrictEquals(toNumber.length, 1);
1365 });
1366 });
1367
1368 describe(".name", () => {
1369 it("[[Get]] returns the correct name", () => {
1370 assertStrictEquals(toNumber.name, "toNumber");
1371 });
1372 });
1373 });
1374
1375 describe("toNumeric", () => {
1376 it("[[Call]] returns a big·int argument", () => {
1377 assertStrictEquals(toNumeric(231n), 231n);
1378 });
1379
1380 it("[[Call]] converts to a numeric", () => {
1381 assertStrictEquals(toNumeric("231"), 231);
1382 });
1383
1384 it("[[Call]] prefers `valueOf`", () => {
1385 assertStrictEquals(
1386 toNumeric({
1387 toString() {
1388 return 0;
1389 },
1390 valueOf() {
1391 return 231n;
1392 },
1393 }),
1394 231n,
1395 );
1396 });
1397
1398 it("[[Construct]] throws an error", () => {
1399 assertThrows(() => new toNumeric(1));
1400 });
1401
1402 describe(".length", () => {
1403 it("[[Get]] returns the correct length", () => {
1404 assertStrictEquals(toNumeric.length, 1);
1405 });
1406 });
1407
1408 describe(".name", () => {
1409 it("[[Get]] returns the correct name", () => {
1410 assertStrictEquals(toNumeric.name, "toNumeric");
1411 });
1412 });
1413 });
1414
1415 describe("toUnsignedIntegralNumeric", () => {
1416 it("[[Call]] converts to an int·n", () => {
1417 assertStrictEquals(toUnsignedIntegralNumeric(2, 7n), 3n);
1418 });
1419
1420 it("[[Call]] works with numbers", () => {
1421 assertStrictEquals(toUnsignedIntegralNumeric(2, 7), 3);
1422 });
1423
1424 it("[[Call]] works with non‐integers", () => {
1425 assertStrictEquals(toUnsignedIntegralNumeric(2, 7.21), 3);
1426 assertStrictEquals(toUnsignedIntegralNumeric(2, Infinity), 0);
1427 });
1428
1429 it("[[Construct]] throws an error", () => {
1430 assertThrows(() => new toUnsignedIntegralNumeric(1, 1));
1431 });
1432
1433 describe(".length", () => {
1434 it("[[Get]] returns the correct length", () => {
1435 assertStrictEquals(toUnsignedIntegralNumeric.length, 2);
1436 });
1437 });
1438
1439 describe(".name", () => {
1440 it("[[Get]] returns the correct name", () => {
1441 assertStrictEquals(
1442 toUnsignedIntegralNumeric.name,
1443 "toUnsignedIntegralNumeric",
1444 );
1445 });
1446 });
1447 });
1448
1449 describe("trunc", () => {
1450 it("[[Call]] returns the trunc", () => {
1451 assertStrictEquals(trunc(1), 1);
1452 assertStrictEquals(trunc(0.5), 0);
1453 assertStrictEquals(trunc(-0.5), -0);
1454 });
1455
1456 it("[[Call]] throws with big·ints", () => {
1457 assertThrows(() => trunc(1n));
1458 });
1459
1460 it("[[Construct]] throws an error", () => {
1461 assertThrows(() => new trunc(1));
1462 });
1463
1464 describe(".length", () => {
1465 it("[[Get]] returns the correct length", () => {
1466 assertStrictEquals(trunc.length, 1);
1467 });
1468 });
1469
1470 describe(".name", () => {
1471 it("[[Get]] returns the correct name", () => {
1472 assertStrictEquals(trunc.name, "trunc");
1473 });
1474 });
1475 });
This page took 1.040931 seconds and 5 git commands to generate.