tstdlib_various.nim 4.5 KB

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