001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.util;
019:
020: import junit.framework.*;
021:
022: /**
023: * Class to test IntList
024: *
025: * @author Marc Johnson
026: */
027:
028: public class TestIntList extends TestCase {
029:
030: /**
031: * Constructor TestIntList
032: *
033: * @param name
034: */
035:
036: public TestIntList(String name) {
037: super (name);
038: }
039:
040: /**
041: * test the various IntListconstructors
042: */
043:
044: public void testConstructors() {
045: IntList list = new IntList();
046:
047: assertTrue(list.isEmpty());
048: list.add(0);
049: list.add(1);
050: IntList list2 = new IntList(list);
051:
052: assertEquals(list, list2);
053: IntList list3 = new IntList(2);
054:
055: assertTrue(list3.isEmpty());
056: }
057:
058: /**
059: * test the add method
060: */
061:
062: public void testAdd() {
063: IntList list = new IntList();
064: int[] testArray = { 0, 1, 2, 3, 5 };
065:
066: for (int j = 0; j < testArray.length; j++) {
067: list.add(testArray[j]);
068: }
069: for (int j = 0; j < testArray.length; j++) {
070: assertEquals(testArray[j], list.get(j));
071: }
072: assertEquals(testArray.length, list.size());
073:
074: // add at the beginning
075: list.add(0, -1);
076: assertEquals(-1, list.get(0));
077: assertEquals(testArray.length + 1, list.size());
078: for (int j = 0; j < testArray.length; j++) {
079: assertEquals(testArray[j], list.get(j + 1));
080: }
081:
082: // add in the middle
083: list.add(5, 4);
084: assertEquals(4, list.get(5));
085: assertEquals(testArray.length + 2, list.size());
086: for (int j = 0; j < list.size(); j++) {
087: assertEquals(j - 1, list.get(j));
088: }
089:
090: // add at the end
091: list.add(list.size(), 6);
092: assertEquals(testArray.length + 3, list.size());
093: for (int j = 0; j < list.size(); j++) {
094: assertEquals(j - 1, list.get(j));
095: }
096:
097: // add past end
098: try {
099: list.add(list.size() + 1, 8);
100: fail("should have thrown exception");
101: } catch (IndexOutOfBoundsException e) {
102:
103: // as expected
104: }
105:
106: // test growth
107: list = new IntList(0);
108: for (int j = 0; j < 1000; j++) {
109: list.add(j);
110: }
111: assertEquals(1000, list.size());
112: for (int j = 0; j < 1000; j++) {
113: assertEquals(j, list.get(j));
114: }
115: list = new IntList(0);
116: for (int j = 0; j < 1000; j++) {
117: list.add(0, j);
118: }
119: assertEquals(1000, list.size());
120: for (int j = 0; j < 1000; j++) {
121: assertEquals(j, list.get(999 - j));
122: }
123: }
124:
125: /**
126: * test the addAll method
127: */
128:
129: public void testAddAll() {
130: IntList list = new IntList();
131:
132: for (int j = 0; j < 5; j++) {
133: list.add(j);
134: }
135: IntList list2 = new IntList(0);
136:
137: list2.addAll(list);
138: list2.addAll(list);
139: assertEquals(2 * list.size(), list2.size());
140: for (int j = 0; j < 5; j++) {
141: assertEquals(list2.get(j), j);
142: assertEquals(list2.get(j + list.size()), j);
143: }
144: IntList empty = new IntList();
145: int limit = list.size();
146:
147: for (int j = 0; j < limit; j++) {
148: assertTrue(list.addAll(j, empty));
149: assertEquals(limit, list.size());
150: }
151: try {
152: list.addAll(limit + 1, empty);
153: fail("should have thrown an exception");
154: } catch (IndexOutOfBoundsException e) {
155:
156: // as expected
157: }
158:
159: // try add at beginning
160: empty.addAll(0, list);
161: assertEquals(empty, list);
162:
163: // try in the middle
164: empty.addAll(1, list);
165: assertEquals(2 * list.size(), empty.size());
166: assertEquals(list.get(0), empty.get(0));
167: assertEquals(list.get(0), empty.get(1));
168: assertEquals(list.get(1), empty.get(2));
169: assertEquals(list.get(1), empty.get(6));
170: assertEquals(list.get(2), empty.get(3));
171: assertEquals(list.get(2), empty.get(7));
172: assertEquals(list.get(3), empty.get(4));
173: assertEquals(list.get(3), empty.get(8));
174: assertEquals(list.get(4), empty.get(5));
175: assertEquals(list.get(4), empty.get(9));
176:
177: // try at the end
178: empty.addAll(empty.size(), list);
179: assertEquals(3 * list.size(), empty.size());
180: assertEquals(list.get(0), empty.get(0));
181: assertEquals(list.get(0), empty.get(1));
182: assertEquals(list.get(0), empty.get(10));
183: assertEquals(list.get(1), empty.get(2));
184: assertEquals(list.get(1), empty.get(6));
185: assertEquals(list.get(1), empty.get(11));
186: assertEquals(list.get(2), empty.get(3));
187: assertEquals(list.get(2), empty.get(7));
188: assertEquals(list.get(2), empty.get(12));
189: assertEquals(list.get(3), empty.get(4));
190: assertEquals(list.get(3), empty.get(8));
191: assertEquals(list.get(3), empty.get(13));
192: assertEquals(list.get(4), empty.get(5));
193: assertEquals(list.get(4), empty.get(9));
194: assertEquals(list.get(4), empty.get(14));
195: }
196:
197: /**
198: * test the clear method
199: */
200:
201: public void testClear() {
202: IntList list = new IntList();
203:
204: for (int j = 0; j < 500; j++) {
205: list.add(j);
206: }
207: assertEquals(500, list.size());
208: list.clear();
209: assertEquals(0, list.size());
210: for (int j = 0; j < 500; j++) {
211: list.add(j + 1);
212: }
213: assertEquals(500, list.size());
214: for (int j = 0; j < 500; j++) {
215: assertEquals(j + 1, list.get(j));
216: }
217: }
218:
219: /**
220: * test the contains method
221: */
222:
223: public void testContains() {
224: IntList list = new IntList();
225:
226: for (int j = 0; j < 1000; j += 2) {
227: list.add(j);
228: }
229: for (int j = 0; j < 1000; j++) {
230: if (j % 2 == 0) {
231: assertTrue(list.contains(j));
232: } else {
233: assertTrue(!list.contains(j));
234: }
235: }
236: }
237:
238: /**
239: * test the containsAll method
240: */
241:
242: public void testContainsAll() {
243: IntList list = new IntList();
244:
245: assertTrue(list.containsAll(list));
246: for (int j = 0; j < 10; j++) {
247: list.add(j);
248: }
249: IntList list2 = new IntList(list);
250:
251: assertTrue(list2.containsAll(list));
252: assertTrue(list.containsAll(list2));
253: list2.add(10);
254: assertTrue(list2.containsAll(list));
255: assertTrue(!list.containsAll(list2));
256: list.add(11);
257: assertTrue(!list2.containsAll(list));
258: assertTrue(!list.containsAll(list2));
259: }
260:
261: /**
262: * test the equals method
263: */
264:
265: public void testEquals() {
266: IntList list = new IntList();
267:
268: assertEquals(list, list);
269: assertTrue(!list.equals(null));
270: IntList list2 = new IntList(200);
271:
272: assertEquals(list, list2);
273: assertEquals(list2, list);
274: assertEquals(list.hashCode(), list2.hashCode());
275: list.add(0);
276: list.add(1);
277: list2.add(1);
278: list2.add(0);
279: assertTrue(!list.equals(list2));
280: list2.removeValue(1);
281: list2.add(1);
282: assertEquals(list, list2);
283: assertEquals(list2, list);
284: list2.add(2);
285: assertTrue(!list.equals(list2));
286: assertTrue(!list2.equals(list));
287: }
288:
289: /**
290: * test the get method
291: */
292:
293: public void testGet() {
294: IntList list = new IntList();
295:
296: for (int j = 0; j < 1000; j++) {
297: list.add(j);
298: }
299: for (int j = 0; j < 1001; j++) {
300: try {
301: assertEquals(j, list.get(j));
302: if (j == 1000) {
303: fail("should have gotten exception");
304: }
305: } catch (IndexOutOfBoundsException e) {
306: if (j != 1000) {
307: fail("unexpected IndexOutOfBoundsException");
308: }
309: }
310: }
311: }
312:
313: /**
314: * test the indexOf method
315: */
316:
317: public void testIndexOf() {
318: IntList list = new IntList();
319:
320: for (int j = 0; j < 1000; j++) {
321: list.add(j / 2);
322: }
323: for (int j = 0; j < 1000; j++) {
324: if (j < 500) {
325: assertEquals(j * 2, list.indexOf(j));
326: } else {
327: assertEquals(-1, list.indexOf(j));
328: }
329: }
330: }
331:
332: /**
333: * test the isEmpty method
334: */
335:
336: public void testIsEmpty() {
337: IntList list1 = new IntList();
338: IntList list2 = new IntList(1000);
339: IntList list3 = new IntList(list1);
340:
341: assertTrue(list1.isEmpty());
342: assertTrue(list2.isEmpty());
343: assertTrue(list3.isEmpty());
344: list1.add(1);
345: list2.add(2);
346: list3 = new IntList(list2);
347: assertTrue(!list1.isEmpty());
348: assertTrue(!list2.isEmpty());
349: assertTrue(!list3.isEmpty());
350: list1.clear();
351: list2.remove(0);
352: list3.removeValue(2);
353: assertTrue(list1.isEmpty());
354: assertTrue(list2.isEmpty());
355: assertTrue(list3.isEmpty());
356: }
357:
358: /**
359: * test the lastIndexOf method
360: */
361:
362: public void testLastIndexOf() {
363: IntList list = new IntList();
364:
365: for (int j = 0; j < 1000; j++) {
366: list.add(j / 2);
367: }
368: for (int j = 0; j < 1000; j++) {
369: if (j < 500) {
370: assertEquals(1 + j * 2, list.lastIndexOf(j));
371: } else {
372: assertEquals(-1, list.indexOf(j));
373: }
374: }
375: }
376:
377: /**
378: * test the remove method
379: */
380:
381: public void testRemove() {
382: IntList list = new IntList();
383:
384: for (int j = 0; j < 1000; j++) {
385: list.add(j);
386: }
387: for (int j = 0; j < 1000; j++) {
388: assertEquals(j, list.remove(0));
389: assertEquals(999 - j, list.size());
390: }
391: for (int j = 0; j < 1000; j++) {
392: list.add(j);
393: }
394: for (int j = 0; j < 1000; j++) {
395: assertEquals(999 - j, list.remove(999 - j));
396: assertEquals(999 - j, list.size());
397: }
398: try {
399: list.remove(0);
400: fail("should have caught IndexOutOfBoundsException");
401: } catch (IndexOutOfBoundsException e) {
402:
403: // as expected
404: }
405: }
406:
407: /**
408: * test the removeValue method
409: */
410:
411: public void testRemoveValue() {
412: IntList list = new IntList();
413:
414: for (int j = 0; j < 1000; j++) {
415: list.add(j / 2);
416: }
417: for (int j = 0; j < 1000; j++) {
418: if (j < 500) {
419: assertTrue(list.removeValue(j));
420: assertTrue(list.removeValue(j));
421: }
422: assertTrue(!list.removeValue(j));
423: }
424: }
425:
426: /**
427: * test the removeAll method
428: */
429:
430: public void testRemoveAll() {
431: IntList list = new IntList();
432:
433: for (int j = 0; j < 1000; j++) {
434: list.add(j);
435: }
436: IntList listCopy = new IntList(list);
437: IntList listOdd = new IntList();
438: IntList listEven = new IntList();
439:
440: for (int j = 0; j < 1000; j++) {
441: if (j % 2 == 0) {
442: listEven.add(j);
443: } else {
444: listOdd.add(j);
445: }
446: }
447: list.removeAll(listEven);
448: assertEquals(list, listOdd);
449: list.removeAll(listOdd);
450: assertTrue(list.isEmpty());
451: listCopy.removeAll(listOdd);
452: assertEquals(listCopy, listEven);
453: listCopy.removeAll(listEven);
454: assertTrue(listCopy.isEmpty());
455: }
456:
457: /**
458: * test the retainAll method
459: */
460:
461: public void testRetainAll() {
462: IntList list = new IntList();
463:
464: for (int j = 0; j < 1000; j++) {
465: list.add(j);
466: }
467: IntList listCopy = new IntList(list);
468: IntList listOdd = new IntList();
469: IntList listEven = new IntList();
470:
471: for (int j = 0; j < 1000; j++) {
472: if (j % 2 == 0) {
473: listEven.add(j);
474: } else {
475: listOdd.add(j);
476: }
477: }
478: list.retainAll(listOdd);
479: assertEquals(list, listOdd);
480: list.retainAll(listEven);
481: assertTrue(list.isEmpty());
482: listCopy.retainAll(listEven);
483: assertEquals(listCopy, listEven);
484: listCopy.retainAll(listOdd);
485: assertTrue(listCopy.isEmpty());
486: }
487:
488: /**
489: * test the set method
490: */
491:
492: public void testSet() {
493: IntList list = new IntList();
494:
495: for (int j = 0; j < 1000; j++) {
496: list.add(j);
497: }
498: for (int j = 0; j < 1001; j++) {
499: try {
500: list.set(j, j + 1);
501: if (j == 1000) {
502: fail("Should have gotten exception");
503: }
504: assertEquals(j + 1, list.get(j));
505: } catch (IndexOutOfBoundsException e) {
506: if (j != 1000) {
507: fail("premature exception");
508: }
509: }
510: }
511: }
512:
513: /**
514: * test the size method
515: */
516:
517: public void testSize() {
518: IntList list = new IntList();
519:
520: for (int j = 0; j < 1000; j++) {
521: assertEquals(j, list.size());
522: list.add(j);
523: assertEquals(j + 1, list.size());
524: }
525: for (int j = 0; j < 1000; j++) {
526: assertEquals(1000 - j, list.size());
527: list.removeValue(j);
528: assertEquals(999 - j, list.size());
529: }
530: }
531:
532: /**
533: * test the toArray method
534: */
535:
536: public void testToArray() {
537: IntList list = new IntList();
538:
539: for (int j = 0; j < 1000; j++) {
540: list.add(j);
541: }
542: int[] a1 = list.toArray();
543:
544: assertEquals(a1.length, list.size());
545: for (int j = 0; j < 1000; j++) {
546: assertEquals(a1[j], list.get(j));
547: }
548: int[] a2 = new int[list.size()];
549: int[] a3 = list.toArray(a2);
550:
551: assertSame(a2, a3);
552: for (int j = 0; j < 1000; j++) {
553: assertEquals(a2[j], list.get(j));
554: }
555: int[] aShort = new int[list.size() - 1];
556: int[] aLong = new int[list.size() + 1];
557: int[] a4 = list.toArray(aShort);
558: int[] a5 = list.toArray(aLong);
559:
560: assertTrue(a4 != aShort);
561: assertTrue(a5 != aLong);
562: assertEquals(a4.length, list.size());
563: for (int j = 0; j < 1000; j++) {
564: assertEquals(a3[j], list.get(j));
565: }
566: assertEquals(a5.length, list.size());
567: for (int j = 0; j < 1000; j++) {
568: assertEquals(a5[j], list.get(j));
569: }
570: }
571:
572: /**
573: * main method to run the unit tests
574: *
575: * @param unused_args
576: */
577:
578: public static void main(String[] unused_args) {
579: System.out.println("Testing util.IntList functionality");
580: junit.textui.TestRunner.run(TestIntList.class);
581: }
582: }
|