]> Lady’s Gitweb - Pisces/blob - numeric.test.js
Add toIntegerOrInfinity 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 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
155 describe("toIntegerOrInfinity", () => {
156 it("[[Call]] converts nan to zero", () => {
157 assertStrictEquals(toIntegerOrInfinity(NaN), 0);
158 });
159
160 it("[[Call]] converts negative zero to positive zero", () => {
161 assertStrictEquals(toIntegerOrInfinity(-0), 0);
162 });
163
164 it("[[Call]] drops the fractional part of negative numbers", () => {
165 assertStrictEquals(toIntegerOrInfinity(-1.79), -1);
166 });
167
168 it("[[Call]] returns infinity for infinity", () => {
169 assertStrictEquals(toIntegerOrInfinity(Infinity), Infinity);
170 });
171
172 it("[[Call]] returns negative infinity for negative infinity", () => {
173 assertStrictEquals(toIntegerOrInfinity(Infinity), Infinity);
174 });
175 });
176
177 describe("toNumber", () => {
178 it("[[Call]] converts to a number", () => {
179 assertStrictEquals(toNumber(2n), 2);
180 });
181 });
182
183 describe("toNumeric", () => {
184 it("[[Call]] returns a big·int argument", () => {
185 assertStrictEquals(toNumeric(231n), 231n);
186 });
187
188 it("[[Call]] converts to a numeric", () => {
189 assertStrictEquals(toNumeric("231"), 231);
190 });
191
192 it("[[Call]] prefers `valueOf`", () => {
193 assertStrictEquals(
194 toNumeric({
195 toString() {
196 return 0;
197 },
198 valueOf() {
199 return 231n;
200 },
201 }),
202 231n,
203 );
204 });
205 });
206
207 describe("toUintN", () => {
208 it("[[Call]] converts to an int·n", () => {
209 assertStrictEquals(toUintN(2, 7n), 3n);
210 });
211
212 it("[[Call]] works with numbers", () => {
213 assertStrictEquals(toUintN(2, 7), 3);
214 });
215 });
This page took 0.16612 seconds and 5 git commands to generate.