]> Lady’s Gitweb - Pisces/blob - numeric.test.js
23dbd892af8fff7ad01ea4c4c6bde141188e0155
[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 toFloat32,
27 toIntegralNumber,
28 toIntegralNumberOrInfinity,
29 toIntN,
30 toNumber,
31 toNumeric,
32 toUintN,
33 } from "./numeric.js";
34
35 describe("NEGATIVE_ZERO", () => {
36 it("[[Get]] is negative zero", () => {
37 assertStrictEquals(NEGATIVE_ZERO, -0);
38 });
39 });
40
41 describe("POSITIVE_ZERO", () => {
42 it("[[Get]] is positive zero", () => {
43 assertStrictEquals(POSITIVE_ZERO, 0);
44 });
45 });
46
47 describe("abs", () => {
48 it("[[Call]] returns the absolute value", () => {
49 assertStrictEquals(abs(-1), 1);
50 });
51
52 it("[[Call]] works with big·ints", () => {
53 const bn = BigInt(Number.MAX_SAFE_INTEGER) + 2n;
54 assertStrictEquals(abs(-bn), bn);
55 });
56 });
57
58 describe("atan2", () => {
59 it("[[Call]] returns the atan2", () => {
60 assertStrictEquals(atan2(6, 9), Math.atan2(6, 9));
61 });
62
63 it("[[Call]] works with big·ints", () => {
64 assertStrictEquals(atan2(6n, 9n), Math.atan2(6, 9));
65 });
66 });
67
68 describe("clz32", () => {
69 it("[[Call]] returns the clz32", () => {
70 assertStrictEquals(clz32(1 << 28), 3);
71 });
72
73 it("[[Call]] works with big·ints", () => {
74 assertStrictEquals(clz32(1n << 28n), 3);
75 });
76 });
77
78 describe("max", () => {
79 it("[[Call]] returns the largest number", () => {
80 assertStrictEquals(max(1, -6, 92, -Infinity, 0), 92);
81 });
82
83 it("[[Call]] returns the largest big·int", () => {
84 assertStrictEquals(max(1n, -6n, 92n, 0n), 92n);
85 });
86
87 it("[[Call]] returns nan if any argument is nan", () => {
88 assertStrictEquals(max(0, NaN, 1), NaN);
89 });
90
91 it("[[Call]] returns -Infinity when called with no arguments", () => {
92 assertStrictEquals(max(), -Infinity);
93 });
94
95 it("[[Call]] throws if both big·int and number arguments are provided", () => {
96 assertThrows(() => max(-Infinity, 0n));
97 });
98 });
99
100 describe("min", () => {
101 it("[[Call]] returns the largest number", () => {
102 assertStrictEquals(min(1, -6, 92, Infinity, 0), -6);
103 });
104
105 it("[[Call]] returns the largest big·int", () => {
106 assertStrictEquals(min(1n, -6n, 92n, 0n), -6n);
107 });
108
109 it("[[Call]] returns nan if any argument is nan", () => {
110 assertStrictEquals(min(0, NaN, 1), NaN);
111 });
112
113 it("[[Call]] returns Infinity when called with no arguments", () => {
114 assertStrictEquals(min(), Infinity);
115 });
116
117 it("[[Call]] throws if both big·int and number arguments are provided", () => {
118 assertThrows(() => min(Infinity, 0n));
119 });
120 });
121
122 describe("sgn", () => {
123 it("[[Call]] returns the sign", () => {
124 assertStrictEquals(sgn(Infinity), 1);
125 assertStrictEquals(sgn(-Infinity), -1);
126 assertStrictEquals(sgn(0), 0);
127 assertStrictEquals(sgn(-0), -0);
128 assertStrictEquals(sgn(NaN), NaN);
129 });
130
131 it("[[Call]] works with big·ints", () => {
132 assertStrictEquals(sgn(0n), 0n);
133 assertStrictEquals(sgn(92n), 1n);
134 assertStrictEquals(sgn(-92n), -1n);
135 });
136 });
137
138 describe("toBigInt", () => {
139 it("[[Call]] converts to a big·int", () => {
140 assertStrictEquals(toBigInt(2), 2n);
141 });
142 });
143
144 describe("toFloat32", () => {
145 it("[[Call]] returns the 32‐bit floating‐point representation", () => {
146 assertStrictEquals(
147 toFloat32(562949953421313),
148 Math.fround(562949953421313),
149 );
150 });
151
152 it("[[Call]] works with big·ints", () => {
153 assertStrictEquals(
154 toFloat32(562949953421313n),
155 Math.fround(562949953421313),
156 );
157 });
158 });
159
160 describe("toIntN", () => {
161 it("[[Call]] converts to an int·n", () => {
162 assertStrictEquals(toIntN(2, 7n), -1n);
163 });
164
165 it("[[Call]] works with numbers", () => {
166 assertStrictEquals(toIntN(2, 7), -1);
167 });
168
169 it("[[Call]] works with non‐integers", () => {
170 assertStrictEquals(toIntN(2, 7.21), -1);
171 assertStrictEquals(toIntN(2, Infinity), 0);
172 });
173 });
174
175 describe("toIntegralNumber", () => {
176 it("[[Call]] converts nan to zero", () => {
177 assertStrictEquals(toIntegralNumber(NaN), 0);
178 });
179
180 it("[[Call]] converts negative zero to positive zero", () => {
181 assertStrictEquals(toIntegralNumber(-0), 0);
182 });
183
184 it("[[Call]] drops the fractional part of negative numbers", () => {
185 assertStrictEquals(toIntegralNumber(-1.79), -1);
186 });
187
188 it("[[Call]] returns zero for infinity", () => {
189 assertStrictEquals(toIntegralNumber(Infinity), 0);
190 });
191
192 it("[[Call]] returns zero for negative infinity", () => {
193 assertStrictEquals(toIntegralNumber(-Infinity), 0);
194 });
195
196 it("[[Call]] works with big·ints", () => {
197 assertStrictEquals(toIntegralNumber(2n), 2);
198 });
199 });
200
201 describe("toIntegralNumberOrInfinity", () => {
202 it("[[Call]] converts nan to zero", () => {
203 assertStrictEquals(toIntegralNumberOrInfinity(NaN), 0);
204 });
205
206 it("[[Call]] converts negative zero to positive zero", () => {
207 assertStrictEquals(toIntegralNumberOrInfinity(-0), 0);
208 });
209
210 it("[[Call]] drops the fractional part of negative numbers", () => {
211 assertStrictEquals(toIntegralNumberOrInfinity(-1.79), -1);
212 });
213
214 it("[[Call]] returns infinity for infinity", () => {
215 assertStrictEquals(toIntegralNumberOrInfinity(Infinity), Infinity);
216 });
217
218 it("[[Call]] returns negative infinity for negative infinity", () => {
219 assertStrictEquals(
220 toIntegralNumberOrInfinity(-Infinity),
221 -Infinity,
222 );
223 });
224
225 it("[[Call]] works with big·ints", () => {
226 assertStrictEquals(toIntegralNumberOrInfinity(2n), 2);
227 });
228 });
229
230 describe("toNumber", () => {
231 it("[[Call]] converts to a number", () => {
232 assertStrictEquals(toNumber(2n), 2);
233 });
234 });
235
236 describe("toNumeric", () => {
237 it("[[Call]] returns a big·int argument", () => {
238 assertStrictEquals(toNumeric(231n), 231n);
239 });
240
241 it("[[Call]] converts to a numeric", () => {
242 assertStrictEquals(toNumeric("231"), 231);
243 });
244
245 it("[[Call]] prefers `valueOf`", () => {
246 assertStrictEquals(
247 toNumeric({
248 toString() {
249 return 0;
250 },
251 valueOf() {
252 return 231n;
253 },
254 }),
255 231n,
256 );
257 });
258 });
259
260 describe("toUintN", () => {
261 it("[[Call]] converts to an int·n", () => {
262 assertStrictEquals(toUintN(2, 7n), 3n);
263 });
264
265 it("[[Call]] works with numbers", () => {
266 assertStrictEquals(toUintN(2, 7), 3);
267 });
268
269 it("[[Call]] works with non‐integers", () => {
270 assertStrictEquals(toUintN(2, 7.21), 3);
271 assertStrictEquals(toUintN(2, Infinity), 0);
272 });
273 });
This page took 0.122757 seconds and 3 git commands to generate.