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