diff options
Diffstat (limited to 'tests/stdlib')
-rw-r--r-- | tests/stdlib/concurrency/tatomics_size.nim | 18 | ||||
-rw-r--r-- | tests/stdlib/t15663.nim | 7 | ||||
-rw-r--r-- | tests/stdlib/t15835.nim | 17 | ||||
-rw-r--r-- | tests/stdlib/tfrexp1.nim | 2 | ||||
-rw-r--r-- | tests/stdlib/thttpcore.nim | 27 | ||||
-rw-r--r-- | tests/stdlib/tintsets.nim | 65 | ||||
-rw-r--r-- | tests/stdlib/tjsonutils.nim | 2 | ||||
-rw-r--r-- | tests/stdlib/tpackedsets.nim | 259 | ||||
-rw-r--r-- | tests/stdlib/trstgen.nim | 24 | ||||
-rw-r--r-- | tests/stdlib/tsharedlist.nim | 17 | ||||
-rw-r--r-- | tests/stdlib/tstring.nim | 7 | ||||
-rw-r--r-- | tests/stdlib/tterminal_15874.nim | 8 |
12 files changed, 386 insertions, 67 deletions
diff --git a/tests/stdlib/concurrency/tatomics_size.nim b/tests/stdlib/concurrency/tatomics_size.nim new file mode 100644 index 000000000..49387c0c1 --- /dev/null +++ b/tests/stdlib/concurrency/tatomics_size.nim @@ -0,0 +1,18 @@ +discard """ + targets: "c cpp" +""" +import std/atomics + +block testSize: # issue 12726 + type + Node = ptr object + # works + next: Atomic[pointer] + f:AtomicFlag + MyChannel = object + # type not defined completely + back: Atomic[ptr int] + f: AtomicFlag + static: + doAssert sizeof(Node) == sizeof(pointer) + doAssert sizeof(MyChannel) == sizeof(pointer) * 2 \ No newline at end of file diff --git a/tests/stdlib/t15663.nim b/tests/stdlib/t15663.nim new file mode 100644 index 000000000..1ad5677fd --- /dev/null +++ b/tests/stdlib/t15663.nim @@ -0,0 +1,7 @@ +discard """ + cmd: "nim c --gc:arc $file" + output: "Test" +""" + +let ws = newWideCString("Test") +echo ws \ No newline at end of file diff --git a/tests/stdlib/t15835.nim b/tests/stdlib/t15835.nim new file mode 100644 index 000000000..bddfa87aa --- /dev/null +++ b/tests/stdlib/t15835.nim @@ -0,0 +1,17 @@ +import json + +type + Foo = object + ii*: int + data*: JsonNode + +block: + const jt = """{"ii": 123, "data": ["some", "data"]}""" + let js = parseJson(jt) + discard js.to(Foo) + +block: + const jt = """{"ii": 123}""" + let js = parseJson(jt) + doAssertRaises(KeyError): + echo js.to(Foo) diff --git a/tests/stdlib/tfrexp1.nim b/tests/stdlib/tfrexp1.nim index 6da185420..21ecc7491 100644 --- a/tests/stdlib/tfrexp1.nim +++ b/tests/stdlib/tfrexp1.nim @@ -1,5 +1,5 @@ discard """ - targets: "js c c++" + targets: "js c cpp" output: '''ok''' """ diff --git a/tests/stdlib/thttpcore.nim b/tests/stdlib/thttpcore.nim index 33c24453e..cb1a0875f 100644 --- a/tests/stdlib/thttpcore.nim +++ b/tests/stdlib/thttpcore.nim @@ -51,3 +51,30 @@ suite "httpcore": doAssert parseHeader("Accept: foo, bar") == (key: "Accept", value: @["foo", "bar"]) doAssert parseHeader("Accept: foo, bar, prologue") == (key: "Accept", value: @["foo", "bar", "prologue"]) doAssert parseHeader("Accept: foo, bar, prologue, starlight") == (key: "Accept", value: @["foo", "bar", "prologue", "starlight"]) + + test "add empty sequence to HTTP headers": + block: + var headers = newHttpHeaders() + headers["empty"] = @[] + + doAssert not headers.hasKey("empty") + + block: + var headers = newHttpHeaders() + headers["existing"] = "true" + headers["existing"] = @[] + + doAssert not headers.hasKey("existing") + + block: + var headers = newHttpHeaders() + headers["existing"] = @["true"] + headers["existing"] = @[] + + doAssert not headers.hasKey("existing") + + block: + var headers = newHttpHeaders() + headers["existing"] = @[] + headers["existing"] = @["true"] + doAssert headers.hasKey("existing") diff --git a/tests/stdlib/tintsets.nim b/tests/stdlib/tintsets.nim deleted file mode 100644 index f859b87ae..000000000 --- a/tests/stdlib/tintsets.nim +++ /dev/null @@ -1,65 +0,0 @@ -import intsets -import std/sets - -from sequtils import toSeq -from algorithm import sorted - -proc sortedPairs[T](t: T): auto = toSeq(t.pairs).sorted -template sortedItems(t: untyped): untyped = sorted(toSeq(t)) - -block: # we use HashSet as groundtruth, it's well tested elsewhere - template testDel(t, t0) = - - template checkEquals() = - doAssert t.len == t0.len - for k in t0: - doAssert k in t - for k in t: - doAssert k in t0 - - doAssert sortedItems(t) == sortedItems(t0) - - template incl2(i) = - t.incl i - t0.incl i - - template excl2(i) = - t.excl i - t0.excl i - - block: - var expected: seq[int] - let n = 100 - let n2 = n*2 - for i in 0..<n: - incl2(i) - checkEquals() - for i in 0..<n: - if i mod 3 == 0: - if i < n div 2: - excl2(i) - else: - t0.excl i - doAssert i in t - doAssert not t.missingOrExcl(i) - - checkEquals() - for i in n..<n2: - incl2(i) - checkEquals() - for i in 0..<n2: - if i mod 7 == 0: - excl2(i) - checkEquals() - - # notin check - for i in 0..<t.len: - if i mod 7 == 0: - doAssert i notin t0 - doAssert i notin t - # issue #13505 - doAssert t.missingOrExcl(i) - - var t: IntSet - var t0: HashSet[int] - testDel(t, t0) diff --git a/tests/stdlib/tjsonutils.nim b/tests/stdlib/tjsonutils.nim index fefd412e7..28f05ecbe 100644 --- a/tests/stdlib/tjsonutils.nim +++ b/tests/stdlib/tjsonutils.nim @@ -57,7 +57,7 @@ template fn() = """[1.1,"fo",120,[10,11],[true,false],[{"mode":"modeCaseSensitive","table":{"y":"Y","z":"Z"}},{"mode":"modeCaseSensitive","table":{}}],[0,3],-4,{"foo":0.5,"bar":{"a1":"abc"},"bar2":null}]""" block: - # edge case when user defined `==` doesn't handle `nil` well, eg: + # edge case when user defined `==` doesn't handle `nil` well, e.g.: # https://github.com/nim-lang/nimble/blob/63695f490728e3935692c29f3d71944d83bb1e83/src/nimblepkg/version.nim#L105 testRoundtrip(@[Foo(id: 10), nil]): """[{"id":10},null]""" diff --git a/tests/stdlib/tpackedsets.nim b/tests/stdlib/tpackedsets.nim new file mode 100644 index 000000000..d0149adc5 --- /dev/null +++ b/tests/stdlib/tpackedsets.nim @@ -0,0 +1,259 @@ +import std/packedsets +import std/sets + +import sequtils +import algorithm + +block basicIntSetTests: + var y = initPackedSet[int]() + y.incl(1) + y.incl(2) + y.incl(7) + y.incl(1056) + + y.incl(1044) + y.excl(1044) + + doAssert y == [1, 2, 7, 1056].toPackedSet + doAssert toSeq(y.items) == [1, 2, 7, 1056] + + doAssert y.containsOrIncl(888) == false + doAssert 888 in y + doAssert y.containsOrIncl(888) == true + + doAssert y.missingOrExcl(888) == false + doAssert 888 notin y + doAssert y.missingOrExcl(888) == true + +proc sortedPairs[T](t: T): auto = toSeq(t.pairs).sorted +template sortedItems(t: untyped): untyped = sorted(toSeq(t)) + +type Id = distinct int +proc `$`(x: Id): string {.borrow.} +proc cmp(a: Id, b: Id): int {.borrow.} +proc `==`(a: Id, b: Id): bool {.borrow.} +proc `<`(a: Id, b: Id): bool {.borrow.} + +block genericTests: + # we use HashSet as groundtruth, it's well tested elsewhere + template testDel(A: typedesc, t: typed, t0: typed) = + + block: + template checkEquals() = + doAssert t.len == t0.len + for k in t0: + doAssert k in t + for k in t: + doAssert k in t0 + + doAssert sortedItems(t) == sortedItems(t0) + + template incl2(i) = + t.incl i + t0.incl i + + template excl2(i) = + t.excl i + t0.excl i + + var expected: seq[A] + let n = 100 + let n2 = n*2 + for i in 0..<n: + incl2(A(i)) + checkEquals() + for i in 0..<n: + if i mod 3 == 0: + if i < n div 2: + excl2(A(i)) + else: + t0.excl A(i) + doAssert A(i) in t + doAssert not t.missingOrExcl A(i) + + checkEquals() + for i in n..<n2: + incl2(A(i)) + checkEquals() + for i in 0..<n2: + if i mod 7 == 0: + excl2(A(i)) + checkEquals() + + # notin check + for i in 0..<t.len: + if i mod 7 == 0: + doAssert A(i) notin t0 + doAssert A(i) notin t + # issue #13505 + doAssert t.missingOrExcl(A(i)) + + var t: PackedSet[int] + var t0: HashSet[int] + testDel(int, t, t0) + + var distT: PackedSet[Id] + var distT0: HashSet[Id] + testDel(Id, distT, distT0) + + doAssert union(distT, initPackedSet[Id]()) == distT + + var charT: PackedSet[char] + var charT0: HashSet[char] + testDel(char, charT, charT0) + + +block typeSafetyTest: + # mixing sets of different types shouldn't compile + doAssert not compiles( union(initPackedSet[Id](), initPackedSet[int]()) ) + doAssert compiles( union(initPackedSet[Id](), initPackedSet[Id]())) + + var ids: PackedSet[Id] + doAssert not compiles( ids.incl(3) ) + doAssert compiles( ids.incl(Id(3)) ) + + type NonOrdinal = string + doAssert not compiles( initPackedSet[NonOrdinal]() ) + +type EnumABCD = enum A, B, C, D + +block enumTest: + var letterSet = initPackedSet[EnumABCD]() + + for x in [A, C]: + letterSet.incl(x) + + doAssert A in letterSet + doAssert B notin letterSet + doAssert C in letterSet + doAssert D notin letterSet + +type Foo = distinct int16 +proc `$`(a: Foo): string {.borrow.} # `echo a` below won't work without `$` defined, as expected + +block printTest: + var a = initPackedSet[EnumABCD]() + a.incl A + a.incl C + doAssert $a == "{A, C}" + +import intsets + +block legacyMainModuleTests: + template genericTests(A: typedesc[Ordinal], x: typed) = + block: + proc typSeq(s: seq[int]): seq[A] = s.map(proc (i: int): A = A(i)) + x.incl(A(1)) + x.incl(A(2)) + x.incl(A(7)) + x.incl(A(1056)) + + x.incl(A(1044)) + x.excl(A(1044)) + + doAssert x == typSeq(@[1, 2, 7, 1056]).toPackedSet + + doAssert x.containsOrIncl(A(888)) == false + doAssert A(888) in x + doAssert x.containsOrIncl(A(888)) == true + + doAssert x.missingOrExcl(A(888)) == false + doAssert A(888) notin x + doAssert x.missingOrExcl(A(888)) == true + + var xs = toSeq(items(x)) + xs.sort(cmp[A]) + doAssert xs == typSeq(@[1, 2, 7, 1056]) + + var y: PackedSet[A] + assign(y, x) + var ys = toSeq(items(y)) + ys.sort(cmp[A]) + doAssert ys == typSeq(@[1, 2, 7, 1056]) + + doAssert x == y + + var z: PackedSet[A] + for i in 0..1000: + incl z, A(i) + doAssert z.len() == i+1 + for i in 0..1000: + doAssert z.contains(A(i)) + + var w = initPackedSet[A]() + w.incl(A(1)) + w.incl(A(4)) + w.incl(A(50)) + w.incl(A(1001)) + w.incl(A(1056)) + + var xuw = x.union(w) + var xuws = toSeq(items(xuw)) + xuws.sort(cmp) + doAssert xuws == typSeq(@[1, 2, 4, 7, 50, 1001, 1056]) + + var xiw = x.intersection(w) + var xiws = toSeq(items(xiw)) + xiws.sort(cmp) + doAssert xiws == @[A(1), A(1056)] + + var xdw = x.difference(w) + var xdws = toSeq(items(xdw)) + xdws.sort(cmp[A]) + doAssert xdws == @[A(2), A(7)] + + var xsw = x.symmetricDifference(w) + var xsws = toSeq(items(xsw)) + xsws.sort(cmp[A]) + doAssert xsws == typSeq(@[2, 4, 7, 50, 1001]) + + x.incl(w) + xs = toSeq(items(x)) + xs.sort(cmp[A]) + doAssert xs == typSeq(@[1, 2, 4, 7, 50, 1001, 1056]) + + doAssert w <= x + + doAssert w < x + + doAssert(not disjoint(w, x)) + + var u = initPackedSet[A]() + u.incl(A(3)) + u.incl(A(5)) + u.incl(A(500)) + doAssert disjoint(u, x) + + var v = initPackedSet[A]() + v.incl(A(2)) + v.incl(A(50)) + + x.excl(v) + xs = toSeq(items(x)) + xs.sort(cmp[A]) + doAssert xs == typSeq(@[1, 4, 7, 1001, 1056]) + + proc bug12366 = + var + x = initPackedSet[A]() + y = initPackedSet[A]() + n = 3584 + + for i in 0..n: + x.incl(A(i)) + y.incl(A(i)) + + let z = symmetricDifference(x, y) + doAssert z.len == 0 + doAssert $z == "{}" + + bug12366() + + var legacyInit = initIntSet() + genericTests(int, legacyInit) + + var intGenericInit = initPackedSet[int]() + genericTests(int, intGenericInit) + + var intDistinct = initPackedSet[Id]() + genericTests(Id, intDistinct) diff --git a/tests/stdlib/trstgen.nim b/tests/stdlib/trstgen.nim index 8fdbf3911..d35bc5821 100644 --- a/tests/stdlib/trstgen.nim +++ b/tests/stdlib/trstgen.nim @@ -153,3 +153,27 @@ suite "YAML syntax highlighting": assert a == """(( <a class="reference external" href="https://nim-lang.org/">Nim</a> ))""" assert b == """((<a class="reference external" href="https://nim-lang.org/">Nim</a>))""" assert c == """[<a class="reference external" href="https://nim-lang.org/">Nim</a>]""" + + test "Markdown tables": + let input1 = """ +| A1 header | A2 \| not fooled +| :--- | ----: | +| C1 | C2 **bold** | ignored | +| D1 `code \|` | D2 | also ignored +| E1 \| text | +| | F2 without pipe +not in table""" + let output1 = rstToHtml(input1, {roSupportMarkdown}, defaultConfig()) + assert output1 == """<table border="1" class="docutils"><tr><th>A1 header</th><th>A2 | not fooled</th></tr> +<tr><td>C1</td><td>C2 <strong>bold</strong></td></tr> +<tr><td>D1 <tt class="docutils literal"><span class="pre">code |</span></tt></td><td>D2</td></tr> +<tr><td>E1 | text</td><td></td></tr> +<tr><td></td><td>F2 without pipe</td></tr> +</table><p>not in table</p> +""" + let input2 = """ +| A1 header | A2 | +| --- | --- |""" + let output2 = rstToHtml(input2, {roSupportMarkdown}, defaultConfig()) + assert output2 == """<table border="1" class="docutils"><tr><th>A1 header</th><th>A2</th></tr> +</table>""" diff --git a/tests/stdlib/tsharedlist.nim b/tests/stdlib/tsharedlist.nim new file mode 100644 index 000000000..a795be0f3 --- /dev/null +++ b/tests/stdlib/tsharedlist.nim @@ -0,0 +1,17 @@ +import sharedlist + +var + list: SharedList[int] + count: int + +init(list) + +for i in 1 .. 250: + list.add i + +for i in list: + inc count + +doAssert count == 250 + +deinitSharedList(list) diff --git a/tests/stdlib/tstring.nim b/tests/stdlib/tstring.nim index 0a7bf0511..ff3d41b49 100644 --- a/tests/stdlib/tstring.nim +++ b/tests/stdlib/tstring.nim @@ -91,3 +91,10 @@ proc tester[T](x: T) = tester(1) +# #14497 +func reverse*(a: string): string = + result = a + for i in 0 ..< a.len div 2: + swap(result[i], result[^(i + 1)]) + +doAssert reverse("hello") == "olleh" diff --git a/tests/stdlib/tterminal_15874.nim b/tests/stdlib/tterminal_15874.nim new file mode 100644 index 000000000..c3455c350 --- /dev/null +++ b/tests/stdlib/tterminal_15874.nim @@ -0,0 +1,8 @@ +discard """ + cmd: "nim c --app:console $file" + action: "compile" +""" + +import terminal + +writeStyled("hello", {styleBright}) |