From f59ca2736dac0e544c11aaca431408931d3e21de Mon Sep 17 00:00:00 2001 From: Felix Krause Date: Fri, 27 Jun 2014 16:57:01 +0200 Subject: [PATCH 1/2] Fixed `==` for PTables, added test. --- lib/pure/collections/tables.nim | 7 +++++-- tests/table/ptables.nim | 9 +++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim index ce9df09e10..146cb76c9a 100644 --- a/lib/pure/collections/tables.nim +++ b/lib/pure/collections/tables.nim @@ -246,7 +246,8 @@ template equalsImpl() = # different insertion orders mean different 'data' seqs, so we have # to use the slow route here: for key, val in s: - if not hasKey(t, key): return false + # prefix notation leads to automatic dereference in case of PTable + if not t.hasKey(key): return false if t[key] != val: return false return true @@ -332,7 +333,9 @@ proc `$`*[A, B](t: PTable[A, B]): string = dollarImpl() proc `==`*[A, B](s, t: PTable[A, B]): bool = - equalsImpl() + if isNil(s): result = isNil(t) + elif isNil(t): result = false + else: equalsImpl() proc newTableFrom*[A, B, C](collection: A, index: proc(x: B): C): PTable[C, B] = ## Index the collection with the proc provided. diff --git a/tests/table/ptables.nim b/tests/table/ptables.nim index ec52d08c33..79a9aab171 100644 --- a/tests/table/ptables.nim +++ b/tests/table/ptables.nim @@ -104,6 +104,15 @@ block countTableTest1: block SyntaxTest: var x = newTable[int, string]({:}) +block nilTest: + var i, j: PTable[int, int] = nil + assert i == j + j = newTable[int, int]() + assert i != j + assert j != i + i = newTable[int, int]() + assert i == j + proc orderedTableSortTest() = var t = newOrderedTable[string, int](2) for key, val in items(data): t[key] = val From 18003ff1966fc54ed1b2b39988dda6961ce35c80 Mon Sep 17 00:00:00 2001 From: Clay Sweetser Date: Thu, 24 Jul 2014 18:17:20 -0400 Subject: [PATCH 2/2] Added stylistic consistancy. --- lib/pure/collections/tables.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim index 146cb76c9a..dcf2ab4811 100644 --- a/lib/pure/collections/tables.nim +++ b/lib/pure/collections/tables.nim @@ -335,7 +335,7 @@ proc `$`*[A, B](t: PTable[A, B]): string = proc `==`*[A, B](s, t: PTable[A, B]): bool = if isNil(s): result = isNil(t) elif isNil(t): result = false - else: equalsImpl() + else: result = equalsImpl() proc newTableFrom*[A, B, C](collection: A, index: proc(x: B): C): PTable[C, B] = ## Index the collection with the proc provided.