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