1/*
2 * Copyright (C) 2008 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.primitives;
18
19import static com.google.common.base.Preconditions.checkNotNull;
20
21import com.google.common.annotations.GwtCompatible;
22import com.google.common.annotations.GwtIncompatible;
23import com.google.common.collect.ImmutableList;
24import com.google.common.collect.testing.ListTestSuiteBuilder;
25import com.google.common.collect.testing.SampleElements;
26import com.google.common.collect.testing.TestListGenerator;
27import com.google.common.collect.testing.features.CollectionFeature;
28import com.google.common.collect.testing.features.CollectionSize;
29import com.google.common.collect.testing.features.ListFeature;
30
31import junit.framework.Test;
32import junit.framework.TestCase;
33import junit.framework.TestSuite;
34
35import java.util.List;
36
37/**
38 * Test suite covering {@link Ints#asList(int[])}.
39 *
40 * @author Kevin Bourrillion
41 */
42@GwtCompatible(emulated = true)
43@SuppressWarnings("cast") // redundant casts are intentional and harmless
44public class IntArrayAsListTest extends TestCase {
45
46  private static List<Integer> asList(Integer[] values) {
47    int[] temp = new int[values.length];
48    for (int i = 0; i < values.length; i++) {
49      temp[i] = checkNotNull(values[i]);  // checkNotNull for GWT (do not optimize).
50    }
51    return Ints.asList(temp);
52  }
53
54  @GwtIncompatible("suite")
55  public static Test suite() {
56    List<ListTestSuiteBuilder<Integer>> builders =
57        ImmutableList.of(
58            ListTestSuiteBuilder.using(new IntsAsListGenerator())
59                .named("Ints.asList"),
60
61            ListTestSuiteBuilder.using(new IntsAsListHeadSubListGenerator())
62                .named("Ints.asList, head subList"),
63
64            ListTestSuiteBuilder.using(new IntsAsListTailSubListGenerator())
65                .named("Ints.asList, tail subList"),
66
67            ListTestSuiteBuilder.using(new IntsAsListMiddleSubListGenerator())
68                .named("Ints.asList, middle subList")
69        );
70
71    TestSuite suite = new TestSuite();
72    for (ListTestSuiteBuilder<Integer> builder : builders) {
73      suite.addTest(
74          builder
75          .withFeatures(CollectionSize.ONE,
76                        CollectionSize.SEVERAL,
77                        CollectionFeature.RESTRICTS_ELEMENTS,
78                        ListFeature.SUPPORTS_SET)
79          .createTestSuite());
80    }
81    return suite;
82  }
83
84  // Test generators.  To let the GWT test suite generator access them, they need to be
85  // public named classes with a public default constructor.
86
87  public static final class IntsAsListGenerator extends TestIntegerListGenerator {
88    @Override protected List<Integer> create(Integer[] elements) {
89      return asList(elements);
90    }
91  }
92
93  public static final class IntsAsListHeadSubListGenerator extends TestIntegerListGenerator {
94    @Override protected List<Integer> create(Integer[] elements) {
95      Integer[] suffix = {Integer.MIN_VALUE, Integer.MAX_VALUE};
96      Integer[] all = concat(elements, suffix);
97      return asList(all).subList(0, elements.length);
98    }
99  }
100
101  public static final class IntsAsListTailSubListGenerator extends TestIntegerListGenerator {
102    @Override protected List<Integer> create(Integer[] elements) {
103      Integer[] prefix = {(int) 86, (int) 99};
104      Integer[] all = concat(prefix, elements);
105      return asList(all).subList(2, elements.length + 2);
106    }
107  }
108
109  public static final class IntsAsListMiddleSubListGenerator extends TestIntegerListGenerator {
110    @Override protected List<Integer> create(Integer[] elements) {
111      Integer[] prefix = {Integer.MIN_VALUE, Integer.MAX_VALUE};
112      Integer[] suffix = {(int) 86, (int) 99};
113      Integer[] all = concat(concat(prefix, elements), suffix);
114      return asList(all).subList(2, elements.length + 2);
115    }
116  }
117
118  private static Integer[] concat(Integer[] left, Integer[] right) {
119    Integer[] result = new Integer[left.length + right.length];
120    System.arraycopy(left, 0, result, 0, left.length);
121    System.arraycopy(right, 0, result, left.length, right.length);
122    return result;
123  }
124
125  public static abstract class TestIntegerListGenerator
126      implements TestListGenerator<Integer> {
127    @Override
128    public SampleElements<Integer> samples() {
129      return new SampleIntegers();
130    }
131
132    @Override
133    public List<Integer> create(Object... elements) {
134      Integer[] array = new Integer[elements.length];
135      int i = 0;
136      for (Object e : elements) {
137        array[i++] = (Integer) e;
138      }
139      return create(array);
140    }
141
142    /**
143     * Creates a new collection containing the given elements; implement this
144     * method instead of {@link #create(Object...)}.
145     */
146    protected abstract List<Integer> create(Integer[] elements);
147
148    @Override public Integer[] createArray(int length) {
149      return new Integer[length];
150    }
151
152    /** Returns the original element list, unchanged. */
153    @Override
154    public List<Integer> order(List<Integer> insertionOrder) {
155      return insertionOrder;
156    }
157  }
158
159  public static class SampleIntegers extends SampleElements<Integer> {
160    public SampleIntegers() {
161      super((int) 0, (int) 1, (int) 2, (int) 3, (int) 4);
162    }
163  }
164}
165