View Javadoc
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.testing.SampleElements;
23  import com.google.common.collect.testing.TestListGenerator;
24  
25  import junit.framework.TestCase;
26  
27  import java.util.List;
28  
29  /**
30   * Test suite covering {@link Floats#asList(float[])})}.
31   *
32   * @author Kevin Bourrillion
33   */
34  @GwtCompatible(emulated = true)
35  public class FloatArrayAsListTest extends TestCase {
36  
37    private static List<Float> asList(Float[] values) {
38      float[] temp = new float[values.length];
39      for (int i = 0; i < values.length; i++) {
40        temp[i] = checkNotNull(values[i]);  // checkNotNull for GWT (do not optimize).
41      }
42      return Floats.asList(temp);
43    }
44  
45    // Test generators.  To let the GWT test suite generator access them, they need to be
46    // public named classes with a public default constructor.
47  
48    public static final class FloatsAsListGenerator extends TestFloatListGenerator {
49      @Override protected List<Float> create(Float[] elements) {
50        return asList(elements);
51      }
52    }
53  
54    public static final class FloatsAsListHeadSubListGenerator extends TestFloatListGenerator {
55      @Override protected List<Float> create(Float[] elements) {
56        Float[] suffix = {Float.MIN_VALUE, Float.MAX_VALUE};
57        Float[] all = concat(elements, suffix);
58        return asList(all).subList(0, elements.length);
59      }
60    }
61  
62    public static final class FloatsAsListTailSubListGenerator extends TestFloatListGenerator {
63      @Override protected List<Float> create(Float[] elements) {
64        Float[] prefix = {(float) 86, (float) 99};
65        Float[] all = concat(prefix, elements);
66        return asList(all).subList(2, elements.length + 2);
67      }
68    }
69  
70    public static final class FloatsAsListMiddleSubListGenerator extends TestFloatListGenerator {
71      @Override protected List<Float> create(Float[] elements) {
72        Float[] prefix = {Float.MIN_VALUE, Float.MAX_VALUE};
73        Float[] suffix = {(float) 86, (float) 99};
74        Float[] all = concat(concat(prefix, elements), suffix);
75        return asList(all).subList(2, elements.length + 2);
76      }
77    }
78  
79    private static Float[] concat(Float[] left, Float[] right) {
80      Float[] result = new Float[left.length + right.length];
81      System.arraycopy(left, 0, result, 0, left.length);
82      System.arraycopy(right, 0, result, left.length, right.length);
83      return result;
84    }
85  
86    public static abstract class TestFloatListGenerator
87        implements TestListGenerator<Float> {
88      @Override
89      public SampleElements<Float> samples() {
90        return new SampleFloats();
91      }
92  
93      @Override
94      public List<Float> create(Object... elements) {
95        Float[] array = new Float[elements.length];
96        int i = 0;
97        for (Object e : elements) {
98          array[i++] = (Float) e;
99        }
100       return create(array);
101     }
102 
103     /**
104      * Creates a new collection containing the given elements; implement this
105      * method instead of {@link #create(Object...)}.
106      */
107     protected abstract List<Float> create(Float[] elements);
108 
109     @Override
110     public Float[] createArray(int length) {
111       return new Float[length];
112     }
113 
114     /** Returns the original element list, unchanged. */
115     @Override
116     public List<Float> order(List<Float> insertionOrder) {
117       return insertionOrder;
118     }
119   }
120 
121   public static class SampleFloats extends SampleElements<Float> {
122     public SampleFloats() {
123       super((float) 0, (float) 1, (float) 2, (float) 3, (float) 4);
124     }
125   }
126 }
127