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 org.junit.contrib.truth.Truth.ASSERT;
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() throws Exception {
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    ASSERT.that(result).hasContentsInOrder("a", "b");
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    ASSERT.that(result).hasContentsInOrder("a", "b");
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    ASSERT.that(result).hasContentsInOrder("a", "b", "c", "d");
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 = Platform.clone(array1);
156    Integer[] array2 = Platform.clone(array1);
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    ASSERT.that(result).hasContentsInOrder("foo");
174  }
175
176  public void testPrependOneElement() {
177    String[] result = ObjectArrays.concat("foo", new String[]{ "bar" });
178    ASSERT.that(result).hasContentsInOrder("foo", "bar");
179  }
180
181  public void testPrependTwoElements() {
182    String[] result = ObjectArrays.concat("foo", new String[]{ "bar", "baz" });
183    ASSERT.that(result).hasContentsInOrder("foo", "bar", "baz");
184  }
185
186  public void testAppendZeroElements() {
187    String[] result = ObjectArrays.concat(new String[] {}, "foo");
188    ASSERT.that(result).hasContentsInOrder("foo");
189  }
190
191  public void testAppendOneElement() {
192    String[] result = ObjectArrays.concat(new String[]{ "foo" }, "bar");
193    ASSERT.that(result).hasContentsInOrder("foo", "bar");
194  }
195
196  public void testAppendTwoElements() {
197    String[] result = ObjectArrays.concat(new String[]{ "foo", "bar" }, "baz");
198    ASSERT.that(result).hasContentsInOrder("foo", "bar", "baz");
199  }
200}
201