001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.set;
017:
018: import java.util.Collection;
019: import java.util.Iterator;
020: import java.util.Set;
021: import java.util.SortedSet;
022: import java.util.TreeSet;
023:
024: import org.apache.commons.collections.BulkTest;
025:
026: /**
027: * Abstract test class for {@link SortedSet} methods and contracts.
028: * <p>
029: * To use, subclass and override the {@link #makeEmptySet()}
030: * method. You may have to override other protected methods if your
031: * set is not modifiable, or if your set restricts what kinds of
032: * elements may be added; see {@link AbstractTestCollection} for more details.
033: *
034: * @since Commons Collections 3.0
035: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
036: *
037: * @author Stephen Colebourne
038: * @author Dieter Wimberger
039: */
040: public abstract class AbstractTestSortedSet extends AbstractTestSet {
041:
042: /**
043: * JUnit constructor.
044: *
045: * @param name name for test
046: */
047: public AbstractTestSortedSet(String name) {
048: super (name);
049: }
050:
051: //-----------------------------------------------------------------------
052: /**
053: * Verification extension, will check the order of elements,
054: * the sets should already be verified equal.
055: */
056: public void verify() {
057: super .verify();
058:
059: // Check that iterator returns elements in order and first() and last()
060: // are consistent
061: Iterator colliter = collection.iterator();
062: Iterator confiter = confirmed.iterator();
063: Object first = null;
064: Object last = null;
065: while (colliter.hasNext()) {
066: if (first == null) {
067: first = colliter.next();
068: last = first;
069: } else {
070: last = colliter.next();
071: }
072: assertEquals("Element appears to be out of order.", last,
073: confiter.next());
074: }
075: if (collection.size() > 0) {
076: assertEquals("Incorrect element returned by first().",
077: first, ((SortedSet) collection).first());
078: assertEquals("Incorrect element returned by last().", last,
079: ((SortedSet) collection).last());
080: }
081: }
082:
083: //-----------------------------------------------------------------------
084: /**
085: * Overridden because SortedSets don't allow null elements (normally).
086: * @return false
087: */
088: public boolean isNullSupported() {
089: return false;
090: }
091:
092: //-----------------------------------------------------------------------
093: /**
094: * Returns an empty {@link TreeSet} for use in modification testing.
095: *
096: * @return a confirmed empty collection
097: */
098: public Collection makeConfirmedCollection() {
099: return new TreeSet();
100: }
101:
102: //-----------------------------------------------------------------------
103: /**
104: * Return the {@link AbstractTestCollection#confirmed} fixture, but cast as a
105: * SortedSet.
106: */
107: public SortedSet getConfirmedSortedSet() {
108: return (SortedSet) confirmed;
109: }
110:
111: //-----------------------------------------------------------------------
112: /**
113: * Override to return comparable objects.
114: */
115: public Object[] getFullNonNullElements() {
116: Object[] elements = new Object[30];
117:
118: for (int i = 0; i < 30; i++) {
119: elements[i] = new Integer(i + i + 1);
120: }
121: return elements;
122: }
123:
124: /**
125: * Override to return comparable objects.
126: */
127: public Object[] getOtherNonNullElements() {
128: Object[] elements = new Object[30];
129: for (int i = 0; i < 30; i++) {
130: elements[i] = new Integer(i + i + 2);
131: }
132: return elements;
133: }
134:
135: //-----------------------------------------------------------------------
136: /**
137: * Bulk test {@link SortedSet#subSet(Object, Object)}. This method runs through all of
138: * the tests in {@link AbstractTestSortedSet}.
139: * After modification operations, {@link #verify()} is invoked to ensure
140: * that the set and the other collection views are still valid.
141: *
142: * @return a {@link AbstractTestSet} instance for testing a subset.
143: */
144: public BulkTest bulkTestSortedSetSubSet() {
145: int length = getFullElements().length;
146:
147: int lobound = length / 3;
148: int hibound = lobound * 2;
149: return new TestSortedSetSubSet(lobound, hibound);
150:
151: }
152:
153: /**
154: * Bulk test {@link SortedSet#headSet(Object)}. This method runs through all of
155: * the tests in {@link AbstractTestSortedSet}.
156: * After modification operations, {@link #verify()} is invoked to ensure
157: * that the set and the other collection views are still valid.
158: *
159: * @return a {@link AbstractTestSet} instance for testing a headset.
160: */
161: public BulkTest bulkTestSortedSetHeadSet() {
162: int length = getFullElements().length;
163:
164: int lobound = length / 3;
165: int hibound = lobound * 2;
166: return new TestSortedSetSubSet(hibound, true);
167:
168: }
169:
170: /**
171: * Bulk test {@link SortedSet#tailSet(Object)}. This method runs through all of
172: * the tests in {@link AbstractTestSortedSet}.
173: * After modification operations, {@link #verify()} is invoked to ensure
174: * that the set and the other collection views are still valid.
175: *
176: * @return a {@link AbstractTestSet} instance for testing a tailset.
177: */
178: public BulkTest bulkTestSortedSetTailSet() {
179: int length = getFullElements().length;
180: int lobound = length / 3;
181: return new TestSortedSetSubSet(lobound, false);
182: }
183:
184: public class TestSortedSetSubSet extends AbstractTestSortedSet {
185:
186: private int m_Type;
187: private int m_LowBound;
188: private int m_HighBound;
189: private Object[] m_FullElements;
190: private Object[] m_OtherElements;
191:
192: public TestSortedSetSubSet(int bound, boolean head) {
193: super ("TestSortedSetSubSet");
194: if (head) {
195: //System.out.println("HEADSET");
196: m_Type = TYPE_HEADSET;
197: m_HighBound = bound;
198: m_FullElements = new Object[bound];
199: System
200: .arraycopy(AbstractTestSortedSet.this
201: .getFullElements(), 0, m_FullElements,
202: 0, bound);
203: m_OtherElements = new Object[bound - 1];
204: System.arraycopy(
205: //src src_pos dst dst_pos length
206: AbstractTestSortedSet.this .getOtherElements(),
207: 0, m_OtherElements, 0, bound - 1);
208: //System.out.println(new TreeSet(Arrays.asList(m_FullElements)));
209: //System.out.println(new TreeSet(Arrays.asList(m_OtherElements)));
210: } else {
211: //System.out.println("TAILSET");
212: m_Type = TYPE_TAILSET;
213: m_LowBound = bound;
214: Object[] allelements = AbstractTestSortedSet.this
215: .getFullElements();
216: //System.out.println("bound = "+bound +"::length="+allelements.length);
217: m_FullElements = new Object[allelements.length - bound];
218: System.arraycopy(allelements, bound, m_FullElements, 0,
219: allelements.length - bound);
220: m_OtherElements = new Object[allelements.length - bound
221: - 1];
222: System.arraycopy(
223: //src src_pos dst dst_pos length
224: AbstractTestSortedSet.this .getOtherElements(),
225: bound, m_OtherElements, 0, allelements.length
226: - bound - 1);
227: //System.out.println(new TreeSet(Arrays.asList(m_FullElements)));
228: //System.out.println(new TreeSet(Arrays.asList(m_OtherElements)));
229: //resetFull();
230: //System.out.println(collection);
231: //System.out.println(confirmed);
232:
233: }
234:
235: } //type
236:
237: public TestSortedSetSubSet(int lobound, int hibound) {
238: super ("TestSortedSetSubSet");
239: //System.out.println("SUBSET");
240: m_Type = TYPE_SUBSET;
241: m_LowBound = lobound;
242: m_HighBound = hibound;
243: int length = hibound - lobound;
244: //System.out.println("Low=" + lobound + "::High=" + hibound + "::Length=" + length);
245: m_FullElements = new Object[length];
246: System.arraycopy(AbstractTestSortedSet.this
247: .getFullElements(), lobound, m_FullElements, 0,
248: length);
249: m_OtherElements = new Object[length - 1];
250: System.arraycopy(
251: //src src_pos dst dst_pos length
252: AbstractTestSortedSet.this .getOtherElements(),
253: lobound, m_OtherElements, 0, length - 1);
254:
255: //System.out.println(new TreeSet(Arrays.asList(m_FullElements)));
256: //System.out.println(new TreeSet(Arrays.asList(m_OtherElements)));
257:
258: }
259:
260: public boolean isNullSupported() {
261: return AbstractTestSortedSet.this .isNullSupported();
262: }
263:
264: public boolean isAddSupported() {
265: return AbstractTestSortedSet.this .isAddSupported();
266: }
267:
268: public boolean isRemoveSupported() {
269: return AbstractTestSortedSet.this .isRemoveSupported();
270: }
271:
272: public boolean isFailFastSupported() {
273: return AbstractTestSortedSet.this .isFailFastSupported();
274: }
275:
276: public Object[] getFullElements() {
277: return m_FullElements;
278: }
279:
280: public Object[] getOtherElements() {
281: return m_OtherElements;
282: }
283:
284: private SortedSet getSubSet(SortedSet set) {
285: Object[] elements = AbstractTestSortedSet.this
286: .getFullElements();
287: switch (m_Type) {
288: case TYPE_SUBSET:
289: return set.subSet(elements[m_LowBound],
290: elements[m_HighBound]);
291: case TYPE_HEADSET:
292: return set.headSet(elements[m_HighBound]);
293: case TYPE_TAILSET:
294: return set.tailSet(elements[m_LowBound]);
295: default:
296: return null;
297: }
298: }
299:
300: public Set makeEmptySet() {
301: SortedSet s = (SortedSet) AbstractTestSortedSet.this
302: .makeEmptySet();
303: return getSubSet(s);
304: }
305:
306: public Set makeFullSet() {
307: SortedSet s = (SortedSet) AbstractTestSortedSet.this
308: .makeFullCollection();
309: return getSubSet(s);
310: }
311:
312: public boolean isTestSerialization() {
313: return false;
314: }
315:
316: public BulkTest bulkTestSortedSetSubSet() {
317: return null; // prevent infinite recursion
318: }
319:
320: public BulkTest bulkTestSortedSetHeadSet() {
321: return null; // prevent infinite recursion
322: }
323:
324: public BulkTest bulkTestSortedSetTailSet() {
325: return null; // prevent infinite recursion
326: }
327:
328: static final int TYPE_SUBSET = 0;
329: static final int TYPE_TAILSET = 1;
330: static final int TYPE_HEADSET = 2;
331:
332: }
333:
334: }
|