tarc_orc.nim 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. discard """
  2. targets: "c cpp"
  3. matrix: "--mm:arc; --mm:orc"
  4. """
  5. block:
  6. type
  7. PublicKey = array[32, uint8]
  8. PrivateKey = array[64, uint8]
  9. proc ed25519_create_keypair(publicKey: ptr PublicKey; privateKey: ptr PrivateKey) =
  10. publicKey[][0] = uint8(88)
  11. type
  12. KeyPair = object
  13. public: PublicKey
  14. private: PrivateKey
  15. proc initKeyPair(): KeyPair =
  16. ed25519_create_keypair(result.public.addr, result.private.addr)
  17. let keys = initKeyPair()
  18. doAssert keys.public[0] == 88
  19. template minIndexByIt: untyped =
  20. var other = 3
  21. other
  22. proc bug20303() =
  23. var hlibs = @["hello", "world", "how", "are", "you"]
  24. let res = hlibs[minIndexByIt()]
  25. doAssert res == "are"
  26. bug20303()
  27. proc main() = # todo bug with templates
  28. block: # bug #11267
  29. var a: seq[char] = block: @[]
  30. doAssert a == @[]
  31. # 2
  32. proc b: seq[string] =
  33. discard
  34. @[]
  35. doAssert b() == @[]
  36. static: main()
  37. main()
  38. type Obj = tuple
  39. value: int
  40. arr: seq[int]
  41. proc bug(): seq[Obj] =
  42. result.add (value: 0, arr: @[])
  43. result[^1].value = 1
  44. result[^1].arr.add 1
  45. # bug #19990
  46. let s = bug()
  47. doAssert s[0] == (value: 1, arr: @[1])
  48. block: # bug #21974
  49. type Test[T] = ref object
  50. values : seq[T]
  51. counter: int
  52. proc newTest[T](): Test[T] =
  53. result = new(Test[T])
  54. result.values = newSeq[T](16)
  55. result.counter = 0
  56. proc push[T](self: Test[T], value: T) =
  57. self.counter += 1
  58. if self.counter >= self.values.len:
  59. self.values.setLen(self.values.len * 2)
  60. self.values[self.counter - 1] = value
  61. proc pop[T](self: Test[T]): T =
  62. result = self.values[0]
  63. self.values[0] = self.values[self.counter - 1] # <--- This line
  64. self.counter -= 1
  65. type X = tuple
  66. priority: int
  67. value : string
  68. var a = newTest[X]()
  69. a.push((1, "One"))
  70. doAssert a.pop.value == "One"
  71. # bug #21987
  72. type
  73. EmbeddedImage* = distinct Image
  74. Image = object
  75. len: int
  76. proc imageCopy*(image: Image): Image {.nodestroy.}
  77. proc `=destroy`*(x: var Image) =
  78. discard
  79. proc `=sink`*(dest: var Image; source: Image) =
  80. `=destroy`(dest)
  81. wasMoved(dest)
  82. proc `=dup`*(source: Image): Image {.nodestroy.} =
  83. result = imageCopy(source)
  84. proc `=copy`*(dest: var Image; source: Image) =
  85. dest = imageCopy(source) # calls =sink implicitly
  86. proc `=destroy`*(x: var EmbeddedImage) = discard
  87. proc `=dup`*(source: EmbeddedImage): EmbeddedImage {.nodestroy.} = source
  88. proc `=copy`*(dest: var EmbeddedImage; source: EmbeddedImage) {.nodestroy.} =
  89. dest = source
  90. proc imageCopy*(image: Image): Image =
  91. result = image
  92. proc main2 =
  93. block:
  94. var a = Image(len: 2).EmbeddedImage
  95. var b = Image(len: 1).EmbeddedImage
  96. b = a
  97. doAssert Image(a).len == 2
  98. doAssert Image(b).len == 2
  99. block:
  100. var a = Image(len: 2)
  101. var b = Image(len: 1)
  102. b = a
  103. doAssert a.len == 2
  104. doAssert b.len == 0
  105. main2()