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
17package com.google.common.collect;
18
19import static com.google.common.truth.Truth.assertThat;
20
21import com.google.common.annotations.GwtCompatible;
22import com.google.common.annotations.GwtIncompatible;
23import com.google.common.testing.NullPointerTester;
24
25import junit.framework.TestCase;
26
27import java.io.Serializable;
28import java.util.Arrays;
29import java.util.List;
30
31/**
32 * Unit test for {@code ObjectArrays}.
33 *
34 * @author Kevin Bourrillion
35 */
36@GwtCompatible(emulated = true)
37public class ObjectArraysTest extends TestCase {
38
39  @GwtIncompatible("NullPointerTester")
40  public void testNullPointerExceptions() {
41    NullPointerTester tester = new NullPointerTester();
42    tester.testAllPublicStaticMethods(ObjectArrays.class);
43  }
44
45  @GwtIncompatible("ObjectArrays.newArray(Class, int)")
46  public void testNewArray_fromClass_Empty() {
47    String[] empty = ObjectArrays.newArray(String.class, 0);
48    assertEquals(String[].class, empty.getClass());
49    assertEquals(0, empty.length);
50  }
51
52  @GwtIncompatible("ObjectArrays.newArray(Class, int)")
53  public void testNewArray_fromClass_Nonempty() {
54    String[] array = ObjectArrays.newArray(String.class, 2);
55    assertEquals(String[].class, array.getClass());
56    assertEquals(2, array.length);
57    assertNull(array[0]);
58  }
59
60  @GwtIncompatible("ObjectArrays.newArray(Class, int)")
61  public void testNewArray_fromClass_OfArray() {
62    String[][] array = ObjectArrays.newArray(String[].class, 1);
63    assertEquals(String[][].class, array.getClass());
64    assertEquals(1, array.length);
65    assertNull(array[0]);
66  }
67
68  public void testNewArray_fromArray_Empty() {
69    String[] in = new String[0];
70    String[] empty = ObjectArrays.newArray(in, 0);
71    assertEquals(0, empty.length);
72  }
73
74  public void testNewArray_fromArray_Nonempty() {
75    String[] array = ObjectArrays.newArray(new String[0], 2);
76    assertEquals(String[].class, array.getClass());
77    assertEquals(2, array.length);
78    assertNull(array[0]);
79  }
80
81  public void testNewArray_fromArray_OfArray() {
82    String[][] array = ObjectArrays.newArray(new String[0][0], 1);
83    assertEquals(String[][].class, array.getClass());
84    assertEquals(1, array.length);
85    assertNull(array[0]);
86  }
87
88  @GwtIncompatible("ObjectArrays.concat(Object[], Object[], Class)")
89  public void testConcatEmptyEmpty() {
90    String[] result
91        = ObjectArrays.concat(new String[0], new String[0], String.class);
92    assertEquals(String[].class, result.getClass());
93    assertEquals(0, result.length);
94  }
95
96  @GwtIncompatible("ObjectArrays.concat(Object[], Object[], Class)")
97  public void testConcatEmptyNonempty() {
98    String[] result = ObjectArrays.concat(
99        new String[0], new String[] { "a", "b" }, String.class);
100    assertEquals(String[].class, result.getClass());
101    assertThat(result).asList().has().exactly("a", "b").inOrder();
102  }
103
104  @GwtIncompatible("ObjectArrays.concat(Object[], Object[], Class)")
105  public void testConcatNonemptyEmpty() {
106    String[] result = ObjectArrays.concat(
107        new String[] { "a", "b" }, new String[0], String.class);
108    assertEquals(String[].class, result.getClass());
109    assertThat(result).asList().has().exactly("a", "b").inOrder();
110  }
111
112  @GwtIncompatible("ObjectArrays.concat(Object[], Object[], Class)")
113  public void testConcatBasic() {
114    String[] result = ObjectArrays.concat(
115        new String[] { "a", "b" }, new String[] { "c", "d" }, String.class);
116    assertEquals(String[].class, result.getClass());
117    assertThat(result).asList().has().exactly("a", "b", "c", "d").inOrder();
118  }
119
120  @GwtIncompatible("ObjectArrays.concat(Object[], Object[], Class)")
121  public void testConcatWithMoreGeneralType() {
122    Serializable[] result
123        = ObjectArrays.concat(new String[0], new String[0], Serializable.class);
124    assertEquals(Serializable[].class, result.getClass());
125  }
126
127  public void testToArrayImpl1() {
128    doTestToArrayImpl1(Lists.<Integer>newArrayList());
129    doTestToArrayImpl1(Lists.newArrayList(1));
130    doTestToArrayImpl1(Lists.newArrayList(1, null, 3));
131  }
132
133  private void doTestToArrayImpl1(List<Integer> list) {
134    Object[] reference = list.toArray();
135    Object[] target = ObjectArrays.toArrayImpl(list);
136    assertEquals(reference.getClass(), target.getClass());
137    assertTrue(Arrays.equals(reference, target));
138  }
139
140  public void testToArrayImpl2() {
141    doTestToArrayImpl2(Lists.<Integer>newArrayList(), new Integer[0], false);
142    doTestToArrayImpl2(Lists.<Integer>newArrayList(), new Integer[1], true);
143
144    doTestToArrayImpl2(Lists.newArrayList(1), new Integer[0], false);
145    doTestToArrayImpl2(Lists.newArrayList(1), new Integer[1], true);
146    doTestToArrayImpl2(Lists.newArrayList(1), new Integer[] { 2, 3 }, true);
147
148    doTestToArrayImpl2(Lists.newArrayList(1, null, 3), new Integer[0], false);
149    doTestToArrayImpl2(Lists.newArrayList(1, null, 3), new Integer[2], false);
150    doTestToArrayImpl2(Lists.newArrayList(1, null, 3), new Integer[3], true);
151  }
152
153  private void doTestToArrayImpl2(List<Integer> list, Integer[] array1,
154      boolean expectModify) {
155    Integer[] starting = ObjectArrays.arraysCopyOf(array1, array1.length);
156    Integer[] array2 = ObjectArrays.arraysCopyOf(array1, array1.length);
157    Object[] reference = list.toArray(array1);
158
159    Object[] target = ObjectArrays.toArrayImpl(list, array2);
160
161    assertEquals(reference.getClass(), target.getClass());
162    assertTrue(Arrays.equals(reference, target));
163    assertTrue(Arrays.equals(reference, target));
164
165    Object[] expectedArray1 = expectModify ? reference : starting;
166    Object[] expectedArray2 = expectModify ? target : starting;
167    assertTrue(Arrays.equals(expectedArray1, array1));
168    assertTrue(Arrays.equals(expectedArray2, array2));
169  }
170
171  public void testPrependZeroElements() {
172    String[] result = ObjectArrays.concat("foo", new String[] {});
173    assertThat(result).asList().has().item("foo");
174  }
175
176  public void testPrependOneElement() {
177    String[] result = ObjectArrays.concat("foo", new String[] { "bar" });
178    assertThat(result).asList().has().exactly("foo", "bar").inOrder();
179  }
180
181  public void testPrependTwoElements() {
182    String[] result = ObjectArrays.concat("foo", new String[] { "bar", "baz" });
183    assertThat(result).asList().has().exactly("foo", "bar", "baz").inOrder();
184  }
185
186  public void testAppendZeroElements() {
187    String[] result = ObjectArrays.concat(new String[] {}, "foo");
188    assertThat(result).asList().has().item("foo");
189  }
190
191  public void testAppendOneElement() {
192    String[] result = ObjectArrays.concat(new String[] { "foo" }, "bar");
193    assertThat(result).asList().has().exactly("foo", "bar").inOrder();
194  }
195
196  public void testAppendTwoElements() {
197    String[] result = ObjectArrays.concat(new String[] { "foo", "bar" }, "baz");
198    assertThat(result).asList().has().exactly("foo", "bar", "baz").inOrder();
199  }
200
201  public void testEmptyArrayToEmpty() {
202    doTestNewArrayEquals(new Object[0], 0);
203  }
204
205  public void testEmptyArrayToNonEmpty() {
206    checkArrayEquals(new Long[5], ObjectArrays.newArray(new Long[0], 5));
207  }
208
209  public void testNonEmptyToShorter() {
210    checkArrayEquals(new String[9], ObjectArrays.newArray(new String[10], 9));
211  }
212
213  public void testNonEmptyToSameLength() {
214    doTestNewArrayEquals(new String[10], 10);
215  }
216
217  public void testNonEmptyToLonger() {
218    checkArrayEquals(new String[10],
219        ObjectArrays.newArray(new String[] { "a", "b", "c", "d", "e" }, 10));
220  }
221
222  private static void checkArrayEquals(Object[] expected, Object[] actual) {
223    assertTrue("expected(" + expected.getClass() + "): " + Arrays.toString(expected)
224        + " actual(" + actual.getClass() + "): " + Arrays.toString(actual),
225        arrayEquals(expected, actual));
226  }
227
228  private static boolean arrayEquals(Object[] array1, Object[] array2) {
229    assertSame(array1.getClass(), array2.getClass());
230    return Arrays.equals(array1, array2);
231  }
232
233  private static void doTestNewArrayEquals(Object[] expected, int length) {
234    checkArrayEquals(expected, ObjectArrays.newArray(expected, length));
235  }
236}
237