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