tcollections.nim 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. discard """
  2. targets: "c js"
  3. """
  4. # see also: tdeques, tlists, tcritbits
  5. import sets, tables, sequtils
  6. block tapply:
  7. var x = @[1, 2, 3]
  8. x.apply(proc(x: var int) = x = x+10)
  9. x.apply(proc(x: int): int = x+100)
  10. x.applyIt(it+5000)
  11. doAssert x == @[5111, 5112, 5113]
  12. block tmapit:
  13. var x = @[1, 2, 3]
  14. # This mapIt call will run with preallocation because ``len`` is available.
  15. var y = x.mapIt($(it+10))
  16. doAssert y == @["11", "12", "13"]
  17. type structureWithoutLen = object
  18. a: array[5, int]
  19. iterator items(s: structureWithoutLen): int {.inline.} =
  20. yield s.a[0]
  21. yield s.a[1]
  22. yield s.a[2]
  23. yield s.a[3]
  24. yield s.a[4]
  25. var st: structureWithoutLen
  26. st.a[0] = 0
  27. st.a[1] = 1
  28. st.a[2] = 2
  29. st.a[3] = 3
  30. st.a[4] = 4
  31. # this will run without preallocating the result
  32. # since ``len`` is not available
  33. var r = st.mapIt($(it+10))
  34. doAssert r == @["10", "11", "12", "13", "14"]
  35. # Collections to string:
  36. # Tests for tuples
  37. doAssert $(1, 2, 3) == "(1, 2, 3)"
  38. doAssert $("1", "2", "3") == """("1", "2", "3")"""
  39. doAssert $('1', '2', '3') == """('1', '2', '3')"""
  40. # Tests for seqs
  41. doAssert $(@[1, 2, 3]) == "@[1, 2, 3]"
  42. doAssert $(@["1", "2", "3"]) == """@["1", "2", "3"]"""
  43. doAssert $(@['1', '2', '3']) == """@['1', '2', '3']"""
  44. # Tests for sets
  45. doAssert $(toHashSet([1])) == "{1}"
  46. doAssert $(toHashSet(["1"])) == """{"1"}"""
  47. doAssert $(toHashSet(['1'])) == """{'1'}"""
  48. doAssert $(toOrderedSet([1, 2, 3])) == "{1, 2, 3}"
  49. doAssert $(toOrderedSet(["1", "2", "3"])) == """{"1", "2", "3"}"""
  50. doAssert $(toOrderedSet(['1', '2', '3'])) == """{'1', '2', '3'}"""
  51. # see also: tcritbitsToString, tlistsToString
  52. # Tests for tables
  53. when defined(nimIntHash1):
  54. doAssert $({1: "1", 2: "2"}.toTable) == """{1: "1", 2: "2"}"""
  55. else:
  56. doAssert $({1: "1", 2: "2"}.toTable) == """{2: "2", 1: "1"}"""
  57. let tabStr = $({"1": 1, "2": 2}.toTable)
  58. doAssert (tabStr == """{"2": 2, "1": 1}""" or tabStr == """{"1": 1, "2": 2}""")
  59. # Test escaping behavior
  60. block:
  61. var s = ""
  62. s.addQuoted('\0')
  63. s.addQuoted('\31')
  64. s.addQuoted('\127')
  65. doAssert s == "'\\x00''\\x1F''\\x7F'"
  66. block:
  67. var s = ""
  68. s.addQuoted('\\')
  69. s.addQuoted('\'')
  70. s.addQuoted('\"')
  71. doAssert s == """'\\''\'''\"'"""
  72. block:
  73. var s = ""
  74. s.addQuoted("å")
  75. s.addQuoted("ä")
  76. s.addQuoted("ö")
  77. s.addEscapedChar('\xFF')
  78. doAssert s == """"å""ä""ö"\xFF"""
  79. # Test customized element representation
  80. type CustomString = object
  81. proc addQuoted(s: var string, x: CustomString) =
  82. s.add("<CustomString>")
  83. block:
  84. let s = @[CustomString()]
  85. doAssert $s == "@[<CustomString>]"