diff options
Diffstat (limited to 'tests/int')
-rw-r--r-- | tests/int/t1.nim | 61 | ||||
-rw-r--r-- | tests/int/tarithm.nim | 187 | ||||
-rw-r--r-- | tests/int/tashr.nim | 46 | ||||
-rw-r--r-- | tests/int/tdiv.nim | 19 | ||||
-rw-r--r-- | tests/int/tints.nim | 150 | ||||
-rw-r--r-- | tests/int/tunsigned64mod.nim | 24 | ||||
-rw-r--r-- | tests/int/tunsignedcmp.nim | 43 | ||||
-rw-r--r-- | tests/int/tunsignedcomp.nim | 136 | ||||
-rw-r--r-- | tests/int/tunsignedconv.nim | 115 | ||||
-rw-r--r-- | tests/int/tunsignedinc.nim | 40 | ||||
-rw-r--r-- | tests/int/tunsignedmisc.nim | 66 | ||||
-rw-r--r-- | tests/int/twrongexplicitvarconv.nim | 16 | ||||
-rw-r--r-- | tests/int/twrongvarconv.nim | 9 |
13 files changed, 912 insertions, 0 deletions
diff --git a/tests/int/t1.nim b/tests/int/t1.nim new file mode 100644 index 000000000..6e5cdc8d4 --- /dev/null +++ b/tests/int/t1.nim @@ -0,0 +1,61 @@ +discard """ + targets: "c cpp" +""" + +doAssert typeOf(1.int64 + 1.int) is int64 +doAssert typeOf(1.uint64 + 1.uint) is uint64 +doAssert int64 is SomeNumber +doAssert int64 is (SomeNumber and not(uint32|uint64|uint|int)) +doAssert int64 is (not(uint32|uint64|uint|int)) +doAssert int isnot int64 +doAssert int64 isnot int +var myInt16 = 5i16 +var myInt: int +doAssert typeOf(myInt16 + 34) is int16 # of type `int16` +doAssert typeOf(myInt16 + myInt) is int # of type `int` +doAssert typeOf(myInt16 + 2i32) is int32 # of type `int32` +doAssert int32 isnot int64 +doAssert int32 isnot int + +block: # bug #23947 + template foo = + let test_u64 : uint64 = 0xFF07.uint64 + let test_u8 : uint8 = test_u64.uint8 + # Error: illegal conversion from '65287' to '[0..255]' + doAssert test_u8 == 7 + + static: foo() + foo() + +block: + # bug #22085 + const + x = uint32(uint64.high) # vm error + u = uint64.high + v = uint32(u) # vm error + + let + z = uint64.high + y = uint32(z) # runtime ok + + let + w = uint32(uint64.high) # semfold error + + doAssert x == w + doAssert v == y + + # bug #14522 + doAssert 0xFF000000_00000000.uint64 == 18374686479671623680'u64 + +block: # bug #23954 + let testRT_u8 : uint8 = 0x107.uint8 + doAssert testRT_u8 == 7 + const testCT_u8 : uint8 = 0x107.uint8 + doAssert testCT_u8 == 7 + +block: # issue #24104 + type P = distinct uint # uint, uint8, uint16, uint32, uint64 + let v = 0.P + case v + of 0.P: discard + else: discard diff --git a/tests/int/tarithm.nim b/tests/int/tarithm.nim new file mode 100644 index 000000000..d0943d225 --- /dev/null +++ b/tests/int/tarithm.nim @@ -0,0 +1,187 @@ +discard """ + output: ''' +int32 +int32 +1280 +1280 +3 +1 +2 +2 +3 +4294967295 +2 +0 +tUnsignedOps OK +''' +nimout: "tUnsignedOps OK" +""" + +import typetraits + + +block tand: + # bug #5216 + echo(name typeof((0x0A'i8 and 0x7F'i32) shl 7'i32)) + + let i8 = 0x0A'i8 + echo(name typeof((i8 and 0x7F'i32) shl 7'i32)) + + echo((0x0A'i8 and 0x7F'i32) shl 7'i32) + + let ii8 = 0x0A'i8 + echo((ii8 and 0x7F'i32) shl 7'i32) + + + +block tcast: + template crossCheck(ty: untyped, exp: untyped) = + let rt = ty(exp) + const ct = ty(exp) + if $rt != $ct: + echo astToStr(exp) + echo "Got ", ct + echo "Expected ", rt + + template add1(x: uint8): untyped = x + 1 + template add1(x: uint16): untyped = x + 1 + template add1(x: uint32): untyped = x + 1 + + template sub1(x: uint8): untyped = x - 1 + template sub1(x: uint16): untyped = x - 1 + template sub1(x: uint32): untyped = x - 1 + + crossCheck(int8, 0'i16 - 5'i16) + crossCheck(int16, 0'i32 - 5'i32) + crossCheck(int32, 0'i64 - 5'i64) + + crossCheck(uint8, 0'u8 - 5'u8) + crossCheck(uint16, 0'u16 - 5'u16) + crossCheck(uint32, 0'u32 - 5'u32) + crossCheck(uint64, 0'u64 - 5'u64) + + crossCheck(uint8, uint8.high + 5'u8) + crossCheck(uint16, uint16.high + 5'u16) + crossCheck(uint32, uint32.high + 5'u32) + crossCheck(uint64, 0xFFFFFFFFFFFFFFFF'u64 + 5'u64) + crossCheck(uint64, uint64.high + 5'u64) + + doAssert $sub1(0'u8) == "255" + doAssert $sub1(0'u16) == "65535" + doAssert $sub1(0'u32) == "4294967295" + + doAssert $add1(255'u8) == "0" + doAssert $add1(65535'u16) == "0" + doAssert $add1(4294967295'u32) == "0" + + crossCheck(int32, high(int32)) + crossCheck(int32, high(int32).int32) + crossCheck(int32, low(int32)) + crossCheck(int32, low(int32).int32) + crossCheck(int64, high(int8).int16.int32.int64) + crossCheck(int64, low(int8).int16.int32.int64) + + doAssert not compiles(echo int64(0xFFFFFFFFFFFFFFFF'u64)) + doAssert not compiles(echo int32(0xFFFFFFFFFFFFFFFF'u64)) + doAssert not compiles(echo int16(0xFFFFFFFFFFFFFFFF'u64)) + doAssert not compiles(echo int8(0xFFFFFFFFFFFFFFFF'u64)) + +block tnot: + # Signed types + block: + const t0: int8 = not 4 + const t1: int16 = not 4 + const t2: int32 = not 4 + const t3: int64 = not 4 + const t4: int8 = not -5 + const t5: int16 = not -5 + const t6: int32 = not -5 + const t7: int64 = not -5 + doAssert t0 == -5 + doAssert t1 == -5 + doAssert t2 == -5 + doAssert t3 == -5 + doAssert t4 == 4 + doAssert t5 == 4 + doAssert t6 == 4 + doAssert t7 == 4 + + # Unsigned types + block: + const t0: uint8 = not 4'u8 + const t1: uint16 = not 4'u16 + const t2: uint32 = not 4'u32 + const t3: uint64 = not 4'u64 + const t4: uint8 = not 251'u8 + const t5: uint16 = not 65531'u16 + const t6: uint32 = not 4294967291'u32 + const t7: uint64 = not 18446744073709551611'u64 + doAssert t0 == 251 + doAssert t1 == 65531 + doAssert t2 == 4294967291'u32 + doAssert t3 == 18446744073709551611'u64 + doAssert t4 == 4 + doAssert t5 == 4 + doAssert t6 == 4 + doAssert t7 == 4 + + +block tshr: + proc T() = + # let VI = -8 + let VI64 = -8'i64 + let VI32 = -8'i32 + let VI16 = -8'i16 + let VI8 = -8'i8 + # doAssert( (VI shr 1) == 9_223_372_036_854_775_804, "Actual: " & $(VI shr 1)) + doAssert( (VI64 shr 1) == -4, "Actual: " & $(VI64 shr 1)) + doAssert( (VI32 shr 1) == -4, "Actual: " & $(VI32 shr 1)) + doAssert( (VI16 shr 1) == -4, "Actual: " & $(VI16 shr 1)) + doAssert( (VI8 shr 1) == -4, "Actual: " & $(VI8 shr 1)) + + T() + static: + T() + + + +block tsubrange: + # bug #5854 + type + n16 = range[0'i16..high(int16)] + + var level: n16 = 1 + let maxLevel: n16 = 1 + + level = min(level + 2, maxLevel).n16 + doAssert level == 1 + +block tissue12177: + var a: uint16 = 1 + var b: uint32 = 2 + + echo(b + a) + echo(b - a) + echo(b * a) + echo(b div a) + + echo(a + b) + echo(a - b) + echo(a * b) + echo(a div b) + +block tUnsignedOps: + proc testUnsignedOps() = + let a: int8 = -128 + let b: int8 = 127 + + doAssert b +% 1 == -128 + doAssert b -% -1 == -128 + doAssert b *% 2 == -2 + doAssert a /% 4 == 32 + doAssert a %% 7 == 2 + echo "tUnsignedOps OK" + + testUnsignedOps() + static: + testUnsignedOps() diff --git a/tests/int/tashr.nim b/tests/int/tashr.nim new file mode 100644 index 000000000..aeb3b6843 --- /dev/null +++ b/tests/int/tashr.nim @@ -0,0 +1,46 @@ +discard """ + output: '''''' + targets: '''c js''' +""" + +# issue #6255, feature request +# arithmetic right shift + +var x1 = -123'i8 +var x2 = -123'i16 +var x3 = -123'i32 +var x4 = -123'i64 +var x5 = -123 + +block codegen_test: + doAssert ashr(x1, 1) == -62 + doAssert ashr(x2, 1) == -62 + doAssert ashr(x3, 1) == -62 + doAssert ashr(x4, 1) == -62 + doAssert ashr(x5, 1) == -62 + +block semfold_test: + doAssert ashr(-123'i8 , 1) == -62 + doAssert ashr(-123'i16, 1) == -62 + doAssert ashr(-123'i32, 1) == -62 + doAssert ashr(-123'i64, 1) == -62 + doAssert ashr(-123 , 1) == -62 + +static: # VM test + doAssert ashr(-123'i8 , 1) == -62 + doAssert ashr(-123'i16, 1) == -62 + doAssert ashr(-123'i32, 1) == -62 + doAssert ashr(-123'i64, 1) == -62 + doAssert ashr(-123 , 1) == -62 + + var y1 = -123'i8 + var y2 = -123'i16 + var y3 = -123'i32 + var y4 = -123'i64 + var y5 = -123 + + doAssert ashr(y1, 1) == -62 + doAssert ashr(y2, 1) == -62 + doAssert ashr(y3, 1) == -62 + doAssert ashr(y4, 1) == -62 + doAssert ashr(y5, 1) == -62 diff --git a/tests/int/tdiv.nim b/tests/int/tdiv.nim new file mode 100644 index 000000000..5d8eed84d --- /dev/null +++ b/tests/int/tdiv.nim @@ -0,0 +1,19 @@ +discard """ + targets: "c js" +""" + + +block divUint64: + proc divTest() = + let x1 = 12'u16 + let y = x1 div 5'u16 + let x2 = 1345567'u32 + let z = x2 div 5'u32 + let a = 1345567'u64 div uint64(x1) + doAssert y == 2 + doAssert z == 269113 + doAssert a == 112130 + + static: divTest() + divTest() + diff --git a/tests/int/tints.nim b/tests/int/tints.nim new file mode 100644 index 000000000..773e8ccad --- /dev/null +++ b/tests/int/tints.nim @@ -0,0 +1,150 @@ +discard """ + matrix: "; --backend:js --jsbigint64:off -d:nimStringHash2; --backend:js --jsbigint64:on" + output: ''' +0 0 +0 0 +Success''' +""" +# Test the different integer operations + + +import std/private/jsutils + +var testNumber = 0 + +template test(opr, a, b, c: untyped): untyped = + # test the expression at compile and runtime + block: + const constExpr = opr(a, b) + when constExpr != c: + {.error: "Test failed " & $constExpr & " " & $c.} + inc(testNumber) + #Echo("Test: " & $testNumber) + var aa = a + var bb = b + var varExpr = opr(aa, bb) + assert(varExpr == c) + +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) + +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) + +whenJsNoBigInt64: discard +do: + test(`shr`, 0xffffffff'i64, 0x4'i64, 0x0fffffff'i64) +test(`shr`, 0xffffffff'i32, 0x4'i32, 0xffffffff'i32) + +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) + +whenJsNoBigInt64: discard +do: + test(`shl`, 0xffffffff'i64, 0x4'i64, 0xffffffff0'i64) +test(`shl`, 0xffffffff'i32, 0x4'i32, 0xfffffff0'i32) + +# bug #916 +proc unc(a: float): float = + return a + +echo int(unc(0.5)), " ", int(unc(-0.5)) +echo int(0.5), " ", int(-0.5) + +block: # Casts to uint + template testCast(fromValue: typed, toType: typed, expectedResult: typed) = + let src = fromValue + let dst = cast[toType](src) + if dst != expectedResult: + echo "Casting ", astToStr(fromValue), " to ", astToStr(toType), " = ", dst.int, " instead of ", astToStr(expectedResult) + doAssert(dst == expectedResult) + + testCast(-1'i16, uint16, 0xffff'u16) + testCast(0xffff'u16, int16, -1'i16) + + testCast(0xff'u16, uint8, 0xff'u8) + testCast(0xffff'u16, uint8, 0xff'u8) + + testCast(-1'i16, uint32, 0xffffffff'u32) + testCast(0xffffffff'u32, int32, -1) + + testCast(0xfffffffe'u32, int32, -2'i32) + testCast(0xffffff'u32, int16, -1'i32) + + testCast(-5'i32, uint8, 251'u8) + +# issue #7174 +let c = 1'u +let val = c > 0 +doAssert val + +block: # bug #6752 + when not defined(js) or (defined(js) and compileOption("jsbigint64")): + let x = 711127'i64 + doAssert x * 86400'i64 == 61441372800'i64 + +block: # bug #17604 + let a = 2147483648'u + doAssert (a and a) == a + doAssert (a or 0) == a + +block: # bitwise not + let + z8 = 0'u8 + z16 = 0'u16 + z32 = 0'u32 + z64 = 0'u64 + doAssert (not z8) == uint8.high + doAssert (not z16) == uint16.high + doAssert (not z32) == uint32.high + when not defined(js) or (defined(js) and compileOption("jsbigint64")): + doAssert (not z64) == uint64.high + +block: # shl + let i8 = int8.high + let i16 = int16.high + let i32 = int32.high + let i64 = int64.high + doAssert i8 shl 1 == -2 + doAssert i8 shl 2 == -4 + doAssert i16 shl 1 == -2 + doAssert i16 shl 2 == -4 + doAssert i32 shl 1 == -2 + doAssert i32 shl 2 == -4 + when not defined(js) or (defined(js) and compileOption("jsbigint64")): + doAssert i64 shl 1 == -2 + doAssert i64 shl 2 == -4 + + let u8 = uint8.high + let u16 = uint16.high + let u32 = uint32.high + let u64 = uint64.high + doAssert u8 shl 1 == u8 - 1 + doAssert u16 shl 1 == u16 - 1 + doAssert u32 shl 1 == u32 - 1 + when not defined(js) or (defined(js) and compileOption("jsbigint64")): + doAssert u64 shl 1 == u64 - 1 + +block: # bug #23378 + var neg = -1 # prevent compile-time evaluation + let n = abs BiggestInt neg + doAssert n == 1 + +echo("Success") #OUT Success diff --git a/tests/int/tunsigned64mod.nim b/tests/int/tunsigned64mod.nim new file mode 100644 index 000000000..ca3286df3 --- /dev/null +++ b/tests/int/tunsigned64mod.nim @@ -0,0 +1,24 @@ + +# bug #1638 + +let v1 = 7 +let v2 = 7'u64 + +let t1 = v1 mod 2 # works +let t2 = 7'u64 mod 2'u64 # works +let t3 = v2 mod 2'u64 # Error: invalid type: 'range 0..1(uint64) +let t4 = (v2 mod 2'u64).uint64 # works + +# bug #2550 + +var x: uint # doesn't work +doAssert x mod 2 == 0 + +var y: uint64 # doesn't work +doAssert y mod 2 == 0 + +var z: uint32 # works +doAssert z mod 2 == 0 + +var a: int # works +doAssert a mod 2 == 0 diff --git a/tests/int/tunsignedcmp.nim b/tests/int/tunsignedcmp.nim new file mode 100644 index 000000000..11b67ac5f --- /dev/null +++ b/tests/int/tunsignedcmp.nim @@ -0,0 +1,43 @@ +discard """ + output: '''true +true +true +5 +4 +3 +2 +1 +0 +it should stop now +18446744073709551615 +4294967295 +''' +""" + +# bug 1420 +var x = 40'u32 +var y = 30'u32 +echo x > y # works + +echo((40'i32) > (30'i32)) +echo((40'u32) > (30'u32)) # Error: ordinal type expected + +# bug #4220 + +const count: uint = 5 +var stop_me = false + +for i in countdown(count, 0): + echo i + if stop_me: break + if i == 0: + echo "it should stop now" + stop_me = true + +# bug #3985 +const + HIGHEST_64BIT_UINT = 0xFFFFFFFFFFFFFFFF'u + HIGHEST_32BIT_UINT = 0xFFFFFFFF'u + +echo($HIGHEST_64BIT_UINT) +echo($HIGHEST_32BIT_UINT) diff --git a/tests/int/tunsignedcomp.nim b/tests/int/tunsignedcomp.nim new file mode 100644 index 000000000..970c4ae9d --- /dev/null +++ b/tests/int/tunsignedcomp.nim @@ -0,0 +1,136 @@ +discard """ + output: '''''' + disabled: "true" +""" + +# All operations involving uint64 are commented out +# as they're not yet supported. +# All other operations are handled by implicit conversions from uints to ints +# uint64 could be supported but would need special implementation of the operators + +# unsigned < signed + +doAssert 10'u8 < 20'i8 +doAssert 10'u8 < 20'i16 +doAssert 10'u8 < 20'i32 +doAssert 10'u8 < 20'i64 + +doAssert 10'u16 < 20'i8 +doAssert 10'u16 < 20'i16 +doAssert 10'u16 < 20'i32 +doAssert 10'u16 < 20'i64 + +doAssert 10'u32 < 20'i8 +doAssert 10'u32 < 20'i16 +doAssert 10'u32 < 20'i32 +doAssert 10'u32 < 20'i64 + +# doAssert 10'u64 < 20'i8 +# doAssert 10'u64 < 20'i16 +# doAssert 10'u64 < 20'i32 +# doAssert 10'u64 < 20'i64 + +# signed < unsigned +doAssert 10'i8 < 20'u8 +doAssert 10'i8 < 20'u16 +doAssert 10'i8 < 20'u32 +# doAssert 10'i8 < 20'u64 + +doAssert 10'i16 < 20'u8 +doAssert 10'i16 < 20'u16 +doAssert 10'i16 < 20'u32 +# doAssert 10'i16 < 20'u64 + +doAssert 10'i32 < 20'u8 +doAssert 10'i32 < 20'u16 +doAssert 10'i32 < 20'u32 +# doAssert 10'i32 < 20'u64 + +doAssert 10'i64 < 20'u8 +doAssert 10'i64 < 20'u16 +doAssert 10'i64 < 20'u32 +# doAssert 10'i64 < 20'u64 + +# unsigned <= signed +doAssert 10'u8 <= 20'i8 +doAssert 10'u8 <= 20'i16 +doAssert 10'u8 <= 20'i32 +doAssert 10'u8 <= 20'i64 + +doAssert 10'u16 <= 20'i8 +doAssert 10'u16 <= 20'i16 +doAssert 10'u16 <= 20'i32 +doAssert 10'u16 <= 20'i64 + +doAssert 10'u32 <= 20'i8 +doAssert 10'u32 <= 20'i16 +doAssert 10'u32 <= 20'i32 +doAssert 10'u32 <= 20'i64 + +# doAssert 10'u64 <= 20'i8 +# doAssert 10'u64 <= 20'i16 +# doAssert 10'u64 <= 20'i32 +# doAssert 10'u64 <= 20'i64 + +# signed <= unsigned +doAssert 10'i8 <= 20'u8 +doAssert 10'i8 <= 20'u16 +doAssert 10'i8 <= 20'u32 +# doAssert 10'i8 <= 20'u64 + +doAssert 10'i16 <= 20'u8 +doAssert 10'i16 <= 20'u16 +doAssert 10'i16 <= 20'u32 +# doAssert 10'i16 <= 20'u64 + +doAssert 10'i32 <= 20'u8 +doAssert 10'i32 <= 20'u16 +doAssert 10'i32 <= 20'u32 +# doAssert 10'i32 <= 20'u64 + +doAssert 10'i64 <= 20'u8 +doAssert 10'i64 <= 20'u16 +doAssert 10'i64 <= 20'u32 +# doAssert 10'i64 <= 20'u64 + +# signed == unsigned +doAssert 10'i8 == 10'u8 +doAssert 10'i8 == 10'u16 +doAssert 10'i8 == 10'u32 +# doAssert 10'i8 == 10'u64 + +doAssert 10'i16 == 10'u8 +doAssert 10'i16 == 10'u16 +doAssert 10'i16 == 10'u32 +# doAssert 10'i16 == 10'u64 + +doAssert 10'i32 == 10'u8 +doAssert 10'i32 == 10'u16 +doAssert 10'i32 == 10'u32 +# doAssert 10'i32 == 10'u64 + +doAssert 10'i64 == 10'u8 +doAssert 10'i64 == 10'u16 +doAssert 10'i64 == 10'u32 +# doAssert 10'i64 == 10'u64 + +# unsigned == signed +doAssert 10'u8 == 10'i8 +doAssert 10'u8 == 10'i16 +doAssert 10'u8 == 10'i32 +# doAssert 10'u8 == 10'i64 + +doAssert 10'u16 == 10'i8 +doAssert 10'u16 == 10'i16 +doAssert 10'u16 == 10'i32 +# doAssert 10'u16 == 10'i64 + +doAssert 10'u32 == 10'i8 +doAssert 10'u32 == 10'i16 +doAssert 10'u32 == 10'i32 +# doAssert 10'u32 == 10'i64 + +# doAssert 10'u64 == 10'i8 +# doAssert 10'u64 == 10'i16 +# doAssert 10'u64 == 10'i32 +# doAssert 10'u64 == 10'i64 diff --git a/tests/int/tunsignedconv.nim b/tests/int/tunsignedconv.nim new file mode 100644 index 000000000..6c73521d3 --- /dev/null +++ b/tests/int/tunsignedconv.nim @@ -0,0 +1,115 @@ +discard """ + targets: "c cpp js" +""" + +# Tests unsigned literals and implicit conversion between uints and ints + +var h8: uint8 = 128 +var h16: uint16 = 32768 +var h32: uint32 = 2147483648'u32 +var h64: uint64 = 9223372036854775808'u64 +var foobar: uint64 = 9223372036854775813'u64 # Issue 728 + +var v8: uint8 = 10 +var v16: uint16 = 10 +var v32: uint32 = 10 +var v64: uint64 = 10 + +# u8 + literal produces u8: +var a8: uint8 = v8 + 10 +var a16: uint16 = v16 + 10 + +when false: + var d8 = v8 + 10'i8 + var d16 = v8 + 10'i16 + var d32 = v8 + 10'i32 + +when false: + # these don't work yet because unsigned.nim is stupid. XXX We need to fix this. + var f8 = v16 + 10'u8 + var f16 = v16 + 10'u16 + var f32 = v16 + 10'u32 + + var g8 = v32 + 10'u8 + var g16 = v32 + 10'u16 + var g32 = v32 + 10'u32 + +var ar: array[0..20, int] +var n8 = ar[v8] +var n16 = ar[v16] +var n32 = ar[v32] +var n64 = ar[v64] + + +block t4176: + var yyy: uint8 = 0 + yyy = yyy - 127 + doAssert type(yyy) is uint8 + +# bug #13661 + +proc fun(): uint = cast[uint](-1) +const x0 = fun() + +doAssert typeof(x0) is uint + +discard $x0 + +# bug #13671 + +const x1 = cast[uint](-1) +discard $(x1,) + +# bug #13698 +let n2: csize_t = 1 +doAssert $n2.int32 == "1" + +# bug #14616 + +let limit = 1'u64 + +let rangeVar = 0'u64 ..< limit + +when not defined(gcRefc): + doAssert repr(rangeVar) == """0 .. 0""", repr(rangeVar) + +# bug #15210 + +let a3 = not 0'u64 +var success = false +try: + discard a3.int64 +except RangeDefect: + success = true + +doAssert success, "conversion should fail at runtime" + +template main() = + # xxx move all tests under here so it gets tested in CT and RT + block: # bug #17572 + type T = distinct uint64 + func f(x: uint64): auto = + let a = T(x) + (x, a.uint64) + const x = 1'u64 shl 63 or 7 + const b = T(x) + doAssert b.uint64 == 9223372036854775815'u64 + doAssert $b.uint64 == "9223372036854775815" + doAssert f(x) == (9223372036854775815'u64, 9223372036854775815'u64) + +static: main() +main() + +block: + let a = uint64.high + let b = uint32.high + + doAssert a.uint64 == a + doAssert a.uint32 == uint32.high + doAssert a.uint16 == uint16.high + doAssert a.uint8 == uint8.high + + doAssert b.uint64 == b + doAssert b.uint32 == b + doAssert b.uint16 == uint16.high + doAssert b.uint8 == uint8.high diff --git a/tests/int/tunsignedinc.nim b/tests/int/tunsignedinc.nim new file mode 100644 index 000000000..9392f1b74 --- /dev/null +++ b/tests/int/tunsignedinc.nim @@ -0,0 +1,40 @@ + +block: # bug #2427 + var x = 0'u8 + dec x # OverflowDefect + x -= 1 # OverflowDefect + x = x - 1 # No error + + doAssert(x == 253'u8) + +block: + var x = 130'u8 + x += 130'u8 + doAssert(x == 4'u8) + +block: + var x = 40000'u16 + x = x + 40000'u16 + doAssert(x == 14464'u16) + +block: + var x = 4000000000'u32 + x = x + 4000000000'u32 + doAssert(x == 3705032704'u32) + +block: + var x = 123'u16 + x -= 125 + doAssert(x == 65534'u16) + +block t4175: + let i = 0u - 1u + const j = 0u - 1u + doAssert i == j + doAssert j + 1u == 0u + +block: # https://forum.nim-lang.org/t/12465#76998 + var a: int = 1 + var x: uint8 = 1 + a.inc(x) # Error: type mismatch + doAssert a == 2 diff --git a/tests/int/tunsignedmisc.nim b/tests/int/tunsignedmisc.nim new file mode 100644 index 000000000..b2a3849cf --- /dev/null +++ b/tests/int/tunsignedmisc.nim @@ -0,0 +1,66 @@ +discard """ + errormsg: "number out of range: '0x123'u8'" +""" + +# Bug #1179 + +# Unsigneds + +# 8 bit +let ref1 = 128'u8 shr 7 +let hex1 = 0x80'u8 shr 7 +let oct1 = 0o200'u8 shr 7 +let dig1 = 0b10000000'u8 shr 7 + +doAssert(ref1 == 1) +doAssert(ref1 == hex1) +doAssert(ref1 == oct1) +doAssert(ref1 == dig1) + +# 16 bit +let ref2 = 32768'u16 shr 15 +let hex2 = 0x8000'u16 shr 15 +let oct2 = 0o100000'u16 shr 15 +let dig2 = 0b1000000000000000'u16 shr 15 + +doAssert(ref2 == 1) +doAssert(ref2 == hex2) +doAssert(ref2 == oct2) +doAssert(ref2 == dig2) + +# 32 bit +let ref3 = 2147483648'u32 shr 31 +let hex3 = 0x80000000'u32 shr 31 +let oct3 = 0o20000000000'u32 shr 31 +let dig3 = 0b10000000000000000000000000000000'u32 shr 31 + +doAssert(ref3 == 1) +doAssert(ref3 == hex3) +doAssert(ref3 == oct3) +doAssert(ref3 == dig3) + +# Below doesn't work for lexer stage errors... +# doAssert(compiles(0xFF'u8) == true) +# doAssert(compiles(0xFFF'u16) == true) +# doAssert(compiles(0x7FFF'i16) == true) + +# doAssert(compiles(0x123'u8) == false) +# doAssert(compiles(0x123'i8) == false) +# doAssert(compiles(0x123123'u16) == false) +# doAssert(compiles(0x123123'i16) == false) + +# Should compile # +let boundOkHex1 = 0xFF'u8 +let boundOkHex2 = 0xFFFF'u16 +let boundOkHex3 = 0x7FFF'i16 + +let boundOkHex4 = 0x80'i8 +let boundOkHex5 = 0xFF'i8 +let boundOkHex6 = 0xFFFF'i16 +let boundOkHex7 = 0x7FFF'i16 + +# Should _not_ compile # +let boundBreakingHex1 = 0x123'u8 +let boundBreakingHex2 = 0x123'i8 +let boundBreakingHex3 = 0x123123'u16 +let boundBreakingHex4 = 0x123123'i16 diff --git a/tests/int/twrongexplicitvarconv.nim b/tests/int/twrongexplicitvarconv.nim new file mode 100644 index 000000000..79f770e8e --- /dev/null +++ b/tests/int/twrongexplicitvarconv.nim @@ -0,0 +1,16 @@ +discard """ + action: reject + nimout: ''' + but expression 'int(a)' is immutable, not 'var' +''' +""" + +proc `++`(n: var int) = + n += 1 + +var a: int32 = 15 + +++int(a) #[tt.Error +^ type mismatch: got <int>]# + +echo a diff --git a/tests/int/twrongvarconv.nim b/tests/int/twrongvarconv.nim new file mode 100644 index 000000000..db6ac2c53 --- /dev/null +++ b/tests/int/twrongvarconv.nim @@ -0,0 +1,9 @@ +proc `++`(n: var int) = + n += 1 + +var a: int32 = 15 + +++a #[tt.Error +^ type mismatch: got <int32>]# + +echo a |