tstring_openarray.nim 957 B

1234567891011121314151617181920212223242526272829303132333435
  1. # tests various bug when passing string to openArray argument in VM.
  2. # bug #6086
  3. proc map*[T, S](data: openArray[T], op: proc (x: T): S {.closure.}):
  4. seq[S]{.inline.} =
  5. # map inlined from sequtils
  6. newSeq(result, data.len)
  7. for i in 0..data.len-1: result[i] = op(data[i])
  8. proc set_all[T](s: var openArray[T]; val: T) =
  9. for i in 0..<s.len:
  10. s[i] = val
  11. proc test() =
  12. var a0 = "hello_world"
  13. var a1 = [1,2,3,4,5,6,7,8,9]
  14. var a2 = @[1,2,3,4,5,6,7,8,9]
  15. a0.set_all('i')
  16. a1.set_all(4)
  17. a2.set_all(4)
  18. doAssert a0 == "iiiiiiiiiii"
  19. doAssert a1 == [4,4,4,4,4,4,4,4,4]
  20. doAssert a2 == @[4,4,4,4,4,4,4,4,4]
  21. const constval0 = "hello".map(proc(x: char): char = x)
  22. const constval1 = [1,2,3,4].map(proc(x: int): int = x)
  23. doAssert("hello".map(proc(x: char): char = x) == constval0)
  24. doAssert([1,2,3,4].map(proc(x: int): int = x) == constval1)
  25. test()
  26. static:
  27. test()