tstdlib_various.nim 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. 055this should be the casehugh@["(", "+", " 1", " 2", ")"]
  22. caught a crash!
  23. caught a crash!
  24. caught a crash!
  25. caught a crash!
  26. caught a crash!
  27. caught a crash!
  28. [5]
  29. [4, 5]
  30. [3, 4, 5]
  31. [2, 3, 4, 5]
  32. [2, 3, 4, 5, 6]
  33. [1, 2, 3, 4, 5, 6]
  34. true
  35. <h1><a href="http://force7.de/nim">Nim</a></h1>
  36. '''
  37. """
  38. import
  39. critbits, cstrutils, sets, strutils, tables, random, algorithm, re, ropes,
  40. segfaults, lists, parsesql, streams, os, htmlgen, xmltree, strtabs
  41. block tcritbits:
  42. var r: CritBitTree[void]
  43. r.incl "abc"
  44. r.incl "xyz"
  45. r.incl "def"
  46. r.incl "definition"
  47. r.incl "prefix"
  48. doAssert r.contains"def"
  49. #r.del "def"
  50. for w in r.items:
  51. echo w
  52. for w in r.itemsWithPrefix("de"):
  53. echo w
  54. block testequivalence:
  55. doAssert(toHashSet(@[1,2,3]) <= toHashSet(@[1,2,3,4]), "equivalent or subset")
  56. doAssert(toHashSet(@[1,2,3]) <= toHashSet(@[1,2,3]), "equivalent or subset")
  57. doAssert((not(toHashSet(@[1,2,3]) <= toHashSet(@[1,2]))), "equivalent or subset")
  58. doAssert(toHashSet(@[1,2,3]) <= toHashSet(@[1,2,3,4]), "strict subset")
  59. doAssert((not(toHashSet(@[1,2,3]) < toHashSet(@[1,2,3]))), "strict subset")
  60. doAssert((not(toHashSet(@[1,2,3]) < toHashSet(@[1,2]))), "strict subset")
  61. doAssert((not(toHashSet(@[1,2,3]) == toHashSet(@[1,2,3,4]))), "==")
  62. doAssert(toHashSet(@[1,2,3]) == toHashSet(@[1,2,3]), "==")
  63. doAssert((not(toHashSet(@[1,2,3]) == toHashSet(@[1,2]))), "==")
  64. block tformat:
  65. echo("Hi $1! How do you feel, $2?\n" % ["Andreas", "Rumpf"])
  66. block tnilecho:
  67. var x = @["1", "", "3"]
  68. doAssert $x == """@["1", "", "3"]"""
  69. block torderedtable:
  70. var t = initOrderedTable[int,string]()
  71. # this tests issue #5917
  72. var data = newSeq[int]()
  73. for i in 0..<1000:
  74. var x = rand(1000)
  75. if x notin t: data.add(x)
  76. t[x] = "meh"
  77. # this checks that keys are re-inserted
  78. # in order when table is enlarged.
  79. var i = 0
  80. for k, v in t:
  81. doAssert(k == data[i])
  82. doAssert(v == "meh")
  83. inc(i)
  84. block tpermutations:
  85. var v = @[0, 1, 2]
  86. while v.nextPermutation():
  87. echo v
  88. while v.prevPermutation():
  89. echo v
  90. block treguse:
  91. proc main(a, b: int) =
  92. var x = 0
  93. write(stdout, x)
  94. if x == 0:
  95. var y = 55
  96. write(stdout, y)
  97. write(stdout, "this should be the case")
  98. var input = "<no input>"
  99. if input == "Andreas":
  100. write(stdout, "wow")
  101. else:
  102. write(stdout, "hugh")
  103. else:
  104. var z = 66
  105. write(stdout, z) # "bug!")
  106. main(45, 1000)
  107. block treloop:
  108. let str = "(+ 1 2)"
  109. var tokenRE = re"""[\s,]*(~@|[\[\]{}()'`~^@]|"(?:\\.|[^\\"])*"|;.*|[^\s\[\]{}('"`,;)]*)"""
  110. echo str.findAll(tokenRE)
  111. block tropes:
  112. var
  113. r1 = rope("")
  114. r2 = rope("123")
  115. doAssert r1.len == 0
  116. doAssert r2.len == 3
  117. doAssert $r1 == ""
  118. doAssert $r2 == "123"
  119. r1.add("123")
  120. r2.add("456")
  121. doAssert r1.len == 3
  122. doAssert r2.len == 6
  123. doAssert $r1 == "123"
  124. doAssert $r2 == "123456"
  125. doAssert $r1[1] == "2"
  126. doAssert $r2[2] == "3"
  127. block tsegfaults:
  128. proc main =
  129. try:
  130. var x: ptr int
  131. echo x[]
  132. try:
  133. raise newException(ValueError, "not a crash")
  134. except ValueError:
  135. discard
  136. except NilAccessDefect:
  137. echo "caught a crash!"
  138. for i in 0..5:
  139. main()
  140. block tsinglylinkedring:
  141. var r = initSinglyLinkedRing[int]()
  142. r.prepend(5)
  143. echo r
  144. r.prepend(4)
  145. echo r
  146. r.prepend(3)
  147. echo r
  148. r.prepend(2)
  149. echo r
  150. r.append(6)
  151. echo r
  152. r.prepend(1)
  153. echo r
  154. block tsplit:
  155. var s = ""
  156. for w in split("|abc|xy|z", {'|'}):
  157. s.add("#")
  158. s.add(w)
  159. doAssert s == "##abc#xy#z"
  160. block tsplit2:
  161. var s = ""
  162. for w in split("|abc|xy|z", {'|'}):
  163. s.add("#")
  164. s.add(w)
  165. try:
  166. discard "hello".split("")
  167. echo "false"
  168. except AssertionDefect:
  169. echo "true"
  170. block tsqlparser:
  171. # Just check that we can parse 'somesql' and render it without crashes.
  172. var tree = parseSql(newFileStream( parentDir(currentSourcePath) / "somesql.sql"), "somesql")
  173. discard renderSql(tree)
  174. block txmlgen:
  175. var nim = "Nim"
  176. echo h1(a(href="http://force7.de/nim", nim))
  177. block txmltree:
  178. var x = <>a(href="nim.de", newText("www.nim-test.de"))
  179. doAssert($x == "<a href=\"nim.de\">www.nim-test.de</a>")
  180. doAssert(newText("foo").innerText == "foo")
  181. doAssert(newEntity("bar").innerText == "bar")
  182. doAssert(newComment("baz").innerText == "")
  183. let y = newXmlTree("x", [
  184. newText("foo"),
  185. newXmlTree("y", [
  186. newText("bar")
  187. ])
  188. ])
  189. doAssert(y.innerText == "foobar")
  190. block tcstrutils:
  191. let s = cstring "abcdef"
  192. doAssert s.startsWith("a")
  193. doAssert not s.startsWith("b")
  194. doAssert s.endsWith("f")
  195. doAssert not s.endsWith("a")
  196. let a = cstring "abracadabra"
  197. doAssert a.startsWith("abra")
  198. doAssert not a.startsWith("bra")
  199. doAssert a.endsWith("abra")
  200. doAssert not a.endsWith("dab")
  201. doAssert cmpIgnoreCase(cstring "FooBar", "foobar") == 0
  202. doAssert cmpIgnoreCase(cstring "bar", "Foo") < 0
  203. doAssert cmpIgnoreCase(cstring "Foo5", "foo4") > 0
  204. doAssert cmpIgnoreStyle(cstring "foo_bar", "FooBar") == 0
  205. doAssert cmpIgnoreStyle(cstring "foo_bar_5", "FooBar4") > 0