diff options
author | Araq <rumpf_a@web.de> | 2011-06-07 03:37:36 +0200 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2011-06-07 03:37:36 +0200 |
commit | 3bc821aa5c676869d65f1448a005dfd47f197f61 (patch) | |
tree | 16b757f5f8bcab86e46b677b8b8becd63ff04bed /tests/accept | |
parent | 42eb21be7b422b47732a56a1b8420b6f1b63f2c6 (diff) | |
download | Nim-3bc821aa5c676869d65f1448a005dfd47f197f61.tar.gz |
basic generic collections implemented and tested
Diffstat (limited to 'tests/accept')
-rw-r--r-- | tests/accept/run/tlists.nim | 66 | ||||
-rw-r--r-- | tests/accept/run/tsets2.nim | 62 | ||||
-rw-r--r-- | tests/accept/run/ttables.nim | 82 |
3 files changed, 202 insertions, 8 deletions
diff --git a/tests/accept/run/tlists.nim b/tests/accept/run/tlists.nim index e69de29bb..7d5379945 100644 --- a/tests/accept/run/tlists.nim +++ b/tests/accept/run/tlists.nim @@ -0,0 +1,66 @@ +discard """ + output: '''true''' +""" + +import lists + +const + data = [1, 2, 3, 4, 5, 6] + +block SinglyLinkedListTest1: + var L: TSinglyLinkedList[int] + for d in items(data): L.prepend(d) + assert($L == "[6, 5, 4, 3, 2, 1]") + + assert(4 in L) + +block SinglyLinkedListTest2: + var L: TSinglyLinkedList[string] + for d in items(data): L.prepend($d) + assert($L == "[6, 5, 4, 3, 2, 1]") + + assert("4" in L) + + +block DoublyLinkedListTest1: + var L: TDoublyLinkedList[int] + for d in items(data): L.prepend(d) + for d in items(data): L.append(d) + L.remove(L.find(1)) + assert($L == "[6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6]") + + assert(4 in L) + +block SinglyLinkedRingTest1: + var L: TSinglyLinkedRing[int] + L.prepend(4) + assert($L == "[4]") + L.prepend(4) + + assert($L == "[4, 4]") + assert(4 in L) + + +block DoublyLinkedRingTest1: + var L: TDoublyLinkedRing[int] + L.prepend(4) + assert($L == "[4]") + L.prepend(4) + + assert($L == "[4, 4]") + assert(4 in L) + + L.append(3) + L.append(5) + assert($L == "[4, 4, 3, 5]") + + L.remove(L.find(3)) + L.remove(L.find(5)) + L.remove(L.find(4)) + L.remove(L.find(4)) + assert($L == "[]") + assert(4 notin L) + + +echo "true" + diff --git a/tests/accept/run/tsets2.nim b/tests/accept/run/tsets2.nim new file mode 100644 index 000000000..89935072f --- /dev/null +++ b/tests/accept/run/tsets2.nim @@ -0,0 +1,62 @@ +discard """ + output: '''true''' + cmd: "nimrod cc --gc:none --hints:on $# $#" +""" + +import hashes, sets + +const + data = [ + "34", "12", + "90", "0", + "1", "2", + "3", "4", + "5", "6", + "7", "8", + "9", "---00", + "10", "11", "19", + "20", "30", "40", + "50", "60", "70", + "80"] + +block tableTest1: + var t = initSet[tuple[x, y: int]]() + t.incl((0,0)) + t.incl((1,0)) + assert(not t.containsOrIncl((0,1))) + t.incl((1,1)) + + for x in 0..1: + for y in 0..1: + assert((x,y) in t) + #assert($t == + # "{(x: 0, y: 0), (x: 0, y: 1), (x: 1, y: 0), (x: 1, y: 1)}") + +block setTest2: + var t = initSet[string]() + t.incl("test") + t.incl("111") + t.incl("123") + t.excl("111") + + t.incl("012") + t.incl("123") # test duplicates + + assert "123" in t + assert "111" notin t # deleted + + for key in items(data): t.incl(key) + for key in items(data): assert key in t + + +block orderedSetTest1: + var t = data.toOrderedSet + for key in items(data): assert key in t + var i = 0 + # `items` needs to yield in insertion order: + for key in items(t): + assert key == data[i] + inc(i) + +echo "true" + diff --git a/tests/accept/run/ttables.nim b/tests/accept/run/ttables.nim index c7033bf70..72799414e 100644 --- a/tests/accept/run/ttables.nim +++ b/tests/accept/run/ttables.nim @@ -1,18 +1,84 @@ discard """ output: '''true''' + cmd: "nimrod cc --gc:none --hints:on $# $#" """ import hashes, tables -var t = initTable[tuple[x, y: int], string]() -t[(0,0)] = "00" -t[(1,0)] = "10" -t[(0,1)] = "01" -t[(1,1)] = "11" +const + data = { + "34": 123456, "12": 789, + "90": 343, "0": 34404, + "1": 344004, "2": 344774, + "3": 342244, "4": 3412344, + "5": 341232144, "6": 34214544, + "7": 3434544, "8": 344544, + "9": 34435644, "---00": 346677844, + "10": 34484, "11": 34474, "19": 34464, + "20": 34454, "30": 34141244, "40": 344114, + "50": 344490, "60": 344491, "70": 344492, + "80": 344497} -for x in 0..1: - for y in 0..1: - assert t[(x,y)] == $x & $y +block tableTest1: + var t = initTable[tuple[x, y: int], string]() + t[(0,0)] = "00" + t[(1,0)] = "10" + t[(0,1)] = "01" + t[(1,1)] = "11" + for x in 0..1: + for y in 0..1: + assert t[(x,y)] == $x & $y + assert($t == + "{(x: 0, y: 0): 00, (x: 0, y: 1): 01, (x: 1, y: 0): 10, (x: 1, y: 1): 11}") + +block tableTest2: + var t = initTable[string, float]() + t["test"] = 1.2345 + t["111"] = 1.000043 + t["123"] = 1.23 + t.del("111") + + t["012"] = 67.9 + t["123"] = 1.5 # test overwriting + + assert t["123"] == 1.5 + assert t["111"] == 0.0 # deleted + assert(not hasKey(t, "111")) + + for key, val in items(data): t[key] = val.toFloat + for key, val in items(data): assert t[key] == val.toFloat + + +block orderedTableTest1: + var t = initOrderedTable[string, int](2) + for key, val in items(data): t[key] = val + for key, val in items(data): assert t[key] == val + var i = 0 + # `pairs` needs to yield in insertion order: + for key, val in pairs(t): + assert key == data[i][0] + assert val == data[i][1] + inc(i) + +block countTableTest1: + var s = data.toTable + var t = initCountTable[string]() + for k in s.Keys: t.inc(k) + for k in t.keys: assert t[k] == 1 + t.inc("90", 3) + t.inc("12", 2) + t.inc("34", 1) + assert t.largest()[0] == "90" + + t.sort() + var i = 0 + for k, v in t.pairs: + case i + of 0: assert k == "90" and v == 4 + of 1: assert k == "12" and v == 3 + of 2: assert k == "34" and v == 2 + else: break + inc i echo "true" |