topenarray.nim 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. discard """
  2. input: "hi"
  3. output: '''
  4. hi
  5. Nim
  6. '''
  7. matrix: "--gc:arc -d:useMalloc; --gc:arc"
  8. """
  9. {.experimental: "views".}
  10. block: # bug 18627
  11. proc setPosition(params: openArray[string]) =
  12. for i in params.toOpenArray(0, params.len - 1):
  13. echo i
  14. proc uciLoop() =
  15. let params = @[readLine(stdin)]
  16. setPosition(params)
  17. uciLoop()
  18. proc uciLoop2() =
  19. let params = @["Nim"]
  20. for i in params.toOpenArray(0, params.len - 1):
  21. echo i
  22. uciLoop2()
  23. when defined(nimPreviewSlimSystem):
  24. import std/assertions
  25. block: # bug #20954
  26. block:
  27. doAssertRaises(IndexDefect):
  28. var v: array[10, int]
  29. echo len(toOpenArray(v, 20, 30))
  30. block:
  31. doAssertRaises(IndexDefect):
  32. var v: seq[int]
  33. echo len(toOpenArray(v, 20, 30))
  34. # bug #20422
  35. proc f(a: var string) =
  36. var v = a.toOpenArray(1, 3)
  37. v[0] = 'a'
  38. var a = "Hello"
  39. f(a)
  40. doAssert a == "Hallo"
  41. # bug #22132
  42. block:
  43. func foo[T](arr: openArray[T], idx: int = arr.low): string =
  44. doAssert idx == 0
  45. return $arr
  46. let bug = ["0", "c", "a"]
  47. let str = foo(bug)
  48. const expected = """["0", "c", "a"]"""
  49. doAssert str == expected
  50. const noBugConst = ["0", "c", "a"]
  51. doAssert foo(noBugConst) == expected
  52. let noBugSeq = @["0", "c", "a"]
  53. doAssert foo(noBugSeq) == expected