Skip to content

Commit

Permalink
Merge pull request #22 from Ebmtranceboy/main
Browse files Browse the repository at this point in the history
making sure NaN is not passed to BigInt constructor + 2 tests back
  • Loading branch information
Ebmtranceboy committed Sep 22, 2023
2 parents bdb061e + 05860c2 commit 135a276
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/JS/BigInt.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ export const fromStringAsImpl = function (just) {
while (i < value.length) parts.push(value.slice(i, i += size));

function f(acc, chunk) {
let n = BigInt(parseInt(chunk, radix));
let n = parseInt(chunk, radix);
if (isNaN(n)) {
throw new Error("Invalid number");
} else {
return acc * factor + n;
return acc * factor + BigInt(n);
}
};

Expand Down
6 changes: 3 additions & 3 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ main = do
assert $ fromString "123456789" == Just (fromInt 123456789)
assert $ fromString "10000000" == Just (fromInt 10000000)
quickCheck $ \(TestBigInt a) -> (fromString <<< toString) a == Just a
{-

quickCheck $ \(TestBigInt a) ->
let radixes = [binary, octal, decimal, hexadecimal, base36]
in un Conj $ flip foldMap radixes $ \r ->
Conj $ (fromStringAs r $ toStringAs r a) == Just a
-}

log "Parsing strings with a different base"
assert $ fromString "0b100" == Just four
assert $ fromString "0xff" == fromString "255"
Expand All @@ -104,7 +104,7 @@ main = do

log "Can parse 256 bit numbers"
assert $ isJust $ fromString "115792089237316195423570985008687907853269984665640564039457584007913129639935"
-- assert $ isJust $ fromStringAs hexadecimal "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0cbf"
assert $ isJust $ fromStringAs hexadecimal "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0cbf"

-- To test the multiplication, we need to make sure that Int does not overflow
quickCheck (\x y -> fromSmallInt x * fromSmallInt y == fromInt (runSmallInt x * runSmallInt y))
Expand Down

0 comments on commit 135a276

Please sign in to comment.