tcollections_to_string.nim 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. discard """
  2. exitcode: 0
  3. output: ""
  4. """
  5. import sets
  6. import tables
  7. import deques
  8. import lists
  9. import critbits
  10. # Tests for tuples
  11. doAssert $(1, 2, 3) == "(1, 2, 3)"
  12. doAssert $("1", "2", "3") == """("1", "2", "3")"""
  13. doAssert $('1', '2', '3') == """('1', '2', '3')"""
  14. # Tests for seqs
  15. doAssert $(@[1, 2, 3]) == "@[1, 2, 3]"
  16. doAssert $(@["1", "2", "3"]) == """@["1", "2", "3"]"""
  17. doAssert $(@['1', '2', '3']) == """@['1', '2', '3']"""
  18. # Tests for sets
  19. doAssert $(toHashSet([1])) == "{1}"
  20. doAssert $(toHashSet(["1"])) == """{"1"}"""
  21. doAssert $(toHashSet(['1'])) == """{'1'}"""
  22. doAssert $(toOrderedSet([1, 2, 3])) == "{1, 2, 3}"
  23. doAssert $(toOrderedSet(["1", "2", "3"])) == """{"1", "2", "3"}"""
  24. doAssert $(toOrderedSet(['1', '2', '3'])) == """{'1', '2', '3'}"""
  25. # Tests for tables
  26. when defined(nimIntHash1):
  27. doAssert $({1: "1", 2: "2"}.toTable) == """{1: "1", 2: "2"}"""
  28. else:
  29. doAssert $({1: "1", 2: "2"}.toTable) == """{2: "2", 1: "1"}"""
  30. doAssert $({"1": 1, "2": 2}.toTable) == """{"1": 1, "2": 2}"""
  31. # Tests for deques
  32. block:
  33. var d = initDeque[int]()
  34. d.addLast(1)
  35. doAssert $d == "[1]"
  36. block:
  37. var d = initDeque[string]()
  38. d.addLast("1")
  39. doAssert $d == """["1"]"""
  40. block:
  41. var d = initDeque[char]()
  42. d.addLast('1')
  43. doAssert $d == "['1']"
  44. # Tests for lists
  45. block:
  46. var l = initDoublyLinkedList[int]()
  47. l.append(1)
  48. l.append(2)
  49. l.append(3)
  50. doAssert $l == "[1, 2, 3]"
  51. block:
  52. var l = initDoublyLinkedList[string]()
  53. l.append("1")
  54. l.append("2")
  55. l.append("3")
  56. doAssert $l == """["1", "2", "3"]"""
  57. block:
  58. var l = initDoublyLinkedList[char]()
  59. l.append('1')
  60. l.append('2')
  61. l.append('3')
  62. doAssert $l == """['1', '2', '3']"""
  63. # Tests for critbits
  64. block:
  65. var t: CritBitTree[int]
  66. t["a"] = 1
  67. doAssert $t == """{"a": 1}"""
  68. block:
  69. var t: CritBitTree[string]
  70. t["a"] = "1"
  71. doAssert $t == """{"a": "1"}"""
  72. block:
  73. var t: CritBitTree[char]
  74. t["a"] = '1'
  75. doAssert $t == """{"a": '1'}"""
  76. # Test escaping behavior
  77. block:
  78. var s = ""
  79. s.addQuoted('\0')
  80. s.addQuoted('\31')
  81. s.addQuoted('\127')
  82. doAssert s == "'\\x00''\\x1F''\\x7F'"
  83. block:
  84. var s = ""
  85. s.addQuoted('\\')
  86. s.addQuoted('\'')
  87. s.addQuoted('\"')
  88. doAssert s == """'\\''\'''\"'"""
  89. block:
  90. var s = ""
  91. s.addQuoted("å")
  92. s.addQuoted("ä")
  93. s.addQuoted("ö")
  94. s.addEscapedChar('\xFF')
  95. doAssert s == """"å""ä""ö"\xFF"""
  96. # Test customized element representation
  97. type CustomString = object
  98. proc addQuoted(s: var string, x: CustomString) =
  99. s.add("<CustomString>")
  100. block:
  101. let s = @[CustomString()]
  102. doAssert $s == "@[<CustomString>]"