]> Lady’s Gitweb - Pisces/blob - numeric.test.js
Add numeric functions and unit tests
[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 sgn,
23 toBigInt,
24 toFloat32,
25 toIntN,
26 toNumber,
27 toNumeric,
28 toUintN,
29 } from "./numeric.js";
30
31 describe("abs", () => {
32 it("[[Call]] returns the absolute value", () => {
33 assertStrictEquals(abs(-1), 1);
34 });
35
36 it("[[Call]] works with big·ints", () => {
37 const bn = BigInt(Number.MAX_SAFE_INTEGER) + 2n;
38 assertStrictEquals(abs(-bn), bn);
39 });
40 });
41
42 describe("atan2", () => {
43 it("[[Call]] returns the atan2", () => {
44 assertStrictEquals(atan2(6, 9), Math.atan2(6, 9));
45 });
46
47 it("[[Call]] works with big·ints", () => {
48 assertStrictEquals(atan2(6n, 9n), Math.atan2(6, 9));
49 });
50 });
51
52 describe("clz32", () => {
53 it("[[Call]] returns the clz32", () => {
54 assertStrictEquals(clz32(1 << 28), 3);
55 });
56
57 it("[[Call]] works with big·ints", () => {
58 assertStrictEquals(clz32(1n << 28n), 3);
59 });
60 });
61
62 describe("max", () => {
63 it("[[Call]] returns the largest number", () => {
64 assertStrictEquals(max(1, -6, 92, -Infinity, 0), 92);
65 });
66
67 it("[[Call]] returns the largest big·int", () => {
68 assertStrictEquals(max(1n, -6n, 92n, 0n), 92n);
69 });
70
71 it("[[Call]] returns nan if any argument is nan", () => {
72 assertStrictEquals(max(0, NaN, 1), NaN);
73 });
74
75 it("[[Call]] returns -Infinity when called with no arguments", () => {
76 assertStrictEquals(max(), -Infinity);
77 });
78
79 it("[[Call]] throws if both big·int and number arguments are provided", () => {
80 assertThrows(() => max(-Infinity, 0n));
81 });
82 });
83
84 describe("min", () => {
85 it("[[Call]] returns the largest number", () => {
86 assertStrictEquals(min(1, -6, 92, Infinity, 0), -6);
87 });
88
89 it("[[Call]] returns the largest big·int", () => {
90 assertStrictEquals(min(1n, -6n, 92n, 0n), -6n);
91 });
92
93 it("[[Call]] returns nan if any argument is nan", () => {
94 assertStrictEquals(min(0, NaN, 1), NaN);
95 });
96
97 it("[[Call]] returns Infinity when called with no arguments", () => {
98 assertStrictEquals(min(), Infinity);
99 });
100
101 it("[[Call]] throws if both big·int and number arguments are provided", () => {
102 assertThrows(() => min(Infinity, 0n));
103 });
104 });
105
106 describe("sgn", () => {
107 it("[[Call]] returns the sign", () => {
108 assertStrictEquals(sgn(Infinity), 1);
109 assertStrictEquals(sgn(-Infinity), -1);
110 assertStrictEquals(sgn(0), 0);
111 assertStrictEquals(sgn(-0), -0);
112 assertStrictEquals(sgn(NaN), NaN);
113 });
114
115 it("[[Call]] works with big·ints", () => {
116 assertStrictEquals(sgn(0n), 0n);
117 assertStrictEquals(sgn(92n), 1n);
118 assertStrictEquals(sgn(-92n), -1n);
119 });
120 });
121
122 describe("toBigInt", () => {
123 it("[[Call]] converts to a big·int", () => {
124 assertStrictEquals(toBigInt(2), 2n);
125 });
126 });
127
128 describe("toFloat32", () => {
129 it("[[Call]] returns the 32‐bit floating‐point representation", () => {
130 assertStrictEquals(
131 toFloat32(562949953421313),
132 Math.fround(562949953421313),
133 );
134 });
135
136 it("[[Call]] works with big·ints", () => {
137 assertStrictEquals(
138 toFloat32(562949953421313n),
139 Math.fround(562949953421313),
140 );
141 });
142 });
143
144 describe("toIntN", () => {
145 it("[[Call]] converts to an int·n", () => {
146 assertStrictEquals(toIntN(2, 7n), -1n);
147 });
148
149 it("[[Call]] works with numbers", () => {
150 assertStrictEquals(toIntN(2, 7), -1);
151 });
152 });
153
154 describe("toNumber", () => {
155 it("[[Call]] converts to a number", () => {
156 assertStrictEquals(toNumber(2n), 2);
157 });
158 });
159
160 describe("toNumeric", () => {
161 it("[[Call]] returns a big·int argument", () => {
162 assertStrictEquals(toNumeric(231n), 231n);
163 });
164
165 it("[[Call]] converts to a numeric", () => {
166 assertStrictEquals(toNumeric("231"), 231);
167 });
168
169 it("[[Call]] prefers `valueOf`", () => {
170 assertStrictEquals(
171 toNumeric({
172 toString() {
173 return 0;
174 },
175 valueOf() {
176 return 231n;
177 },
178 }),
179 231n,
180 );
181 });
182 });
183
184 describe("toUintN", () => {
185 it("[[Call]] converts to an int·n", () => {
186 assertStrictEquals(toUintN(2, 7n), 3n);
187 });
188
189 it("[[Call]] works with numbers", () => {
190 assertStrictEquals(toUintN(2, 7), 3);
191 });
192 });
This page took 0.44679 seconds and 5 git commands to generate.