• 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 Floats#asList(float[])})}.
37  *
38  * @author Kevin Bourrillion
39  */
40 @GwtCompatible(emulated = true)
41 public class FloatArrayAsListTest extends TestCase {
42 
asList(Float[] values)43   private static List<Float> asList(Float[] values) {
44     float[] temp = new float[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 Floats.asList(temp);
49   }
50 
51   @GwtIncompatible // suite
suite()52   public static Test suite() {
53     List<ListTestSuiteBuilder<Float>> builders =
54         ImmutableList.of(
55             ListTestSuiteBuilder.using(new FloatsAsListGenerator()).named("Floats.asList"),
56             ListTestSuiteBuilder.using(new FloatsAsListHeadSubListGenerator())
57                 .named("Floats.asList, head subList"),
58             ListTestSuiteBuilder.using(new FloatsAsListTailSubListGenerator())
59                 .named("Floats.asList, tail subList"),
60             ListTestSuiteBuilder.using(new FloatsAsListMiddleSubListGenerator())
61                 .named("Floats.asList, middle subList"));
62 
63     TestSuite suite = new TestSuite();
64     for (ListTestSuiteBuilder<Float> 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 FloatsAsListGenerator extends TestFloatListGenerator {
81     @Override
create(Float[] elements)82     protected List<Float> create(Float[] elements) {
83       return asList(elements);
84     }
85   }
86 
87   public static final class FloatsAsListHeadSubListGenerator extends TestFloatListGenerator {
88     @Override
create(Float[] elements)89     protected List<Float> create(Float[] elements) {
90       Float[] suffix = {Float.MIN_VALUE, Float.MAX_VALUE};
91       Float[] all = concat(elements, suffix);
92       return asList(all).subList(0, elements.length);
93     }
94   }
95 
96   public static final class FloatsAsListTailSubListGenerator extends TestFloatListGenerator {
97     @Override
create(Float[] elements)98     protected List<Float> create(Float[] elements) {
99       Float[] prefix = {(float) 86, (float) 99};
100       Float[] all = concat(prefix, elements);
101       return asList(all).subList(2, elements.length + 2);
102     }
103   }
104 
105   public static final class FloatsAsListMiddleSubListGenerator extends TestFloatListGenerator {
106     @Override
create(Float[] elements)107     protected List<Float> create(Float[] elements) {
108       Float[] prefix = {Float.MIN_VALUE, Float.MAX_VALUE};
109       Float[] suffix = {(float) 86, (float) 99};
110       Float[] all = concat(concat(prefix, elements), suffix);
111       return asList(all).subList(2, elements.length + 2);
112     }
113   }
114 
concat(Float[] left, Float[] right)115   private static Float[] concat(Float[] left, Float[] right) {
116     Float[] result = new Float[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 TestFloatListGenerator implements TestListGenerator<Float> {
123     @Override
samples()124     public SampleElements<Float> samples() {
125       return new SampleFloats();
126     }
127 
128     @Override
create(Object... elements)129     public List<Float> create(Object... elements) {
130       Float[] array = new Float[elements.length];
131       int i = 0;
132       for (Object e : elements) {
133         array[i++] = (Float) 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(Float[] elements)142     protected abstract List<Float> create(Float[] elements);
143 
144     @Override
createArray(int length)145     public Float[] createArray(int length) {
146       return new Float[length];
147     }
148 
149     /** Returns the original element list, unchanged. */
150     @Override
order(List<Float> insertionOrder)151     public List<Float> order(List<Float> insertionOrder) {
152       return insertionOrder;
153     }
154   }
155 
156   public static class SampleFloats extends SampleElements<Float> {
SampleFloats()157     public SampleFloats() {
158       super((float) 0, (float) 1, (float) 2, (float) 3, (float) 4);
159     }
160   }
161 }
162