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