tstdlib_various.nim 4.6 KB

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