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 ShortList
024: *
025: * @author Marc Johnson
026: */
027:
028: public class TestShortList extends TestCase {
029:
030: /**
031: * Constructor TestShortList
032: *
033: * @param name
034: */
035:
036: public TestShortList(String name) {
037: super (name);
038: }
039:
040: /**
041: * test the various ShortListconstructors
042: */
043:
044: public void testConstructors() {
045: ShortList list = new ShortList();
046:
047: assertTrue(list.isEmpty());
048: list.add((short) 0);
049: list.add((short) 1);
050: ShortList list2 = new ShortList(list);
051:
052: assertEquals(list, list2);
053: ShortList list3 = new ShortList(2);
054:
055: assertTrue(list3.isEmpty());
056: }
057:
058: /**
059: * test the add method
060: */
061:
062: public void testAdd() {
063: ShortList list = new ShortList();
064: short[] 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, (short) -1);
076: assertEquals((short) -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, (short) 4);
084: assertEquals((short) 4, list.get(5));
085: assertEquals(testArray.length + 2, list.size());
086: for (int j = 0; j < list.size(); j++) {
087: assertEquals((short) (j - 1), list.get(j));
088: }
089:
090: // add at the end
091: list.add(list.size(), (short) 6);
092: assertEquals(testArray.length + 3, list.size());
093: for (int j = 0; j < list.size(); j++) {
094: assertEquals((short) (j - 1), list.get(j));
095: }
096:
097: // add past end
098: try {
099: list.add(list.size() + 1, (short) 8);
100: fail("should have thrown exception");
101: } catch (IndexOutOfBoundsException e) {
102:
103: // as expected
104: }
105:
106: // test growth
107: list = new ShortList(0);
108: for (short j = 0; j < 1000; j++) {
109: list.add(j);
110: }
111: assertEquals(1000, list.size());
112: for (short j = 0; j < 1000; j++) {
113: assertEquals(j, list.get(j));
114: }
115: list = new ShortList(0);
116: for (short j = 0; j < 1000; j++) {
117: list.add(0, j);
118: }
119: assertEquals(1000, list.size());
120: for (short 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: ShortList list = new ShortList();
131:
132: for (short j = 0; j < 5; j++) {
133: list.add(j);
134: }
135: ShortList list2 = new ShortList(0);
136:
137: list2.addAll(list);
138: list2.addAll(list);
139: assertEquals(2 * list.size(), list2.size());
140: for (short j = 0; j < 5; j++) {
141: assertEquals(list2.get(j), j);
142: assertEquals(list2.get(j + list.size()), j);
143: }
144: ShortList empty = new ShortList();
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: ShortList list = new ShortList();
203:
204: for (short 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 (short j = 0; j < 500; j++) {
211: list.add((short) (j + 1));
212: }
213: assertEquals(500, list.size());
214: for (short 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: ShortList list = new ShortList();
225:
226: for (short j = 0; j < 1000; j += 2) {
227: list.add(j);
228: }
229: for (short 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: ShortList list = new ShortList();
244:
245: assertTrue(list.containsAll(list));
246: for (short j = 0; j < 10; j++) {
247: list.add(j);
248: }
249: ShortList list2 = new ShortList(list);
250:
251: assertTrue(list2.containsAll(list));
252: assertTrue(list.containsAll(list2));
253: list2.add((short) 10);
254: assertTrue(list2.containsAll(list));
255: assertTrue(!list.containsAll(list2));
256: list.add((short) 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: ShortList list = new ShortList();
267:
268: assertEquals(list, list);
269: assertTrue(!list.equals(null));
270: ShortList list2 = new ShortList(200);
271:
272: assertEquals(list, list2);
273: assertEquals(list2, list);
274: assertEquals(list.hashCode(), list2.hashCode());
275: list.add((short) 0);
276: list.add((short) 1);
277: list2.add((short) 1);
278: list2.add((short) 0);
279: assertTrue(!list.equals(list2));
280: list2.removeValue((short) 1);
281: list2.add((short) 1);
282: assertEquals(list, list2);
283: assertEquals(list2, list);
284: list2.add((short) 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: ShortList list = new ShortList();
295:
296: for (short j = 0; j < 1000; j++) {
297: list.add(j);
298: }
299: for (short 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: ShortList list = new ShortList();
319:
320: for (short j = 0; j < 1000; j++) {
321: list.add((short) (j / 2));
322: }
323: for (short 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: ShortList list1 = new ShortList();
338: ShortList list2 = new ShortList(1000);
339: ShortList list3 = new ShortList(list1);
340:
341: assertTrue(list1.isEmpty());
342: assertTrue(list2.isEmpty());
343: assertTrue(list3.isEmpty());
344: list1.add((short) 1);
345: list2.add((short) 2);
346: list3 = new ShortList(list2);
347: assertTrue(!list1.isEmpty());
348: assertTrue(!list2.isEmpty());
349: assertTrue(!list3.isEmpty());
350: list1.clear();
351: list2.remove(0);
352: list3.removeValue((short) 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: ShortList list = new ShortList();
364:
365: for (short j = 0; j < 1000; j++) {
366: list.add((short) (j / 2));
367: }
368: for (short 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: ShortList list = new ShortList();
383:
384: for (short j = 0; j < 1000; j++) {
385: list.add(j);
386: }
387: for (short j = 0; j < 1000; j++) {
388: assertEquals(j, list.remove(0));
389: assertEquals((short) (999 - j), list.size());
390: }
391: for (short j = 0; j < 1000; j++) {
392: list.add(j);
393: }
394: for (short j = 0; j < 1000; j++) {
395: assertEquals((short) (999 - j), list
396: .remove((short) (999 - j)));
397: assertEquals(999 - j, list.size());
398: }
399: try {
400: list.remove(0);
401: fail("should have caught IndexOutOfBoundsException");
402: } catch (IndexOutOfBoundsException e) {
403:
404: // as expected
405: }
406: }
407:
408: /**
409: * test the removeValue method
410: */
411:
412: public void testRemoveValue() {
413: ShortList list = new ShortList();
414:
415: for (short j = 0; j < 1000; j++) {
416: list.add((short) (j / 2));
417: }
418: for (short j = 0; j < 1000; j++) {
419: if (j < 500) {
420: assertTrue(list.removeValue(j));
421: assertTrue(list.removeValue(j));
422: }
423: assertTrue(!list.removeValue(j));
424: }
425: }
426:
427: /**
428: * test the removeAll method
429: */
430:
431: public void testRemoveAll() {
432: ShortList list = new ShortList();
433:
434: for (short j = 0; j < 1000; j++) {
435: list.add(j);
436: }
437: ShortList listCopy = new ShortList(list);
438: ShortList listOdd = new ShortList();
439: ShortList listEven = new ShortList();
440:
441: for (short j = 0; j < 1000; j++) {
442: if (j % 2 == 0) {
443: listEven.add(j);
444: } else {
445: listOdd.add(j);
446: }
447: }
448: list.removeAll(listEven);
449: assertEquals(list, listOdd);
450: list.removeAll(listOdd);
451: assertTrue(list.isEmpty());
452: listCopy.removeAll(listOdd);
453: assertEquals(listCopy, listEven);
454: listCopy.removeAll(listEven);
455: assertTrue(listCopy.isEmpty());
456: }
457:
458: /**
459: * test the retainAll method
460: */
461:
462: public void testRetainAll() {
463: ShortList list = new ShortList();
464:
465: for (short j = 0; j < 1000; j++) {
466: list.add(j);
467: }
468: ShortList listCopy = new ShortList(list);
469: ShortList listOdd = new ShortList();
470: ShortList listEven = new ShortList();
471:
472: for (short j = 0; j < 1000; j++) {
473: if (j % 2 == 0) {
474: listEven.add(j);
475: } else {
476: listOdd.add(j);
477: }
478: }
479: list.retainAll(listOdd);
480: assertEquals(list, listOdd);
481: list.retainAll(listEven);
482: assertTrue(list.isEmpty());
483: listCopy.retainAll(listEven);
484: assertEquals(listCopy, listEven);
485: listCopy.retainAll(listOdd);
486: assertTrue(listCopy.isEmpty());
487: }
488:
489: /**
490: * test the set method
491: */
492:
493: public void testSet() {
494: ShortList list = new ShortList();
495:
496: for (short j = 0; j < 1000; j++) {
497: list.add(j);
498: }
499: for (short j = 0; j < 1001; j++) {
500: try {
501: list.set(j, (short) (j + 1));
502: if (j == 1000) {
503: fail("Should have gotten exception");
504: }
505: assertEquals(j + 1, list.get(j));
506: } catch (IndexOutOfBoundsException e) {
507: if (j != 1000) {
508: fail("premature exception");
509: }
510: }
511: }
512: }
513:
514: /**
515: * test the size method
516: */
517:
518: public void testSize() {
519: ShortList list = new ShortList();
520:
521: for (short j = 0; j < 1000; j++) {
522: assertEquals(j, list.size());
523: list.add(j);
524: assertEquals(j + 1, list.size());
525: }
526: for (short j = 0; j < 1000; j++) {
527: assertEquals(1000 - j, list.size());
528: list.removeValue(j);
529: assertEquals(999 - j, list.size());
530: }
531: }
532:
533: /**
534: * test the toArray method
535: */
536:
537: public void testToArray() {
538: ShortList list = new ShortList();
539:
540: for (short j = 0; j < 1000; j++) {
541: list.add(j);
542: }
543: short[] a1 = list.toArray();
544:
545: assertEquals(a1.length, list.size());
546: for (short j = 0; j < 1000; j++) {
547: assertEquals(a1[j], list.get(j));
548: }
549: short[] a2 = new short[list.size()];
550: short[] a3 = list.toArray(a2);
551:
552: assertSame(a2, a3);
553: for (short j = 0; j < 1000; j++) {
554: assertEquals(a2[j], list.get(j));
555: }
556: short[] aShort = new short[list.size() - 1];
557: short[] aLong = new short[list.size() + 1];
558: short[] a4 = list.toArray(aShort);
559: short[] a5 = list.toArray(aLong);
560:
561: assertTrue(a4 != aShort);
562: assertTrue(a5 != aLong);
563: assertEquals(a4.length, list.size());
564: for (short j = 0; j < 1000; j++) {
565: assertEquals(a3[j], list.get(j));
566: }
567: assertEquals(a5.length, list.size());
568: for (short j = 0; j < 1000; j++) {
569: assertEquals(a5[j], list.get(j));
570: }
571: }
572:
573: /**
574: * main method to run the unit tests
575: *
576: * @param unused_args
577: */
578:
579: public static void main(String[] unused_args) {
580: System.out.println("Testing util.ShortList functionality");
581: junit.textui.TestRunner.run(TestShortList.class);
582: }
583: }
|