tcollections.nim 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. discard """
  2. output: ""
  3. """
  4. import sets, tables, deques, lists, critbits, sequtils
  5. block tapply:
  6. var x = @[1, 2, 3]
  7. x.apply(proc(x: var int) = x = x+10)
  8. x.apply(proc(x: int): int = x+100)
  9. x.applyIt(it+5000)
  10. doAssert x == @[5111, 5112, 5113]
  11. block tdeques:
  12. proc index(self: Deque[int], idx: Natural): int =
  13. self[idx]
  14. proc main =
  15. var testDeque = initDeque[int]()
  16. testDeque.addFirst(1)
  17. assert testDeque.index(0) == 1
  18. main()
  19. block tmapit:
  20. var x = @[1, 2, 3]
  21. # This mapIt call will run with preallocation because ``len`` is available.
  22. var y = x.mapIt($(it+10))
  23. doAssert y == @["11", "12", "13"]
  24. type structureWithoutLen = object
  25. a: array[5, int]
  26. iterator items(s: structureWithoutLen): int {.inline.} =
  27. yield s.a[0]
  28. yield s.a[1]
  29. yield s.a[2]
  30. yield s.a[3]
  31. yield s.a[4]
  32. var st: structureWithoutLen
  33. st.a[0] = 0
  34. st.a[1] = 1
  35. st.a[2] = 2
  36. st.a[3] = 3
  37. st.a[4] = 4
  38. # this will run without preallocating the result
  39. # since ``len`` is not available
  40. var r = st.mapIt($(it+10))
  41. doAssert r == @["10", "11", "12", "13", "14"]
  42. # Collections to string:
  43. # Tests for tuples
  44. doAssert $(1, 2, 3) == "(1, 2, 3)"
  45. doAssert $("1", "2", "3") == """("1", "2", "3")"""
  46. doAssert $('1', '2', '3') == """('1', '2', '3')"""
  47. # Tests for seqs
  48. doAssert $(@[1, 2, 3]) == "@[1, 2, 3]"
  49. doAssert $(@["1", "2", "3"]) == """@["1", "2", "3"]"""
  50. doAssert $(@['1', '2', '3']) == """@['1', '2', '3']"""
  51. # Tests for sets
  52. doAssert $(toHashSet([1])) == "{1}"
  53. doAssert $(toHashSet(["1"])) == """{"1"}"""
  54. doAssert $(toHashSet(['1'])) == """{'1'}"""
  55. doAssert $(toOrderedSet([1, 2, 3])) == "{1, 2, 3}"
  56. doAssert $(toOrderedSet(["1", "2", "3"])) == """{"1", "2", "3"}"""
  57. doAssert $(toOrderedSet(['1', '2', '3'])) == """{'1', '2', '3'}"""
  58. # Tests for tables
  59. when defined(nimIntHash1):
  60. doAssert $({1: "1", 2: "2"}.toTable) == """{1: "1", 2: "2"}"""
  61. else:
  62. doAssert $({1: "1", 2: "2"}.toTable) == """{2: "2", 1: "1"}"""
  63. let tabStr = $({"1": 1, "2": 2}.toTable)
  64. doAssert (tabStr == """{"2": 2, "1": 1}""" or tabStr == """{"1": 1, "2": 2}""")
  65. # Tests for deques
  66. block:
  67. var d = initDeque[int]()
  68. d.addLast(1)
  69. doAssert $d == "[1]"
  70. block:
  71. var d = initDeque[string]()
  72. d.addLast("1")
  73. doAssert $d == """["1"]"""
  74. block:
  75. var d = initDeque[char]()
  76. d.addLast('1')
  77. doAssert $d == "['1']"
  78. # Tests for lists
  79. block:
  80. var l = initDoublyLinkedList[int]()
  81. l.append(1)
  82. l.append(2)
  83. l.append(3)
  84. doAssert $l == "[1, 2, 3]"
  85. block:
  86. var l = initDoublyLinkedList[string]()
  87. l.append("1")
  88. l.append("2")
  89. l.append("3")
  90. doAssert $l == """["1", "2", "3"]"""
  91. block:
  92. var l = initDoublyLinkedList[char]()
  93. l.append('1')
  94. l.append('2')
  95. l.append('3')
  96. doAssert $l == """['1', '2', '3']"""
  97. # Tests for critbits
  98. block:
  99. var t: CritBitTree[int]
  100. t["a"] = 1
  101. doAssert $t == """{"a": 1}"""
  102. block:
  103. var t: CritBitTree[string]
  104. t["a"] = "1"
  105. doAssert $t == """{"a": "1"}"""
  106. block:
  107. var t: CritBitTree[char]
  108. t["a"] = '1'
  109. doAssert $t == """{"a": '1'}"""
  110. # Test escaping behavior
  111. block:
  112. var s = ""
  113. s.addQuoted('\0')
  114. s.addQuoted('\31')
  115. s.addQuoted('\127')
  116. doAssert s == "'\\x00''\\x1F''\\x7F'"
  117. block:
  118. var s = ""
  119. s.addQuoted('\\')
  120. s.addQuoted('\'')
  121. s.addQuoted('\"')
  122. doAssert s == """'\\''\'''\"'"""
  123. block:
  124. var s = ""
  125. s.addQuoted("å")
  126. s.addQuoted("ä")
  127. s.addQuoted("ö")
  128. s.addEscapedChar('\xFF')
  129. doAssert s == """"å""ä""ö"\xFF"""
  130. # Test customized element representation
  131. type CustomString = object
  132. proc addQuoted(s: var string, x: CustomString) =
  133. s.add("<CustomString>")
  134. block:
  135. let s = @[CustomString()]
  136. doAssert $s == "@[<CustomString>]"