tviews2.nim 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. discard """
  2. targets: "c js"
  3. """
  4. {.experimental: "views".}
  5. block:
  6. type
  7. Foo = object
  8. id: openArray[char]
  9. proc foo(): Foo =
  10. var source = "1245"
  11. result = Foo(id: source.toOpenArray(0, 1))
  12. doAssert foo().id == @['1', '2']
  13. block: # bug #15778
  14. type
  15. Reader = object
  16. data: openArray[char]
  17. current: int
  18. var count = 0
  19. proc read(data: var Reader, length: int): openArray[char] =
  20. inc count
  21. let start = data.current
  22. data.current.inc length
  23. return data.data.toOpenArray(start, data.current-1)
  24. var data = "hello there"
  25. var reader = Reader(data: data.toOpenArray(0, data.len-1), current: 0)
  26. doAssert @(reader.read(2)) == @['h', 'e']
  27. doAssert @(reader.read(3)) == @['l', 'l', 'o']
  28. doAssert count == 2
  29. block: # bug #16671
  30. block:
  31. type X = ref object of RootObj
  32. type Y = ref object of X
  33. field: openArray[int]
  34. var s: seq[X]
  35. proc f() =
  36. s.add(Y(field: [1]))
  37. f()
  38. block:
  39. type X = ref object of RootObj
  40. type Y = ref object of X
  41. field: openArray[int]
  42. var s: seq[X]
  43. proc f() =
  44. s.add(Y(field: toOpenArray([1, 2, 3], 0, 1)))
  45. f()
  46. block: # bug #15746
  47. type
  48. Reader = object
  49. data: openArray[char]
  50. current: int
  51. proc initReader(data: openArray[char], offset = 0): Reader =
  52. result = Reader(data: data, current: offset)
  53. let s = "\x01\x00\x00\x00"
  54. doAssert initReader(s).data[0].int == 1
  55. block:
  56. proc foo(x: openArray[char]) =
  57. discard x
  58. foo("12254")
  59. foo(@['a', 'b'])
  60. var a1 = "12254"
  61. foo(a1)
  62. var a2 = @['a', 'b']
  63. foo(a2)
  64. var s = "138443"
  65. var ooo: openArray[char] = s
  66. var xxx: openArray[char] = ooo
  67. foo(ooo)
  68. foo(xxx)