diff options
author | LemonBoy <LemonBoy@users.noreply.github.com> | 2018-09-08 19:41:07 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2018-09-08 19:41:07 +0200 |
commit | 7107ec05de3a231799dbba66564fb2e22e94217a (patch) | |
tree | 09d435527b46b1b4e8b98153368f5a8b08c3dc94 /tests/arithm | |
parent | 3f16711254bffaa48dcb9ef0ac1cabc2fad7c5d7 (diff) | |
download | Nim-7107ec05de3a231799dbba66564fb2e22e94217a.tar.gz |
Narrowing casts are applied after every op if needed (#8918)
This way we make sure not to end up with weird values every now and then. Fixes #7300 Fixes #8909
Diffstat (limited to 'tests/arithm')
-rw-r--r-- | tests/arithm/tcast.nim | 43 |
1 files changed, 39 insertions, 4 deletions
diff --git a/tests/arithm/tcast.nim b/tests/arithm/tcast.nim index 954e2e677..4017ed1c5 100644 --- a/tests/arithm/tcast.nim +++ b/tests/arithm/tcast.nim @@ -4,6 +4,9 @@ B0 B1 B2 B3 +B4 +B5 +B6 ''' """ @@ -14,6 +17,14 @@ template crossCheck(ty: untyped, exp: untyped) = 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 + block: when true: echo "B0" @@ -34,10 +45,34 @@ block: crossCheck(uint64, (-1).uint64 + 5'u64) echo "B3" - crossCheck(int8, 0'u8 - 5'u8) - crossCheck(int16, 0'u16 - 5'u16) - crossCheck(int32, 0'u32 - 5'u32) - crossCheck(int64, 0'u64 - 5'u64) + doAssert $sub1(0'u8) == "255" + doAssert $sub1(0'u16) == "65535" + doAssert $sub1(0'u32) == "4294967295" + + echo "B4" + doAssert $add1(255'u8) == "0" + doAssert $add1(65535'u16) == "0" + doAssert $add1(4294967295'u32) == "0" + + echo "B5" + 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) + + echo "B6" + crossCheck(int64, 0xFFFFFFFFFFFFFFFF'u64) + crossCheck(int32, 0xFFFFFFFFFFFFFFFF'u64) + crossCheck(int16, 0xFFFFFFFFFFFFFFFF'u64) + crossCheck(int8 , 0xFFFFFFFFFFFFFFFF'u64) + + # Out of range conversion, caught for `let`s only + # crossCheck(int8, 0'u8 - 5'u8) + # crossCheck(int16, 0'u16 - 5'u16) + # crossCheck(int32, 0'u32 - 5'u32) + # crossCheck(int64, 0'u64 - 5'u64) # crossCheck(int8, 0'u16 - 129'u16) # crossCheck(uint8, 0'i16 + 257'i16) |