]> Lady’s Gitweb - Pisces/blobdiff - numeric.test.js
Add numeric functions and unit tests
[Pisces] / numeric.test.js
diff --git a/numeric.test.js b/numeric.test.js
new file mode 100644 (file)
index 0000000..e82e04b
--- /dev/null
@@ -0,0 +1,192 @@
+// ♓🌟 Piscēs ∷ numeric.test.js
+// ====================================================================
+//
+// Copyright © 2022 Lady [@ Lady’s Computer].
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
+
+import {
+  assertStrictEquals,
+  assertThrows,
+  describe,
+  it,
+} from "./dev-deps.js";
+import {
+  abs,
+  atan2,
+  clz32,
+  max,
+  min,
+  sgn,
+  toBigInt,
+  toFloat32,
+  toIntN,
+  toNumber,
+  toNumeric,
+  toUintN,
+} from "./numeric.js";
+
+describe("abs", () => {
+  it("[[Call]] returns the absolute value", () => {
+    assertStrictEquals(abs(-1), 1);
+  });
+
+  it("[[Call]] works with big·ints", () => {
+    const bn = BigInt(Number.MAX_SAFE_INTEGER) + 2n;
+    assertStrictEquals(abs(-bn), bn);
+  });
+});
+
+describe("atan2", () => {
+  it("[[Call]] returns the atan2", () => {
+    assertStrictEquals(atan2(6, 9), Math.atan2(6, 9));
+  });
+
+  it("[[Call]] works with big·ints", () => {
+    assertStrictEquals(atan2(6n, 9n), Math.atan2(6, 9));
+  });
+});
+
+describe("clz32", () => {
+  it("[[Call]] returns the clz32", () => {
+    assertStrictEquals(clz32(1 << 28), 3);
+  });
+
+  it("[[Call]] works with big·ints", () => {
+    assertStrictEquals(clz32(1n << 28n), 3);
+  });
+});
+
+describe("max", () => {
+  it("[[Call]] returns the largest number", () => {
+    assertStrictEquals(max(1, -6, 92, -Infinity, 0), 92);
+  });
+
+  it("[[Call]] returns the largest big·int", () => {
+    assertStrictEquals(max(1n, -6n, 92n, 0n), 92n);
+  });
+
+  it("[[Call]] returns nan if any argument is nan", () => {
+    assertStrictEquals(max(0, NaN, 1), NaN);
+  });
+
+  it("[[Call]] returns -Infinity when called with no arguments", () => {
+    assertStrictEquals(max(), -Infinity);
+  });
+
+  it("[[Call]] throws if both big·int and number arguments are provided", () => {
+    assertThrows(() => max(-Infinity, 0n));
+  });
+});
+
+describe("min", () => {
+  it("[[Call]] returns the largest number", () => {
+    assertStrictEquals(min(1, -6, 92, Infinity, 0), -6);
+  });
+
+  it("[[Call]] returns the largest big·int", () => {
+    assertStrictEquals(min(1n, -6n, 92n, 0n), -6n);
+  });
+
+  it("[[Call]] returns nan if any argument is nan", () => {
+    assertStrictEquals(min(0, NaN, 1), NaN);
+  });
+
+  it("[[Call]] returns Infinity when called with no arguments", () => {
+    assertStrictEquals(min(), Infinity);
+  });
+
+  it("[[Call]] throws if both big·int and number arguments are provided", () => {
+    assertThrows(() => min(Infinity, 0n));
+  });
+});
+
+describe("sgn", () => {
+  it("[[Call]] returns the sign", () => {
+    assertStrictEquals(sgn(Infinity), 1);
+    assertStrictEquals(sgn(-Infinity), -1);
+    assertStrictEquals(sgn(0), 0);
+    assertStrictEquals(sgn(-0), -0);
+    assertStrictEquals(sgn(NaN), NaN);
+  });
+
+  it("[[Call]] works with big·ints", () => {
+    assertStrictEquals(sgn(0n), 0n);
+    assertStrictEquals(sgn(92n), 1n);
+    assertStrictEquals(sgn(-92n), -1n);
+  });
+});
+
+describe("toBigInt", () => {
+  it("[[Call]] converts to a big·int", () => {
+    assertStrictEquals(toBigInt(2), 2n);
+  });
+});
+
+describe("toFloat32", () => {
+  it("[[Call]] returns the 32‐bit floating‐point representation", () => {
+    assertStrictEquals(
+      toFloat32(562949953421313),
+      Math.fround(562949953421313),
+    );
+  });
+
+  it("[[Call]] works with big·ints", () => {
+    assertStrictEquals(
+      toFloat32(562949953421313n),
+      Math.fround(562949953421313),
+    );
+  });
+});
+
+describe("toIntN", () => {
+  it("[[Call]] converts to an int·n", () => {
+    assertStrictEquals(toIntN(2, 7n), -1n);
+  });
+
+  it("[[Call]] works with numbers", () => {
+    assertStrictEquals(toIntN(2, 7), -1);
+  });
+});
+
+describe("toNumber", () => {
+  it("[[Call]] converts to a number", () => {
+    assertStrictEquals(toNumber(2n), 2);
+  });
+});
+
+describe("toNumeric", () => {
+  it("[[Call]] returns a big·int argument", () => {
+    assertStrictEquals(toNumeric(231n), 231n);
+  });
+
+  it("[[Call]] converts to a numeric", () => {
+    assertStrictEquals(toNumeric("231"), 231);
+  });
+
+  it("[[Call]] prefers `valueOf`", () => {
+    assertStrictEquals(
+      toNumeric({
+        toString() {
+          return 0;
+        },
+        valueOf() {
+          return 231n;
+        },
+      }),
+      231n,
+    );
+  });
+});
+
+describe("toUintN", () => {
+  it("[[Call]] converts to an int·n", () => {
+    assertStrictEquals(toUintN(2, 7n), 3n);
+  });
+
+  it("[[Call]] works with numbers", () => {
+    assertStrictEquals(toUintN(2, 7), 3);
+  });
+});
This page took 0.224986 seconds and 4 git commands to generate.