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