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