From ac0f15379cb0409e47512bcd40af28d1e7816337 Mon Sep 17 00:00:00 2001 From: Simon Hafner Date: Thu, 30 Jan 2014 02:07:21 -0600 Subject: added Cartesian product --- tests/stdlib/talgorithm.nim | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 tests/stdlib/talgorithm.nim (limited to 'tests') diff --git a/tests/stdlib/talgorithm.nim b/tests/stdlib/talgorithm.nim new file mode 100644 index 000000000..ea57883b0 --- /dev/null +++ b/tests/stdlib/talgorithm.nim @@ -0,0 +1,9 @@ +import unittest + +suite "product": + test "a simple case of one element": + check product(@[@[1,2]]) == @[@[1,2]] + test "two elements": + check product(@[@[1,2], @[3,4]]) == @[@[2,4],@[1,4],@[2,3],@[1,3]] + test "three elements": + check product(@[@[1,2], @[3,4], @[5,6]]) == @[@[2,4,6],@[1,4,6],@[2,3,6],@[1,3,6], @[2,4,5],@[1,4,5],@[2,3,5],@[1,3,5]] -- cgit 1.4.1-2-gfad0 From e01fb17d023b046b3403a4413a637d24a9dc492f Mon Sep 17 00:00:00 2001 From: Simon Hafner Date: Thu, 30 Jan 2014 23:55:43 -0600 Subject: product more robust against empty input --- lib/pure/algorithm.nim | 1 + tests/stdlib/talgorithm.nim | 5 +++++ 2 files changed, 6 insertions(+) (limited to 'tests') diff --git a/lib/pure/algorithm.nim b/lib/pure/algorithm.nim index b71b2c0fc..921c659de 100644 --- a/lib/pure/algorithm.nim +++ b/lib/pure/algorithm.nim @@ -148,6 +148,7 @@ proc product*[T](x: openarray[seq[T]]): seq[seq[T]] = var next: seq[T] = @[] next.setLen(x.len) for i in 0..(x.len-1): + if len(x[i]) == 0: return initial[i] = len(x[i])-1 indexes = initial while true: diff --git a/tests/stdlib/talgorithm.nim b/tests/stdlib/talgorithm.nim index ea57883b0..37de1262f 100644 --- a/tests/stdlib/talgorithm.nim +++ b/tests/stdlib/talgorithm.nim @@ -1,6 +1,11 @@ import unittest +import algorithm suite "product": + test "empty input": + check product[int](newSeq[seq[int]]()) == newSeq[seq[int]]() + test "bit more empty input": + check product[int](@[newSeq[int](), @[], @[]]) == newSeq[seq[int]]() test "a simple case of one element": check product(@[@[1,2]]) == @[@[1,2]] test "two elements": -- cgit 1.4.1-2-gfad0 From f9c2ec8d9238afaff285b2ba286f8c5534b05eec Mon Sep 17 00:00:00 2001 From: Dominik Picheta Date: Sat, 1 Feb 2014 14:57:52 +0000 Subject: Fixes wrong slurp path in tester. --- tests/tester.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/tester.nim b/tests/tester.nim index 0e125b1bb..9f9da6bfe 100644 --- a/tests/tester.nim +++ b/tests/tester.nim @@ -193,8 +193,8 @@ const Test results -- cgit 1.4.1-2-gfad0