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