• 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.annotations.J2ktIncompatible;
24 import com.google.common.collect.ImmutableList;
25 import com.google.common.collect.testing.ListTestSuiteBuilder;
26 import com.google.common.collect.testing.SampleElements;
27 import com.google.common.collect.testing.TestListGenerator;
28 import com.google.common.collect.testing.features.CollectionFeature;
29 import com.google.common.collect.testing.features.CollectionSize;
30 import com.google.common.collect.testing.features.ListFeature;
31 import java.util.List;
32 import junit.framework.Test;
33 import junit.framework.TestCase;
34 import junit.framework.TestSuite;
35 
36 /**
37  * Test suite covering {@link Doubles#asList(double[])}.
38  *
39  * @author Kevin Bourrillion
40  */
41 @GwtCompatible(emulated = true)
42 public class DoubleArrayAsListTest extends TestCase {
43 
asList(Double[] values)44   private static List<Double> asList(Double[] values) {
45     double[] temp = new double[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 Doubles.asList(temp);
50   }
51 
52   @J2ktIncompatible
53   @GwtIncompatible // suite
suite()54   public static Test suite() {
55     List<ListTestSuiteBuilder<Double>> builders =
56         ImmutableList.of(
57             ListTestSuiteBuilder.using(new DoublesAsListGenerator()).named("Doubles.asList"),
58             ListTestSuiteBuilder.using(new DoublesAsListHeadSubListGenerator())
59                 .named("Doubles.asList, head subList"),
60             ListTestSuiteBuilder.using(new DoublesAsListTailSubListGenerator())
61                 .named("Doubles.asList, tail subList"),
62             ListTestSuiteBuilder.using(new DoublesAsListMiddleSubListGenerator())
63                 .named("Doubles.asList, middle subList"));
64 
65     TestSuite suite = new TestSuite();
66     for (ListTestSuiteBuilder<Double> builder : builders) {
67       suite.addTest(
68           builder
69               .withFeatures(
70                   CollectionSize.ONE,
71                   CollectionSize.SEVERAL,
72                   CollectionFeature.RESTRICTS_ELEMENTS,
73                   ListFeature.SUPPORTS_SET)
74               .createTestSuite());
75     }
76     return suite;
77   }
78 
79   // Test generators.  To let the GWT test suite generator access them, they need to be
80   // public named classes with a public default constructor.
81 
82   public static final class DoublesAsListGenerator extends TestDoubleListGenerator {
83     @Override
create(Double[] elements)84     protected List<Double> create(Double[] elements) {
85       return asList(elements);
86     }
87   }
88 
89   public static final class DoublesAsListHeadSubListGenerator extends TestDoubleListGenerator {
90     @Override
create(Double[] elements)91     protected List<Double> create(Double[] elements) {
92       Double[] suffix = {Double.MIN_VALUE, Double.MAX_VALUE};
93       Double[] all = concat(elements, suffix);
94       return asList(all).subList(0, elements.length);
95     }
96   }
97 
98   public static final class DoublesAsListTailSubListGenerator extends TestDoubleListGenerator {
99     @Override
create(Double[] elements)100     protected List<Double> create(Double[] elements) {
101       Double[] prefix = {(double) 86, (double) 99};
102       Double[] all = concat(prefix, elements);
103       return asList(all).subList(2, elements.length + 2);
104     }
105   }
106 
107   public static final class DoublesAsListMiddleSubListGenerator extends TestDoubleListGenerator {
108     @Override
create(Double[] elements)109     protected List<Double> create(Double[] elements) {
110       Double[] prefix = {Double.MIN_VALUE, Double.MAX_VALUE};
111       Double[] suffix = {(double) 86, (double) 99};
112       Double[] all = concat(concat(prefix, elements), suffix);
113       return asList(all).subList(2, elements.length + 2);
114     }
115   }
116 
concat(Double[] left, Double[] right)117   private static Double[] concat(Double[] left, Double[] right) {
118     Double[] result = new Double[left.length + right.length];
119     System.arraycopy(left, 0, result, 0, left.length);
120     System.arraycopy(right, 0, result, left.length, right.length);
121     return result;
122   }
123 
124   public abstract static class TestDoubleListGenerator implements TestListGenerator<Double> {
125     @Override
samples()126     public SampleElements<Double> samples() {
127       return new SampleDoubles();
128     }
129 
130     @Override
create(Object... elements)131     public List<Double> create(Object... elements) {
132       Double[] array = new Double[elements.length];
133       int i = 0;
134       for (Object e : elements) {
135         array[i++] = (Double) e;
136       }
137       return create(array);
138     }
139 
140     /**
141      * Creates a new collection containing the given elements; implement this method instead of
142      * {@link #create(Object...)}.
143      */
create(Double[] elements)144     protected abstract List<Double> create(Double[] elements);
145 
146     @Override
createArray(int length)147     public Double[] createArray(int length) {
148       return new Double[length];
149     }
150 
151     /** Returns the original element list, unchanged. */
152     @Override
order(List<Double> insertionOrder)153     public List<Double> order(List<Double> insertionOrder) {
154       return insertionOrder;
155     }
156   }
157 
158   public static class SampleDoubles extends SampleElements<Double> {
SampleDoubles()159     public SampleDoubles() {
160       super((double) 0, (double) 1, (double) 2, (double) 3, (double) 4);
161     }
162   }
163 }
164