Home | History | Annotate | Download | only in primitives
      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 Shorts#asList(short[])}.
     38  *
     39  * @author Kevin Bourrillion
     40  */
     41 @GwtCompatible
     42 public class ShortArrayAsListTest extends TestCase {
     43 
     44   private static List<Short> asList(Short[] values) {
     45     short[] temp = new short[values.length];
     46     for (short i = 0; i < values.length; i++) {
     47       temp[i] = checkNotNull(values[i]);  // checkNotNull for GWT (do not optimize).
     48     }
     49     return Shorts.asList(temp);
     50   }
     51 
     52   public static Test suite() {
     53     List<ListTestSuiteBuilder<Short>> builders =
     54         ImmutableList.of(
     55             ListTestSuiteBuilder.using(new ShortsAsListGenerator())
     56                 .named("Shorts.asList"),
     57 
     58             ListTestSuiteBuilder.using(new ShortsAsListHeadSubListGenerator())
     59                 .named("Shorts.asList, head subList"),
     60 
     61             ListTestSuiteBuilder.using(new ShortsAsListTailSubListGenerator())
     62                 .named("Shorts.asList, tail subList"),
     63 
     64             ListTestSuiteBuilder.using(new ShortsAsListMiddleSubListGenerator())
     65                 .named("Shorts.asList, middle subList")
     66             );
     67 
     68     TestSuite suite = new TestSuite();
     69     for (ListTestSuiteBuilder<Short> 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 ShortsAsListGenerator extends TestShortListGenerator {
     85     @Override protected List<Short> create(Short[] elements) {
     86       return asList(elements);
     87     }
     88   }
     89 
     90   public static final class ShortsAsListHeadSubListGenerator extends TestShortListGenerator {
     91     @Override protected List<Short> create(Short[] elements) {
     92       Short[] suffix = {Short.MIN_VALUE, Short.MAX_VALUE};
     93       Short[] all = concat(elements, suffix);
     94       return asList(all).subList(0, elements.length);
     95     }
     96   }
     97 
     98   public static final class ShortsAsListTailSubListGenerator extends TestShortListGenerator {
     99     @Override protected List<Short> create(Short[] elements) {
    100       Short[] prefix = {(short) 86, (short) 99};
    101       Short[] all = concat(prefix, elements);
    102       return asList(all).subList(2, elements.length + 2);
    103     }
    104   }
    105 
    106   public static final class ShortsAsListMiddleSubListGenerator extends TestShortListGenerator {
    107     @Override protected List<Short> create(Short[] elements) {
    108       Short[] prefix = {Short.MIN_VALUE, Short.MAX_VALUE};
    109       Short[] suffix = {(short) 86, (short) 99};
    110       Short[] all = concat(concat(prefix, elements), suffix);
    111       return asList(all).subList(2, elements.length + 2);
    112     }
    113   }
    114 
    115   private static Short[] concat(Short[] left, Short[] right) {
    116     Short[] result = new Short[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 TestShortListGenerator
    123       implements TestListGenerator<Short> {
    124     @Override
    125     public SampleElements<Short> samples() {
    126       return new SampleShorts();
    127     }
    128 
    129     @Override
    130     public List<Short> create(Object... elements) {
    131       Short[] array = new Short[elements.length];
    132       short i = 0;
    133       for (Object e : elements) {
    134         array[i++] = (Short) 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      */
    143     protected abstract List<Short> create(Short[] elements);
    144 
    145     @Override
    146     public Short[] createArray(int length) {
    147       return new Short[length];
    148     }
    149 
    150     /** Returns the original element list, unchanged. */
    151     @Override
    152     public List<Short> order(List<Short> insertionOrder) {
    153       return insertionOrder;
    154     }
    155   }
    156 
    157   public static class SampleShorts extends SampleElements<Short> {
    158     public SampleShorts() {
    159       super((short) 0, (short) 1, (short) 2, (short) 3, (short) 4);
    160     }
    161   }
    162 }
    163