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.collect.ImmutableList; 23 import com.google.common.collect.testing.ListTestSuiteBuilder; 24 import com.google.common.collect.testing.SampleElements; 25 import com.google.common.collect.testing.TestListGenerator; 26 import com.google.common.collect.testing.features.CollectionFeature; 27 import com.google.common.collect.testing.features.CollectionSize; 28 import com.google.common.collect.testing.features.ListFeature; 29 30 import junit.framework.Test; 31 import junit.framework.TestCase; 32 import junit.framework.TestSuite; 33 34 import java.util.List; 35 36 /** 37 * Test suite covering {@link Chars#asList(char[])}. 38 * 39 * @author Kevin Bourrillion 40 */ 41 @GwtCompatible 42 public class CharArrayAsListTest extends TestCase { 43 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 public static Test suite() { 53 List<ListTestSuiteBuilder<Character>> builders = 54 ImmutableList.of( 55 ListTestSuiteBuilder.using(new CharsAsListGenerator()) 56 .named("Chars.asList"), 57 58 ListTestSuiteBuilder.using(new CharsAsListHeadSubListGenerator()) 59 .named("Chars.asList, head subList"), 60 61 ListTestSuiteBuilder.using(new CharsAsListTailSubListGenerator()) 62 .named("Chars.asList, tail subList"), 63 64 ListTestSuiteBuilder.using(new CharsAsListMiddleSubListGenerator()) 65 .named("Chars.asList, middle subList") 66 ); 67 68 TestSuite suite = new TestSuite(); 69 for (ListTestSuiteBuilder<Character> builder : builders) { 70 suite.addTest( 71 builder 72 .withFeatures(CollectionSize.ONE, 73 CollectionSize.SEVERAL, 74 CollectionFeature.RESTRICTS_ELEMENTS, 75 ListFeature.SUPPORTS_SET) 76 .createTestSuite()); 77 } 78 return suite; 79 } 80 81 // Test generators. To let the GWT test suite generator access them, they need to be 82 // public named classes with a public default constructor. 83 84 public static final class CharsAsListGenerator extends TestCharListGenerator { 85 @Override protected List<Character> create(Character[] elements) { 86 return asList(elements); 87 } 88 } 89 90 public static final class CharsAsListHeadSubListGenerator extends TestCharListGenerator { 91 @Override 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 protected List<Character> create(Character[] elements) { 100 Character[] prefix = {(char) 86, (char) 99}; 101 Character[] all = concat(prefix, elements); 102 return asList(all).subList(2, elements.length + 2); 103 } 104 } 105 106 public static final class CharsAsListMiddleSubListGenerator extends TestCharListGenerator { 107 @Override 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 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 static abstract class TestCharListGenerator 123 implements TestListGenerator<Character> { 124 @Override 125 public SampleElements<Character> samples() { 126 return new SampleChars(); 127 } 128 129 @Override 130 public List<Character> create(Object... elements) { 131 Character[] array = new Character[elements.length]; 132 int i = 0; 133 for (Object e : elements) { 134 array[i++] = (Character) e; 135 } 136 return create(array); 137 } 138 139 /** 140 * Creates a new collection containing the given elements; implement this 141 * method instead of {@link #create(Object...)}. 142 */ 143 protected abstract List<Character> create(Character[] elements); 144 145 @Override 146 public Character[] createArray(int length) { 147 return new Character[length]; 148 } 149 150 /** Returns the original element list, unchanged. */ 151 @Override 152 public List<Character> order(List<Character> insertionOrder) { 153 return insertionOrder; 154 } 155 } 156 157 public static class SampleChars extends SampleElements<Character> { 158 public SampleChars() { 159 super((char) 0, (char) 1, (char) 2, (char) 3, (char) 4); 160 } 161 } 162 } 163