001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.db4ounit.jre5.collections;
022:
023: import java.util.*;
024:
025: import com.db4o.collections.*;
026:
027: import db4ounit.*;
028:
029: public class ArrayList4Asserter {
030: public static int CAPACITY = 100;
031: private static final Integer ITEM = new Integer((CAPACITY / 2));
032:
033: public static void createList(final List<Integer> list)
034: throws Exception {
035: for (int i = 0; i < CAPACITY; i++) {
036: list.add(new Integer(i));
037: }
038: }
039:
040: public static void assertAdd(final List<Integer> list)
041: throws Exception {
042: for (int i = 0; i < CAPACITY; ++i) {
043: list.add(new Integer(CAPACITY + i));
044: }
045: checkAdd(list);
046: }
047:
048: public static void checkAdd(final List<Integer> list) {
049: for (int i = 0; i < CAPACITY * 2; ++i) {
050: Assert.areEqual(new Integer(i), list.get(i));
051: }
052: }
053:
054: public static void assertAdd_LObject(final List<Integer> list)
055: throws Exception {
056: Assert.expect(IndexOutOfBoundsException.class, new CodeBlock() {
057: public void run() throws Throwable {
058: list.add(-1, new Integer(0));
059: }
060: });
061:
062: Assert.expect(IndexOutOfBoundsException.class, new CodeBlock() {
063: public void run() throws Throwable {
064: list.add(CAPACITY + 1, new Integer(0));
065: }
066: });
067:
068: Integer i1 = new Integer(0);
069: list.add(0, i1);
070: // elements: 0, 0,1 - 100
071: // index: 0, 1,2 - 101
072: Assert.areSame(i1, list.get(0));
073:
074: for (int i = 1; i < CAPACITY + 1; ++i) {
075: Assert.areEqual(new Integer(i - 1), list.get(i));
076: }
077:
078: list.add(CAPACITY / 2, ITEM);
079: checkAdd_LObject(list);
080: }
081:
082: public static void checkAdd_LObject(final List<Integer> list) {
083: // elements: 0, 0,1 - C/2, C/2, C+1 - C
084: // index: 0, 1,2 - C/2-1, C/2, C/2+1 - C
085: for (int i = 1; i < (CAPACITY / 2); ++i) {
086: Assert.areEqual(new Integer(i - 1), list.get(i));
087: }
088:
089: Assert.areEqual(ITEM, list.get(CAPACITY / 2));
090: Assert.areEqual(new Integer((CAPACITY / 2) / 2), list
091: .get((CAPACITY / 2) / 2 + 1));
092:
093: for (int i = (CAPACITY / 2) + 1; i < CAPACITY + 2; ++i) {
094: Assert.areEqual(new Integer(i - 2), list.get(i));
095: }
096: }
097:
098: public static void assertAddAll_LCollection(final List<Integer> list)
099: throws Exception {
100: final Vector<Integer> v = new Vector<Integer>();
101: Assert.expect(IndexOutOfBoundsException.class, new CodeBlock() {
102: public void run() throws Throwable {
103: list.addAll(-1, v);
104: }
105: });
106:
107: Assert.expect(IndexOutOfBoundsException.class, new CodeBlock() {
108: public void run() throws Throwable {
109: list.addAll(CAPACITY + 1, v);
110: }
111: });
112:
113: for (int i = 0; i < CAPACITY; ++i) {
114: v.add(new Integer(CAPACITY + i));
115: }
116:
117: list.addAll(v);
118:
119: checkAddAll_LCollection(list);
120: }
121:
122: public static void checkAddAll_LCollection(final List<Integer> list) {
123: for (int i = 0; i < CAPACITY * 2; ++i) {
124: Assert.areEqual(new Integer(i), list.get(i));
125: }
126: }
127:
128: public static void assertAddAll_ILCollection(
129: final List<Integer> list) throws Exception {
130: final Vector<Integer> v = new Vector<Integer>();
131: final int INDEX = 42;
132:
133: for (int i = 0; i < CAPACITY; ++i) {
134: v.add(new Integer(CAPACITY + i));
135: }
136:
137: list.addAll(INDEX, v);
138: // elements: 0 - 41, 100 - 199, 42 - 100
139: // index: 0 - 41, 42 - 141, 142 - 200
140: for (int i = 0; i < INDEX; ++i) {
141: Assert.areEqual(new Integer(i), list.get(i));
142: }
143:
144: for (int i = INDEX, j = 0; j < CAPACITY; ++i, ++j) {
145: Assert.areEqual(new Integer(CAPACITY + j), list.get(i));
146: }
147:
148: for (int i = INDEX + CAPACITY; i < CAPACITY * 2; ++i) {
149: Assert.areEqual(new Integer(i - CAPACITY), list.get(i));
150: }
151:
152: Assert.expect(IndexOutOfBoundsException.class, new CodeBlock() {
153: public void run() throws Throwable {
154: list.addAll(-1, v);
155: }
156: });
157: }
158:
159: public static void assertClear(final List<Integer> list)
160: throws Exception {
161: list.clear();
162: checkClear(list);
163: }
164:
165: public static void checkClear(final List<Integer> list) {
166: Assert.areEqual(0, list.size());
167: }
168:
169: public static void assertContains(final List<Integer> list)
170: throws Exception {
171: Assert.isTrue(list.contains(new Integer(0)));
172: Assert.isTrue(list.contains(new Integer(CAPACITY / 2)));
173: Assert.isTrue(list.contains(new Integer(CAPACITY / 3)));
174: Assert.isTrue(list.contains(new Integer(CAPACITY / 4)));
175:
176: Assert.isFalse(list.contains(new Integer(-1)));
177: Assert.isFalse(list.contains(new Integer(CAPACITY)));
178:
179: // returns false because current data doesn't contain null.
180: // Quotes from j.u.List spec: More formally, returns true if and only if
181: // this list contains at least one element e such that (o==null ?
182: // e==null : o.equals(e)).
183: Assert.isFalse(list.contains(null));
184: }
185:
186: public static void assertContainsAll(final List<Integer> list)
187: throws Exception {
188: Vector<Integer> v = new Vector<Integer>();
189:
190: v.add(new Integer(0));
191: Assert.isTrue(list.containsAll(v));
192:
193: v.add(new Integer(0));
194: Assert.isTrue(list.containsAll(v));
195:
196: v.add(new Integer(CAPACITY / 2));
197: Assert.isTrue(list.containsAll(v));
198:
199: v.add(new Integer(CAPACITY / 3));
200: Assert.isTrue(list.containsAll(v));
201:
202: v.add(new Integer(CAPACITY / 4));
203: Assert.isTrue(list.containsAll(v));
204:
205: v.add(new Integer(CAPACITY));
206: Assert.isFalse(list.containsAll(v));
207: }
208:
209: public static void assertGet(final List<Integer> list)
210: throws Exception {
211: for (int i = 0; i < CAPACITY; ++i) {
212: Assert.areEqual(new Integer(i), list.get(i));
213: }
214:
215: Assert.expect(IndexOutOfBoundsException.class, new CodeBlock() {
216: public void run() throws Throwable {
217: list.get(-1);
218: }
219: });
220:
221: Assert.expect(IndexOutOfBoundsException.class, new CodeBlock() {
222: public void run() throws Throwable {
223: list.get(CAPACITY);
224: }
225: });
226: }
227:
228: public static void assertIndexOf(final List<Integer> list)
229: throws Exception {
230: Assert.areEqual(0, list.indexOf(new Integer(0)));
231: Assert.areEqual(CAPACITY / 2, list.indexOf(new Integer(
232: CAPACITY / 2)));
233: Assert.areEqual(CAPACITY / 3, list.indexOf(new Integer(
234: CAPACITY / 3)));
235: Assert.areEqual(CAPACITY / 4, list.indexOf(new Integer(
236: CAPACITY / 4)));
237:
238: Assert.areEqual(-1, list.indexOf(new Integer(-1)));
239: Assert.areEqual(-1, list.indexOf(new Integer(CAPACITY)));
240:
241: // returns false because current data doesn't contain null.
242: // Quotes from j.u.List spec: More formally, returns the lowest index i
243: // such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there
244: // is no such index.
245: Assert.areEqual(-1, list.indexOf(null));
246: }
247:
248: public static void assertIsEmpty(final List<Integer> list)
249: throws Exception {
250: Assert.isTrue(new ArrayList4<Integer>().isEmpty());
251: Assert.isFalse(list.isEmpty());
252: list.clear();
253: Assert.isTrue(list.isEmpty());
254: }
255:
256: public static void assertIterator(final List<Integer> list)
257: throws Exception {
258: Iterator<Integer> iter = list.iterator();
259: int count = 0;
260: while (iter.hasNext()) {
261: Integer i = (Integer) iter.next();
262: Assert.areEqual(count, i.intValue());
263: ++count;
264: }
265: Assert.areEqual(CAPACITY, count);
266:
267: list.clear();
268: iter = list.iterator();
269: Assert.isFalse(iter.hasNext());
270: }
271:
272: public static void assertLastIndexOf(final List<Integer> list)
273: throws Exception {
274: Assert.areEqual(0, list.indexOf(new Integer(0)));
275: Assert.areEqual(CAPACITY / 2, list.lastIndexOf(new Integer(
276: CAPACITY / 2)));
277: Assert.areEqual(CAPACITY / 3, list.lastIndexOf(new Integer(
278: CAPACITY / 3)));
279: Assert.areEqual(CAPACITY / 4, list.lastIndexOf(new Integer(
280: CAPACITY / 4)));
281:
282: Assert.areEqual(-1, list.lastIndexOf(new Integer(-1)));
283: Assert.areEqual(-1, list.lastIndexOf(new Integer(CAPACITY)));
284:
285: // returns false because current data doesn't contain null.
286: // Quotes from j.u.List spec: More formally, returns the lowest index i
287: // such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there
288: // is no such index.
289: Assert.areEqual(-1, list.lastIndexOf(null));
290:
291: list.add(new Integer(0));
292: list.add(new Integer(CAPACITY / 2));
293: list.add(new Integer(CAPACITY / 3));
294: list.add(new Integer(CAPACITY / 4));
295:
296: Assert.areEqual(CAPACITY, list.lastIndexOf(new Integer(0)));
297: Assert.areEqual(CAPACITY + 1, list.lastIndexOf(new Integer(
298: CAPACITY / 2)));
299: Assert.areEqual(CAPACITY + 2, list.lastIndexOf(new Integer(
300: CAPACITY / 3)));
301: Assert.areEqual(CAPACITY + 3, list.lastIndexOf(new Integer(
302: CAPACITY / 4)));
303:
304: Assert.areEqual(-1, list.lastIndexOf(new Integer(-1)));
305: Assert.areEqual(-1, list.lastIndexOf(new Integer(CAPACITY)));
306:
307: // returns false because current data doesn't contain null.
308: // Quotes from j.u.List spec: More formally, returns the lowest index i
309: // such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there
310: // is no such index.
311: Assert.areEqual(-1, list.lastIndexOf(null));
312: }
313:
314: public static void assertRemove_LObject(final List<Integer> list)
315: throws Exception {
316: list.remove(new Integer(0));
317: Assert.areEqual(new Integer(1), list.get(0));
318:
319: Assert.areEqual(CAPACITY - 1, list.size());
320:
321: int val = CAPACITY / 2;
322: list.remove(new Integer(val));
323: Assert.areEqual(new Integer(val + 1), list.get(val - 1));
324: Assert.areEqual(new Integer(val + 2), list.get(val));
325: Assert.areEqual(new Integer(val + 3), list.get(val + 1));
326: Assert.areEqual(CAPACITY - 2, list.size());
327:
328: for (int i = 0; i < CAPACITY - 2; ++i) {
329: list.remove(list.get(0));
330: Assert.areEqual(CAPACITY - 3 - i, list.size());
331: }
332: Assert.isTrue(list.isEmpty());
333: }
334:
335: public static void assertRemove_I(final List<Integer> list)
336: throws Exception {
337: list.remove(0);
338: Assert.areEqual(new Integer(1), list.get(0));
339: Assert.isFalse(list.contains(new Integer(0)));
340: Assert.areEqual(CAPACITY - 1, list.size());
341:
342: list.remove(42);
343: Assert.areEqual(new Integer(44), list.get(42));
344: Assert.areEqual(new Integer(42), list.get(41));
345: Assert.isFalse(list.contains(new Integer(43)));
346: Assert.areEqual(CAPACITY - 2, list.size());
347:
348: for (int i = 0; i < CAPACITY - 2; ++i) {
349: list.remove(0);
350: Assert.areEqual(CAPACITY - 3 - i, list.size());
351: }
352: checkRemove_LObject(list);
353: }
354:
355: public static void checkRemove_LObject(final List<Integer> list) {
356: Assert.isTrue(list.isEmpty());
357: }
358:
359: public static void assertRemoveAll(final List<Integer> list)
360: throws Exception {
361: Vector<Integer> v = new Vector<Integer>();
362:
363: list.removeAll(v);
364: Assert.areEqual(CAPACITY, list.size());
365:
366: int val = CAPACITY / 2;
367: v.add(new Integer(0));
368: v.add(new Integer(val));
369: list.removeAll(v);
370: Assert.isFalse(list.contains(new Integer(0)));
371: Assert.isFalse(list.contains(new Integer(val)));
372: Assert.areEqual(CAPACITY - 2, list.size());
373:
374: v.add(new Integer(1));
375: v.add(new Integer(2));
376: list.removeAll(v);
377: Assert.isFalse(list.contains(new Integer(1)));
378: Assert.isFalse(list.contains(new Integer(2)));
379: Assert.areEqual(CAPACITY - 4, list.size());
380:
381: for (int i = 0; i < CAPACITY; ++i) {
382: v.add(new Integer(i));
383: }
384: list.removeAll(v);
385: checkRemoveAll(list);
386: }
387:
388: public static void checkRemoveAll(final List<Integer> list) {
389: Assert.isTrue(list.isEmpty());
390: }
391:
392: public static void assertRetainAll(final List<Integer> list)
393: throws Exception {
394: Vector<Integer> v = new Vector<Integer>();
395: v.add(new Integer(0));
396: v.add(new Integer(42));
397:
398: boolean ret = list.retainAll(list);
399: Assert.isFalse(ret);
400: Assert.areEqual(100, list.size());
401: for (int i = 0; i < CAPACITY; ++i) {
402: Assert.isTrue(list.contains(new Integer(i)));
403: }
404:
405: ret = list.retainAll(v);
406: Assert.isTrue(ret);
407: Assert.areEqual(2, list.size());
408: list.contains(new Integer(0));
409: list.contains(new Integer(42));
410:
411: ret = list.retainAll(v);
412: Assert.isFalse(ret);
413: list.contains(new Integer(0));
414: list.contains(new Integer(42));
415: }
416:
417: public static void assertSet(final List<Integer> list)
418: throws Exception {
419: Assert.expect(IndexOutOfBoundsException.class, new CodeBlock() {
420: public void run() throws Throwable {
421: list.set(-1, new Integer(0));
422: }
423: });
424:
425: Assert.expect(IndexOutOfBoundsException.class, new CodeBlock() {
426: public void run() throws Throwable {
427: list.set(CAPACITY, new Integer(0));
428: }
429: });
430:
431: Integer element = new Integer(1);
432:
433: Integer previousElement = list.get(0);
434: Assert.areSame(previousElement, list.set(0, element));
435: Assert.areSame(element, list.get(0));
436:
437: int val = CAPACITY / 2;
438: previousElement = list.get(val);
439: Assert.areSame(previousElement, list.set(val, element));
440: Assert.areSame(element, list.get(val));
441:
442: for (int i = 0; i < CAPACITY; ++i) {
443: element = new Integer(i);
444: previousElement = list.get(i);
445: Assert.areSame(previousElement, list.set(i, element));
446: Assert.areSame(element, list.get(i));
447: }
448: }
449:
450: public static void assertSize(final List<Integer> list)
451: throws Exception {
452: Assert.areEqual(CAPACITY, list.size());
453: for (int i = 0; i < CAPACITY; ++i) {
454: list.remove(0);
455: Assert.areEqual(CAPACITY - 1 - i, list.size());
456: }
457: for (int i = 0; i < CAPACITY; ++i) {
458: list.add(new Integer(i));
459: Assert.areEqual(i + 1, list.size());
460: }
461: }
462:
463: public static void assertToArray(final List<Integer> list)
464: throws Exception {
465: Object[] array = list.toArray();
466: Assert.areEqual(CAPACITY, array.length);
467: for (int i = 0; i < CAPACITY; ++i) {
468: Integer element = (Integer) array[i];
469: Assert.areEqual(new Integer(i), element);
470: }
471:
472: list.clear();
473: array = list.toArray();
474: Assert.areEqual(0, array.length);
475: }
476:
477: public static void assertToArray_LObject(final List<Integer> list)
478: throws Exception {
479: Object[] array1;
480: Object[] array2 = new Integer[CAPACITY];
481: array1 = list.toArray(array2);
482: Assert.areSame(array1, array2);
483: Assert.areEqual(CAPACITY, array2.length);
484: for (int i = 0; i < CAPACITY; ++i) {
485: Integer element = (Integer) array2[i];
486: Assert.areEqual(new Integer(i), element);
487: }
488:
489: list.clear();
490:
491: array1 = new Integer[0];
492: array2 = new Integer[CAPACITY];
493: array1 = list.toArray(array2);
494: Assert.areSame(array1, array2);
495: Assert.areEqual(CAPACITY, array1.length);
496:
497: array2 = new Integer[0];
498: array1 = list.toArray(array2);
499: Assert.areEqual(0, array1.length);
500: }
501:
502: public static void assertToString(final List<Integer> list)
503: throws Exception {
504: StringBuffer expected = new StringBuffer("[");
505: for (int i = 0; i < CAPACITY - 1; ++i) {
506: expected.append(i + ", ");
507: }
508: expected.append(CAPACITY - 1 + "]");
509: Assert.areEqual(expected.toString(), list.toString());
510:
511: ArrayList4<Object> oList = new ArrayList4<Object>();
512: Assert.areEqual("[]", oList.toString());
513:
514: oList.add(new Integer(1));
515: oList.add(new Integer(2));
516: Assert.areEqual("[1, 2]", oList.toString());
517:
518: oList.add(oList);
519: oList.add(3);
520: Assert.areEqual("[1, 2, (this Collection), 3]", oList
521: .toString());
522: }
523:
524: public static void assertTrimToSize_EnsureCapacity(
525: final ArrayList4<Integer> list) throws Exception {
526: list.ensureCapacity(CAPACITY * 2);
527: checkTrimToSize_EnsureCapacity(list);
528: list.trimToSize();
529: checkTrimToSize_EnsureCapacity(list);
530: }
531:
532: public static void checkTrimToSize_EnsureCapacity(
533: final ArrayList4<Integer> list) {
534: Assert.areEqual(CAPACITY, list.size());
535: for (int i = 0; i < CAPACITY; ++i) {
536: Integer element = (Integer) list.get(i);
537: Assert.areEqual(new Integer(i), element);
538: }
539: }
540:
541: public static void assertTrimToSize_Remove(final List<Integer> list)
542: throws Exception {
543: for (int i = CAPACITY - 1; i >= 10; i--) {
544: list.remove(i);
545: }
546: Assert.areEqual(10, list.size());
547: for (int i = 0; i < 10; ++i) {
548: Integer element = (Integer) list.get(i);
549: Assert.areEqual(new Integer(i), element);
550: }
551: }
552:
553: public static void assertTrimToSize_Iterator(
554: final ArrayList4<Integer> list) throws Exception {
555: final Iterator<Integer> iterator = list.iterator();
556: list.trimToSize();
557: Assert.expect(ConcurrentModificationException.class,
558: new CodeBlock() {
559: public void run() throws Throwable {
560: iterator.next();
561: }
562: });
563: }
564:
565: public static void assertEnsureCapacity_Iterator(
566: final ArrayList4<Integer> list) throws Exception {
567: final Iterator<Integer> iterator = list.iterator();
568: list.ensureCapacity(CAPACITY * 2);
569: Assert.expect(ConcurrentModificationException.class,
570: new CodeBlock() {
571: public void run() throws Throwable {
572: iterator.next();
573: }
574: });
575: }
576:
577: public static void assertClear_Iterator(
578: final ArrayList4<Integer> list) throws Exception {
579: final Iterator<Integer> iterator = list.iterator();
580: list.clear();
581: Assert.expect(ConcurrentModificationException.class,
582: new CodeBlock() {
583: public void run() throws Throwable {
584: iterator.next();
585: }
586: });
587: }
588:
589: @SuppressWarnings("unchecked")
590: public static void assertClone(final ArrayList4<Integer> list)
591: throws Exception {
592: list.add(null);
593: ArrayList4<Integer> cloned = (ArrayList4<Integer>) list.clone();
594: for (int i = 0; i < CAPACITY; i++) {
595: Assert.areSame(list.get(i), cloned.get(i));
596: }
597: }
598:
599: public static void assertEquals(final ArrayList4<Integer> list)
600: throws Exception {
601: Assert.isFalse(list.equals(null));
602: Assert.isFalse(list.equals(new Integer(1)));
603: Assert.isTrue(list.equals(list));
604: Vector<Integer> v = new Vector<Integer>(list);
605: Assert.isTrue(list.equals(v));
606: v = new Vector<Integer>();
607: Assert.isFalse(list.equals(v));
608: Assert.isTrue(list.equals(list.clone()));
609: }
610:
611: public static void assertIteratorNext_NoSuchElementException(
612: final List<Integer> list) throws Exception {
613: final Iterator<Integer> iterator = list.iterator();
614: Assert.expect(NoSuchElementException.class, new CodeBlock() {
615: public void run() throws Throwable {
616: while (true) {
617: iterator.next();
618: }
619: }
620: });
621: }
622:
623: public static void assertIteratorNext_ConcurrentModificationException(
624: final List<Integer> list) throws Exception {
625: final Iterator<Integer> iterator = list.iterator();
626: Assert.expect(NoSuchElementException.class, new CodeBlock() {
627: public void run() throws Throwable {
628: while (true) {
629: iterator.next();
630: }
631: }
632: });
633: list.clear();
634: Assert.expect(ConcurrentModificationException.class,
635: new CodeBlock() {
636: public void run() throws Throwable {
637: iterator.next();
638: }
639: });
640:
641: }
642:
643: public static void assertIteratorNext(final List<Integer> list)
644: throws Exception {
645: final Iterator<Integer> iterator = list.iterator();
646: int i = 0;
647: while (iterator.hasNext()) {
648: Integer e1 = iterator.next();
649: Assert.areSame(e1, list.get(i));
650: i++;
651: }
652: }
653:
654: public static void assertIteratorRemove(final List<Integer> list)
655: throws Exception {
656: final Iterator<Integer> iterator = list.iterator();
657: int i = CAPACITY - 1;
658: while (iterator.hasNext()) {
659: Integer e1 = iterator.next();
660: Assert.areSame(e1, list.get(0));
661: Assert.areEqual(new Integer(CAPACITY - 1), list.get(i));
662: iterator.remove();
663: Assert.areEqual(i, list.size());
664: i--;
665: }
666: }
667:
668: public static void assertRemove_IllegalStateException(
669: final List<Integer> list) throws Exception {
670: final Iterator<Integer> iterator = list.iterator();
671: Assert.expect(IllegalStateException.class, new CodeBlock() {
672: public void run() throws Throwable {
673: iterator.remove();
674: }
675: });
676:
677: iterator.next();
678:
679: Assert.expect(IllegalStateException.class, new CodeBlock() {
680: public void run() throws Throwable {
681: iterator.remove();
682: iterator.remove();
683: }
684: });
685: }
686:
687: public static void assertIteratorRemove_ConcurrentModificationException(
688: final List<Integer> list) throws Exception {
689: final Iterator<Integer> iterator = list.iterator();
690: iterator.next();
691: list.clear();
692: Assert.expect(ConcurrentModificationException.class,
693: new CodeBlock() {
694: public void run() throws Throwable {
695: iterator.remove();
696: }
697: });
698: }
699:
700: public static void assertSubList(List<Integer> list)
701: throws Exception {
702: int val = CAPACITY / 2;
703: List<Integer> subList1 = list.subList(val, CAPACITY);
704: for (int index = 0; index < subList1.size(); index++) {
705: Assert.areSame(list.get(val + index), subList1.get(index));
706: }
707: list.set(val, new Integer(1001));
708: Assert.areEqual(new Integer(1001), subList1.get(0));
709:
710: subList1.set(1, new Integer(1001));
711: Assert.areEqual(new Integer(1001), list.get(val + 1));
712: }
713:
714: public static void assertSubList_ConcurrentModification(
715: List<Integer> list) throws Exception {
716: int val = CAPACITY / 2;
717: final List<Integer> subList1 = list.subList(val, CAPACITY);
718: for (int index = 0; index < subList1.size(); index++) {
719: Assert.areSame(list.get(val + index), subList1.get(index));
720: }
721: list.remove(0);
722: Assert.expect(ConcurrentModificationException.class,
723: new CodeBlock() {
724: public void run() throws Throwable {
725: subList1.get(0);
726: }
727: });
728: }
729:
730: public static void assertSubList_Clear(List<Integer> list)
731: throws Exception {
732: list.subList(CAPACITY - 10, CAPACITY).clear();
733: int expectedSize = CAPACITY - 10;
734: Assert.areEqual(expectedSize, list.size());
735: for (int i = 0; i < expectedSize; ++i) {
736: Assert.areEqual(new Integer(i), list.get(i));
737: }
738: }
739:
740: public static void assertSubList_Clear2(List<Integer> list)
741: throws Exception {
742: list.subList(0, 10).clear();
743: int expectedSize = CAPACITY - 10;
744: Assert.areEqual(expectedSize, list.size());
745: for (int i = 0; i < expectedSize; ++i) {
746: Assert.areEqual(new Integer(i + 10), list.get(i));
747: }
748: }
749:
750: }
|