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