tstdlib_various.nim 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. discard """
  2. output: '''
  3. abc
  4. def
  5. definition
  6. prefix
  7. xyz
  8. def
  9. definition
  10. Hi Andreas! How do you feel, Rumpf?
  11. @[0, 2, 1]
  12. @[1, 0, 2]
  13. @[1, 2, 0]
  14. @[2, 0, 1]
  15. @[2, 1, 0]
  16. @[2, 0, 1]
  17. @[1, 2, 0]
  18. @[1, 0, 2]
  19. @[0, 2, 1]
  20. @[0, 1, 2]
  21. [5]
  22. [4, 5]
  23. [3, 4, 5]
  24. [2, 3, 4, 5]
  25. [2, 3, 4, 5, 6]
  26. [1, 2, 3, 4, 5, 6]
  27. '''
  28. """
  29. import
  30. critbits, cstrutils, sets, strutils, tables, random, algorithm, ropes,
  31. lists, htmlgen, xmltree, strtabs
  32. block tcritbits:
  33. var r: CritBitTree[void]
  34. r.incl "abc"
  35. r.incl "xyz"
  36. r.incl "def"
  37. r.incl "definition"
  38. r.incl "prefix"
  39. doAssert r.contains"def"
  40. #r.del "def"
  41. for w in r.items:
  42. echo w
  43. for w in r.itemsWithPrefix("de"):
  44. echo w
  45. block testequivalence:
  46. doAssert(toHashSet(@[1,2,3]) <= toHashSet(@[1,2,3,4]), "equivalent or subset")
  47. doAssert(toHashSet(@[1,2,3]) <= toHashSet(@[1,2,3]), "equivalent or subset")
  48. doAssert((not(toHashSet(@[1,2,3]) <= toHashSet(@[1,2]))), "equivalent or subset")
  49. doAssert(toHashSet(@[1,2,3]) <= toHashSet(@[1,2,3,4]), "strict subset")
  50. doAssert((not(toHashSet(@[1,2,3]) < toHashSet(@[1,2,3]))), "strict subset")
  51. doAssert((not(toHashSet(@[1,2,3]) < toHashSet(@[1,2]))), "strict subset")
  52. doAssert((not(toHashSet(@[1,2,3]) == toHashSet(@[1,2,3,4]))), "==")
  53. doAssert(toHashSet(@[1,2,3]) == toHashSet(@[1,2,3]), "==")
  54. doAssert((not(toHashSet(@[1,2,3]) == toHashSet(@[1,2]))), "==")
  55. block tformat:
  56. echo("Hi $1! How do you feel, $2?\n" % ["Andreas", "Rumpf"])
  57. block tnilecho:
  58. var x = @["1", "", "3"]
  59. doAssert $x == """@["1", "", "3"]"""
  60. block torderedtable:
  61. var t = initOrderedTable[int,string]()
  62. # this tests issue #5917
  63. var data = newSeq[int]()
  64. for i in 0..<1000:
  65. var x = rand(1000)
  66. if x notin t: data.add(x)
  67. t[x] = "meh"
  68. # this checks that keys are re-inserted
  69. # in order when table is enlarged.
  70. var i = 0
  71. for k, v in t:
  72. doAssert(k == data[i])
  73. doAssert(v == "meh")
  74. inc(i)
  75. block tpermutations:
  76. var v = @[0, 1, 2]
  77. while v.nextPermutation():
  78. echo v
  79. while v.prevPermutation():
  80. echo v
  81. block tropes:
  82. var
  83. r1 = rope("")
  84. r2 = rope("123")
  85. doAssert r1.len == 0
  86. doAssert r2.len == 3
  87. doAssert $r1 == ""
  88. doAssert $r2 == "123"
  89. r1.add("123")
  90. r2.add("456")
  91. doAssert r1.len == 3
  92. doAssert r2.len == 6
  93. doAssert $r1 == "123"
  94. doAssert $r2 == "123456"
  95. doAssert $r1[1] == "2"
  96. doAssert $r2[2] == "3"
  97. block tsinglylinkedring:
  98. var r = initSinglyLinkedRing[int]()
  99. r.prepend(5)
  100. echo r
  101. r.prepend(4)
  102. echo r
  103. r.prepend(3)
  104. echo r
  105. r.prepend(2)
  106. echo r
  107. r.append(6)
  108. echo r
  109. r.prepend(1)
  110. echo r
  111. block tsplit:
  112. var s = ""
  113. for w in split("|abc|xy|z", {'|'}):
  114. s.add("#")
  115. s.add(w)
  116. doAssert s == "##abc#xy#z"
  117. block tsplit2:
  118. var s = ""
  119. for w in split("|abc|xy|z", {'|'}):
  120. s.add("#")
  121. s.add(w)
  122. var errored = false
  123. try:
  124. discard "hello".split("")
  125. except AssertionError:
  126. errored = true
  127. doAssert errored
  128. block txmlgen:
  129. var nim = "Nim"
  130. doAssert h1(a(href="http://force7.de/nim", nim)) ==
  131. "<h1><a href=\"http://force7.de/nim\">Nim</a></h1>"
  132. block txmltree:
  133. var x = <>a(href="nim.de", newText("www.nim-test.de"))
  134. doAssert($x == "<a href=\"nim.de\">www.nim-test.de</a>")
  135. doAssert(newText("foo").innerText == "foo")
  136. doAssert(newEntity("bar").innerText == "bar")
  137. doAssert(newComment("baz").innerText == "")
  138. let y = newXmlTree("x", [
  139. newText("foo"),
  140. newXmlTree("y", [
  141. newText("bar")
  142. ])
  143. ])
  144. doAssert(y.innerText == "foobar")
  145. block tcstrutils:
  146. let s = cstring "abcdef"
  147. doAssert s.startsWith("a")
  148. doAssert not s.startsWith("b")
  149. doAssert s.endsWith("f")
  150. doAssert not s.endsWith("a")
  151. let a = cstring "abracadabra"
  152. doAssert a.startsWith("abra")
  153. doAssert not a.startsWith("bra")
  154. doAssert a.endsWith("abra")
  155. doAssert not a.endsWith("dab")