1 17 package org.apache.commons.collections.primitives; 18 19 import java.io.Serializable ; 20 import java.util.ArrayList ; 21 import java.util.ConcurrentModificationException ; 22 import java.util.List ; 23 24 import org.apache.commons.collections.primitives.adapters.BaseTestList; 25 import org.apache.commons.collections.primitives.adapters.ListShortList; 26 import org.apache.commons.collections.primitives.adapters.ShortListList; 27 28 32 public abstract class TestShortList extends BaseTestList { 33 34 37 public TestShortList(String testName) { 38 super(testName); 39 } 40 41 44 47 protected abstract ShortList makeEmptyShortList(); 48 49 protected ShortList makeFullShortList() { 50 ShortList list = makeEmptyShortList(); 51 short[] values = getFullShorts(); 52 for(int i=0;i<values.length;i++) { 53 list.add(values[i]); 54 } 55 return list; 56 } 57 58 protected short[] getFullShorts() { 59 short[] result = new short[19]; 60 for(short i = 0; i < result.length; i++) { 61 result[i] = (short)(Short.MAX_VALUE - i); 62 } 63 return result; 64 } 65 66 protected short[] getOtherShorts() { 67 short[] result = new short[16]; 68 for(short i = 0; i < result.length; i++) { 69 result[i] = (short)(i + 43); 70 } 71 return result; 72 } 73 74 77 public List makeEmptyList() { 78 return new ShortListList(makeEmptyShortList()); 79 } 80 81 public Object [] getFullElements() { 82 return wrapArray(getFullShorts()); 83 } 84 85 public Object [] getOtherElements() { 86 return wrapArray(getOtherShorts()); 87 } 88 89 92 private Short [] wrapArray(short[] primitives) { 93 Short [] result = new Short [primitives.length]; 94 for(int i=0;i<result.length;i++) { 95 result[i] = new Short (primitives[i]); 96 } 97 return result; 98 } 99 100 103 public void testExceptionOnConcurrentModification() { 104 ShortList list = makeFullShortList(); 105 ShortIterator iter = list.iterator(); 106 iter.next(); 107 list.add((short)3); 108 try { 109 iter.next(); 110 fail("Expected ConcurrentModificationException"); 111 } catch(ConcurrentModificationException e) { 112 } 114 } 115 116 public void testAddAllShortListAtIndex() { 117 ShortList source = makeFullShortList(); 118 ShortList dest = makeFullShortList(); 119 dest.addAll(1,source); 120 121 ShortIterator iter = dest.iterator(); 122 assertTrue(iter.hasNext()); 123 assertEquals(source.get(0),iter.next()); 124 for(int i=0;i<source.size();i++) { 125 assertTrue(iter.hasNext()); 126 assertEquals(source.get(i),iter.next()); 127 } 128 for(int i=1;i<source.size();i++) { 129 assertTrue(iter.hasNext()); 130 assertEquals(source.get(i),iter.next()); 131 } 132 assertFalse(iter.hasNext()); 133 } 134 135 public void testToJustBigEnoughShortArray() { 136 ShortList list = makeFullShortList(); 137 short[] dest = new short[list.size()]; 138 assertSame(dest,list.toArray(dest)); 139 int i=0; 140 for(ShortIterator iter = list.iterator(); iter.hasNext();i++) { 141 assertEquals(iter.next(),dest[i]); 142 } 143 } 144 145 public void testToLargerThanNeededShortArray() { 146 ShortList list = makeFullShortList(); 147 short[] dest = new short[list.size()*2]; 148 for(int i=0;i<dest.length;i++) { 149 dest[i] = Short.MAX_VALUE; 150 } 151 assertSame(dest,list.toArray(dest)); 152 int i=0; 153 for(ShortIterator iter = list.iterator(); iter.hasNext();i++) { 154 assertEquals(iter.next(),dest[i]); 155 } 156 for(;i<dest.length;i++) { 157 assertEquals(Short.MAX_VALUE,dest[i]); 158 } 159 } 160 161 public void testToSmallerThanNeededShortArray() { 162 ShortList list = makeFullShortList(); 163 short[] dest = new short[list.size()/2]; 164 short[] dest2 = list.toArray(dest); 165 assertTrue(dest != dest2); 166 int i=0; 167 for(ShortIterator iter = list.iterator(); iter.hasNext();i++) { 168 assertEquals(iter.next(),dest2[i]); 169 } 170 } 171 172 public void testHashCodeSpecification() { 173 ShortList list = makeFullShortList(); 174 int hash = 1; 175 for(ShortIterator iter = list.iterator(); iter.hasNext(); ) { 176 hash = 31*hash + iter.next(); 177 } 178 assertEquals(hash,list.hashCode()); 179 } 180 181 public void testEqualsWithTwoShortLists() { 182 ShortList one = makeEmptyShortList(); 183 assertEquals("Equals is reflexive on empty list",one,one); 184 ShortList two = makeEmptyShortList(); 185 assertEquals("Empty lists are equal",one,two); 186 assertEquals("Equals is symmetric on empty lists",two,one); 187 188 one.add((short)1); 189 assertEquals("Equals is reflexive on non empty list",one,one); 190 assertTrue(!one.equals(two)); 191 assertTrue(!two.equals(one)); 192 193 two.add((short)1); 194 assertEquals("Non empty lists are equal",one,two); 195 assertEquals("Equals is symmetric on non empty list",one,two); 196 197 one.add((short)1); one.add((short)2); one.add((short)3); one.add((short)5); one.add((short)8); 198 assertEquals("Equals is reflexive on larger non empty list",one,one); 199 assertTrue(!one.equals(two)); 200 assertTrue(!two.equals(one)); 201 202 two.add((short)1); two.add((short)2); two.add((short)3); two.add((short)5); two.add((short)8); 203 assertEquals("Larger non empty lists are equal",one,two); 204 assertEquals("Equals is symmetric on larger non empty list",two,one); 205 206 one.add((short)9); 207 two.add((short)10); 208 assertTrue(!one.equals(two)); 209 assertTrue(!two.equals(one)); 210 211 } 212 213 public void testShortSubListEquals() { 214 ShortList one = makeEmptyShortList(); 215 assertEquals(one,one.subList(0,0)); 216 assertEquals(one.subList(0,0),one); 217 218 one.add((short)1); 219 assertEquals(one,one.subList(0,1)); 220 assertEquals(one.subList(0,1),one); 221 222 one.add((short)1); one.add((short)2); one.add((short)3); one.add((short)5); one.add((short)8); 223 assertEquals(one.subList(0,4),one.subList(0,4)); 224 assertEquals(one.subList(3,5),one.subList(3,5)); 225 } 226 227 public void testEqualsWithShortListAndList() { 228 ShortList ilist = makeEmptyShortList(); 229 List list = new ArrayList (); 230 231 assertTrue("Unwrapped, empty List should not be equal to empty ShortList.",!ilist.equals(list)); 232 assertTrue("Unwrapped, empty ShortList should not be equal to empty List.",!list.equals(ilist)); 233 234 assertEquals(new ListShortList(list),ilist); 235 assertEquals(ilist,new ListShortList(list)); 236 assertEquals(new ShortListList(ilist),list); 237 assertEquals(list,new ShortListList(ilist)); 238 239 ilist.add((short)1); 240 list.add(new Short ((short)1)); 241 242 assertTrue("Unwrapped, non-empty List is not equal to non-empty ShortList.",!ilist.equals(list)); 243 assertTrue("Unwrapped, non-empty ShortList is not equal to non-empty List.",!list.equals(ilist)); 244 245 assertEquals(new ListShortList(list),ilist); 246 assertEquals(ilist,new ListShortList(list)); 247 assertEquals(new ShortListList(ilist),list); 248 assertEquals(list,new ShortListList(ilist)); 249 250 ilist.add((short)1); ilist.add((short)2); ilist.add((short)3); ilist.add((short)5); ilist.add((short)8); 251 list.add(new Short ((short)1)); list.add(new Short ((short)2)); list.add(new Short ((short)3)); list.add(new Short ((short)5)); list.add(new Short ((short)8)); 252 253 assertTrue("Unwrapped, non-empty List is not equal to non-empty ShortList.",!ilist.equals(list)); 254 assertTrue("Unwrapped, non-empty ShortList is not equal to non-empty List.",!list.equals(ilist)); 255 256 assertEquals(new ListShortList(list),ilist); 257 assertEquals(ilist,new ListShortList(list)); 258 assertEquals(new ShortListList(ilist),list); 259 assertEquals(list,new ShortListList(ilist)); 260 261 } 262 263 public void testClearAndSize() { 264 ShortList list = makeEmptyShortList(); 265 assertEquals(0, list.size()); 266 for(int i = 0; i < 100; i++) { 267 list.add((short)i); 268 } 269 assertEquals(100, list.size()); 270 list.clear(); 271 assertEquals(0, list.size()); 272 } 273 274 public void testRemoveViaSubList() { 275 ShortList list = makeEmptyShortList(); 276 for(int i = 0; i < 100; i++) { 277 list.add((short)i); 278 } 279 ShortList sub = list.subList(25,75); 280 assertEquals(50,sub.size()); 281 for(int i = 0; i < 50; i++) { 282 assertEquals(100-i,list.size()); 283 assertEquals(50-i,sub.size()); 284 assertEquals(25+i,sub.removeElementAt(0)); 285 assertEquals(50-i-1,sub.size()); 286 assertEquals(100-i-1,list.size()); 287 } 288 assertEquals(0,sub.size()); 289 assertEquals(50,list.size()); 290 } 291 292 public void testAddGet() { 293 ShortList list = makeEmptyShortList(); 294 for (int i = 0; i < 255; i++) { 295 list.add((short)i); 296 } 297 for (int i = 0; i < 255; i++) { 298 assertEquals((short)i, list.get(i)); 299 } 300 } 301 302 public void testAddAndShift() { 303 ShortList list = makeEmptyShortList(); 304 list.add(0, (short)1); 305 assertEquals("Should have one entry", 1, list.size()); 306 list.add((short)3); 307 list.add((short)4); 308 list.add(1, (short)2); 309 for(int i = 0; i < 4; i++) { 310 assertEquals("Should get entry back", (short)(i + 1), list.get(i)); 311 } 312 list.add(0, (short)0); 313 for (int i = 0; i < 5; i++) { 314 assertEquals("Should get entry back", (short)i, list.get(i)); 315 } 316 } 317 318 public void testIsSerializable() throws Exception { 319 ShortList list = makeFullShortList(); 320 assertTrue(list instanceof Serializable ); 321 byte[] ser = writeExternalFormToBytes((Serializable )list); 322 ShortList deser = (ShortList)(readExternalFormFromBytes(ser)); 323 assertEquals(list,deser); 324 assertEquals(deser,list); 325 } 326 327 public void testShortListSerializeDeserializeThenCompare() throws Exception { 328 ShortList list = makeFullShortList(); 329 if(list instanceof Serializable ) { 330 byte[] ser = writeExternalFormToBytes((Serializable )list); 331 ShortList deser = (ShortList)(readExternalFormFromBytes(ser)); 332 assertEquals("obj != deserialize(serialize(obj))",list,deser); 333 } 334 } 335 336 public void testSubListsAreNotSerializable() throws Exception { 337 ShortList list = makeFullShortList().subList(2,3); 338 assertTrue( ! (list instanceof Serializable ) ); 339 } 340 341 public void testSubListOutOfBounds() throws Exception { 342 try { 343 makeEmptyShortList().subList(2,3); 344 fail("Expected IndexOutOfBoundsException"); 345 } catch(IndexOutOfBoundsException e) { 346 } 348 349 try { 350 makeFullShortList().subList(-1,3); 351 fail("Expected IndexOutOfBoundsException"); 352 } catch(IndexOutOfBoundsException e) { 353 } 355 356 357 try { 358 makeFullShortList().subList(5,2); 359 fail("Expected IllegalArgumentException"); 360 } catch(IllegalArgumentException e) { 361 } 363 364 try { 365 makeFullShortList().subList(2,makeFullShortList().size()+2); 366 fail("Expected IndexOutOfBoundsException"); 367 } catch(IndexOutOfBoundsException e) { 368 } 370 } 371 372 public void testListIteratorOutOfBounds() throws Exception { 373 try { 374 makeEmptyShortList().listIterator(2); 375 fail("Expected IndexOutOfBoundsException"); 376 } catch(IndexOutOfBoundsException e) { 377 } 379 380 try { 381 makeFullShortList().listIterator(-1); 382 fail("Expected IndexOutOfBoundsException"); 383 } catch(IndexOutOfBoundsException e) { 384 } 386 387 try { 388 makeFullShortList().listIterator(makeFullShortList().size()+2); 389 fail("Expected IndexOutOfBoundsException"); 390 } catch(IndexOutOfBoundsException e) { 391 } 393 } 394 395 public void testListIteratorSetWithoutNext() throws Exception { 396 ShortListIterator iter = makeFullShortList().listIterator(); 397 try { 398 iter.set((short)3); 399 fail("Expected IllegalStateException"); 400 } catch(IllegalStateException e) { 401 } 403 } 404 405 public void testListIteratorSetAfterRemove() throws Exception { 406 ShortListIterator iter = makeFullShortList().listIterator(); 407 iter.next(); 408 iter.remove(); 409 try { 410 iter.set((short)3); 411 fail("Expected IllegalStateException"); 412 } catch(IllegalStateException e) { 413 } 415 } 416 417 } 418 | Popular Tags |