1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
|
/*
* Copyright (C) 2008 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.primitives;
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.collect.testing.Helpers;
import com.google.common.testing.NullPointerTester;
import com.google.common.testing.SerializableTester;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import junit.framework.TestCase;
/**
* Unit test for {@link Chars}.
*
* @author Kevin Bourrillion
*/
@GwtCompatible(emulated = true)
@SuppressWarnings("cast") // redundant casts are intentional and harmless
public class CharsTest extends TestCase {
private static final char[] EMPTY = {};
private static final char[] ARRAY1 = {(char) 1};
private static final char[] ARRAY234 = {(char) 2, (char) 3, (char) 4};
private static final char LEAST = Character.MIN_VALUE;
private static final char GREATEST = Character.MAX_VALUE;
private static final char[] VALUES = {LEAST, 'a', '\u00e0', '\udcaa', GREATEST};
public void testHashCode() {
for (char value : VALUES) {
assertEquals(((Character) value).hashCode(), Chars.hashCode(value));
}
}
public void testCheckedCast() {
for (char value : VALUES) {
assertEquals(value, Chars.checkedCast((long) value));
}
assertCastFails(GREATEST + 1L);
assertCastFails(LEAST - 1L);
assertCastFails(Long.MAX_VALUE);
assertCastFails(Long.MIN_VALUE);
}
public void testSaturatedCast() {
for (char value : VALUES) {
assertEquals(value, Chars.saturatedCast((long) value));
}
assertEquals(GREATEST, Chars.saturatedCast(GREATEST + 1L));
assertEquals(LEAST, Chars.saturatedCast(LEAST - 1L));
assertEquals(GREATEST, Chars.saturatedCast(Long.MAX_VALUE));
assertEquals(LEAST, Chars.saturatedCast(Long.MIN_VALUE));
}
private void assertCastFails(long value) {
try {
Chars.checkedCast(value);
fail("Cast to char should have failed: " + value);
} catch (IllegalArgumentException ex) {
assertTrue(
value + " not found in exception text: " + ex.getMessage(),
ex.getMessage().contains(String.valueOf(value)));
}
}
public void testCompare() {
for (char x : VALUES) {
for (char y : VALUES) {
// note: spec requires only that the sign is the same
assertEquals(x + ", " + y, Character.valueOf(x).compareTo(y), Chars.compare(x, y));
}
}
}
public void testContains() {
assertFalse(Chars.contains(EMPTY, (char) 1));
assertFalse(Chars.contains(ARRAY1, (char) 2));
assertFalse(Chars.contains(ARRAY234, (char) 1));
assertTrue(Chars.contains(new char[] {(char) -1}, (char) -1));
assertTrue(Chars.contains(ARRAY234, (char) 2));
assertTrue(Chars.contains(ARRAY234, (char) 3));
assertTrue(Chars.contains(ARRAY234, (char) 4));
}
public void testIndexOf() {
assertEquals(-1, Chars.indexOf(EMPTY, (char) 1));
assertEquals(-1, Chars.indexOf(ARRAY1, (char) 2));
assertEquals(-1, Chars.indexOf(ARRAY234, (char) 1));
assertEquals(0, Chars.indexOf(new char[] {(char) -1}, (char) -1));
assertEquals(0, Chars.indexOf(ARRAY234, (char) 2));
assertEquals(1, Chars.indexOf(ARRAY234, (char) 3));
assertEquals(2, Chars.indexOf(ARRAY234, (char) 4));
assertEquals(1, Chars.indexOf(new char[] {(char) 2, (char) 3, (char) 2, (char) 3}, (char) 3));
}
public void testIndexOf_arrayTarget() {
assertEquals(0, Chars.indexOf(EMPTY, EMPTY));
assertEquals(0, Chars.indexOf(ARRAY234, EMPTY));
assertEquals(-1, Chars.indexOf(EMPTY, ARRAY234));
assertEquals(-1, Chars.indexOf(ARRAY234, ARRAY1));
assertEquals(-1, Chars.indexOf(ARRAY1, ARRAY234));
assertEquals(0, Chars.indexOf(ARRAY1, ARRAY1));
assertEquals(0, Chars.indexOf(ARRAY234, ARRAY234));
assertEquals(0, Chars.indexOf(ARRAY234, new char[] {(char) 2, (char) 3}));
assertEquals(1, Chars.indexOf(ARRAY234, new char[] {(char) 3, (char) 4}));
assertEquals(1, Chars.indexOf(ARRAY234, new char[] {(char) 3}));
assertEquals(2, Chars.indexOf(ARRAY234, new char[] {(char) 4}));
assertEquals(
1,
Chars.indexOf(
new char[] {(char) 2, (char) 3, (char) 3, (char) 3, (char) 3}, new char[] {(char) 3}));
assertEquals(
2,
Chars.indexOf(
new char[] {(char) 2, (char) 3, (char) 2, (char) 3, (char) 4, (char) 2, (char) 3},
new char[] {(char) 2, (char) 3, (char) 4}));
assertEquals(
1,
Chars.indexOf(
new char[] {(char) 2, (char) 2, (char) 3, (char) 4, (char) 2, (char) 3, (char) 4},
new char[] {(char) 2, (char) 3, (char) 4}));
assertEquals(
-1,
Chars.indexOf(
new char[] {(char) 4, (char) 3, (char) 2}, new char[] {(char) 2, (char) 3, (char) 4}));
}
public void testLastIndexOf() {
assertEquals(-1, Chars.lastIndexOf(EMPTY, (char) 1));
assertEquals(-1, Chars.lastIndexOf(ARRAY1, (char) 2));
assertEquals(-1, Chars.lastIndexOf(ARRAY234, (char) 1));
assertEquals(0, Chars.lastIndexOf(new char[] {(char) -1}, (char) -1));
assertEquals(0, Chars.lastIndexOf(ARRAY234, (char) 2));
assertEquals(1, Chars.lastIndexOf(ARRAY234, (char) 3));
assertEquals(2, Chars.lastIndexOf(ARRAY234, (char) 4));
assertEquals(
3, Chars.lastIndexOf(new char[] {(char) 2, (char) 3, (char) 2, (char) 3}, (char) 3));
}
public void testMax_noArgs() {
try {
Chars.max();
fail();
} catch (IllegalArgumentException expected) {
}
}
public void testMax() {
assertEquals(LEAST, Chars.max(LEAST));
assertEquals(GREATEST, Chars.max(GREATEST));
assertEquals(
(char) 9, Chars.max((char) 8, (char) 6, (char) 7, (char) 5, (char) 3, (char) 0, (char) 9));
}
public void testMin_noArgs() {
try {
Chars.min();
fail();
} catch (IllegalArgumentException expected) {
}
}
public void testMin() {
assertEquals(LEAST, Chars.min(LEAST));
assertEquals(GREATEST, Chars.min(GREATEST));
assertEquals(
(char) 0, Chars.min((char) 8, (char) 6, (char) 7, (char) 5, (char) 3, (char) 0, (char) 9));
}
public void testConstrainToRange() {
assertEquals((char) 1, Chars.constrainToRange((char) 1, (char) 0, (char) 5));
assertEquals((char) 1, Chars.constrainToRange((char) 1, (char) 1, (char) 5));
assertEquals((char) 3, Chars.constrainToRange((char) 1, (char) 3, (char) 5));
assertEquals((char) 254, Chars.constrainToRange((char) 255, (char) 250, (char) 254));
assertEquals((char) 2, Chars.constrainToRange((char) 5, (char) 2, (char) 2));
try {
Chars.constrainToRange((char) 1, (char) 3, (char) 2);
fail();
} catch (IllegalArgumentException expected) {
}
}
public void testConcat() {
assertTrue(Arrays.equals(EMPTY, Chars.concat()));
assertTrue(Arrays.equals(EMPTY, Chars.concat(EMPTY)));
assertTrue(Arrays.equals(EMPTY, Chars.concat(EMPTY, EMPTY, EMPTY)));
assertTrue(Arrays.equals(ARRAY1, Chars.concat(ARRAY1)));
assertNotSame(ARRAY1, Chars.concat(ARRAY1));
assertTrue(Arrays.equals(ARRAY1, Chars.concat(EMPTY, ARRAY1, EMPTY)));
assertTrue(
Arrays.equals(
new char[] {(char) 1, (char) 1, (char) 1}, Chars.concat(ARRAY1, ARRAY1, ARRAY1)));
assertTrue(
Arrays.equals(
new char[] {(char) 1, (char) 2, (char) 3, (char) 4}, Chars.concat(ARRAY1, ARRAY234)));
}
@GwtIncompatible // Chars.fromByteArray
public void testFromByteArray() {
assertEquals('\u2345', Chars.fromByteArray(new byte[] {0x23, 0x45, (byte) 0xDC}));
assertEquals('\uFEDC', Chars.fromByteArray(new byte[] {(byte) 0xFE, (byte) 0xDC}));
}
@GwtIncompatible // Chars.fromByteArray
public void testFromByteArrayFails() {
try {
Chars.fromByteArray(new byte[Chars.BYTES - 1]);
fail();
} catch (IllegalArgumentException expected) {
}
}
@GwtIncompatible // Chars.fromBytes
public void testFromBytes() {
assertEquals('\u2345', Chars.fromBytes((byte) 0x23, (byte) 0x45));
assertEquals('\uFEDC', Chars.fromBytes((byte) 0xFE, (byte) 0xDC));
}
@GwtIncompatible // Chars.fromByteArray, Chars.toByteArray
public void testByteArrayRoundTrips() {
char c = 0;
for (int hi = 0; hi < 256; hi++) {
for (int lo = 0; lo < 256; lo++) {
char result = Chars.fromByteArray(new byte[] {(byte) hi, (byte) lo});
assertEquals(
String.format(
Locale.ROOT, "hi=%s, lo=%s, expected=%s, result=%s", hi, lo, (int) c, (int) result),
c,
result);
byte[] bytes = Chars.toByteArray(c);
assertEquals((byte) hi, bytes[0]);
assertEquals((byte) lo, bytes[1]);
c++;
}
}
assertEquals((char) 0, c); // sanity check
}
@GwtIncompatible // Chars.fromByteArray, Chars.toByteArray
public void testByteArrayRoundTripsFails() {
try {
Chars.fromByteArray(new byte[] {0x11});
fail();
} catch (IllegalArgumentException expected) {
}
}
public void testEnsureCapacity() {
assertSame(EMPTY, Chars.ensureCapacity(EMPTY, 0, 1));
assertSame(ARRAY1, Chars.ensureCapacity(ARRAY1, 0, 1));
assertSame(ARRAY1, Chars.ensureCapacity(ARRAY1, 1, 1));
assertTrue(
Arrays.equals(
new char[] {(char) 1, (char) 0, (char) 0}, Chars.ensureCapacity(ARRAY1, 2, 1)));
}
public void testEnsureCapacity_fail() {
try {
Chars.ensureCapacity(ARRAY1, -1, 1);
fail();
} catch (IllegalArgumentException expected) {
}
try {
// notice that this should even fail when no growth was needed
Chars.ensureCapacity(ARRAY1, 1, -1);
fail();
} catch (IllegalArgumentException expected) {
}
}
public void testJoin() {
assertEquals("", Chars.join(",", EMPTY));
assertEquals("1", Chars.join(",", '1'));
assertEquals("1,2", Chars.join(",", '1', '2'));
assertEquals("123", Chars.join("", '1', '2', '3'));
}
public void testLexicographicalComparator() {
List<char[]> ordered =
Arrays.asList(
new char[] {},
new char[] {LEAST},
new char[] {LEAST, LEAST},
new char[] {LEAST, (char) 1},
new char[] {(char) 1},
new char[] {(char) 1, LEAST},
new char[] {GREATEST, GREATEST - (char) 1},
new char[] {GREATEST, GREATEST},
new char[] {GREATEST, GREATEST, GREATEST});
Comparator<char[]> comparator = Chars.lexicographicalComparator();
Helpers.testComparator(comparator, ordered);
}
@GwtIncompatible // SerializableTester
public void testLexicographicalComparatorSerializable() {
Comparator<char[]> comparator = Chars.lexicographicalComparator();
assertSame(comparator, SerializableTester.reserialize(comparator));
}
public void testReverse() {
testReverse(new char[] {}, new char[] {});
testReverse(new char[] {'1'}, new char[] {'1'});
testReverse(new char[] {'1', '2'}, new char[] {'2', '1'});
testReverse(new char[] {'3', '1', '1'}, new char[] {'1', '1', '3'});
testReverse(new char[] {'A', '1', 'B', '2'}, new char[] {'2', 'B', '1', 'A'});
}
private static void testReverse(char[] input, char[] expectedOutput) {
input = Arrays.copyOf(input, input.length);
Chars.reverse(input);
assertTrue(Arrays.equals(expectedOutput, input));
}
private static void testReverse(char[] input, int fromIndex, int toIndex, char[] expectedOutput) {
input = Arrays.copyOf(input, input.length);
Chars.reverse(input, fromIndex, toIndex);
assertTrue(Arrays.equals(expectedOutput, input));
}
public void testReverseIndexed() {
testReverse(new char[] {}, 0, 0, new char[] {});
testReverse(new char[] {'1'}, 0, 1, new char[] {'1'});
testReverse(new char[] {'1', '2'}, 0, 2, new char[] {'2', '1'});
testReverse(new char[] {'3', '1', '1'}, 0, 2, new char[] {'1', '3', '1'});
testReverse(new char[] {'3', '1', '1'}, 0, 1, new char[] {'3', '1', '1'});
testReverse(new char[] {'A', '1', 'B', '2'}, 1, 3, new char[] {'A', 'B', '1', '2'});
}
public void testSortDescending() {
testSortDescending(new char[] {}, new char[] {});
testSortDescending(new char[] {'1'}, new char[] {'1'});
testSortDescending(new char[] {'1', '2'}, new char[] {'2', '1'});
testSortDescending(new char[] {'1', '3', '1'}, new char[] {'3', '1', '1'});
testSortDescending(new char[] {'A', '1', 'B', '2'}, new char[] {'B', 'A', '2', '1'});
}
private static void testSortDescending(char[] input, char[] expectedOutput) {
input = Arrays.copyOf(input, input.length);
Chars.sortDescending(input);
assertTrue(Arrays.equals(expectedOutput, input));
}
private static void testSortDescending(
char[] input, int fromIndex, int toIndex, char[] expectedOutput) {
input = Arrays.copyOf(input, input.length);
Chars.sortDescending(input, fromIndex, toIndex);
assertTrue(Arrays.equals(expectedOutput, input));
}
public void testSortDescendingIndexed() {
testSortDescending(new char[] {}, 0, 0, new char[] {});
testSortDescending(new char[] {'1'}, 0, 1, new char[] {'1'});
testSortDescending(new char[] {'1', '2'}, 0, 2, new char[] {'2', '1'});
testSortDescending(new char[] {'1', '3', '1'}, 0, 2, new char[] {'3', '1', '1'});
testSortDescending(new char[] {'1', '3', '1'}, 0, 1, new char[] {'1', '3', '1'});
testSortDescending(new char[] {'A', '1', 'B', '2'}, 1, 3, new char[] {'A', 'B', '1', '2'});
}
public void testToArray() {
// need explicit type parameter to avoid javac warning!?
List<Character> none = Arrays.<Character>asList();
assertTrue(Arrays.equals(EMPTY, Chars.toArray(none)));
List<Character> one = Arrays.asList((char) 1);
assertTrue(Arrays.equals(ARRAY1, Chars.toArray(one)));
char[] array = {(char) 0, (char) 1, 'A'};
List<Character> three = Arrays.asList((char) 0, (char) 1, 'A');
assertTrue(Arrays.equals(array, Chars.toArray(three)));
assertTrue(Arrays.equals(array, Chars.toArray(Chars.asList(array))));
}
public void testToArray_threadSafe() {
for (int delta : new int[] {+1, 0, -1}) {
for (int i = 0; i < VALUES.length; i++) {
List<Character> list = Chars.asList(VALUES).subList(0, i);
Collection<Character> misleadingSize = Helpers.misleadingSizeCollection(delta);
misleadingSize.addAll(list);
char[] arr = Chars.toArray(misleadingSize);
assertEquals(i, arr.length);
for (int j = 0; j < i; j++) {
assertEquals(VALUES[j], arr[j]);
}
}
}
}
public void testToArray_withNull() {
List<Character> list = Arrays.asList((char) 0, (char) 1, null);
try {
Chars.toArray(list);
fail();
} catch (NullPointerException expected) {
}
}
public void testAsList_isAView() {
char[] array = {(char) 0, (char) 1};
List<Character> list = Chars.asList(array);
list.set(0, (char) 2);
assertTrue(Arrays.equals(new char[] {(char) 2, (char) 1}, array));
array[1] = (char) 3;
assertEquals(Arrays.asList((char) 2, (char) 3), list);
}
public void testAsList_toArray_roundTrip() {
char[] array = {(char) 0, (char) 1, (char) 2};
List<Character> list = Chars.asList(array);
char[] newArray = Chars.toArray(list);
// Make sure it returned a copy
list.set(0, (char) 4);
assertTrue(Arrays.equals(new char[] {(char) 0, (char) 1, (char) 2}, newArray));
newArray[1] = (char) 5;
assertEquals((char) 1, (char) list.get(1));
}
// This test stems from a real bug found by andrewk
public void testAsList_subList_toArray_roundTrip() {
char[] array = {(char) 0, (char) 1, (char) 2, (char) 3};
List<Character> list = Chars.asList(array);
assertTrue(Arrays.equals(new char[] {(char) 1, (char) 2}, Chars.toArray(list.subList(1, 3))));
assertTrue(Arrays.equals(new char[] {}, Chars.toArray(list.subList(2, 2))));
}
public void testAsListEmpty() {
assertSame(Collections.emptyList(), Chars.asList(EMPTY));
}
@GwtIncompatible // NullPointerTester
public void testNulls() {
new NullPointerTester().testAllPublicStaticMethods(Chars.class);
}
}
|