tenum.nim 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. discard """
  2. output: '''
  3. B
  4. B
  5. ABCDC
  6. foo
  7. first0second32third64
  8. my value A1my value Bconc2valueCabc4abc
  9. my value A0my value Bconc1valueCabc3abc
  10. '''
  11. """
  12. import macros
  13. block tenum1:
  14. type E = enum a, b, c, x, y, z
  15. var en: E
  16. en = a
  17. # Bug #4066
  18. macro genEnum(): untyped = newNimNode(nnkEnumTy).add(newEmptyNode(), newIdentNode("geItem1"))
  19. type GeneratedEnum = genEnum()
  20. doAssert(type(geItem1) is GeneratedEnum)
  21. block tenum2:
  22. type
  23. TEnumHole = enum
  24. eA = 0,
  25. eB = 4,
  26. eC = 5
  27. var
  28. e: TEnumHole = eB
  29. case e
  30. of eA: echo "A"
  31. of eB: echo "B"
  32. of eC: echo "C"
  33. block tenum3:
  34. type
  35. TEnumHole {.size: sizeof(int).} = enum
  36. eA = 0,
  37. eB = 4,
  38. eC = 5
  39. var
  40. e: TEnumHole = eB
  41. case e
  42. of eA: echo "A"
  43. of eB: echo "B"
  44. of eC: echo "C"
  45. block tbasic:
  46. type
  47. MyEnum = enum
  48. A,B,C,D
  49. # trick the optimizer with an seq:
  50. var x = @[A,B,C,D]
  51. echo x[0],x[1],x[2],x[3],MyEnum(2)
  52. block talias:
  53. # bug #5148
  54. type
  55. A = enum foo, bar
  56. B = A
  57. echo B.foo
  58. block thole:
  59. type Holed = enum
  60. hFirst = (0,"first")
  61. hSecond = (32,"second")
  62. hThird = (64,"third")
  63. var x = @[0,32,64] # This is just to avoid the compiler inlining the value of the enum
  64. echo Holed(x[0]),ord Holed(x[0]),Holed(x[1]),ord Holed(x[1]),Holed(x[2]),ord Holed(x[2])
  65. block toffset:
  66. const
  67. strValB = "my value B"
  68. type
  69. TMyEnum = enum
  70. valueA = (1, "my value A"),
  71. valueB = strValB & "conc",
  72. valueC,
  73. valueD = (4, "abc")
  74. proc getValue(i:int): TMyEnum = TMyEnum(i)
  75. # trick the optimizer with a variable:
  76. var x = getValue(4)
  77. echo getValue(1), ord(valueA), getValue(2), ord(valueB), getValue(3), getValue(4), ord(valueD), x
  78. block tnamedfields:
  79. const strValB = "my value B"
  80. type
  81. TMyEnum = enum
  82. valueA = (0, "my value A"),
  83. valueB = strValB & "conc",
  84. valueC,
  85. valueD = (3, "abc"),
  86. valueE = 4
  87. # trick the optimizer with a variable:
  88. var x = valueD
  89. echo valueA, ord(valueA), valueB, ord(valueB), valueC, valueD, ord(valueD), x
  90. doAssert $x == $valueD, $x
  91. doAssert $x == "abc", $x
  92. block tfakeOptions:
  93. type
  94. TFakeOption = enum
  95. fakeNone, fakeForceFullMake, fakeBoehmGC, fakeRefcGC, fakeRangeCheck,
  96. fakeBoundsCheck, fakeOverflowCheck, fakeNilCheck, fakeAssert, fakeLineDir,
  97. fakeWarns, fakeHints, fakeListCmd, fakeCompileOnly,
  98. fakeSafeCode, # only allow safe code
  99. fakeStyleCheck, fakeOptimizeSpeed, fakeOptimizeSize, fakeGenDynLib,
  100. fakeGenGuiApp, fakeStackTrace
  101. TFakeOptionset = set[TFakeOption]
  102. var
  103. gFakeOptions: TFakeOptionset = {fakeRefcGC, fakeRangeCheck, fakeBoundsCheck,
  104. fakeOverflowCheck, fakeAssert, fakeWarns, fakeHints, fakeLineDir, fakeStackTrace}
  105. compilerArgs: int
  106. gExitcode: int8
  107. block nonzero: # bug #6959
  108. type SomeEnum = enum
  109. A = 10
  110. B
  111. C
  112. let slice = SomeEnum.low..SomeEnum.high
  113. block size_one_byte: # bug #15752
  114. type
  115. Flag = enum
  116. Disabled = 0x00
  117. Enabled = 0xFF
  118. static:
  119. assert 1 == sizeof(Flag)
  120. block: # bug #12589
  121. when not defined(i386):
  122. type
  123. OGRwkbGeometryType {.size: sizeof(cuint).} = enum
  124. wkbPoint25D = 0x80000001.cuint, wkbLineString25D = 0x80000002,
  125. wkbPolygon25D = 0x80000003
  126. proc typ(): OGRwkbGeometryType =
  127. return wkbPoint25D
  128. when not defined(gcRefc):
  129. doAssert $typ() == "wkbPoint25D"
  130. block: # bug #21280
  131. type
  132. Test = enum
  133. B = 19
  134. A = int64.high()
  135. doAssert ord(A) == int64.high()
  136. import std/enumutils
  137. from std/sequtils import toSeq
  138. import std/macros
  139. block: # unordered enum
  140. block:
  141. type
  142. unordered_enum = enum
  143. a = 1
  144. b = 0
  145. doAssert (ord(a), ord(b)) == (1, 0)
  146. doAssert unordered_enum.toSeq == @[a, b]
  147. block:
  148. type
  149. unordered_enum = enum
  150. a = 1
  151. b = 0
  152. c
  153. doAssert (ord(a), ord(b), ord(c)) == (1, 0, 2)
  154. block:
  155. type
  156. unordered_enum = enum
  157. a = 100
  158. b
  159. c = 50
  160. d
  161. doAssert (ord(a), ord(b), ord(c), ord(d)) == (100, 101, 50, 51)
  162. block:
  163. type
  164. unordered_enum = enum
  165. a = 7
  166. b = 6
  167. c = 5
  168. d
  169. doAssert (ord(a), ord(b), ord(c), ord(d)) == (7, 6, 5, 8)
  170. doAssert unordered_enum.toSeq == @[a, b, c, d]
  171. block:
  172. type
  173. unordered_enum = enum
  174. a = 100
  175. b
  176. c = 500
  177. d
  178. e
  179. f = 50
  180. g
  181. h
  182. doAssert (ord(a), ord(b), ord(c), ord(d), ord(e), ord(f), ord(g), ord(h)) ==
  183. (100, 101, 500, 501, 502, 50, 51, 52)
  184. block:
  185. type
  186. unordered_enum = enum
  187. A
  188. B
  189. C = -1
  190. D
  191. E
  192. G = -999
  193. doAssert (ord(A), ord(B), ord(C), ord(D), ord(E), ord(G)) ==
  194. (0, 1, -1, 2, 3, -999)
  195. block:
  196. type
  197. SomeEnum = enum
  198. seA = 3
  199. seB = 2
  200. seC = "foo"
  201. doAssert (ord(seA), ord(seB), ord(seC)) == (3, 2, 4)