diff options
author | Lolo Iccl <oxisccl@gmail.com> | 2018-05-05 08:06:29 +0900 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2018-05-09 17:41:41 +0200 |
commit | 5c7b66e07a3811b3fa0b8e7bbfe4cf25a6361d3a (patch) | |
tree | 1214f58cb2281532f4b162a2362f6d1d9e873e66 /tests/collections | |
parent | ee8313da3ff19d9340285c7177db84e1c62fc108 (diff) | |
download | Nim-5c7b66e07a3811b3fa0b8e7bbfe4cf25a6361d3a.tar.gz |
Modify previous commit and add tests
Diffstat (limited to 'tests/collections')
-rw-r--r-- | tests/collections/tsets.nim | 56 |
1 files changed, 52 insertions, 4 deletions
diff --git a/tests/collections/tsets.nim b/tests/collections/tsets.nim index 6139560bd..be624ffe8 100644 --- a/tests/collections/tsets.nim +++ b/tests/collections/tsets.nim @@ -1,4 +1,6 @@ import sets +import hashes +import algorithm block setEquality: var @@ -35,7 +37,7 @@ block setWithSequences: doAssert( not s.contains(@[4, 5, 6]) ) block setClearWorked: - var s = initSet[char]() + var s = initSet[char]() for c in "this is a test": s.incl(c) @@ -68,12 +70,58 @@ block orderedSetClearWorked: for c in "eat at joes": s.incl(c) - r = "" + r = "" for c in items(s): add(r, c) doAssert r == "zeat jos" +block hashForHashedSet: + let + seq1 = "This is the test." + seq2 = "the test is This." + s1 = seq1.toSet() + s2 = seq2.toSet() + var hashSeq: seq[Hash] = @[] + doAssert s1 == s2 + doAssert hash(s1) == hash(s2) + for c in seq1: + if (not (hash(c) in hashSeq)): + hashSeq.add(hash(c)) + doAssert hash(s1) == hash(sorted(hashSeq, cmp[Hash])) + +block hashForOrderdSet: + let + str = "This is the test." + rstr = str.reversed - - + var + s1 = initOrderedSet[char]() + s2 = initOrderedSet[char]() + r = initOrderedSet[char]() + expected: Hash + added: seq[char] = @[] + reversed: Hash + radded: seq[char] = @[] + + expected = 0 + for c in str: + if (not (c in added)): + expected = expected !& hash(c) + added.add(c) + s1.incl(c) + s2.incl(c) + expected = !$expected + doAssert hash(s1) == expected + doAssert hash(s1) == hash(s2) + doAssert hash(s1) != hash(r) + + reversed = 0 + for c in rstr: + if (not (c in radded)): + reversed = reversed !& hash(c) + radded.add(c) + r.incl(c) + reversed = !$reversed + doAssert hash(r) == reversed + doAssert hash(s1) != reversed |