diff options
Diffstat (limited to 'tests/range')
-rw-r--r-- | tests/range/t19678.nim | 17 | ||||
-rw-r--r-- | tests/range/tcompiletime_range_checks.nim | 6 | ||||
-rw-r--r-- | tests/range/texplicitvarconv.nim | 13 | ||||
-rw-r--r-- | tests/range/toutofrangevarconv.nim | 14 | ||||
-rw-r--r-- | tests/range/trange.nim | 34 |
5 files changed, 70 insertions, 14 deletions
diff --git a/tests/range/t19678.nim b/tests/range/t19678.nim new file mode 100644 index 000000000..88f7eff89 --- /dev/null +++ b/tests/range/t19678.nim @@ -0,0 +1,17 @@ +discard """ + cmd: "nim check --hints:off $file" + errormsg: "" + nimout: ''' +t19678.nim(13, 13) Error: range of string is invalid + + + +''' +""" + +case "5": + of "0" .. "9": + discard + else: + discard + diff --git a/tests/range/tcompiletime_range_checks.nim b/tests/range/tcompiletime_range_checks.nim index 37095e0b7..2d3f292ec 100644 --- a/tests/range/tcompiletime_range_checks.nim +++ b/tests/range/tcompiletime_range_checks.nim @@ -1,8 +1,8 @@ discard """ - cmd: "nim check --hint[Processing]:off --hint[Conf]:off $file" + cmd: "nim check --hint:Processing:off --hint:Conf:off $file" errormsg: "18446744073709551615 can't be converted to int8" - nimout: '''tcompiletime_range_checks.nim(36, 21) Error: 2147483648 can't be converted to int32 -tcompiletime_range_checks.nim(37, 23) Error: -1 can't be converted to uint64 + nimout: ''' +tcompiletime_range_checks.nim(36, 21) Error: 2147483648 can't be converted to int32 tcompiletime_range_checks.nim(38, 34) Error: 255 can't be converted to FullNegativeRange tcompiletime_range_checks.nim(39, 34) Error: 18446744073709551615 can't be converted to HalfNegativeRange tcompiletime_range_checks.nim(40, 34) Error: 300 can't be converted to FullPositiveRange diff --git a/tests/range/texplicitvarconv.nim b/tests/range/texplicitvarconv.nim new file mode 100644 index 000000000..8da8a8878 --- /dev/null +++ b/tests/range/texplicitvarconv.nim @@ -0,0 +1,13 @@ +# related to issue #24032 + +proc `++`(n: var int) = + n += 1 + +type + r = range[ 0..15 ] + +var a: r = 14 + +++int(a) # this should be mutable + +doAssert a == 15 diff --git a/tests/range/toutofrangevarconv.nim b/tests/range/toutofrangevarconv.nim new file mode 100644 index 000000000..1ee4d340e --- /dev/null +++ b/tests/range/toutofrangevarconv.nim @@ -0,0 +1,14 @@ +discard """ + outputsub: "value out of range: 5 notin 0 .. 3 [RangeDefect]" + exitcode: "1" +""" + +# make sure out of bounds range conversion is detected for `var` conversions + +type R = range[0..3] + +proc foo(x: var R) = + doAssert x in 0..3 + +var x = 5 +foo(R(x)) diff --git a/tests/range/trange.nim b/tests/range/trange.nim index c864387f6..abfa7d474 100644 --- a/tests/range/trange.nim +++ b/tests/range/trange.nim @@ -74,37 +74,37 @@ block tn8vsint16: import strutils block tcolors: - type TColor = distinct int32 + type TColor = distinct uint32 proc rgb(r, g, b: range[0..255]): TColor = result = TColor(r or g shl 8 or b shl 16) proc `$`(c: TColor): string = - result = "#" & toHex(int32(c), 6) + result = "#" & toHex(uint32(c), 6) echo rgb(34, 55, 255) - when false: + block: type - TColor = distinct int32 - TColorComponent = distinct int8 + TColor = distinct uint32 + TColorComponent = distinct uint8 proc red(a: TColor): TColorComponent = - result = TColorComponent(int32(a) and 0xff'i32) + result = TColorComponent(uint32(a) and 0xff'u32) proc green(a: TColor): TColorComponent = - result = TColorComponent(int32(a) shr 8'i32 and 0xff'i32) + result = TColorComponent(uint32(a) shr 8'u32 and 0xff'u32) proc blue(a: TColor): TColorComponent = - result = TColorComponent(int32(a) shr 16'i32 and 0xff'i32) + result = TColorComponent(uint32(a) shr 16'u32 and 0xff'u32) proc rgb(r, g, b: range[0..255]): TColor = result = TColor(r or g shl 8 or b shl 8) proc `+!` (a, b: TColorComponent): TColorComponent = ## saturated arithmetic: - result = TColorComponent(min(ze(int8(a)) + ze(int8(b)), 255)) + result = TColorComponent(min(int(uint8(a)) + int(uint8(b)), 255)) proc `+` (a, b: TColor): TColor = ## saturated arithmetic for colors makes sense, I think: - return rgb(red(a) +! red(b), green(a) +! green(b), blue(a) +! blue(b)) + return rgb(int(red(a) +! red(b)), int(green(a) +! green(b)), int(blue(a) +! blue(b))) - rgb(34, 55, 255) + discard rgb(34, 55, 255) block: type @@ -142,3 +142,15 @@ var a: array[4'u, string] for i in 0..<a.len: a[i] = "foo" + +# Check range to ordinal conversions +block: + var + a: int16 + b: range[0'i32..45'i32] = 3 + c: uint16 + d: range[0'u32..46'u32] = 3 + a = b + c = d + doAssert a == b + doAssert c == d |