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