1 25 26 package net.killingar.forum.test; 27 28 import net.killingar.forum.internal.*; 29 30 import java.util.ArrayList ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 34 public class IteratorTests 35 { 36 final int length = 15; 37 final int start = 0; 38 39 public static void main(String [] 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 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 i = new TreeIterator(top); 76 for (; i.hasNext(); c++) 77 { 78 i.hasNext(); 79 Message m = (Message)((ParentIDItemTreeNode)i.next()).item; 80 81 83 if (m.getId() != c) 84 throw new RuntimeException ("TreeIterator broken "+c+" != "+m.getId()); 85 } 86 87 if (c != 15)throw new RuntimeException ("TreeIterator broken "+c); 88 89 System.out.println("TreeIterator: passed"); 90 } 91 92 void testFilterMessagesIterator() 93 { 94 { 96 ArrayList l = new ArrayList (); 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 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 ("FilterMessagesIterator broken "+c); 107 } 108 109 if (c != length)throw new RuntimeException ("FilterMessagesIterator broken "+c); 110 } 111 112 System.out.println("FilterMessagesIterator: passed filter off"); 113 114 116 { 119 ArrayList l = new ArrayList (); 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 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 ("FilterMessagesIterator broken "+c); 130 } 131 132 if (c != 6)throw new RuntimeException ("FilterMessagesIterator broken "+c); 133 134 System.out.println("FilterMessagesIterator: passed test 1"); 135 } 136 137 { 140 ArrayList l = new ArrayList (); 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 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 ("FilterMessagesIterator broken "+c); 151 } 152 153 if (c != length)throw new RuntimeException ("FilterMessagesIterator broken "+c); 154 155 System.out.println("FilterMessagesIterator: passed test 2"); 156 } 157 158 { 161 ArrayList l = new ArrayList (); 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 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 ("FilterMessagesIterator broken "+c); 172 } 173 174 if (c != 6)throw new RuntimeException ("FilterMessagesIterator broken "+c); 175 176 System.out.println("FilterMessagesIterator: passed test 3"); 177 } 178 179 { 182 ArrayList l = new ArrayList (); 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 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 ("FilterMessagesIterator broken "+c); 193 } 194 195 if (c != length)throw new RuntimeException ("FilterMessagesIterator broken "+c); 196 197 System.out.println("FilterMessagesIterator: passed test 4"); 198 } 199 } 200 201 void testSubsetIterator() 202 { 203 ArrayList l = new ArrayList (); 204 205 for (int i = start; i != length; i++) 206 l.add(new Integer (i)); 207 208 { 211 int c = 3; 212 for (Iterator i = new SubsetIterator(c, 5, l.iterator()); i.hasNext(); c++) 213 { 214 i.hasNext(); i.hasNext(); 215 if (((Integer )i.next()).intValue() != c) 216 throw new RuntimeException ("SubsetIterator broken "+c); 217 } 218 219 if (c != 6)throw new RuntimeException ("SubsetIterator broken "+c); 220 221 System.out.println("SubsetIterator: passed test 1"); 222 } 223 224 { 227 int c = 3; 228 for (Iterator i = new SubsetIterator(c, length-1, l.iterator()); i.hasNext(); c++) 229 { 230 i.hasNext(); i.hasNext(); 231 if (((Integer )i.next()).intValue() != c) 232 throw new RuntimeException ("SubsetIterator broken "+c); 233 } 234 235 if (c != length)throw new RuntimeException ("SubsetIterator broken "+c); 236 237 System.out.println("SubsetIterator: passed test 2"); 238 } 239 240 { 243 int c = start; 244 for (Iterator i = new SubsetIterator(c, 4, l.iterator()); i.hasNext(); c++) 245 { 246 i.hasNext(); i.hasNext(); 247 if (((Integer )i.next()).intValue() != c) 248 throw new RuntimeException ("SubsetIterator broken "+c); 249 } 250 251 if (c != 5)throw new RuntimeException ("SubsetIterator broken "+c); 252 253 System.out.println("SubsetIterator: passed test 3"); 254 } 255 256 { 259 int c = start; 260 for (Iterator i = new SubsetIterator(c, length-1, l.iterator()); i.hasNext(); c++) 261 { 262 i.hasNext(); i.hasNext(); 263 if (((Integer )i.next()).intValue() != c) 264 throw new RuntimeException ("SubsetIterator broken "+c); 265 } 266 267 if (c != length)throw new RuntimeException ("SubsetIterator broken "+c); 268 269 System.out.println("SubsetIterator: passed test 4"); 270 } 271 } 272 } | Popular Tags |