1 // ♓🌟 Piscēs ∷ numeric.test.js
2 // ====================================================================
4 // Copyright © 2022 Lady [@ Lady’s Computer].
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/>.
15 } from "./dev-deps.js";
26 toExponentialNotation
,
27 toFixedDecimalNotation
,
30 toIntegralNumberOrInfinity
,
35 } from "./numeric.js";
37 describe("NEGATIVE_ZERO", () => {
38 it("[[Get]] is negative zero", () => {
39 assertStrictEquals(NEGATIVE_ZERO
, -0);
43 describe("POSITIVE_ZERO", () => {
44 it("[[Get]] is positive zero", () => {
45 assertStrictEquals(POSITIVE_ZERO
, 0);
49 describe("abs", () => {
50 it("[[Call]] returns the absolute value", () => {
51 assertStrictEquals(abs(-1), 1);
54 it("[[Call]] works with big·ints", () => {
55 const bn
= BigInt(Number
.MAX_SAFE_INTEGER
) + 2n
;
56 assertStrictEquals(abs(-bn
), bn
);
60 describe("atan2", () => {
61 it("[[Call]] returns the atan2", () => {
62 assertStrictEquals(atan2(6, 9), Math
.atan2(6, 9));
65 it("[[Call]] works with big·ints", () => {
66 assertStrictEquals(atan2(6n
, 9n
), Math
.atan2(6, 9));
70 describe("clz32", () => {
71 it("[[Call]] returns the clz32", () => {
72 assertStrictEquals(clz32(1 << 28), 3);
75 it("[[Call]] works with big·ints", () => {
76 assertStrictEquals(clz32(1n
<< 28n
), 3);
80 describe("max", () => {
81 it("[[Call]] returns the largest number", () => {
82 assertStrictEquals(max(1, -6, 92, -Infinity
, 0), 92);
85 it("[[Call]] returns the largest big·int", () => {
86 assertStrictEquals(max(1n
, -6n
, 92n
, 0n
), 92n
);
89 it("[[Call]] returns nan if any argument is nan", () => {
90 assertStrictEquals(max(0, NaN
, 1), NaN
);
93 it("[[Call]] returns -Infinity when called with no arguments", () => {
94 assertStrictEquals(max(), -Infinity
);
97 it("[[Call]] throws if both big·int and number arguments are provided", () => {
98 assertThrows(() => max(-Infinity
, 0n
));
102 describe("min", () => {
103 it("[[Call]] returns the largest number", () => {
104 assertStrictEquals(min(1, -6, 92, Infinity
, 0), -6);
107 it("[[Call]] returns the largest big·int", () => {
108 assertStrictEquals(min(1n
, -6n
, 92n
, 0n
), -6n
);
111 it("[[Call]] returns nan if any argument is nan", () => {
112 assertStrictEquals(min(0, NaN
, 1), NaN
);
115 it("[[Call]] returns Infinity when called with no arguments", () => {
116 assertStrictEquals(min(), Infinity
);
119 it("[[Call]] throws if both big·int and number arguments are provided", () => {
120 assertThrows(() => min(Infinity
, 0n
));
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
);
133 it("[[Call]] works with big·ints", () => {
134 assertStrictEquals(sgn(0n
), 0n
);
135 assertStrictEquals(sgn(92n
), 1n
);
136 assertStrictEquals(sgn(-92n
), -1n
);
140 describe("toBigInt", () => {
141 it("[[Call]] converts to a big·int", () => {
142 assertStrictEquals(toBigInt(2), 2n
);
146 describe("toExponentialNotation", () => {
147 it("[[Call]] converts to exponential notation", () => {
148 assertStrictEquals(toExponentialNotation(231), "2.31e+2");
151 it("[[Call]] works with big·ints", () => {
153 toExponentialNotation(9007199254740993n
),
154 "9.007199254740993e+15",
158 it("[[Call]] respects the specified number of fractional digits", () => {
160 toExponentialNotation(.00000000642, 3),
163 assertStrictEquals(toExponentialNotation(.00691, 1), "6.9e-3");
164 assertStrictEquals(toExponentialNotation(.00685, 1), "6.9e-3");
165 assertStrictEquals(toExponentialNotation(.004199, 2), "4.20e-3");
167 toExponentialNotation(6420000000n
, 3),
170 assertStrictEquals(toExponentialNotation(6910n
, 1), "6.9e+3");
171 assertStrictEquals(toExponentialNotation(6850n
, 1), "6.9e+3");
172 assertStrictEquals(toExponentialNotation(4199n
, 2), "4.20e+3");
176 describe("toFixedDecimalNotation", () => {
177 it("[[Call]] converts to fixed decimal notation", () => {
178 assertStrictEquals(toFixedDecimalNotation(69.4199, 3), "69.420");
181 it("[[Call]] works with big·ints", () => {
183 toFixedDecimalNotation(9007199254740993n
),
187 toFixedDecimalNotation(9007199254740993n
, 2),
188 "9007199254740993.00",
193 describe("toFloat32", () => {
194 it("[[Call]] returns the 32‐bit floating‐point representation", () => {
196 toFloat32(562949953421313),
197 Math
.fround(562949953421313),
201 it("[[Call]] works with big·ints", () => {
203 toFloat32(562949953421313n
),
204 Math
.fround(562949953421313),
209 describe("toIntN", () => {
210 it("[[Call]] converts to an int·n", () => {
211 assertStrictEquals(toIntN(2, 7n
), -1n
);
214 it("[[Call]] works with numbers", () => {
215 assertStrictEquals(toIntN(2, 7), -1);
218 it("[[Call]] works with non‐integers", () => {
219 assertStrictEquals(toIntN(2, 7.21), -1);
220 assertStrictEquals(toIntN(2, Infinity
), 0);
224 describe("toIntegralNumber", () => {
225 it("[[Call]] converts nan to zero", () => {
226 assertStrictEquals(toIntegralNumber(NaN
), 0);
229 it("[[Call]] converts negative zero to positive zero", () => {
230 assertStrictEquals(toIntegralNumber(-0), 0);
233 it("[[Call]] drops the fractional part of negative numbers", () => {
234 assertStrictEquals(toIntegralNumber(-1.79), -1);
237 it("[[Call]] returns zero for infinity", () => {
238 assertStrictEquals(toIntegralNumber(Infinity
), 0);
241 it("[[Call]] returns zero for negative infinity", () => {
242 assertStrictEquals(toIntegralNumber(-Infinity
), 0);
245 it("[[Call]] works with big·ints", () => {
246 assertStrictEquals(toIntegralNumber(2n
), 2);
250 describe("toIntegralNumberOrInfinity", () => {
251 it("[[Call]] converts nan to zero", () => {
252 assertStrictEquals(toIntegralNumberOrInfinity(NaN
), 0);
255 it("[[Call]] converts negative zero to positive zero", () => {
256 assertStrictEquals(toIntegralNumberOrInfinity(-0), 0);
259 it("[[Call]] drops the fractional part of negative numbers", () => {
260 assertStrictEquals(toIntegralNumberOrInfinity(-1.79), -1);
263 it("[[Call]] returns infinity for infinity", () => {
264 assertStrictEquals(toIntegralNumberOrInfinity(Infinity
), Infinity
);
267 it("[[Call]] returns negative infinity for negative infinity", () => {
269 toIntegralNumberOrInfinity(-Infinity
),
274 it("[[Call]] works with big·ints", () => {
275 assertStrictEquals(toIntegralNumberOrInfinity(2n
), 2);
279 describe("toNumber", () => {
280 it("[[Call]] converts to a number", () => {
281 assertStrictEquals(toNumber(2n
), 2);
285 describe("toNumeric", () => {
286 it("[[Call]] returns a big·int argument", () => {
287 assertStrictEquals(toNumeric(231n
), 231n
);
290 it("[[Call]] converts to a numeric", () => {
291 assertStrictEquals(toNumeric("231"), 231);
294 it("[[Call]] prefers `valueOf`", () => {
309 describe("toUintN", () => {
310 it("[[Call]] converts to an int·n", () => {
311 assertStrictEquals(toUintN(2, 7n
), 3n
);
314 it("[[Call]] works with numbers", () => {
315 assertStrictEquals(toUintN(2, 7), 3);
318 it("[[Call]] works with non‐integers", () => {
319 assertStrictEquals(toUintN(2, 7.21), 3);
320 assertStrictEquals(toUintN(2, Infinity
), 0);