]> Lady’s Gitweb - Pisces/blob - numeric.test.js
Add toIntegralNumber to numeric.js
[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 toIntegerOrInfinity,
28 toIntegralNumber,
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("toIntegerOrInfinity", () => {
176 it("[[Call]] converts nan to zero", () => {
177 assertStrictEquals(toIntegerOrInfinity(NaN), 0);
178 });
179
180 it("[[Call]] converts negative zero to positive zero", () => {
181 assertStrictEquals(toIntegerOrInfinity(-0), 0);
182 });
183
184 it("[[Call]] drops the fractional part of negative numbers", () => {
185 assertStrictEquals(toIntegerOrInfinity(-1.79), -1);
186 });
187
188 it("[[Call]] returns infinity for infinity", () => {
189 assertStrictEquals(toIntegerOrInfinity(Infinity), Infinity);
190 });
191
192 it("[[Call]] returns negative infinity for negative infinity", () => {
193 assertStrictEquals(toIntegerOrInfinity(-Infinity), -Infinity);
194 });
195
196 it("[[Call]] works with big·ints", () => {
197 assertStrictEquals(toIntegerOrInfinity(2n), 2);
198 });
199 });
200
201 describe("toIntegralNumber", () => {
202 it("[[Call]] converts nan to zero", () => {
203 assertStrictEquals(toIntegralNumber(NaN), 0);
204 });
205
206 it("[[Call]] converts negative zero to positive zero", () => {
207 assertStrictEquals(toIntegralNumber(-0), 0);
208 });
209
210 it("[[Call]] drops the fractional part of negative numbers", () => {
211 assertStrictEquals(toIntegralNumber(-1.79), -1);
212 });
213
214 it("[[Call]] returns zero for infinity", () => {
215 assertStrictEquals(toIntegralNumber(Infinity), 0);
216 });
217
218 it("[[Call]] returns zero for negative infinity", () => {
219 assertStrictEquals(toIntegralNumber(-Infinity), 0);
220 });
221
222 it("[[Call]] works with big·ints", () => {
223 assertStrictEquals(toIntegralNumber(2n), 2);
224 });
225 });
226
227 describe("toNumber", () => {
228 it("[[Call]] converts to a number", () => {
229 assertStrictEquals(toNumber(2n), 2);
230 });
231 });
232
233 describe("toNumeric", () => {
234 it("[[Call]] returns a big·int argument", () => {
235 assertStrictEquals(toNumeric(231n), 231n);
236 });
237
238 it("[[Call]] converts to a numeric", () => {
239 assertStrictEquals(toNumeric("231"), 231);
240 });
241
242 it("[[Call]] prefers `valueOf`", () => {
243 assertStrictEquals(
244 toNumeric({
245 toString() {
246 return 0;
247 },
248 valueOf() {
249 return 231n;
250 },
251 }),
252 231n,
253 );
254 });
255 });
256
257 describe("toUintN", () => {
258 it("[[Call]] converts to an int·n", () => {
259 assertStrictEquals(toUintN(2, 7n), 3n);
260 });
261
262 it("[[Call]] works with numbers", () => {
263 assertStrictEquals(toUintN(2, 7), 3);
264 });
265
266 it("[[Call]] works with non‐integers", () => {
267 assertStrictEquals(toUintN(2, 7.21), 3);
268 assertStrictEquals(toUintN(2, Infinity), 0);
269 });
270 });
This page took 0.244181 seconds and 5 git commands to generate.