diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-05-18 12:49:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-18 12:49:56 +0200 |
commit | 9dc3ae8523364b8df6b8e92346907d1020e80d33 (patch) | |
tree | c183f1390c0630729fe2ca864d5284641e7a0e8b /test_napi/bigint_test.js | |
parent | 99c30285ffa14097978f220a05809cbf8f68b9d3 (diff) |
fix(napi): BigInt related APIs (#19174)
Doesn't make the API bullet-proof and there are some TODOs left,
but greatly improves the situation. Tests were ported from Node.js.
Closes https://github.com/denoland/deno/issues/18276.
Diffstat (limited to 'test_napi/bigint_test.js')
-rw-r--r-- | test_napi/bigint_test.js | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/test_napi/bigint_test.js b/test_napi/bigint_test.js new file mode 100644 index 000000000..8d05f957d --- /dev/null +++ b/test_napi/bigint_test.js @@ -0,0 +1,63 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +import { assertEquals, assertThrows, loadTestLibrary } from "./common.js"; + +const bi = loadTestLibrary(); + +Deno.test("cases", function () { + const cases = [ + 0n, + -0n, + 1n, + -1n, + 100n, + 2121n, + -1233n, + 986583n, + -976675n, + 98765432213456789876546896323445679887645323232436587988766545658n, + -4350987086545760976737453646576078997096876957864353245245769809n, + ]; + + for (const num of cases) { + if (num > -(2n ** 63n) && num < 2n ** 63n) { + assertEquals(bi.testInt64(num), num); + assertEquals(bi.isLossless(num, true), true); + } else { + assertEquals(bi.isLossless(num, true), false); + } + + if (num >= 0 && num < 2n ** 64n) { + assertEquals(bi.testUint64(num), num); + assertEquals(bi.isLossless(num, false), true); + } else { + assertEquals(bi.isLossless(num, false), false); + } + + assertEquals(bi.testWords(num), num); + } +}); + +Deno.test( + // TODO(bartlomieju): fix this test + { ignore: true }, + function tooBigBigInt() { + assertThrows( + () => bi.createTooBigBigInt(), + Error, + "Invalid argument", + ); + }, +); + +Deno.test( + // TODO(bartlomieju): fix this test + { ignore: true }, + function exceptionForwarding() { + assertThrows( + () => bi.makeBigIntWordsThrow(), + Error, + "Maximum BigInt size exceeded", + ); + }, +); |