1 16 package org.apache.commons.collections.set; 17 18 import java.util.Collection ; 19 import java.util.Iterator ; 20 import java.util.Set ; 21 import java.util.SortedSet ; 22 import java.util.TreeSet ; 23 24 import org.apache.commons.collections.BulkTest; 25 26 40 public abstract class AbstractTestSortedSet extends AbstractTestSet { 41 42 47 public AbstractTestSortedSet(String name) { 48 super(name); 49 } 50 51 56 public void verify() { 57 super.verify(); 58 59 Iterator colliter = collection.iterator(); 62 Iterator confiter = confirmed.iterator(); 63 Object first = null; 64 Object last = null; 65 while (colliter.hasNext()) { 66 if (first == null) { 67 first = colliter.next(); 68 last = first; 69 } else { 70 last = colliter.next(); 71 } 72 assertEquals("Element appears to be out of order.", last, confiter.next()); 73 } 74 if (collection.size() > 0) { 75 assertEquals("Incorrect element returned by first().", first, 76 ((SortedSet ) collection).first()); 77 assertEquals("Incorrect element returned by last().", last, 78 ((SortedSet ) collection).last()); 79 } 80 } 81 82 87 public boolean isNullSupported() { 88 return false; 89 } 90 91 97 public Collection makeConfirmedCollection() { 98 return new TreeSet (); 99 } 100 101 106 public SortedSet getConfirmedSortedSet() { 107 return (SortedSet ) confirmed; 108 } 109 110 114 public Object [] getFullNonNullElements() { 115 Object [] elements = new Object [30]; 116 117 for (int i = 0; i < 30; i++) { 118 elements[i] = new Integer (i + i + 1); 119 } 120 return elements; 121 } 122 123 126 public Object [] getOtherNonNullElements() { 127 Object [] elements = new Object [30]; 128 for (int i = 0; i < 30; i++) { 129 elements[i] = new Integer (i + i + 2); 130 } 131 return elements; 132 } 133 134 143 public BulkTest bulkTestSortedSetSubSet() { 144 int length = getFullElements().length; 145 146 int lobound = length / 3; 147 int hibound = lobound * 2; 148 return new TestSortedSetSubSet(lobound, hibound); 149 150 } 151 152 160 public BulkTest bulkTestSortedSetHeadSet() { 161 int length = getFullElements().length; 162 163 int lobound = length / 3; 164 int hibound = lobound * 2; 165 return new TestSortedSetSubSet(hibound, true); 166 167 } 168 169 177 public BulkTest bulkTestSortedSetTailSet() { 178 int length = getFullElements().length; 179 int lobound = length / 3; 180 return new TestSortedSetSubSet(lobound, false); 181 } 182 183 public class TestSortedSetSubSet extends AbstractTestSortedSet { 184 185 private int m_Type; 186 private int m_LowBound; 187 private int m_HighBound; 188 private Object [] m_FullElements; 189 private Object [] m_OtherElements; 190 191 public TestSortedSetSubSet(int bound, boolean head) { 192 super("TestSortedSetSubSet"); 193 if (head) { 194 m_Type = TYPE_HEADSET; 196 m_HighBound = bound; 197 m_FullElements = new Object [bound]; 198 System.arraycopy(AbstractTestSortedSet.this.getFullElements(), 0, m_FullElements, 0, bound); 199 m_OtherElements = new Object [bound - 1]; 200 System.arraycopy( AbstractTestSortedSet.this.getOtherElements(), 0, m_OtherElements, 0, bound - 1); 202 } else { 205 m_Type = TYPE_TAILSET; 207 m_LowBound = bound; 208 Object [] allelements = AbstractTestSortedSet.this.getFullElements(); 209 m_FullElements = new Object [allelements.length - bound]; 211 System.arraycopy(allelements, bound, m_FullElements, 0, allelements.length - bound); 212 m_OtherElements = new Object [allelements.length - bound - 1]; 213 System.arraycopy( AbstractTestSortedSet.this.getOtherElements(), bound, m_OtherElements, 0, allelements.length - bound - 1); 215 221 } 222 223 } 225 public TestSortedSetSubSet(int lobound, int hibound) { 226 super("TestSortedSetSubSet"); 227 m_Type = TYPE_SUBSET; 229 m_LowBound = lobound; 230 m_HighBound = hibound; 231 int length = hibound - lobound; 232 m_FullElements = new Object [length]; 234 System.arraycopy(AbstractTestSortedSet.this.getFullElements(), lobound, m_FullElements, 0, length); 235 m_OtherElements = new Object [length - 1]; 236 System.arraycopy( AbstractTestSortedSet.this.getOtherElements(), lobound, m_OtherElements, 0, length - 1); 238 239 242 } 243 244 public boolean isNullSupported() { 245 return AbstractTestSortedSet.this.isNullSupported(); 246 } 247 public boolean isAddSupported() { 248 return AbstractTestSortedSet.this.isAddSupported(); 249 } 250 public boolean isRemoveSupported() { 251 return AbstractTestSortedSet.this.isRemoveSupported(); 252 } 253 public boolean isFailFastSupported() { 254 return AbstractTestSortedSet.this.isFailFastSupported(); 255 } 256 257 public Object [] getFullElements() { 258 return m_FullElements; 259 } 260 public Object [] getOtherElements() { 261 return m_OtherElements; 262 } 263 264 private SortedSet getSubSet(SortedSet set) { 265 Object [] elements = AbstractTestSortedSet.this.getFullElements(); 266 switch (m_Type) { 267 case TYPE_SUBSET : 268 return set.subSet(elements[m_LowBound], elements[m_HighBound]); 269 case TYPE_HEADSET : 270 return set.headSet(elements[m_HighBound]); 271 case TYPE_TAILSET : 272 return set.tailSet(elements[m_LowBound]); 273 default : 274 return null; 275 } 276 } 277 278 public Set makeEmptySet() { 279 SortedSet s = (SortedSet ) AbstractTestSortedSet.this.makeEmptySet(); 280 return getSubSet(s); 281 } 282 283 public Set makeFullSet() { 284 SortedSet s = (SortedSet ) AbstractTestSortedSet.this.makeFullCollection(); 285 return getSubSet(s); 286 } 287 288 public boolean isTestSerialization() { 289 return false; 290 } 291 292 public BulkTest bulkTestSortedSetSubSet() { 293 return null; } 295 public BulkTest bulkTestSortedSetHeadSet() { 296 return null; } 298 public BulkTest bulkTestSortedSetTailSet() { 299 return null; } 301 302 static final int TYPE_SUBSET = 0; 303 static final int TYPE_TAILSET = 1; 304 static final int TYPE_HEADSET = 2; 305 306 } 307 308 } 309 | Popular Tags |