KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > test > IteratorTests


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 package net.killingar.forum.test;
27
28 import net.killingar.forum.internal.*;
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33
34 public class IteratorTests
35 {
36     final int length = 15;
37     final int start = 0;
38
39     public static void main(String JavaDoc[] args)
40     {
41         new IteratorTests();
42     }
43
44     IteratorTests()
45     {
46         testTreeIterator();
47         testFilterMessagesIterator();
48         testSubsetIterator();
49     }
50
51     void testTreeIterator()
52     {
53         ParentIDItemTreeNode top = new ParentIDItemTreeNode();
54         {
55             List JavaDoc l = top.getSubnodesAlways();
56             int c = 0;
57             l.add(new ParentIDItemTreeNode(new Message(c++)));
58             l.add(new ParentIDItemTreeNode(new Message(c++)));
59             l.add(new ParentIDItemTreeNode(new Message(c++)));
60             l.add(new ParentIDItemTreeNode(new Message(c++)));
61             l = ((ParentIDItemTreeNode)l.get(3)).getSubnodesAlways();
62             for (int i = 0; i != 5; i++, c++)
63             {
64                 l.add(new ParentIDItemTreeNode(new Message(c)));
65             }
66             l.add(new ParentIDItemTreeNode(new Message(c++)));
67             l.add(new ParentIDItemTreeNode(new Message(c++)));
68             l.add(new ParentIDItemTreeNode(new Message(c++)));
69             l.add(new ParentIDItemTreeNode(new Message(c++)));
70             l.add(new ParentIDItemTreeNode(new Message(c++)));
71             l.add(new ParentIDItemTreeNode(new Message(c++)));
72         }
73
74         int c = 0;
75         Iterator JavaDoc i = new TreeIterator(top);
76         for (; i.hasNext(); c++)
77         {
78             i.hasNext();
79             Message m = (Message)((ParentIDItemTreeNode)i.next()).item;
80
81             //if (m == null && c == 0)continue;
82

83             if (m.getId() != c)
84                 throw new RuntimeException JavaDoc("TreeIterator broken "+c+" != "+m.getId());
85         }
86
87         if (c != 15)throw new RuntimeException JavaDoc("TreeIterator broken "+c);
88
89         System.out.println("TreeIterator: passed");
90     }
91
92     void testFilterMessagesIterator()
93     {
94         // filter off
95
{
96             ArrayList JavaDoc l = new ArrayList JavaDoc();
97
98             for (int i = start; i != length; i++)
99                 l.add(new ParentIDItemTreeNode(new Message(i, i >= 3 && i <= 5)));
100
101             int c = 0;
102             for (Iterator JavaDoc i = new FilterMessagesIterator(true, l.iterator()); i.hasNext(); c++)
103             {
104                 i.hasNext(); i.hasNext();
105                 if (((Message)((ParentIDItemTreeNode)i.next()).item).getId() != c)
106                     throw new RuntimeException JavaDoc("FilterMessagesIterator broken "+c);
107             }
108
109             if (c != length)throw new RuntimeException JavaDoc("FilterMessagesIterator broken "+c);
110         }
111
112         System.out.println("FilterMessagesIterator: passed filter off");
113
114         // filter on
115

116         // 1
117
// middle
118
{
119             ArrayList JavaDoc l = new ArrayList JavaDoc();
120
121             for (int i = start; i != length; i++)
122                 l.add(new ParentIDItemTreeNode(new Message(i, i >= 3 && i <= 5)));
123
124             int c = 3;
125             for (Iterator JavaDoc i = new FilterMessagesIterator(false, l.iterator()); i.hasNext(); c++)
126             {
127                 i.hasNext(); i.hasNext();
128                 if (((Message)((ParentIDItemTreeNode)i.next()).item).getId() != c)
129                     throw new RuntimeException JavaDoc("FilterMessagesIterator broken "+c);
130             }
131
132             if (c != 6)throw new RuntimeException JavaDoc("FilterMessagesIterator broken "+c);
133
134             System.out.println("FilterMessagesIterator: passed test 1");
135         }
136
137         // 2
138
// beginning in middle, including last element
139
{
140             ArrayList JavaDoc l = new ArrayList JavaDoc();
141
142             for (int i = start; i != length; i++)
143                 l.add(new ParentIDItemTreeNode(new Message(i, i >= 3 && i <= length-1)));
144
145             int c = 3;
146             for (Iterator JavaDoc i = new FilterMessagesIterator(false, l.iterator()); i.hasNext(); c++)
147             {
148                 i.hasNext(); i.hasNext();
149                 if (((Message)((ParentIDItemTreeNode)i.next()).item).getId() != c)
150                     throw new RuntimeException JavaDoc("FilterMessagesIterator broken "+c);
151             }
152
153             if (c != length)throw new RuntimeException JavaDoc("FilterMessagesIterator broken "+c);
154
155             System.out.println("FilterMessagesIterator: passed test 2");
156         }
157
158         // 3
159
// beginning at start, ending in middle
160
{
161             ArrayList JavaDoc l = new ArrayList JavaDoc();
162
163             for (int i = start; i != length; i++)
164                 l.add(new ParentIDItemTreeNode(new Message(i, i >= start && i <= 5)));
165
166             int c = start;
167             for (Iterator JavaDoc i = new FilterMessagesIterator(false, l.iterator()); i.hasNext(); c++)
168             {
169                 i.hasNext(); i.hasNext();
170                 if (((Message)((ParentIDItemTreeNode)i.next()).item).getId() != c)
171                     throw new RuntimeException JavaDoc("FilterMessagesIterator broken "+c);
172             }
173
174             if (c != 6)throw new RuntimeException JavaDoc("FilterMessagesIterator broken "+c);
175
176             System.out.println("FilterMessagesIterator: passed test 3");
177         }
178
179         // 4
180
// beginning at start, ending includes last
181
{
182             ArrayList JavaDoc l = new ArrayList JavaDoc();
183
184             for (int i = start; i != length; i++)
185                 l.add(new ParentIDItemTreeNode(new Message(i, i >= start && i <= length-1)));
186
187             int c = start;
188             for (Iterator JavaDoc i = new FilterMessagesIterator(false, l.iterator()); i.hasNext(); c++)
189             {
190                 i.hasNext(); i.hasNext();
191                 if (((Message)((ParentIDItemTreeNode)i.next()).item).getId() != c)
192                     throw new RuntimeException JavaDoc("FilterMessagesIterator broken "+c);
193             }
194
195             if (c != length)throw new RuntimeException JavaDoc("FilterMessagesIterator broken "+c);
196
197             System.out.println("FilterMessagesIterator: passed test 4");
198         }
199     }
200
201     void testSubsetIterator()
202     {
203         ArrayList JavaDoc l = new ArrayList JavaDoc();
204
205         for (int i = start; i != length; i++)
206             l.add(new Integer JavaDoc(i));
207
208         // 1
209
// subset in the middle
210
{
211             int c = 3;
212             for (Iterator JavaDoc i = new SubsetIterator(c, 5, l.iterator()); i.hasNext(); c++)
213             {
214                 i.hasNext(); i.hasNext();
215                 if (((Integer JavaDoc)i.next()).intValue() != c)
216                     throw new RuntimeException JavaDoc("SubsetIterator broken "+c);
217             }
218
219             if (c != 6)throw new RuntimeException JavaDoc("SubsetIterator broken "+c);
220
221             System.out.println("SubsetIterator: passed test 1");
222         }
223
224         // 2
225
// subset beginning in middle, including last element
226
{
227             int c = 3;
228             for (Iterator JavaDoc i = new SubsetIterator(c, length-1, l.iterator()); i.hasNext(); c++)
229             {
230                 i.hasNext(); i.hasNext();
231                 if (((Integer JavaDoc)i.next()).intValue() != c)
232                     throw new RuntimeException JavaDoc("SubsetIterator broken "+c);
233             }
234
235             if (c != length)throw new RuntimeException JavaDoc("SubsetIterator broken "+c);
236
237             System.out.println("SubsetIterator: passed test 2");
238         }
239
240         // 3
241
// subset beginning at start, ending in middle
242
{
243             int c = start;
244             for (Iterator JavaDoc i = new SubsetIterator(c, 4, l.iterator()); i.hasNext(); c++)
245             {
246                 i.hasNext(); i.hasNext();
247                 if (((Integer JavaDoc)i.next()).intValue() != c)
248                     throw new RuntimeException JavaDoc("SubsetIterator broken "+c);
249             }
250
251             if (c != 5)throw new RuntimeException JavaDoc("SubsetIterator broken "+c);
252
253             System.out.println("SubsetIterator: passed test 3");
254         }
255
256         // 4
257
// subset beginning at start, ending includes last
258
{
259             int c = start;
260             for (Iterator JavaDoc i = new SubsetIterator(c, length-1, l.iterator()); i.hasNext(); c++)
261             {
262                 i.hasNext(); i.hasNext();
263                 if (((Integer JavaDoc)i.next()).intValue() != c)
264                     throw new RuntimeException JavaDoc("SubsetIterator broken "+c);
265             }
266
267             if (c != length)throw new RuntimeException JavaDoc("SubsetIterator broken "+c);
268
269             System.out.println("SubsetIterator: passed test 4");
270         }
271     }
272 }
Popular Tags