diff options
author | Timothee Cour <timothee.cour2@gmail.com> | 2020-02-26 13:07:09 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-26 22:07:09 +0100 |
commit | 42dad3a836f7eed860f300e68b33d4c0b39bd1f4 (patch) | |
tree | 1d079a4107f081212a11dde90ec489c49d675cb3 /tests/stdlib | |
parent | 9a93f73983945a44d891013f728e407ee421287b (diff) | |
download | Nim-42dad3a836f7eed860f300e68b33d4c0b39bd1f4.tar.gz |
tables/sharedtables/intsets/etc: fix #13496, #13504, #13505; add lots of tests (#13498) [backport]
* fix #13496 handle tombstones * add test * more tests * fix #13504; add SharedTable tests * fix #https://github.com/nim-lang/Nim/issues/13505 intsets.missingOrExcl silently gave wrong results sometimes * add test for tintsets
Diffstat (limited to 'tests/stdlib')
-rw-r--r-- | tests/stdlib/tintsets.nim | 65 | ||||
-rw-r--r-- | tests/stdlib/tsharedtable.nim | 86 |
2 files changed, 145 insertions, 6 deletions
diff --git a/tests/stdlib/tintsets.nim b/tests/stdlib/tintsets.nim new file mode 100644 index 000000000..f859b87ae --- /dev/null +++ b/tests/stdlib/tintsets.nim @@ -0,0 +1,65 @@ +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/tsharedtable.nim b/tests/stdlib/tsharedtable.nim index 99d20e08a..ce6aa96df 100644 --- a/tests/stdlib/tsharedtable.nim +++ b/tests/stdlib/tsharedtable.nim @@ -6,10 +6,84 @@ output: ''' import sharedtables -var table: SharedTable[int, int] +block: + var table: SharedTable[int, int] -init(table) -table[1] = 10 -assert table.mget(1) == 10 -assert table.mgetOrPut(3, 7) == 7 -assert table.mgetOrPut(3, 99) == 7 + init(table) + table[1] = 10 + assert table.mget(1) == 10 + assert table.mgetOrPut(3, 7) == 7 + assert table.mgetOrPut(3, 99) == 7 + deinitSharedTable(table) + +import sequtils, algorithm +proc sortedPairs[T](t: T): auto = toSeq(t.pairs).sorted +template sortedItems(t: untyped): untyped = sorted(toSeq(t)) + +import tables # refs issue #13504 + +block: # we use Table as groundtruth, it's well tested elsewhere + template testDel(t, t0) = + template put2(i) = + t[i] = i + t0[i] = i + + template add2(i, val) = + t.add(i, val) + t0.add(i, val) + + template del2(i) = + t.del(i) + t0.del(i) + + template checkEquals() = + doAssert t.len == t0.len + for k,v in t0: + doAssert t.mgetOrPut(k, -1) == v # sanity check + doAssert t.mget(k) == v + + let n = 100 + let n2 = n*2 + let n3 = n*3 + let n4 = n*4 + let n5 = n*5 + + for i in 0..<n: + put2(i) + for i in 0..<n: + if i mod 3 == 0: + del2(i) + for i in n..<n2: + put2(i) + for i in 0..<n2: + if i mod 7 == 0: + del2(i) + + checkEquals() + + for i in n2..<n3: + t0[i] = -2 + doAssert t.mgetOrPut(i, -2) == -2 + doAssert t.mget(i) == -2 + + for i in 0..<n4: + let ok = i in t0 + if not ok: t0[i] = -i + doAssert t.hasKeyOrPut(i, -i) == ok + + checkEquals() + + for i in n4..<n5: + add2(i, i*10) + add2(i, i*11) + add2(i, i*12) + del2(i) + del2(i) + + checkEquals() + + var t: SharedTable[int, int] + init(t) # ideally should be auto-init + var t0: Table[int, int] + testDel(t, t0) + deinitSharedTable(t) |