1 | /* | |
2 | This file is part of Combean. | |
3 | ||
4 | Combean is free software; you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
6 | the Free Software Foundation; either version 2 of the License, or | |
7 | (at your option) any later version. | |
8 | ||
9 | Combean is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with Foobar; if not, write to the Free Software | |
16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | */ | |
18 | package net.sourceforge.combean.test.util; | |
19 | ||
20 | import net.sourceforge.combean.util.GNestedIterator; | |
21 | ||
22 | class TestGNestedIterator extends GroovyTestCase { | |
23 | ||
24 | void testConstantInnerIteration() { | |
25 | 3 | List outerList = ["a", "b"]; |
26 | 3 | Range innerRange = 1..2; |
27 | ||
28 | 3 | GNestedIterator itNested = new GNestedIterator(outerList.iterator()) { |
29 | 6 | innerRange.iterator(); |
30 | } | |
31 | ||
32 | 3 | outerList.each{ outer -> |
33 | 6 | innerRange.each { inner -> |
34 | 12 | assertTrue(itNested.hasNext()); |
35 | 12 | Object innerObj = itNested.next(); |
36 | 12 | assertEquals(inner, innerObj); |
37 | } | |
38 | } | |
39 | 3 | assertFalse(itNested.hasNext()); |
40 | } | |
41 | ||
42 | void testVariableInnerIteration() { | |
43 | 3 | Range outerRange = 1..3; |
44 | ||
45 | 3 | GNestedIterator itNested = new GNestedIterator(outerRange.iterator()) { |
46 | 9 | (1..it).iterator(); |
47 | } | |
48 | ||
49 | 3 | outerRange.each{ outer -> |
50 | 9 | (1..outer).each { inner -> |
51 | 18 | assertTrue(itNested.hasNext()); |
52 | 18 | Object innerObj = itNested.next(); |
53 | 18 | assertEquals(inner, innerObj); |
54 | } | |
55 | } | |
56 | 3 | assertFalse(itNested.hasNext()); |
57 | } | |
58 | ||
59 | void testVariableIterationReturningIterables() { | |
60 | 3 | Range outerRange = 1..3; |
61 | ||
62 | 3 | GNestedIterator itNested = new GNestedIterator(outerRange) { 1..it } |
63 | ||
64 | 3 | outerRange.each{ outer -> |
65 | 9 | (1..outer).each { inner -> |
66 | 18 | assertTrue(itNested.hasNext()); |
67 | 18 | Object innerObj = itNested.next(); |
68 | 18 | assertEquals(inner, innerObj); |
69 | } | |
70 | } | |
71 | 3 | assertFalse(itNested.hasNext()); |
72 | } | |
73 | } |