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