tseqops.nim 869 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # bug #4139
  2. type
  3. TestO = object
  4. x, y: int
  5. proc onLoad() =
  6. var test: seq[TestO] = @[]
  7. var foo = TestO(x: 0, y: 0)
  8. test.add(foo)
  9. foo.x = 5
  10. doAssert $test[0] == "(x: 0, y: 0)"
  11. doAssert $foo == "(x: 5, y: 0)"
  12. onLoad()
  13. # 'setLen' bug (part of bug #5933)
  14. type MyObj = object
  15. x: cstring
  16. y: int
  17. proc foo(x: var seq[MyObj]) =
  18. let L = x.len
  19. x.setLen L + 1
  20. x[L] = x[1]
  21. var s = @[MyObj(x: "2", y: 4), MyObj(x: "4", y: 5)]
  22. foo(s)
  23. doAssert $s == """@[(x: "2", y: 4), (x: "4", y: 5), (x: "4", y: 5)]"""
  24. # bug #5933
  25. import sequtils
  26. type
  27. Test = object
  28. a: cstring
  29. b: int
  30. var test = @[Test(a: "1", b: 1), Test(a: "2", b: 2)]
  31. test.insert(@[Test(a: "3", b: 3)], 0)
  32. doAssert $test == """@[(a: "3", b: 3), (a: "1", b: 1), (a: "2", b: 2)]"""
  33. proc hello(): array[5, int] = discard
  34. var x = @(hello())
  35. x.add(2)
  36. doAssert x == @[0, 0, 0, 0, 0, 2]