trange.nim 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. discard """
  2. output: '''
  3. TSubRange: 5 from 1 to 10
  4. #FF3722
  5. '''
  6. """
  7. block tbug499771:
  8. type
  9. TSubRange = range[1 .. 10]
  10. TEnum = enum A, B, C
  11. var sr: TSubRange = 5
  12. echo("TSubRange: " & $sr & " from " & $low(TSubRange) & " to " &
  13. $high(TSubRange))
  14. const cset = {A} + {B}
  15. doAssert A in cset
  16. doAssert B in cset
  17. doAssert C notin cset
  18. include compilehelpers
  19. block tmatrix3:
  20. type
  21. Matrix[M, N, T] = object
  22. aij: array[M, array[N, T]]
  23. Matrix2[T] = Matrix[range[0..1], range[0..1], T]
  24. Matrix3[T] = Matrix[range[0..2], range[0..2], T]
  25. proc mn(x: Matrix): Matrix.T = x.aij[0][0]
  26. proc m2(x: Matrix2): Matrix2.T = x.aij[0][0]
  27. proc m3(x: Matrix3): auto = x.aij[0][0]
  28. var
  29. matn: Matrix[range[0..3], range[0..2], int]
  30. mat2: Matrix2[int]
  31. mat3: Matrix3[float]
  32. doAssert m3(mat3) == 0.0
  33. doAssert mn(mat3) == 0.0
  34. doAssert m2(mat2) == 0
  35. doAssert mn(mat2) == 0
  36. doAssert mn(matn) == 0
  37. reject m3(mat2)
  38. reject m3(matn)
  39. reject m2(mat3)
  40. reject m2(matn)
  41. block tn8vsint16:
  42. type
  43. n32 = range[0..high(int)]
  44. n8 = range[0'i8..high(int8)]
  45. proc `+`(a: n32, b: n32{nkIntLit}): n32 = discard
  46. proc `-`(a: n8, b: n8): n8 = n8(system.`-`(a, b))
  47. var x, y: n8
  48. var z: int16
  49. # ensure this doesn't call our '-' but system.`-` for int16:
  50. doAssert z - n8(9) == -9
  51. import strutils
  52. block tcolors:
  53. type TColor = distinct uint32
  54. proc rgb(r, g, b: range[0..255]): TColor =
  55. result = TColor(r or g shl 8 or b shl 16)
  56. proc `$`(c: TColor): string =
  57. result = "#" & toHex(uint32(c), 6)
  58. echo rgb(34, 55, 255)
  59. block:
  60. type
  61. TColor = distinct uint32
  62. TColorComponent = distinct uint8
  63. proc red(a: TColor): TColorComponent =
  64. result = TColorComponent(uint32(a) and 0xff'u32)
  65. proc green(a: TColor): TColorComponent =
  66. result = TColorComponent(uint32(a) shr 8'u32 and 0xff'u32)
  67. proc blue(a: TColor): TColorComponent =
  68. result = TColorComponent(uint32(a) shr 16'u32 and 0xff'u32)
  69. proc rgb(r, g, b: range[0..255]): TColor =
  70. result = TColor(r or g shl 8 or b shl 8)
  71. proc `+!` (a, b: TColorComponent): TColorComponent =
  72. ## saturated arithmetic:
  73. result = TColorComponent(min(int(uint8(a)) + int(uint8(b)), 255))
  74. proc `+` (a, b: TColor): TColor =
  75. ## saturated arithmetic for colors makes sense, I think:
  76. return rgb(int(red(a) +! red(b)), int(green(a) +! green(b)), int(blue(a) +! blue(b)))
  77. discard rgb(34, 55, 255)
  78. block:
  79. type
  80. R8 = range[0'u8 .. 10'u8]
  81. R16 = range[0'u16 .. 10'u16]
  82. R32 = range[0'u32 .. 10'u32]
  83. var
  84. x1 = R8(4)
  85. x2 = R16(4)
  86. x3 = R32(4)
  87. doAssert $x1 & $x2 & $x3 == "444"
  88. block:
  89. var x1: range[0'f..1'f] = 1
  90. const x2: range[0'f..1'f] = 1
  91. var x3: range[0'u8..1'u8] = 1
  92. const x4: range[0'u8..1'u8] = 1
  93. var x5: range[0'f32..1'f32] = 1'f64
  94. const x6: range[0'f32..1'f32] = 1'f64
  95. reject:
  96. var x09: range[0'i8..1'i8] = 1.int
  97. reject:
  98. var x10: range[0'i64..1'i64] = 1'u64
  99. const x11: range[0'f..1'f] = 2'f
  100. reject:
  101. const x12: range[0'f..1'f] = 2
  102. # ensure unsigned array indexing is remains lenient:
  103. var a: array[4'u, string]
  104. for i in 0..<a.len:
  105. a[i] = "foo"
  106. # Check range to ordinal conversions
  107. block:
  108. var
  109. a: int16
  110. b: range[0'i32..45'i32] = 3
  111. c: uint16
  112. d: range[0'u32..46'u32] = 3
  113. a = b
  114. c = d
  115. doAssert a == b
  116. doAssert c == d