| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.common.collect.testing.testers; |
| 18 |
|
| 19 |
import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER; |
| 20 |
import static com.google.common.collect.testing.features.CollectionSize.ZERO; |
| 21 |
|
| 22 |
import com.google.common.annotations.GwtCompatible; |
| 23 |
import com.google.common.annotations.GwtIncompatible; |
| 24 |
import com.google.common.collect.testing.AbstractCollectionTester; |
| 25 |
import com.google.common.collect.testing.Helpers; |
| 26 |
import com.google.common.collect.testing.WrongType; |
| 27 |
import com.google.common.collect.testing.features.CollectionFeature; |
| 28 |
import com.google.common.collect.testing.features.CollectionSize; |
| 29 |
import java.lang.reflect.Method; |
| 30 |
import java.util.Arrays; |
| 31 |
import java.util.Collection; |
| 32 |
import java.util.List; |
| 33 |
import org.junit.Ignore; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
@GwtCompatible(emulated = true) |
| 43 |
@Ignore |
| 44 |
public class CollectionToArrayTester<E> extends AbstractCollectionTester<E> { |
| 45 |
public void testToArray_noArgs() { |
| 46 |
Object[] array = collection.toArray(); |
| 47 |
expectArrayContentsAnyOrder(createSamplesArray(), array); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public void testToArray_isPlainObjectArray() { |
| 58 |
Object[] array = collection.toArray(); |
| 59 |
assertEquals(Object[].class, array.getClass()); |
| 60 |
} |
| 61 |
|
| 62 |
public void testToArray_emptyArray() { |
| 63 |
E[] empty = getSubjectGenerator().createArray(0); |
| 64 |
E[] array = collection.toArray(empty); |
| 65 |
assertEquals( |
| 66 |
"toArray(emptyT[]) should return an array of type T", empty.getClass(), array.getClass()); |
| 67 |
assertEquals("toArray(emptyT[]).length:", getNumElements(), array.length); |
| 68 |
expectArrayContentsAnyOrder(createSamplesArray(), array); |
| 69 |
} |
| 70 |
|
| 71 |
@CollectionFeature.Require(KNOWN_ORDER) |
| 72 |
public void testToArray_emptyArray_ordered() { |
| 73 |
E[] empty = getSubjectGenerator().createArray(0); |
| 74 |
E[] array = collection.toArray(empty); |
| 75 |
assertEquals( |
| 76 |
"toArray(emptyT[]) should return an array of type T", empty.getClass(), array.getClass()); |
| 77 |
assertEquals("toArray(emptyT[]).length:", getNumElements(), array.length); |
| 78 |
expectArrayContentsInOrder(getOrderedElements(), array); |
| 79 |
} |
| 80 |
|
| 81 |
public void testToArray_emptyArrayOfObject() { |
| 82 |
Object[] in = new Object[0]; |
| 83 |
Object[] array = collection.toArray(in); |
| 84 |
assertEquals( |
| 85 |
"toArray(emptyObject[]) should return an array of type Object", |
| 86 |
Object[].class, |
| 87 |
array.getClass()); |
| 88 |
assertEquals("toArray(emptyObject[]).length", getNumElements(), array.length); |
| 89 |
expectArrayContentsAnyOrder(createSamplesArray(), array); |
| 90 |
} |
| 91 |
|
| 92 |
public void testToArray_rightSizedArray() { |
| 93 |
E[] array = getSubjectGenerator().createArray(getNumElements()); |
| 94 |
assertSame( |
| 95 |
"toArray(sameSizeE[]) should return the given array", array, collection.toArray(array)); |
| 96 |
expectArrayContentsAnyOrder(createSamplesArray(), array); |
| 97 |
} |
| 98 |
|
| 99 |
@CollectionFeature.Require(KNOWN_ORDER) |
| 100 |
public void testToArray_rightSizedArray_ordered() { |
| 101 |
E[] array = getSubjectGenerator().createArray(getNumElements()); |
| 102 |
assertSame( |
| 103 |
"toArray(sameSizeE[]) should return the given array", array, collection.toArray(array)); |
| 104 |
expectArrayContentsInOrder(getOrderedElements(), array); |
| 105 |
} |
| 106 |
|
| 107 |
public void testToArray_rightSizedArrayOfObject() { |
| 108 |
Object[] array = new Object[getNumElements()]; |
| 109 |
assertSame( |
| 110 |
"toArray(sameSizeObject[]) should return the given array", |
| 111 |
array, |
| 112 |
collection.toArray(array)); |
| 113 |
expectArrayContentsAnyOrder(createSamplesArray(), array); |
| 114 |
} |
| 115 |
|
| 116 |
@CollectionFeature.Require(KNOWN_ORDER) |
| 117 |
public void testToArray_rightSizedArrayOfObject_ordered() { |
| 118 |
Object[] array = new Object[getNumElements()]; |
| 119 |
assertSame( |
| 120 |
"toArray(sameSizeObject[]) should return the given array", |
| 121 |
array, |
| 122 |
collection.toArray(array)); |
| 123 |
expectArrayContentsInOrder(getOrderedElements(), array); |
| 124 |
} |
| 125 |
|
| 126 |
public void testToArray_oversizedArray() { |
| 127 |
E[] array = getSubjectGenerator().createArray(getNumElements() + 2); |
| 128 |
array[getNumElements()] = e3(); |
| 129 |
array[getNumElements() + 1] = e3(); |
| 130 |
assertSame( |
| 131 |
"toArray(overSizedE[]) should return the given array", array, collection.toArray(array)); |
| 132 |
|
| 133 |
List<E> subArray = Arrays.asList(array).subList(0, getNumElements()); |
| 134 |
E[] expectedSubArray = createSamplesArray(); |
| 135 |
for (int i = 0; i < getNumElements(); i++) { |
| 136 |
assertTrue( |
| 137 |
"toArray(overSizedE[]) should contain element " + expectedSubArray[i], |
| 138 |
subArray.contains(expectedSubArray[i])); |
| 139 |
} |
| 140 |
assertNull( |
| 141 |
"The array element immediately following the end of the collection should be nulled", |
| 142 |
array[getNumElements()]); |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
@CollectionFeature.Require(KNOWN_ORDER) |
| 147 |
public void testToArray_oversizedArray_ordered() { |
| 148 |
E[] array = getSubjectGenerator().createArray(getNumElements() + 2); |
| 149 |
array[getNumElements()] = e3(); |
| 150 |
array[getNumElements() + 1] = e3(); |
| 151 |
assertSame( |
| 152 |
"toArray(overSizedE[]) should return the given array", array, collection.toArray(array)); |
| 153 |
|
| 154 |
List<E> expected = getOrderedElements(); |
| 155 |
for (int i = 0; i < getNumElements(); i++) { |
| 156 |
assertEquals(expected.get(i), array[i]); |
| 157 |
} |
| 158 |
assertNull( |
| 159 |
"The array element immediately following the end of the collection should be nulled", |
| 160 |
array[getNumElements()]); |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
@CollectionSize.Require(absent = ZERO) |
| 165 |
public void testToArray_emptyArrayOfWrongTypeForNonEmptyCollection() { |
| 166 |
try { |
| 167 |
WrongType[] array = new WrongType[0]; |
| 168 |
collection.toArray(array); |
| 169 |
fail("toArray(notAssignableTo[]) should throw"); |
| 170 |
} catch (ArrayStoreException expected) { |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
@CollectionSize.Require(ZERO) |
| 175 |
public void testToArray_emptyArrayOfWrongTypeForEmptyCollection() { |
| 176 |
WrongType[] array = new WrongType[0]; |
| 177 |
assertSame( |
| 178 |
"toArray(sameSizeNotAssignableTo[]) should return the given array", |
| 179 |
array, |
| 180 |
collection.toArray(array)); |
| 181 |
} |
| 182 |
|
| 183 |
private void expectArrayContentsAnyOrder(Object[] expected, Object[] actual) { |
| 184 |
Helpers.assertEqualIgnoringOrder(Arrays.asList(expected), Arrays.asList(actual)); |
| 185 |
} |
| 186 |
|
| 187 |
private void expectArrayContentsInOrder(List<E> expected, Object[] actual) { |
| 188 |
assertEquals("toArray() ordered contents: ", expected, Arrays.asList(actual)); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
@GwtIncompatible |
| 198 |
public static Method getToArrayIsPlainObjectArrayMethod() { |
| 199 |
return Helpers.getMethod(CollectionToArrayTester.class, "testToArray_isPlainObjectArray"); |
| 200 |
} |
| 201 |
} |