tnewruntime_strutils.nim 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. discard """
  2. valgrind: true
  3. cmd: '''nim c -d:nimAllocStats --gc:arc -d:useMalloc $file'''
  4. output: '''
  5. @[(input: @["KXSC", "BGMC"]), (input: @["PXFX"]), (input: @["WXRQ", "ZSCZD"])]
  6. 14
  7. First tasks completed.
  8. Second tasks completed.
  9. test1'''
  10. """
  11. import strutils, os, std / wordwrap
  12. import system / ansi_c
  13. # bug #11004
  14. proc retTuple(): (seq[int], int) =
  15. return (@[1], 1)
  16. # bug #12899
  17. import sequtils, strmisc
  18. const input = ["KXSC, BGMC => 7 PTHL", "PXFX => LBZJ", "WXRQ, ZSCZD => HLQM"]
  19. type
  20. Reaction = object
  21. input: seq[string]
  22. proc bug12899 =
  23. var reactions: seq[Reaction] = @[]
  24. for l in input:
  25. let x = l.partition(" => ")
  26. reactions.add Reaction(input: @(x[0].split(", ")))
  27. let x = $reactions
  28. echo x
  29. bug12899()
  30. proc nonStaticTests =
  31. doAssert formatBiggestFloat(1234.567, ffDecimal, -1) == "1234.567000"
  32. doAssert formatBiggestFloat(1234.567, ffDecimal, 0) == "1235." # bugs 8242, 12586
  33. doAssert formatBiggestFloat(1234.567, ffDecimal, 1) == "1234.6"
  34. doAssert formatBiggestFloat(0.00000000001, ffDecimal, 11) == "0.00000000001"
  35. doAssert formatBiggestFloat(0.00000000001, ffScientific, 1, ',') in
  36. ["1,0e-11", "1,0e-011"]
  37. doAssert "$# $3 $# $#" % ["a", "b", "c"] == "a c b c"
  38. doAssert "${1}12 ${-1}$2" % ["a", "b"] == "a12 bb"
  39. block: # formatSize tests
  40. when not defined(js):
  41. doAssert formatSize((1'i64 shl 31) + (300'i64 shl 20)) == "2.293GiB" # <=== bug #8231
  42. doAssert formatSize((2.234*1024*1024).int) == "2.234MiB"
  43. doAssert formatSize(4096) == "4KiB"
  44. doAssert formatSize(4096, prefix=bpColloquial, includeSpace=true) == "4 kB"
  45. doAssert formatSize(4096, includeSpace=true) == "4 KiB"
  46. doAssert formatSize(5_378_934, prefix=bpColloquial, decimalSep=',') == "5,13MB"
  47. block: # formatEng tests
  48. doAssert formatEng(0, 2, trim=false) == "0.00"
  49. doAssert formatEng(0, 2) == "0"
  50. doAssert formatEng(53, 2, trim=false) == "53.00"
  51. doAssert formatEng(0.053, 2, trim=false) == "53.00e-3"
  52. doAssert formatEng(0.053, 4, trim=false) == "53.0000e-3"
  53. doAssert formatEng(0.053, 4, trim=true) == "53e-3"
  54. doAssert formatEng(0.053, 0) == "53e-3"
  55. doAssert formatEng(52731234) == "52.731234e6"
  56. doAssert formatEng(-52731234) == "-52.731234e6"
  57. doAssert formatEng(52731234, 1) == "52.7e6"
  58. doAssert formatEng(-52731234, 1) == "-52.7e6"
  59. doAssert formatEng(52731234, 1, decimalSep=',') == "52,7e6"
  60. doAssert formatEng(-52731234, 1, decimalSep=',') == "-52,7e6"
  61. doAssert formatEng(4100, siPrefix=true, unit="V") == "4.1 kV"
  62. doAssert formatEng(4.1, siPrefix=true, unit="V", useUnitSpace=true) == "4.1 V"
  63. doAssert formatEng(4.1, siPrefix=true) == "4.1" # Note lack of space
  64. doAssert formatEng(4100, siPrefix=true) == "4.1 k"
  65. doAssert formatEng(4.1, siPrefix=true, unit="", useUnitSpace=true) == "4.1 " # Includes space
  66. doAssert formatEng(4100, siPrefix=true, unit="") == "4.1 k"
  67. doAssert formatEng(4100) == "4.1e3"
  68. doAssert formatEng(4100, unit="V", useUnitSpace=true) == "4.1e3 V"
  69. doAssert formatEng(4100, unit="", useUnitSpace=true) == "4.1e3 "
  70. # Don't use SI prefix as number is too big
  71. doAssert formatEng(3.1e22, siPrefix=true, unit="a", useUnitSpace=true) == "31e21 a"
  72. # Don't use SI prefix as number is too small
  73. doAssert formatEng(3.1e-25, siPrefix=true, unit="A", useUnitSpace=true) == "310e-27 A"
  74. proc staticTests =
  75. doAssert align("abc", 4) == " abc"
  76. doAssert align("a", 0) == "a"
  77. doAssert align("1232", 6) == " 1232"
  78. doAssert align("1232", 6, '#') == "##1232"
  79. doAssert alignLeft("abc", 4) == "abc "
  80. doAssert alignLeft("a", 0) == "a"
  81. doAssert alignLeft("1232", 6) == "1232 "
  82. doAssert alignLeft("1232", 6, '#') == "1232##"
  83. let
  84. inp = """ this is a long text -- muchlongerthan10chars and here
  85. it goes"""
  86. outp = " this is a\nlong text\n--\nmuchlongerthan10chars\nand here\nit goes"
  87. doAssert wrapWords(inp, 10, false) == outp
  88. let
  89. longInp = """ThisIsOneVeryLongStringWhichWeWillSplitIntoEightSeparatePartsNow"""
  90. longOutp = "ThisIsOn\neVeryLon\ngStringW\nhichWeWi\nllSplitI\nntoEight\nSeparate\nPartsNow"
  91. doAssert wrapWords(longInp, 8, true) == longOutp
  92. doAssert "$animal eats $food." % ["animal", "The cat", "food", "fish"] ==
  93. "The cat eats fish."
  94. doAssert "-ld a-ldz -ld".replaceWord("-ld") == " a-ldz "
  95. doAssert "-lda-ldz -ld abc".replaceWord("-ld") == "-lda-ldz abc"
  96. doAssert "-lda-ldz -ld abc".replaceWord("") == "-lda-ldz -ld abc"
  97. doAssert "oo".replace("", "abc") == "oo"
  98. type MyEnum = enum enA, enB, enC, enuD, enE
  99. doAssert parseEnum[MyEnum]("enu_D") == enuD
  100. doAssert parseEnum("invalid enum value", enC) == enC
  101. doAssert center("foo", 13) == " foo "
  102. doAssert center("foo", 0) == "foo"
  103. doAssert center("foo", 3, fillChar = 'a') == "foo"
  104. doAssert center("foo", 10, fillChar = '\t') == "\t\t\tfoo\t\t\t\t"
  105. doAssert count("foofoofoo", "foofoo") == 1
  106. doAssert count("foofoofoo", "foofoo", overlapping = true) == 2
  107. doAssert count("foofoofoo", 'f') == 3
  108. doAssert count("foofoofoobar", {'f','b'}) == 4
  109. doAssert strip(" foofoofoo ") == "foofoofoo"
  110. doAssert strip("sfoofoofoos", chars = {'s'}) == "foofoofoo"
  111. doAssert strip("barfoofoofoobar", chars = {'b', 'a', 'r'}) == "foofoofoo"
  112. doAssert strip("stripme but don't strip this stripme",
  113. chars = {'s', 't', 'r', 'i', 'p', 'm', 'e'}) ==
  114. " but don't strip this "
  115. doAssert strip("sfoofoofoos", leading = false, chars = {'s'}) == "sfoofoofoo"
  116. doAssert strip("sfoofoofoos", trailing = false, chars = {'s'}) == "foofoofoos"
  117. doAssert " foo\n bar".indent(4, "Q") == "QQQQ foo\nQQQQ bar"
  118. doAssert "abba".multiReplace(("a", "b"), ("b", "a")) == "baab"
  119. doAssert "Hello World.".multiReplace(("ello", "ELLO"), ("World.", "PEOPLE!")) == "HELLO PEOPLE!"
  120. doAssert "aaaa".multiReplace(("a", "aa"), ("aa", "bb")) == "aaaaaaaa"
  121. doAssert rsplit("foo bar", seps=Whitespace) == @["foo", "bar"]
  122. doAssert rsplit(" foo bar", seps=Whitespace, maxsplit=1) == @[" foo", "bar"]
  123. doAssert rsplit(" foo bar ", seps=Whitespace, maxsplit=1) == @[" foo bar", ""]
  124. doAssert rsplit(":foo:bar", sep=':') == @["", "foo", "bar"]
  125. doAssert rsplit(":foo:bar", sep=':', maxsplit=2) == @["", "foo", "bar"]
  126. doAssert rsplit(":foo:bar", sep=':', maxsplit=3) == @["", "foo", "bar"]
  127. doAssert rsplit("foothebar", sep="the") == @["foo", "bar"]
  128. doAssert(unescape(r"\x013", "", "") == "\x013")
  129. doAssert join(["foo", "bar", "baz"]) == "foobarbaz"
  130. doAssert join(@["foo", "bar", "baz"], ", ") == "foo, bar, baz"
  131. doAssert join([1, 2, 3]) == "123"
  132. doAssert join(@[1, 2, 3], ", ") == "1, 2, 3"
  133. doAssert """~~!!foo
  134. ~~!!bar
  135. ~~!!baz""".unindent(2, "~~!!") == "foo\nbar\nbaz"
  136. doAssert """~~!!foo
  137. ~~!!bar
  138. ~~!!baz""".unindent(2, "~~!!aa") == "~~!!foo\n~~!!bar\n~~!!baz"
  139. doAssert """~~foo
  140. ~~ bar
  141. ~~ baz""".unindent(4, "~") == "foo\n bar\n baz"
  142. doAssert """foo
  143. bar
  144. baz
  145. """.unindent(4) == "foo\nbar\nbaz\n"
  146. doAssert """foo
  147. bar
  148. baz
  149. """.unindent(2) == "foo\n bar\n baz\n"
  150. doAssert """foo
  151. bar
  152. baz
  153. """.unindent(100) == "foo\nbar\nbaz\n"
  154. doAssert """foo
  155. foo
  156. bar
  157. """.unindent() == "foo\nfoo\nbar\n"
  158. let s = " this is an example "
  159. let s2 = ":this;is;an:example;;"
  160. doAssert s.split() == @["", "this", "is", "an", "example", "", ""]
  161. doAssert s2.split(seps={':', ';'}) == @["", "this", "is", "an", "example", "", ""]
  162. doAssert s.split(maxsplit=4) == @["", "this", "is", "an", "example "]
  163. doAssert s.split(' ', maxsplit=1) == @["", "this is an example "]
  164. doAssert s.split(" ", maxsplit=4) == @["", "this", "is", "an", "example "]
  165. doAssert s.splitWhitespace() == @["this", "is", "an", "example"]
  166. doAssert s.splitWhitespace(maxsplit=1) == @["this", "is an example "]
  167. doAssert s.splitWhitespace(maxsplit=2) == @["this", "is", "an example "]
  168. doAssert s.splitWhitespace(maxsplit=3) == @["this", "is", "an", "example "]
  169. doAssert s.splitWhitespace(maxsplit=4) == @["this", "is", "an", "example"]
  170. discard retTuple()
  171. nonStaticTests()
  172. staticTests()
  173. # bug #12965
  174. let xaa = @[""].join()
  175. let xbb = @["", ""].join()
  176. # bug #16365
  177. # Task 1:
  178. when true:
  179. # Task 1_a:
  180. var test_string_a = "name_something"
  181. echo test_string_a.len()
  182. let new_len_a = test_string_a.len - "_something".len()
  183. test_string_a.setLen new_len_a
  184. echo "First tasks completed."
  185. # Task 2:
  186. when true:
  187. # Task 2_a
  188. var test_string: string
  189. let some_string = "something"
  190. for i in some_string.items:
  191. test_string.add $i
  192. # Task 2_b
  193. var test_string_b = "name_something"
  194. let new_len_b = test_string_b.len - "_something".len()
  195. test_string_b.setLen new_len_b
  196. echo "Second tasks completed."
  197. # bug #17450
  198. proc main =
  199. var i = 1
  200. echo:
  201. block:
  202. "test" & $i
  203. main()