1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.testing.SampleElements;
23 import com.google.common.collect.testing.TestListGenerator;
24
25 import junit.framework.TestCase;
26
27 import java.util.List;
28
29
30
31
32
33
34 @GwtCompatible(emulated = true)
35 public class ByteArrayAsListTest extends TestCase {
36
37 private static List<Byte> asList(Byte[] values) {
38 byte[] temp = new byte[values.length];
39 for (int i = 0; i < values.length; i++) {
40 temp[i] = checkNotNull(values[i]);
41 }
42 return Bytes.asList(temp);
43 }
44
45
46
47
48 public static final class BytesAsListGenerator extends TestByteListGenerator {
49 @Override protected List<Byte> create(Byte[] elements) {
50 return asList(elements);
51 }
52 }
53
54 public static final class BytesAsListHeadSubListGenerator extends TestByteListGenerator {
55 @Override protected List<Byte> create(Byte[] elements) {
56 Byte[] suffix = {Byte.MIN_VALUE, Byte.MAX_VALUE};
57 Byte[] all = concat(elements, suffix);
58 return asList(all).subList(0, elements.length);
59 }
60 }
61
62 public static final class BytesAsListTailSubListGenerator extends TestByteListGenerator {
63 @Override protected List<Byte> create(Byte[] elements) {
64 Byte[] prefix = {(byte) 86, (byte) 99};
65 Byte[] all = concat(prefix, elements);
66 return asList(all).subList(2, elements.length + 2);
67 }
68 }
69
70 public static final class BytesAsListMiddleSubListGenerator extends TestByteListGenerator {
71 @Override protected List<Byte> create(Byte[] elements) {
72 Byte[] prefix = {Byte.MIN_VALUE, Byte.MAX_VALUE};
73 Byte[] suffix = {(byte) 86, (byte) 99};
74 Byte[] all = concat(concat(prefix, elements), suffix);
75 return asList(all).subList(2, elements.length + 2);
76 }
77 }
78
79 private static Byte[] concat(Byte[] left, Byte[] right) {
80 Byte[] result = new Byte[left.length + right.length];
81 System.arraycopy(left, 0, result, 0, left.length);
82 System.arraycopy(right, 0, result, left.length, right.length);
83 return result;
84 }
85
86 public static abstract class TestByteListGenerator
87 implements TestListGenerator<Byte> {
88 @Override
89 public SampleElements<Byte> samples() {
90 return new SampleBytes();
91 }
92
93 @Override
94 public List<Byte> create(Object... elements) {
95 Byte[] array = new Byte[elements.length];
96 int i = 0;
97 for (Object e : elements) {
98 array[i++] = (Byte) e;
99 }
100 return create(array);
101 }
102
103
104
105
106
107 protected abstract List<Byte> create(Byte[] elements);
108
109 @Override
110 public Byte[] createArray(int length) {
111 return new Byte[length];
112 }
113
114
115 @Override
116 public List<Byte> order(List<Byte> insertionOrder) {
117 return insertionOrder;
118 }
119 }
120
121 public static class SampleBytes extends SampleElements<Byte> {
122 public SampleBytes() {
123 super((byte) 0, (byte) 1, (byte) 2, (byte) 3, (byte) 4);
124 }
125 }
126 }
127