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