diff options
author | metagn <metagngn@gmail.com> | 2023-04-11 22:20:20 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-11 21:20:20 +0200 |
commit | f05387045df55bf7123ee68002238e943716815e (patch) | |
tree | 9ba7959434162830612e31351eadbae79cf3bc37 /tests | |
parent | be06446ffecd7665651a25d6b07fade5cc019296 (diff) | |
download | Nim-f05387045df55bf7123ee68002238e943716815e.tar.gz |
int64/uint64 as bigint in JS (#21613)
* int64/uint64 as bigint in JS * fix CI * convert to compile option * fix lie * smaller diff, changelog entry
Diffstat (limited to 'tests')
-rw-r--r-- | tests/js/ttypedarray.nim | 11 | ||||
-rw-r--r-- | tests/lexer/tunary_minus.nim | 4 | ||||
-rw-r--r-- | tests/misc/tints.nim | 20 | ||||
-rw-r--r-- | tests/stdlib/thashes.nim | 2 | ||||
-rw-r--r-- | tests/stdlib/tjson.nim | 7 | ||||
-rw-r--r-- | tests/stdlib/tjsonmacro.nim | 6 | ||||
-rw-r--r-- | tests/stdlib/trandom.nim | 19 | ||||
-rw-r--r-- | tests/stdlib/tstrutils.nim | 18 | ||||
-rw-r--r-- | tests/stdlib/ttimes.nim | 2 | ||||
-rw-r--r-- | tests/system/tdollars.nim | 10 |
10 files changed, 59 insertions, 40 deletions
diff --git a/tests/js/ttypedarray.nim b/tests/js/ttypedarray.nim index 222f66569..08b5fcdde 100644 --- a/tests/js/ttypedarray.nim +++ b/tests/js/ttypedarray.nim @@ -1,3 +1,7 @@ +discard """ + matrix: "--jsbigint64:off; --jsbigint64:on" +""" + import std/private/jsutils proc main()= @@ -5,9 +9,10 @@ proc main()= doAssert fn(array[2, int8].default) == "Int8Array" doAssert fn(array[2, uint8].default) == "Uint8Array" doAssert fn(array[2, byte].default) == "Uint8Array" - # doAssert fn(array[2, char].default) == "Uint8Array" # xxx fails; bug? - doAssert fn(array[2, uint64].default) == "Array" - # pending https://github.com/nim-lang/RFCs/issues/187 maybe use `BigUint64Array` + doAssert fn(array[2, char].default) == "Uint8Array" + whenJsNoBigInt64: discard + do: + doAssert fn(array[2, uint64].default) == "BigUint64Array" doAssert fn([1'u8]) == "Uint8Array" doAssert fn([1'u16]) == "Uint16Array" doAssert fn([byte(1)]) == "Uint8Array" diff --git a/tests/lexer/tunary_minus.nim b/tests/lexer/tunary_minus.nim index 1641e918c..5ec2b5c70 100644 --- a/tests/lexer/tunary_minus.nim +++ b/tests/lexer/tunary_minus.nim @@ -5,6 +5,7 @@ discard """ # Test numeric literals and handling of minus symbol import std/[macros, strutils] +import std/private/jsutils import mlexerutils @@ -60,7 +61,8 @@ template main = doAssert -2147483648'i32 == int32.low when int.sizeof > 4: doAssert -9223372036854775808 == int.low - when not defined(js): + whenJsNoBigInt64: discard + do: doAssert -9223372036854775808 == int64.low block: # check when a minus (-) is an unary op diff --git a/tests/misc/tints.nim b/tests/misc/tints.nim index d24cbd4ac..cb77d4d89 100644 --- a/tests/misc/tints.nim +++ b/tests/misc/tints.nim @@ -1,4 +1,5 @@ discard """ + matrix: "; --backend:js --jsbigint64:off; --backend:js --jsbigint64:on" output: ''' 0 0 0 0 @@ -6,6 +7,8 @@ Success''' """ # Test the different integer operations +import std/private/jsutils + var testNumber = 0 template test(opr, a, b, c: untyped): untyped = @@ -23,28 +26,37 @@ template test(opr, a, b, c: untyped): untyped = test(`+`, 12'i8, -13'i16, -1'i16) test(`shl`, 0b11, 0b100, 0b110000) +whenJsNoBigInt64: discard +do: + test(`shl`, 0b11'i64, 0b100'i64, 0b110000'i64) when not defined(js): + # mixed type shr needlessly complicates codegen with bigint + # and thus is not yet supported in JS for 64 bit ints test(`shl`, 0b11'i32, 0b100'i64, 0b110000'i64) test(`shl`, 0b11'i32, 0b100'i32, 0b110000'i32) test(`or`, 0xf0f0'i16, 0x0d0d'i16, 0xfdfd'i16) test(`and`, 0xf0f0'i16, 0xfdfd'i16, 0xf0f0'i16) -when not defined(js): +whenJsNoBigInt64: discard +do: test(`shr`, 0xffffffffffffffff'i64, 0x4'i64, 0xffffffffffffffff'i64) test(`shr`, 0xffff'i16, 0x4'i16, 0xffff'i16) test(`shr`, 0xff'i8, 0x4'i8, 0xff'i8) -when not defined(js): +whenJsNoBigInt64: discard +do: test(`shr`, 0xffffffff'i64, 0x4'i64, 0x0fffffff'i64) test(`shr`, 0xffffffff'i32, 0x4'i32, 0xffffffff'i32) -when not defined(js): +whenJsNoBigInt64: discard +do: test(`shl`, 0xffffffffffffffff'i64, 0x4'i64, 0xfffffffffffffff0'i64) test(`shl`, 0xffff'i16, 0x4'i16, 0xfff0'i16) test(`shl`, 0xff'i8, 0x4'i8, 0xf0'i8) -when not defined(js): +whenJsNoBigInt64: discard +do: test(`shl`, 0xffffffff'i64, 0x4'i64, 0xffffffff0'i64) test(`shl`, 0xffffffff'i32, 0x4'i32, 0xfffffff0'i32) diff --git a/tests/stdlib/thashes.nim b/tests/stdlib/thashes.nim index caae79213..526a2839f 100644 --- a/tests/stdlib/thashes.nim +++ b/tests/stdlib/thashes.nim @@ -1,5 +1,5 @@ discard """ - targets: "c cpp js" + matrix: "; --backend:cpp; --backend:js --jsbigint64:on; --backend:js --jsbigint64:off" """ import std/hashes diff --git a/tests/stdlib/tjson.nim b/tests/stdlib/tjson.nim index a60d45aab..691bedeaa 100644 --- a/tests/stdlib/tjson.nim +++ b/tests/stdlib/tjson.nim @@ -1,6 +1,5 @@ discard """ - matrix: "--mm:refc" - targets: "c cpp js" + matrix: "--mm:refc; --backend:cpp --mm:refc; --backend:js --jsbigint64:off; --backend:js --jsbigint64:on" """ @@ -9,6 +8,7 @@ Note: Macro tests are in tests/stdlib/tjsonmacro.nim ]# import std/[json,parsejson,strutils] +import std/private/jsutils from std/math import isNaN when not defined(js): import std/streams @@ -314,7 +314,8 @@ block: # bug #17383 else: testRoundtrip(int.high): "9223372036854775807" testRoundtrip(uint.high): "18446744073709551615" - when not defined(js): + whenJsNoBigInt64: discard + do: testRoundtrip(int64.high): "9223372036854775807" testRoundtrip(uint64.high): "18446744073709551615" diff --git a/tests/stdlib/tjsonmacro.nim b/tests/stdlib/tjsonmacro.nim index 9c1fa833d..f08c3946b 100644 --- a/tests/stdlib/tjsonmacro.nim +++ b/tests/stdlib/tjsonmacro.nim @@ -436,11 +436,7 @@ proc testJson() = block: let s = """{"a": 1, "b": 2}""" let t = parseJson(s).to(Table[string, int]) - when not defined(js): - # For some reason on the JS backend `{"b": 2, "a": 0}` is - # sometimes the value of `t`. This needs investigation. I can't - # reproduce it right now in an isolated test. - doAssert t["a"] == 1 + doAssert t["a"] == 1 doAssert t["b"] == 2 block: diff --git a/tests/stdlib/trandom.nim b/tests/stdlib/trandom.nim index ef71c3442..c35fc47da 100644 --- a/tests/stdlib/trandom.nim +++ b/tests/stdlib/trandom.nim @@ -1,9 +1,10 @@ discard """ joinable: false # to avoid messing with global rand state - targets: "c js" + matrix: "; --backend:js --jsbigint64:off; --backend:js --jsbigint64:on" """ import std/[assertions, formatfloat] import std/[random, math, stats, sets, tables] +import std/private/jsutils when not defined(js): import std/os @@ -208,8 +209,8 @@ block: # bug #16360 when withUint: test cast[uint](int.high) test cast[uint](int.high) + 1 - when not defined(js): - # pending bug #16411 + whenJsNoBigInt64: discard + do: test uint64.high test uint64.high - 1 test uint.high - 2 @@ -239,16 +240,12 @@ block: # bug #16296 test(int.low .. -1) test(int.low .. 1) test(int64.low .. 1'i64) - when not defined(js): - # pending bug #16411 - test(10'u64 .. uint64.high) + test(10'u64 .. uint64.high) block: # bug #17670 - when not defined(js): - # pending bug #16411 - type UInt48 = range[0'u64..2'u64^48-1] - let x = rand(UInt48) - doAssert x is UInt48 + type UInt48 = range[0'u64..2'u64^48-1] + let x = rand(UInt48) + doAssert x is UInt48 block: # bug #17898 # Checks whether `initRand()` generates unique states. diff --git a/tests/stdlib/tstrutils.nim b/tests/stdlib/tstrutils.nim index 4e88197ff..12b0c13b1 100644 --- a/tests/stdlib/tstrutils.nim +++ b/tests/stdlib/tstrutils.nim @@ -1,10 +1,11 @@ discard """ - targets: "c cpp js" + matrix: "; --backend:cpp; --backend:js --jsbigint64:off; --backend:js --jsbigint64:on" """ import std/strutils from stdtest/testutils import disableVm import std/assertions +import std/private/jsutils # xxx each instance of `disableVm` and `when not defined js:` should eventually be fixed template rejectParse(e) = @@ -509,7 +510,8 @@ template main() = block: # toHex doAssert(toHex(100i16, 32) == "00000000000000000000000000000064") doAssert(toHex(-100i16, 32) == "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF9C") - when not defined js: + whenJsNoBigInt64: discard + do: doAssert(toHex(high(uint64)) == "FFFFFFFFFFFFFFFF") doAssert(toHex(high(uint64), 16) == "FFFFFFFFFFFFFFFF") doAssert(toHex(high(uint64), 32) == "0000000000000000FFFFFFFFFFFFFFFF") @@ -530,11 +532,12 @@ template main() = doAssert(spaces(0) == "") block: # toBin, toOct - block:# bug #11369 + whenJsNoBigInt64: # bug #11369 + discard + do: var num: int64 = -1 - when not defined js: - doAssert num.toBin(64) == "1111111111111111111111111111111111111111111111111111111111111111" - doAssert num.toOct(24) == "001777777777777777777777" + doAssert num.toBin(64) == "1111111111111111111111111111111111111111111111111111111111111111" + doAssert num.toOct(24) == "001777777777777777777777" block: # replace doAssert "oo".replace("", "abc") == "oo" @@ -741,7 +744,8 @@ bar block: # formatSize disableVm: - when not defined(js): + whenJsNoBigInt64: discard + do: doAssert formatSize((1'i64 shl 31) + (300'i64 shl 20)) == "2.293GiB" # <=== bug #8231 doAssert formatSize((2.234*1024*1024).int) == "2.234MiB" doAssert formatSize(4096) == "4KiB" diff --git a/tests/stdlib/ttimes.nim b/tests/stdlib/ttimes.nim index f6159d942..47d2efcf1 100644 --- a/tests/stdlib/ttimes.nim +++ b/tests/stdlib/ttimes.nim @@ -1,5 +1,5 @@ discard """ - targets: "c js" + matrix: "; --backend:js --jsbigint64:on; --backend:js --jsbigint64:off" """ import times, strutils, unittest diff --git a/tests/system/tdollars.nim b/tests/system/tdollars.nim index 93fa5cb9e..7eb26cd6b 100644 --- a/tests/system/tdollars.nim +++ b/tests/system/tdollars.nim @@ -1,5 +1,5 @@ discard """ - targets: "c cpp js" + matrix: "; --backend:cpp; --backend:js --jsbigint64:off; --backend:js --jsbigint64:on" """ #[ @@ -12,6 +12,7 @@ duplication (which always results in weaker test coverage in practice). ]# import std/unittest +import std/private/jsutils template test[T](a: T, expected: string) = check $a == expected var b = a @@ -66,7 +67,8 @@ block: # `$`(SomeInteger) testType int testType bool - when not defined(js): # requires BigInt support + whenJsNoBigInt64: discard + do: testType uint64 testType int64 testType BiggestInt @@ -176,10 +178,10 @@ proc main()= res.addInt int64(i) doAssert res == "-9-8-7-6-5-4-3-2-10" - when not defined(js): + whenJsNoBigInt64: discard + do: test2 high(int64), "9223372036854775807" test2 low(int64), "-9223372036854775808" - test2 high(int32), "2147483647" test2 low(int32), "-2147483648" test2 high(int16), "32767" |