tmarshal.nim 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. discard """
  2. cmd: "nim c --gc:orc $file"
  3. output: '''
  4. {"age": 12, "bio": "Я Cletus", "blob": [65, 66, 67, 128], "name": "Cletus"}
  5. true
  6. true
  7. alpha 100
  8. omega 200
  9. 0'''
  10. """
  11. import marshal
  12. template testit(x) = discard $$to[typeof(x)]($$x)
  13. var x: array[0..4, array[0..4, string]] = [
  14. ["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"],
  15. ["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"],
  16. ["test", "1", "2", "3", "4"]]
  17. proc blockA =
  18. testit(x)
  19. var test2: tuple[name: string, s: int] = ("tuple test", 56)
  20. testit(test2)
  21. blockA()
  22. type
  23. TE = enum
  24. blah, blah2
  25. TestObj = object
  26. test, asd: int
  27. case test2: TE
  28. of blah:
  29. help: string
  30. else:
  31. discard
  32. PNode = ref TNode
  33. TNode = object
  34. next, prev: PNode
  35. data: string
  36. proc buildList(): PNode =
  37. new(result)
  38. new(result.next)
  39. new(result.prev)
  40. result.data = "middle"
  41. result.next.data = "next"
  42. result.prev.data = "prev"
  43. result.next.next = result.prev
  44. result.next.prev = result
  45. result.prev.next = result
  46. result.prev.prev = result.next
  47. proc blockB =
  48. var test3: TestObj
  49. test3.test = 42
  50. test3.test2 = blah
  51. testit(test3)
  52. var test4: ref tuple[a, b: string]
  53. new(test4)
  54. test4.a = "ref string test: A"
  55. test4.b = "ref string test: B"
  56. testit(test4)
  57. var test5 = @[(0,1),(2,3),(4,5)]
  58. testit(test5)
  59. var test7 = buildList()
  60. testit(test7)
  61. var test6: set[char] = {'A'..'Z', '_'}
  62. testit(test6)
  63. blockB()
  64. # bug #1352
  65. type
  66. Entity = object of RootObj
  67. name: string
  68. Person = object of Entity
  69. age: int
  70. bio: string
  71. blob: string
  72. proc blockC =
  73. var instance1 = Person(name: "Cletus", age: 12,
  74. bio: "Я Cletus",
  75. blob: "ABC\x80")
  76. echo($$instance1)
  77. echo(to[Person]($$instance1).bio == instance1.bio) # true
  78. echo(to[Person]($$instance1).blob == instance1.blob) # true
  79. blockC()
  80. # bug 5757
  81. type
  82. Something = object
  83. x: string
  84. y: int
  85. proc blockD =
  86. var data1 = """{"x": "alpha", "y": 100}"""
  87. var data2 = """{"x": "omega", "y": 200}"""
  88. var r = to[Something](data1)
  89. echo r.x, " ", r.y
  90. r = to[Something](data2)
  91. echo r.x, " ", r.y
  92. blockD()
  93. type
  94. Foo = object
  95. a1: string
  96. a2: string
  97. a3: seq[string]
  98. a4: seq[int]
  99. a5: seq[int]
  100. a6: seq[int]
  101. proc blockE =
  102. var foo = Foo(a2: "", a4: @[], a6: @[1])
  103. foo.a6.setLen 0
  104. doAssert $$foo == """{"a1": "", "a2": "", "a3": [], "a4": [], "a5": [], "a6": []}"""
  105. testit(foo)
  106. blockE()
  107. GC_fullCollect()
  108. echo getOccupiedMem()