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.adapters; 018 019import java.util.Iterator; 020import java.util.ListIterator; 021import java.util.NoSuchElementException; 022 023import junit.framework.Test; 024import junit.framework.TestSuite; 025 026import org.apache.commons.collections.iterators.AbstractTestListIterator; 027import org.apache.commons.collections.primitives.ArrayFloatList; 028import org.apache.commons.collections.primitives.FloatList; 029 030/** 031 * @version $Revision: 480451 $ $Date: 2006-11-29 02:45:08 -0500 (Wed, 29 Nov 2006) $ 032 * @author Rodney Waldhoff 033 */ 034public class TestFloatListIteratorListIterator extends AbstractTestListIterator { 035 036 // conventional 037 // ------------------------------------------------------------------------ 038 039 public TestFloatListIteratorListIterator(String testName) { 040 super(testName); 041 } 042 043 public static Test suite() { 044 return new TestSuite(TestFloatListIteratorListIterator.class); 045 } 046 047 // collections testing framework 048 // ------------------------------------------------------------------------ 049 050 public ListIterator makeEmptyListIterator() { 051 return FloatListIteratorListIterator.wrap(makeEmptyFloatList().listIterator()); 052 } 053 054 public ListIterator makeFullListIterator() { 055 return FloatListIteratorListIterator.wrap(makeFullFloatList().listIterator()); 056 } 057 058 protected FloatList makeEmptyFloatList() { 059 return new ArrayFloatList(); 060 } 061 062 protected FloatList makeFullFloatList() { 063 FloatList list = makeEmptyFloatList(); 064 float[] elts = getFullElements(); 065 for(int i=0;i<elts.length;i++) { 066 list.add((float)elts[i]); 067 } 068 return list; 069 } 070 071 public float[] getFullElements() { 072 return new float[] { (float)0, (float)1, (float)2, (float)3, (float)4, (float)5, (float)6, (float)7, (float)8, (float)9 }; 073 } 074 075 public Object addSetValue() { 076 return new Float((float)1); 077 } 078 079 // tests 080 // ------------------------------------------------------------------------ 081 082 083 public void testNextHasNextRemove() { 084 float[] elements = getFullElements(); 085 Iterator iter = makeFullIterator(); 086 for(int i=0;i<elements.length;i++) { 087 assertTrue(iter.hasNext()); 088 assertEquals(new Float(elements[i]),iter.next()); 089 if(supportsRemove()) { 090 iter.remove(); 091 } 092 } 093 assertTrue(! iter.hasNext() ); 094 } 095 096 public void testEmptyIterator() { 097 assertTrue( ! makeEmptyIterator().hasNext() ); 098 try { 099 makeEmptyIterator().next(); 100 fail("Expected NoSuchElementException"); 101 } catch(NoSuchElementException e) { 102 // expected 103 } 104 if(supportsRemove()) { 105 try { 106 makeEmptyIterator().remove(); 107 fail("Expected IllegalStateException"); 108 } catch(IllegalStateException e) { 109 // expected 110 } 111 } 112 } 113 114 public void testRemoveBeforeNext() { 115 if(supportsRemove()) { 116 try { 117 makeFullIterator().remove(); 118 fail("Expected IllegalStateException"); 119 } catch(IllegalStateException e) { 120 // expected 121 } 122 } 123 } 124 125 public void testRemoveAfterRemove() { 126 if(supportsRemove()) { 127 Iterator iter = makeFullIterator(); 128 iter.next(); 129 iter.remove(); 130 try { 131 iter.remove(); 132 fail("Expected IllegalStateException"); 133 } catch(IllegalStateException e) { 134 // expected 135 } 136 } 137 } 138 139}