summary refs log tree commit diff stats
path: root/tests/range/trange.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/range/trange.nim')
-rw-r--r--tests/range/trange.nim107
1 files changed, 107 insertions, 0 deletions
diff --git a/tests/range/trange.nim b/tests/range/trange.nim
new file mode 100644
index 000000000..bc48e9566
--- /dev/null
+++ b/tests/range/trange.nim
@@ -0,0 +1,107 @@
+discard """
+  output: '''
+TSubRange: 5 from 1 to 10
+#FF3722
+'''
+"""
+
+
+block tbug499771:
+  type
+    TSubRange = range[1 .. 10]
+    TEnum = enum A, B, C
+  var sr: TSubRange = 5
+  echo("TSubRange: " & $sr & " from " & $low(TSubRange) & " to " &
+       $high(TSubRange))
+
+  const cset = {A} + {B}
+  doAssert A in cset
+  doAssert B in cset
+  doAssert C notin cset
+
+
+
+include compilehelpers
+block tmatrix3:
+  type
+    Matrix[M, N, T] = object
+      aij: array[M, array[N, T]]
+
+    Matrix2[T] = Matrix[range[0..1], range[0..1], T]
+
+    Matrix3[T] = Matrix[range[0..2], range[0..2], T]
+
+  proc mn(x: Matrix): Matrix.T = x.aij[0][0]
+
+  proc m2(x: Matrix2): Matrix2.T = x.aij[0][0]
+
+  proc m3(x: Matrix3): auto = x.aij[0][0]
+
+  var
+    matn: Matrix[range[0..3], range[0..2], int]
+    mat2: Matrix2[int]
+    mat3: Matrix3[float]
+
+  doAssert m3(mat3) == 0.0
+  doAssert mn(mat3) == 0.0
+  doAssert m2(mat2) == 0
+  doAssert mn(mat2) == 0
+  doAssert mn(matn) == 0
+
+  reject m3(mat2)
+  reject m3(matn)
+  reject m2(mat3)
+  reject m2(matn)
+
+
+
+block tn8vsint16:
+  type
+    n32 = range[0..high(int)]
+    n8 = range[0'i8..high(int8)]
+
+  proc `+`(a: n32, b: n32{nkIntLit}): n32 = discard
+
+  proc `-`(a: n8, b: n8): n8 = n8(system.`-`(a, b))
+
+  var x, y: n8
+  var z: int16
+
+  # ensure this doesn't call our '-' but system.`-` for int16:
+  doAssert z - n8(9) == -9
+
+
+
+import strutils
+block tcolors:
+  type TColor = distinct int32
+
+  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)
+  echo rgb(34, 55, 255)
+
+  when false:
+    type
+      TColor = distinct int32
+      TColorComponent = distinct int8
+
+    proc red(a: TColor): TColorComponent =
+      result = TColorComponent(int32(a) and 0xff'i32)
+    proc green(a: TColor): TColorComponent =
+      result = TColorComponent(int32(a) shr 8'i32 and 0xff'i32)
+    proc blue(a: TColor): TColorComponent =
+      result = TColorComponent(int32(a) shr 16'i32 and 0xff'i32)
+    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))
+
+    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))
+
+    rgb(34, 55, 255)