guava

Форк
0
/
CollectionToArrayTester.java 
203 строки · 7.9 Кб
1
/*
2
 * Copyright (C) 2007 The Guava Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
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.annotations.J2ktIncompatible;
25
import com.google.common.collect.testing.AbstractCollectionTester;
26
import com.google.common.collect.testing.Helpers;
27
import com.google.common.collect.testing.WrongType;
28
import com.google.common.collect.testing.features.CollectionFeature;
29
import com.google.common.collect.testing.features.CollectionSize;
30
import java.lang.reflect.Method;
31
import java.util.Arrays;
32
import java.util.Collection;
33
import java.util.List;
34
import org.junit.Ignore;
35

36
/**
37
 * A generic JUnit test which tests {@code toArray()} operations on a collection. Can't be invoked
38
 * directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
39
 *
40
 * @author Kevin Bourrillion
41
 * @author Chris Povirk
42
 */
43
@GwtCompatible(emulated = true)
44
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
45
public class CollectionToArrayTester<E> extends AbstractCollectionTester<E> {
46
  public void testToArray_noArgs() {
47
    Object[] array = collection.toArray();
48
    expectArrayContentsAnyOrder(createSamplesArray(), array);
49
  }
50

51
  /**
52
   * {@link Collection#toArray(Object[])} says: "Note that {@code toArray(new Object[0])} is
53
   * identical in function to {@code toArray()}."
54
   *
55
   * <p>For maximum effect, the collection under test should be created from an element array of a
56
   * type other than {@code Object[]}.
57
   */
58
  public void testToArray_isPlainObjectArray() {
59
    Object[] array = collection.toArray();
60
    assertEquals(Object[].class, array.getClass());
61
  }
62

63
  public void testToArray_emptyArray() {
64
    E[] empty = getSubjectGenerator().createArray(0);
65
    E[] array = collection.toArray(empty);
66
    assertEquals(
67
        "toArray(emptyT[]) should return an array of type T", empty.getClass(), array.getClass());
68
    assertEquals("toArray(emptyT[]).length:", getNumElements(), array.length);
69
    expectArrayContentsAnyOrder(createSamplesArray(), array);
70
  }
71

72
  @CollectionFeature.Require(KNOWN_ORDER)
73
  public void testToArray_emptyArray_ordered() {
74
    E[] empty = getSubjectGenerator().createArray(0);
75
    E[] array = collection.toArray(empty);
76
    assertEquals(
77
        "toArray(emptyT[]) should return an array of type T", empty.getClass(), array.getClass());
78
    assertEquals("toArray(emptyT[]).length:", getNumElements(), array.length);
79
    expectArrayContentsInOrder(getOrderedElements(), array);
80
  }
81

82
  public void testToArray_emptyArrayOfObject() {
83
    Object[] in = new Object[0];
84
    Object[] array = collection.toArray(in);
85
    assertEquals(
86
        "toArray(emptyObject[]) should return an array of type Object",
87
        Object[].class,
88
        array.getClass());
89
    assertEquals("toArray(emptyObject[]).length", getNumElements(), array.length);
90
    expectArrayContentsAnyOrder(createSamplesArray(), array);
91
  }
92

93
  public void testToArray_rightSizedArray() {
94
    E[] array = getSubjectGenerator().createArray(getNumElements());
95
    assertSame(
96
        "toArray(sameSizeE[]) should return the given array", array, collection.toArray(array));
97
    expectArrayContentsAnyOrder(createSamplesArray(), array);
98
  }
99

100
  @CollectionFeature.Require(KNOWN_ORDER)
101
  public void testToArray_rightSizedArray_ordered() {
102
    E[] array = getSubjectGenerator().createArray(getNumElements());
103
    assertSame(
104
        "toArray(sameSizeE[]) should return the given array", array, collection.toArray(array));
105
    expectArrayContentsInOrder(getOrderedElements(), array);
106
  }
107

108
  public void testToArray_rightSizedArrayOfObject() {
109
    Object[] array = new Object[getNumElements()];
110
    assertSame(
111
        "toArray(sameSizeObject[]) should return the given array",
112
        array,
113
        collection.toArray(array));
114
    expectArrayContentsAnyOrder(createSamplesArray(), array);
115
  }
116

117
  @CollectionFeature.Require(KNOWN_ORDER)
118
  public void testToArray_rightSizedArrayOfObject_ordered() {
119
    Object[] array = new Object[getNumElements()];
120
    assertSame(
121
        "toArray(sameSizeObject[]) should return the given array",
122
        array,
123
        collection.toArray(array));
124
    expectArrayContentsInOrder(getOrderedElements(), array);
125
  }
126

127
  public void testToArray_oversizedArray() {
128
    E[] array = getSubjectGenerator().createArray(getNumElements() + 2);
129
    array[getNumElements()] = e3();
130
    array[getNumElements() + 1] = e3();
131
    assertSame(
132
        "toArray(overSizedE[]) should return the given array", array, collection.toArray(array));
133

134
    List<E> subArray = Arrays.asList(array).subList(0, getNumElements());
135
    E[] expectedSubArray = createSamplesArray();
136
    for (int i = 0; i < getNumElements(); i++) {
137
      assertTrue(
138
          "toArray(overSizedE[]) should contain element " + expectedSubArray[i],
139
          subArray.contains(expectedSubArray[i]));
140
    }
141
    assertNull(
142
        "The array element immediately following the end of the collection should be nulled",
143
        array[getNumElements()]);
144
    // array[getNumElements() + 1] might or might not have been nulled
145
  }
146

147
  @CollectionFeature.Require(KNOWN_ORDER)
148
  public void testToArray_oversizedArray_ordered() {
149
    E[] array = getSubjectGenerator().createArray(getNumElements() + 2);
150
    array[getNumElements()] = e3();
151
    array[getNumElements() + 1] = e3();
152
    assertSame(
153
        "toArray(overSizedE[]) should return the given array", array, collection.toArray(array));
154

155
    List<E> expected = getOrderedElements();
156
    for (int i = 0; i < getNumElements(); i++) {
157
      assertEquals(expected.get(i), array[i]);
158
    }
159
    assertNull(
160
        "The array element immediately following the end of the collection should be nulled",
161
        array[getNumElements()]);
162
    // array[getNumElements() + 1] might or might not have been nulled
163
  }
164

165
  @CollectionSize.Require(absent = ZERO)
166
  public void testToArray_emptyArrayOfWrongTypeForNonEmptyCollection() {
167
    try {
168
      WrongType[] array = new WrongType[0];
169
      collection.toArray(array);
170
      fail("toArray(notAssignableTo[]) should throw");
171
    } catch (ArrayStoreException expected) {
172
    }
173
  }
174

175
  @CollectionSize.Require(ZERO)
176
  public void testToArray_emptyArrayOfWrongTypeForEmptyCollection() {
177
    WrongType[] array = new WrongType[0];
178
    assertSame(
179
        "toArray(sameSizeNotAssignableTo[]) should return the given array",
180
        array,
181
        collection.toArray(array));
182
  }
183

184
  private void expectArrayContentsAnyOrder(Object[] expected, Object[] actual) {
185
    Helpers.assertEqualIgnoringOrder(Arrays.asList(expected), Arrays.asList(actual));
186
  }
187

188
  private void expectArrayContentsInOrder(List<E> expected, Object[] actual) {
189
    assertEquals("toArray() ordered contents: ", expected, Arrays.asList(actual));
190
  }
191

192
  /**
193
   * Returns the {@link Method} instance for {@link #testToArray_isPlainObjectArray()} so that tests
194
   * of {@link Arrays#asList(Object[])} can suppress it with {@code
195
   * FeatureSpecificTestSuiteBuilder.suppressing()} until <a
196
   * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6260652">Sun bug 6260652</a> is fixed.
197
   */
198
  @J2ktIncompatible
199
  @GwtIncompatible // reflection
200
  public static Method getToArrayIsPlainObjectArrayMethod() {
201
    return Helpers.getMethod(CollectionToArrayTester.class, "testToArray_isPlainObjectArray");
202
  }
203
}
204

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.