tisolation.nim 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. discard """
  2. targets: "c cpp"
  3. matrix: "--gc:refc; --gc:orc"
  4. """
  5. import std/[isolation, json]
  6. import std/[assertions, objectdollar]
  7. proc main(moveZeroesOut: static bool) =
  8. block:
  9. type
  10. Empty = ref object
  11. var x = isolate(Empty())
  12. discard extract(x)
  13. block: # string literals
  14. var data = isolate("string")
  15. doAssert data.extract == "string"
  16. if moveZeroesOut:
  17. doAssert data.extract == ""
  18. block: # string literals
  19. var data = isolate("")
  20. doAssert data.extract == ""
  21. if moveZeroesOut:
  22. doAssert data.extract == ""
  23. block:
  24. var src = "string"
  25. var data = isolate(move src)
  26. doAssert data.extract == "string"
  27. if moveZeroesOut:
  28. doAssert src.len == 0
  29. block: # int literals
  30. var data = isolate(1)
  31. doAssert data.extract == 1
  32. if moveZeroesOut:
  33. doAssert data.extract == 0
  34. block: # float literals
  35. var data = isolate(1.6)
  36. doAssert data.extract == 1.6
  37. if moveZeroesOut:
  38. doAssert data.extract == 0.0
  39. block:
  40. var data = isolate(@["1", "2"])
  41. doAssert data.extract == @["1", "2"]
  42. if moveZeroesOut:
  43. doAssert data.extract == @[]
  44. block:
  45. var data = isolate(@["1", "2", "3", "4", "5"])
  46. doAssert data.extract == @["1", "2", "3", "4", "5"]
  47. if moveZeroesOut:
  48. doAssert data.extract == @[]
  49. block:
  50. var data = isolate(@["", ""])
  51. doAssert data.extract == @["", ""]
  52. if moveZeroesOut:
  53. doAssert data.extract == @[]
  54. block:
  55. var src = @["1", "2"]
  56. var data = isolate(move src)
  57. doAssert data.extract == @["1", "2"]
  58. if moveZeroesOut:
  59. doAssert src.len == 0
  60. block:
  61. var data = isolate(@[1, 2])
  62. doAssert data.extract == @[1, 2]
  63. if moveZeroesOut:
  64. doAssert data.extract == @[]
  65. block:
  66. var data = isolate(["1", "2"])
  67. doAssert data.extract == ["1", "2"]
  68. if moveZeroesOut:
  69. doAssert data.extract == ["", ""]
  70. block:
  71. var data = isolate([1, 2])
  72. doAssert data.extract == [1, 2]
  73. if moveZeroesOut:
  74. doAssert data.extract == [0, 0]
  75. block:
  76. type
  77. Test = object
  78. id: int
  79. var data = isolate(Test(id: 12))
  80. doAssert data.extract.id == 12
  81. block:
  82. type
  83. Test = object
  84. id: int
  85. var src = Test(id: 12)
  86. var data = isolate(src)
  87. doAssert data.extract.id == 12
  88. block:
  89. type
  90. Test = object
  91. id: int
  92. var src = Test(id: 12)
  93. var data = isolate(move src)
  94. doAssert data.extract.id == 12
  95. block:
  96. type
  97. Test = ref object
  98. id: int
  99. var data = isolate(Test(id: 12))
  100. doAssert data.extract.id == 12
  101. block:
  102. var x: seq[Isolated[JsonNode]]
  103. x.add isolate(newJString("1234"))
  104. doAssert $x == """@[(value: "1234")]"""
  105. static: main(moveZeroesOut = false)
  106. main(moveZeroesOut = true)