/* * Copyright (C) 2007 The Guava Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.common.collect.testing.testers; import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER; import static com.google.common.collect.testing.features.CollectionSize.ZERO; import com.google.common.collect.testing.AbstractCollectionTester; import com.google.common.collect.testing.Helpers; import com.google.common.collect.testing.WrongType; import com.google.common.collect.testing.features.CollectionFeature; import com.google.common.collect.testing.features.CollectionSize; import java.lang.reflect.Method; import java.util.Arrays; import java.util.Collection; import java.util.List; /** * A generic JUnit test which tests {@code toArray()} operations on a * collection. Can't be invoked directly; please see * {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}. * *
This class is GWT compatible.
*
* @author Kevin Bourrillion
* @author Chris Povirk
*/
public class CollectionToArrayTester For maximum effect, the collection under test should be created from an
* element array of a type other than {@code Object[]}.
*/
public void testToArray_isPlainObjectArray() {
Object[] array = collection.toArray();
assertEquals(Object[].class, array.getClass());
}
public void testToArray_emptyArray() {
E[] empty = getSubjectGenerator().createArray(0);
E[] array = collection.toArray(empty);
assertEquals("toArray(emptyT[]) should return an array of type T",
empty.getClass(), array.getClass());
assertEquals("toArray(emptyT[]).length:", getNumElements(), array.length);
expectArrayContentsAnyOrder(createSamplesArray(), array);
}
@CollectionFeature.Require(KNOWN_ORDER)
public void testToArray_emptyArray_ordered() {
E[] empty = getSubjectGenerator().createArray(0);
E[] array = collection.toArray(empty);
assertEquals("toArray(emptyT[]) should return an array of type T",
empty.getClass(), array.getClass());
assertEquals("toArray(emptyT[]).length:", getNumElements(), array.length);
expectArrayContentsInOrder(getOrderedElements(), array);
}
public void testToArray_emptyArrayOfObject() {
Object[] in = new Object[0];
Object[] array = collection.toArray(in);
assertEquals("toArray(emptyObject[]) should return an array of type Object",
Object[].class, array.getClass());
assertEquals("toArray(emptyObject[]).length",
getNumElements(), array.length);
expectArrayContentsAnyOrder(createSamplesArray(), array);
}
public void testToArray_rightSizedArray() {
E[] array = getSubjectGenerator().createArray(getNumElements());
assertSame("toArray(sameSizeE[]) should return the given array",
array, collection.toArray(array));
expectArrayContentsAnyOrder(createSamplesArray(), array);
}
@CollectionFeature.Require(KNOWN_ORDER)
public void testToArray_rightSizedArray_ordered() {
E[] array = getSubjectGenerator().createArray(getNumElements());
assertSame("toArray(sameSizeE[]) should return the given array",
array, collection.toArray(array));
expectArrayContentsInOrder(getOrderedElements(), array);
}
public void testToArray_rightSizedArrayOfObject() {
Object[] array = new Object[getNumElements()];
assertSame("toArray(sameSizeObject[]) should return the given array",
array, collection.toArray(array));
expectArrayContentsAnyOrder(createSamplesArray(), array);
}
@CollectionFeature.Require(KNOWN_ORDER)
public void testToArray_rightSizedArrayOfObject_ordered() {
Object[] array = new Object[getNumElements()];
assertSame("toArray(sameSizeObject[]) should return the given array",
array, collection.toArray(array));
expectArrayContentsInOrder(getOrderedElements(), array);
}
public void testToArray_oversizedArray() {
E[] array = getSubjectGenerator().createArray(getNumElements() + 2);
array[getNumElements()] = samples.e3;
array[getNumElements() + 1] = samples.e3;
assertSame("toArray(overSizedE[]) should return the given array",
array, collection.toArray(array));
List