topenarray.nim 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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
  54. block: # bug #20865
  55. var p: pointer
  56. var x: array[0, int]
  57. # echo toOpenArray(x, 0, 1)[0] # Raises IndexDefect
  58. doAssertRaises(IndexDefect):
  59. echo toOpenArray(cast[ptr array[0, int]](p)[], 0, 1)[0] # Does not raise IndexDefect
  60. block: # bug #20987
  61. var v: array[1, byte]
  62. var p = cast[ptr array[0, byte]](addr v)
  63. doAssertRaises(IndexDefect):
  64. echo toOpenArray(p[], 1, 2)