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