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 */
017package org.apache.commons.collections.primitives;
018
019import junit.framework.Test;
020import junit.framework.TestSuite;
021
022import org.apache.commons.collections.BulkTest;
023
024/**
025 * @version $Revision: 480451 $ $Date: 2006-11-29 02:45:08 -0500 (Wed, 29 Nov 2006) $
026 * @author Rodney Waldhoff
027 */
028public class TestArrayUnsignedShortList extends TestIntList {
029
030    // conventional
031    // ------------------------------------------------------------------------
032
033    public TestArrayUnsignedShortList(String testName) {
034        super(testName);
035    }
036
037    public static Test suite() {
038        TestSuite suite = BulkTest.makeSuite(TestArrayUnsignedShortList.class);
039        return suite;
040    }
041
042    // collections testing framework
043    // ------------------------------------------------------------------------
044
045    protected IntList makeEmptyIntList() {
046        return new ArrayUnsignedShortList();
047    }
048
049    public String[] ignoredTests() {
050        // sublists are not serializable
051        return new String[] { 
052            "TestArrayUnsignedShortList.bulkTestSubList.testFullListSerialization",
053            "TestArrayUnsignedShortList.bulkTestSubList.testEmptyListSerialization",
054            "TestArrayUnsignedShortList.bulkTestSubList.testCanonicalEmptyCollectionExists",
055            "TestArrayUnsignedShortList.bulkTestSubList.testCanonicalFullCollectionExists",
056            "TestArrayUnsignedShortList.bulkTestSubList.testEmptyListCompatibility",
057            "TestArrayUnsignedShortList.bulkTestSubList.testFullListCompatibility",
058            "TestArrayUnsignedShortList.bulkTestSubList.testSerializeDeserializeThenCompare",
059            "TestArrayUnsignedShortList.bulkTestSubList.testSimpleSerialization"
060        };
061    }
062
063    // tests
064    // ------------------------------------------------------------------------
065
066    public void testArrayConstructor() {
067        int[] data = new int[] { 1, 2, 3 };
068        IntList list = new ArrayUnsignedShortList(data);
069        for(int i=0;i<data.length;i++) {
070            assertEquals(data[i],list.get(i));
071        }
072        data[0] = 17;
073        assertEquals(1,list.get(0));
074    }
075    
076    // @TODO need to add serialized form to cvs
077    public void testCanonicalEmptyCollectionExists() {
078        // XXX FIX ME XXX
079        // need to add a serialized form to cvs
080    }
081
082    public void testCanonicalFullCollectionExists() {
083        // XXX FIX ME XXX
084        // need to add a serialized form to cvs
085    }
086
087    public void testEmptyListCompatibility() {
088        // XXX FIX ME XXX
089        // need to add a serialized form to cvs
090    }
091
092    public void testFullListCompatibility() {
093        // XXX FIX ME XXX
094        // need to add a serialized form to cvs
095    }
096
097    public void testZeroInitialCapacityIsValid() {
098        assertNotNull(new ArrayUnsignedShortList(0));
099    }
100    
101    public void testIllegalArgumentExceptionWhenElementOutOfRange() {
102        ArrayUnsignedShortList list = new ArrayUnsignedShortList();
103        list.add(ArrayUnsignedShortList.MIN_VALUE);
104        list.add(ArrayUnsignedShortList.MAX_VALUE);
105        try {
106            list.add(-1);
107            fail("Expected IllegalArgumentException");
108        } catch(IllegalArgumentException e) {
109            // expected
110        }
111        try {
112            list.add(ArrayUnsignedShortList.MAX_VALUE+1);
113            fail("Expected IllegalArgumentException");
114        } catch(IllegalArgumentException e) {
115            // expected
116        }
117    }
118
119    public void testNegativeInitialCapacityIsInvalid() {
120        try {
121            new ArrayUnsignedShortList(-1);
122            fail("Expected IllegalArgumentException");
123        } catch(IllegalArgumentException e) {
124            // expected
125        }
126    }
127
128    public void testCopyConstructor() {
129        ArrayUnsignedShortList expected = new ArrayUnsignedShortList();
130        for(int i=0;i<10;i++) {
131            expected.add(i);
132        }
133        ArrayUnsignedShortList list = new ArrayUnsignedShortList(expected);
134        assertEquals(10,list.size());
135        assertEquals(expected,list);
136    }
137
138    public void testCopyConstructorWithNull() {
139        try {
140            new ArrayUnsignedShortList((IntList)null);
141            fail("Expected NullPointerException");
142        } catch(NullPointerException e) {
143            // expected
144        }
145    }
146
147
148    public void testTrimToSize() {
149        ArrayUnsignedShortList list = new ArrayUnsignedShortList();
150        for(int j=0;j<3;j++) {
151            assertTrue(list.isEmpty());
152    
153            list.trimToSize();
154    
155            assertTrue(list.isEmpty());
156            
157            for(int i=0;i<10;i++) {
158                list.add(i);
159            }
160            
161            for(int i=0;i<10;i++) {
162                assertEquals(i,list.get(i));
163            }
164            
165            list.trimToSize();
166    
167            for(int i=0;i<10;i++) {
168                assertEquals(i,list.get(i));
169            }
170    
171            for(int i=0;i<10;i+=2) {
172                list.removeElement(i);
173            }
174            
175            for(int i=0;i<5;i++) {
176                assertEquals((2*i)+1,list.get(i));
177            }
178    
179            list.trimToSize();
180                    
181            for(int i=0;i<5;i++) {
182                assertEquals((2*i)+1,list.get(i));
183            }
184    
185            list.trimToSize();
186                    
187            for(int i=0;i<5;i++) {
188                assertEquals((2*i)+1,list.get(i));
189            }
190
191            list.clear();
192        }
193    }
194
195}