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