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