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