diff --git a/lib/packages/docutils/rstgen.nim b/lib/packages/docutils/rstgen.nim index 604cc3ca9e9ef..47b4189badc2b 100644 --- a/lib/packages/docutils/rstgen.nim +++ b/lib/packages/docutils/rstgen.nim @@ -1364,9 +1364,3 @@ proc rstToLatex*(rstSource: string; options: RstParseOptions): string {.inline, var rstGenera: RstGenerator rstGenera.initRstGenerator(outLatex, defaultConfig(), "input", options) rstGenera.renderRstToOut(rstParse(rstSource, "", 1, 1, option, options), result) - - -when isMainModule: - assert rstToHtml("*Hello* **world**!", {}, - newStringTable(modeStyleInsensitive)) == - "Hello world!" diff --git a/lib/pure/sugar.nim b/lib/pure/sugar.nim index 46dbb1eceec74..d9c98c36088cd 100644 --- a/lib/pure/sugar.nim +++ b/lib/pure/sugar.nim @@ -363,100 +363,3 @@ macro collect*(init, body: untyped): untyped {.since: (1, 1).} = for i in 1 ..< init.len: call.add init[i] result = newTree(nnkStmtListExpr, newVarStmt(res, call), resBody, res) - - -when isMainModule: - since (1, 1): - block dup_with_field: - type - Foo = object - col, pos: int - name: string - - proc inc_col(foo: var Foo) = inc(foo.col) - proc inc_pos(foo: var Foo) = inc(foo.pos) - proc name_append(foo: var Foo, s: string) = foo.name &= s - - let a = Foo(col: 1, pos: 2, name: "foo") - block: - let b = a.dup(inc_col, inc_pos): - _.pos = 3 - name_append("bar") - inc_pos - - doAssert(b == Foo(col: 2, pos: 4, name: "foobar")) - - block: - let b = a.dup(inc_col, pos = 3, name = "bar"): - name_append("bar") - inc_pos - - doAssert(b == Foo(col: 2, pos: 4, name: "barbar")) - - import algorithm - - var a = @[1, 2, 3, 4, 5, 6, 7, 8, 9] - doAssert dup(a, sort(_)) == sorted(a) - doAssert a.dup(sort) == sorted(a) - #Chaining: - var aCopy = a - aCopy.insert(10) - doAssert a.dup(insert(10)).dup(sort()) == sorted(aCopy) - - import random - - const b = @[0, 1, 2] - let c = b.dup shuffle() - doAssert c[0] == 1 - doAssert c[1] == 0 - - #test collect - import sets, tables - - let data = @["bird", "word"] # if this gets stuck in your head, its not my fault - assert collect(newSeq, for (i, d) in data.pairs: (if i mod 2 == 0: d)) == @["bird"] - assert collect(initTable(2), for (i, d) in data.pairs: {i: d}) == {0: "bird", - 1: "word"}.toTable - assert initHashSet.collect(for d in data.items: {d}) == data.toHashSet - - let x = collect(newSeqOfCap(4)): - for (i, d) in data.pairs: - if i mod 2 == 0: d - assert x == @["bird"] - - # bug #12874 - - let bug1 = collect( - newSeq, - for (i, d) in data.pairs:( - block: - if i mod 2 == 0: - d - else: - d & d - ) - ) - assert bug1 == @["bird", "wordword"] - - import strutils - let y = collect(newSeq): - for (i, d) in data.pairs: - try: parseInt(d) except: 0 - assert y == @[0, 0] - - let z = collect(newSeq): - for (i, d) in data.pairs: - case d - of "bird": "word" - else: d - assert z == @["word", "word"] - - - proc tforum = - let ans = collect(newSeq): - for y in 0..10: - if y mod 5 == 2: - for x in 0..y: - x - - tforum() diff --git a/lib/std/editdistance.nim b/lib/std/editdistance.nim index f365d4ab2caeb..2c9203d6495aa 100644 --- a/lib/std/editdistance.nim +++ b/lib/std/editdistance.nim @@ -264,44 +264,3 @@ proc editDistanceAscii*(a, b: string): int {.noSideEffect.} = if x > c3: x = c3 row[p] = x result = row[e] - - -when isMainModule: - doAssert editDistance("", "") == 0 - doAssert editDistance("kitten", "sitting") == 3 # from Wikipedia - doAssert editDistance("flaw", "lawn") == 2 # from Wikipedia - - doAssert editDistance("привет", "превет") == 1 - doAssert editDistance("Åge", "Age") == 1 - # editDistance, one string is longer in bytes, but shorter in rune length - # first string: 4 bytes, second: 6 bytes, but only 3 runes - doAssert editDistance("aaaa", "×××") == 4 - - block veryLongStringEditDistanceTest: - const cap = 256 - var - s1 = newStringOfCap(cap) - s2 = newStringOfCap(cap) - while len(s1) < cap: - s1.add 'a' - while len(s2) < cap: - s2.add 'b' - doAssert editDistance(s1, s2) == cap - - block combiningCodePointsEditDistanceTest: - const s = "A\xCC\x8Age" - doAssert editDistance(s, "Age") == 1 - - doAssert editDistanceAscii("", "") == 0 - doAssert editDistanceAscii("kitten", "sitting") == 3 # from Wikipedia - doAssert editDistanceAscii("flaw", "lawn") == 2 # from Wikipedia - - - assert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffix") == 0) - assert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffi1") == 1) - assert(editDistance("prefix__hallo_suffix", "prefix__HALLO_suffix") == 5) - assert(editDistance("prefix__hallo_suffix", "prefix__ha_suffix") == 3) - assert(editDistance("prefix__hallo_suffix", "prefix") == 14) - assert(editDistance("prefix__hallo_suffix", "suffix") == 14) - assert(editDistance("prefix__hallo_suffix", "prefix__hao_suffix") == 2) - assert(editDistance("main", "malign") == 2) diff --git a/lib/std/sums.nim b/lib/std/sums.nim index 0f8c5ebfb6a13..cc964b91a7367 100644 --- a/lib/std/sums.nim +++ b/lib/std/sums.nim @@ -60,27 +60,3 @@ runnableExamples: const data = [1, 2, 3, 4, 5, 6, 7, 8, 9] doAssert sumKbn(data) == 45 doAssert sumPairs(data) == 45 - - -when isMainModule: - from math import pow - - var epsilon = 1.0 - while 1.0 + epsilon != 1.0: - epsilon /= 2.0 - let data = @[1.0, epsilon, -epsilon] - assert sumKbn(data) == 1.0 - assert sumPairs(data) != 1.0 # known to fail - assert (1.0 + epsilon) - epsilon != 1.0 - - var tc1: seq[float] - for n in 1 .. 1000: - tc1.add 1.0 / n.float - assert sumKbn(tc1) == 7.485470860550345 - assert sumPairs(tc1) == 7.485470860550345 - - var tc2: seq[float] - for n in 1 .. 1000: - tc2.add pow(-1.0, n.float) / n.float - assert sumKbn(tc2) == -0.6926474305598203 - assert sumPairs(tc2) == -0.6926474305598204 diff --git a/lib/std/with.nim b/lib/std/with.nim index ea26065a1e7a9..e6784478c6a6c 100644 --- a/lib/std/with.nim +++ b/lib/std/with.nim @@ -36,23 +36,3 @@ macro with*(arg: typed; calls: varargs[untyped]): untyped = result = newNimNode(nnkStmtList, arg) underscoredCalls(result, calls, arg) - -when isMainModule: - type - Foo = object - col, pos: string - name: string - - proc setColor(f: var Foo; r, g, b: int) = f.col = $(r, g, b) - proc setPosition(f: var Foo; x, y: float) = f.pos = $(x, y) - - var f: Foo - with(f, setColor(2, 3, 4), setPosition(0.0, 1.0)) - echo f - - f = Foo() - with f: - col = $(2, 3, 4) - pos = $(0.0, 1.0) - _.name = "bar" - echo f diff --git a/lib/std/wordwrap.nim b/lib/std/wordwrap.nim index 27df229bf4117..7dcfc7f599196 100644 --- a/lib/std/wordwrap.nim +++ b/lib/std/wordwrap.nim @@ -72,47 +72,3 @@ proc wrapWords*(s: string, maxLineWidth = 80, for k in i..A1 headerA2 """ + + +assert rstToHtml("*Hello* **world**!", {}, + newStringTable(modeStyleInsensitive)) == + "Hello world!" diff --git a/tests/stdlib/tsugar.nim b/tests/stdlib/tsugar.nim new file mode 100644 index 0000000000000..a6fd70f3418ad --- /dev/null +++ b/tests/stdlib/tsugar.nim @@ -0,0 +1,94 @@ +import sugar + +block dup_with_field: + type + Foo = object + col, pos: int + name: string + + proc inc_col(foo: var Foo) = inc(foo.col) + proc inc_pos(foo: var Foo) = inc(foo.pos) + proc name_append(foo: var Foo, s: string) = foo.name &= s + + let a = Foo(col: 1, pos: 2, name: "foo") + block: + let b = a.dup(inc_col, inc_pos): + _.pos = 3 + name_append("bar") + inc_pos + + doAssert(b == Foo(col: 2, pos: 4, name: "foobar")) + + block: + let b = a.dup(inc_col, pos = 3, name = "bar"): + name_append("bar") + inc_pos + + doAssert(b == Foo(col: 2, pos: 4, name: "barbar")) + +import algorithm + +var a = @[1, 2, 3, 4, 5, 6, 7, 8, 9] +doAssert dup(a, sort(_)) == sorted(a) +doAssert a.dup(sort) == sorted(a) +#Chaining: +var aCopy = a +aCopy.insert(10) +doAssert a.dup(insert(10)).dup(sort()) == sorted(aCopy) + +import random + +const b = @[0, 1, 2] +let c = b.dup shuffle() +doAssert c.len == 3 + +#test collect +import sets, tables + +let data = @["bird", "word"] # if this gets stuck in your head, its not my fault +assert collect(newSeq, for (i, d) in data.pairs: (if i mod 2 == 0: d)) == @["bird"] +assert collect(initTable(2), for (i, d) in data.pairs: {i: d}) == {0: "bird", + 1: "word"}.toTable +assert initHashSet.collect(for d in data.items: {d}) == data.toHashSet + +let x = collect(newSeqOfCap(4)): + for (i, d) in data.pairs: + if i mod 2 == 0: d +assert x == @["bird"] + +# bug #12874 + +let bug1 = collect( + newSeq, + for (i, d) in data.pairs:( + block: + if i mod 2 == 0: + d + else: + d & d + ) +) +assert bug1 == @["bird", "wordword"] + +import strutils +let y = collect(newSeq): + for (i, d) in data.pairs: + try: parseInt(d) except: 0 +assert y == @[0, 0] + +let z = collect(newSeq): + for (i, d) in data.pairs: + case d + of "bird": "word" + else: d +assert z == @["word", "word"] + + +proc tforum = + let ans = collect(newSeq): + for y in 0..10: + if y mod 5 == 2: + for x in 0..y: + x + +tforum() diff --git a/tests/stdlib/tsums.nim b/tests/stdlib/tsums.nim new file mode 100644 index 0000000000000..979ffc391c537 --- /dev/null +++ b/tests/stdlib/tsums.nim @@ -0,0 +1,22 @@ +import std/sums +from math import pow + +var epsilon = 1.0 +while 1.0 + epsilon != 1.0: + epsilon /= 2.0 +let data = @[1.0, epsilon, -epsilon] +assert sumKbn(data) == 1.0 +# assert sumPairs(data) != 1.0 # known to fail in 64 bits +assert (1.0 + epsilon) - epsilon != 1.0 + +var tc1: seq[float] +for n in 1 .. 1000: + tc1.add 1.0 / n.float +assert sumKbn(tc1) == 7.485470860550345 +assert sumPairs(tc1) == 7.485470860550345 + +var tc2: seq[float] +for n in 1 .. 1000: + tc2.add pow(-1.0, n.float) / n.float +assert sumKbn(tc2) == -0.6926474305598203 +assert sumPairs(tc2) == -0.6926474305598204 diff --git a/tests/stdlib/twith.nim b/tests/stdlib/twith.nim new file mode 100644 index 0000000000000..80382f7c42f73 --- /dev/null +++ b/tests/stdlib/twith.nim @@ -0,0 +1,23 @@ +import std/with + +type + Foo = object + col, pos: string + name: string + +proc setColor(f: var Foo; r, g, b: int) = f.col = $(r, g, b) +proc setPosition(f: var Foo; x, y: float) = f.pos = $(x, y) + +var f: Foo +with(f, setColor(2, 3, 4), setPosition(0.0, 1.0)) +doAssert f.col == "(2, 3, 4)" +doAssert f.pos == "(0.0, 1.0)" + +f = Foo() +with f: + col = $(2, 3, 4) + pos = $(0.0, 1.0) + _.name = "bar" +doAssert f.col == "(2, 3, 4)" +doAssert f.pos == "(0.0, 1.0)" +doAssert f.name == "bar" diff --git a/tests/stdlib/twordwrap.nim b/tests/stdlib/twordwrap.nim new file mode 100644 index 0000000000000..c90dd95814b0b --- /dev/null +++ b/tests/stdlib/twordwrap.nim @@ -0,0 +1,43 @@ +import std/wordwrap + +when true: + let + inp = """ this is a long text -- muchlongerthan10chars and here + it goes""" + outp = " this is a\nlong text\n--\nmuchlongerthan10chars\nand here\nit goes" + doAssert wrapWords(inp, 10, false) == outp + + let + longInp = """ThisIsOneVeryLongStringWhichWeWillSplitIntoEightSeparatePartsNow""" + longOutp = "ThisIsOn\neVeryLon\ngStringW\nhichWeWi\nllSplitI\nntoEight\nSeparate\nPartsNow" + doAssert wrapWords(longInp, 8, true) == longOutp + +# test we don't break Umlauts into invalid bytes: +let fies = "äöüöäöüöäöüöäöüööäöüöäößßßßüöäößßßßßß" +let fiesRes = "ä\nö\nü\nö\nä\nö\nü\nö\nä\nö\nü\nö\nä\nö\nü\nö\nö\nä\nö\nü\nö\nä\nö\nß\nß\nß\nß\nü\nö\nä\nö\nß\nß\nß\nß\nß\nß" +doAssert wrapWords(fies, 1, true) == fiesRes + +let longlongword = """abc uitdaeröägfßhydüäpydqfü,träpydqgpmüdträpydföägpydörztdüöäfguiaeowäzjdtrüöäp psnrtuiydrözenrüöäpyfdqazpesnrtulocjtüö +äzydgyqgfqfgprtnwjlcydkqgfüöezmäzydydqüüöäpdtrnvwfhgckdumböäpydfgtdgfhtdrntdrntydfogiayqfguiatrnydrntüöärtniaoeydfgaoeiqfglwcßqfgxvlcwgtfhiaoen +rsüöäapmböäptdrniaoydfglckqfhouenrtsüöäptrniaoeyqfgulocfqclgwxßqflgcwßqfxglcwrniatrnmüböäpmöäbpümöäbpüöämpbaoestnriaesnrtdiaesrtdniaesdrtnaetdr +iaoenvlcyfglwckßqfgvwkßqgfvlwkßqfgvlwckßqvlwkgfUIαοιαοιαχολωχσωχνωκψρχκψρτιεαοσηζϵηζιοεννκεωνιαλωσωκνκψρκγτφγτχκγτεκργτιχνκιωχσιλωσλωχξλξλξωχωχ +ξχλωωχαοεοιαεοαεοιαεοαεοιαοεσναοεκνρκψγκψφϵιηαααοε""" +let longlongwordRes = """ +abc uitdaeröägfßhydüäpydqfü,träpydqgpmüdträpydföägpydörztdüöäfguiaeowäzjdtrüöäp +psnrtuiydrözenrüöäpyfdqazpesnrtulocjtüöäzydgyqgfqfgprtnwjlcydkqgfüöezmäzydydqüü +öäpdtrnvwfhgckdumböäpydfgtdgfhtdrntdrntydfogiayqfguiatrnydrntüöärtniaoeydfgaoeiq +fglwcßqfgxvlcwgtfhiaoenrsüöäapmböäptdrniaoydfglckqfhouenrtsüöäptrniaoeyqfgulocf +qclgwxßqflgcwßqfxglcwrniatrnmüböäpmöäbpümöäbpüöämpbaoestnriaesnrtdiaesrtdniaesdr +tnaetdriaoenvlcyfglwckßqfgvwkßqgfvlwkßqfgvlwckßqvlwkgfUIαοιαοιαχολωχσωχνωκψρχκψ +ρτιεαοσηζϵηζιοεννκεωνιαλωσωκνκψρκγτφγτχκγτεκργτιχνκιωχσιλωσλωχξλξλξωχωχ +ξχλωωχαοεοιαεοαεοιαεοαεοιαοεσναοεκνρκψγκψφϵιηαααοε""" +doAssert wrapWords(longlongword) == longlongwordRes + +# bug #14579 +const input60 = """ +This string is wrapped to 60 characters. If we call +wrapwords on it it will be re-wrapped to 80 characters. +""" +const input60Res = """This string is wrapped to 60 characters. If we call wrapwords on it it will be +re-wrapped to 80 characters.""" +doAssert wrapWords(input60) == input60Res \ No newline at end of file