diff options
Diffstat (limited to 'tests/collections')
-rw-r--r-- | tests/collections/tapply.nim | 11 | ||||
-rw-r--r-- | tests/collections/tcounttable.nim | 19 | ||||
-rw-r--r-- | tests/collections/thashes.nim | 75 | ||||
-rw-r--r-- | tests/collections/tindexby.nim | 4 | ||||
-rw-r--r-- | tests/collections/tmapit.nim | 33 | ||||
-rw-r--r-- | tests/collections/tsets.nim | 48 | ||||
-rw-r--r-- | tests/collections/ttableconstr.nim | 2 | ||||
-rw-r--r-- | tests/collections/ttables.nim | 3 | ||||
-rw-r--r-- | tests/collections/ttablesref.nim | 1 |
9 files changed, 179 insertions, 17 deletions
diff --git a/tests/collections/tapply.nim b/tests/collections/tapply.nim new file mode 100644 index 000000000..2b7464216 --- /dev/null +++ b/tests/collections/tapply.nim @@ -0,0 +1,11 @@ +discard """ + output: '''true''' +""" + +import sequtils + +var x = @[1, 2, 3] +x.apply(proc(x: var int) = x = x+10) +x.apply(proc(x: int): int = x+100) +x.applyIt(it+5000) +echo x == @[5111, 5112, 5113] diff --git a/tests/collections/tcounttable.nim b/tests/collections/tcounttable.nim new file mode 100644 index 000000000..ebbb1c8e5 --- /dev/null +++ b/tests/collections/tcounttable.nim @@ -0,0 +1,19 @@ +discard """ + output: "And we get here" +""" + +# bug #2625 + +const s_len = 32 + +import tables +var substr_counts: CountTable[string] = initCountTable[string]() +var my_string = "Hello, this is sadly broken for strings over 64 characters. Note that it *does* appear to work for short strings." +for i in 0..(my_string.len - s_len): + let s = my_string[i..i+s_len-1] + substr_counts[s] = 1 + # substr_counts[s] = substr_counts[s] + 1 # Also breaks, + 2 as well, etc. + # substr_counts.inc(s) # This works + #echo "Iteration ", i + +echo "And we get here" diff --git a/tests/collections/thashes.nim b/tests/collections/thashes.nim new file mode 100644 index 000000000..b9c639414 --- /dev/null +++ b/tests/collections/thashes.nim @@ -0,0 +1,75 @@ +discard """ + output: '''true''' +""" + +import tables +from hashes import THash + +# Test with int +block: + var t = initTable[int,int]() + t[0] = 42 + t[1] = t[0] + 1 + assert(t[0] == 42) + assert(t[1] == 43) + let t2 = {1: 1, 2: 2}.toTable + assert(t2[2] == 2) + +# Test with char +block: + var t = initTable[char,int]() + t['0'] = 42 + t['1'] = t['0'] + 1 + assert(t['0'] == 42) + assert(t['1'] == 43) + let t2 = {'1': 1, '2': 2}.toTable + assert(t2['2'] == 2) + +# Test with enum +block: + type + E = enum eA, eB, eC + var t = initTable[E,int]() + t[eA] = 42 + t[eB] = t[eA] + 1 + assert(t[eA] == 42) + assert(t[eB] == 43) + let t2 = {eA: 1, eB: 2}.toTable + assert(t2[eB] == 2) + +# Test with range +block: + type + R = range[1..10] + var t = initTable[R,int]() # causes warning, why? + t[1] = 42 # causes warning, why? + t[2] = t[1] + 1 + assert(t[1] == 42) + assert(t[2] == 43) + let t2 = {1.R: 1, 2.R: 2}.toTable + assert(t2[2.R] == 2) + +# Test which combines the generics for tuples + ordinals +block: + type + E = enum eA, eB, eC + var t = initTable[(string, E, int, char), int]() + t[("a", eA, 0, '0')] = 42 + t[("b", eB, 1, '1')] = t[("a", eA, 0, '0')] + 1 + assert(t[("a", eA, 0, '0')] == 42) + assert(t[("b", eB, 1, '1')] == 43) + let t2 = {("a", eA, 0, '0'): 1, ("b", eB, 1, '1'): 2}.toTable + assert(t2[("b", eB, 1, '1')] == 2) + +# Test to check if overloading is possible +# Unfortunately, this does not seem to work for int +# The same test with a custom hash(s: string) does +# work though. +block: + proc hash(x: int): THash {.inline.} = + echo "overloaded hash" + result = x + var t = initTable[int, int]() + t[0] = 0 + +echo "true" diff --git a/tests/collections/tindexby.nim b/tests/collections/tindexby.nim index f374d5504..88c0b263e 100644 --- a/tests/collections/tindexby.nim +++ b/tests/collections/tindexby.nim @@ -11,11 +11,11 @@ type TElem = object foo: int bar: string - + let elem1 = TElem(foo: 1, bar: "bar") elem2 = TElem(foo: 2, bar: "baz") - + var tbl2 = initTable[string, TElem]() tbl2.add("bar", elem1) tbl2.add("baz", elem2) diff --git a/tests/collections/tmapit.nim b/tests/collections/tmapit.nim new file mode 100644 index 000000000..b2afa9429 --- /dev/null +++ b/tests/collections/tmapit.nim @@ -0,0 +1,33 @@ +discard """ + output: '''true +true''' +""" + +import sequtils + +var x = @[1, 2, 3] +# This mapIt call will run with preallocation because ``len`` is available. +var y = x.mapIt($(it+10)) +echo y == @["11", "12", "13"] + +type structureWithoutLen = object + a: array[5, int] + +iterator items(s: structureWithoutLen): int {.inline.} = + yield s.a[0] + yield s.a[1] + yield s.a[2] + yield s.a[3] + yield s.a[4] + +var st: structureWithoutLen +st.a[0] = 0 +st.a[1] = 1 +st.a[2] = 2 +st.a[3] = 3 +st.a[4] = 4 + +# this will run without preallocating the result +# since ``len`` is not available +var r = st.mapIt($(it+10)) +echo r == @["10", "11", "12", "13", "14"] diff --git a/tests/collections/tsets.nim b/tests/collections/tsets.nim index 656c5b3f2..a5bbe8dbd 100644 --- a/tests/collections/tsets.nim +++ b/tests/collections/tsets.nim @@ -1,17 +1,37 @@ -discard """ - output: '''true -true''' -""" - import sets -var - a = initSet[int]() - b = initSet[int]() - c = initSet[string]() -for i in 0..5: a.incl(i) -for i in 1..6: b.incl(i) -for i in 0..5: c.incl($i) +block setEquality: + var + a = initSet[int]() + b = initSet[int]() + c = initSet[string]() + + for i in 0..5: a.incl(i) + for i in 1..6: b.incl(i) + for i in 0..5: c.incl($i) + + doAssert map(a, proc(x: int): int = x + 1) == b + doAssert map(a, proc(x: int): string = $x) == c + + +block setsContainingTuples: + var set = initSet[tuple[i: int, i64: int64, f: float]]() + set.incl( (i: 123, i64: 123'i64, f: 3.14) ) + doAssert set.contains( (i: 123, i64: 123'i64, f: 3.14) ) + doAssert( not set.contains( (i: 456, i64: 789'i64, f: 2.78) ) ) + + +block setWithTuplesWithSeqs: + var s = initSet[tuple[s: seq[int]]]() + s.incl( (s: @[1, 2, 3]) ) + doAssert s.contains( (s: @[1, 2, 3]) ) + doAssert( not s.contains((s: @[4, 5, 6])) ) + + +block setWithSequences: + var s = initSet[seq[int]]() + s.incl( @[1, 2, 3] ) + doAssert s.contains(@[1, 2, 3]) + doAssert( not s.contains(@[4, 5, 6]) ) + -echo map(a, proc(x: int): int = x + 1) == b -echo map(a, proc(x: int): string = $x) == c diff --git a/tests/collections/ttableconstr.nim b/tests/collections/ttableconstr.nim index 1a21a18d1..a9262e70e 100644 --- a/tests/collections/ttableconstr.nim +++ b/tests/collections/ttableconstr.nim @@ -3,7 +3,7 @@ template ignoreExpr(e: expr): stmt {.immediate.} = discard -# test first class '..' syntactical citizen: +# test first class '..' syntactical citizen: ignoreExpr x <> 2..4 # test table constructor: ignoreExpr({:}) diff --git a/tests/collections/ttables.nim b/tests/collections/ttables.nim index e4cff7c7e..a8a182a78 100644 --- a/tests/collections/ttables.nim +++ b/tests/collections/ttables.nim @@ -66,6 +66,9 @@ block tableTest2: discard assert(not hasKey(t, "111")) + assert "123" in t + assert("111" notin t) + for key, val in items(data): t[key] = val.toFloat for key, val in items(data): assert t[key] == val.toFloat diff --git a/tests/collections/ttablesref.nim b/tests/collections/ttablesref.nim index 92bc65da3..32494f1f2 100644 --- a/tests/collections/ttablesref.nim +++ b/tests/collections/ttablesref.nim @@ -65,6 +65,7 @@ block tableTest2: except KeyError: discard assert(not hasKey(t, "111")) + assert "111" notin t for key, val in items(data): t[key] = val.toFloat for key, val in items(data): assert t[key] == val.toFloat |