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 com.google.common.annotations.GwtCompatible; 20 import com.google.common.annotations.GwtIncompatible; 21 import com.google.common.base.Converter; 22 import com.google.common.collect.testing.Helpers; 23 import com.google.common.testing.NullPointerTester; 24 import com.google.common.testing.SerializableTester; 25 import java.util.Arrays; 26 import java.util.Collection; 27 import java.util.Collections; 28 import java.util.Comparator; 29 import java.util.List; 30 import java.util.Random; 31 import junit.framework.TestCase; 32 33 /** 34 * Unit test for {@link Shorts}. 35 * 36 * @author Kevin Bourrillion 37 */ 38 @GwtCompatible(emulated = true) 39 @SuppressWarnings("cast") // redundant casts are intentional and harmless 40 public class ShortsTest extends TestCase { 41 private static final short[] EMPTY = {}; 42 private static final short[] ARRAY1 = {(short) 1}; 43 private static final short[] ARRAY234 = {(short) 2, (short) 3, (short) 4}; 44 45 private static final short LEAST = Short.MIN_VALUE; 46 private static final short GREATEST = Short.MAX_VALUE; 47 48 private static final short[] VALUES = {LEAST, (short) -1, (short) 0, (short) 1, GREATEST}; 49 testHashCode()50 public void testHashCode() { 51 for (short value : VALUES) { 52 assertEquals(((Short) value).hashCode(), Shorts.hashCode(value)); 53 } 54 } 55 testCheckedCast()56 public void testCheckedCast() { 57 for (short value : VALUES) { 58 assertEquals(value, Shorts.checkedCast((long) value)); 59 } 60 assertCastFails(GREATEST + 1L); 61 assertCastFails(LEAST - 1L); 62 assertCastFails(Long.MAX_VALUE); 63 assertCastFails(Long.MIN_VALUE); 64 } 65 testSaturatedCast()66 public void testSaturatedCast() { 67 for (short value : VALUES) { 68 assertEquals(value, Shorts.saturatedCast((long) value)); 69 } 70 assertEquals(GREATEST, Shorts.saturatedCast(GREATEST + 1L)); 71 assertEquals(LEAST, Shorts.saturatedCast(LEAST - 1L)); 72 assertEquals(GREATEST, Shorts.saturatedCast(Long.MAX_VALUE)); 73 assertEquals(LEAST, Shorts.saturatedCast(Long.MIN_VALUE)); 74 } 75 assertCastFails(long value)76 private static void assertCastFails(long value) { 77 try { 78 Shorts.checkedCast(value); 79 fail("Cast to short should have failed: " + value); 80 } catch (IllegalArgumentException ex) { 81 assertTrue( 82 value + " not found in exception text: " + ex.getMessage(), 83 ex.getMessage().contains(String.valueOf(value))); 84 } 85 } 86 testCompare()87 public void testCompare() { 88 for (short x : VALUES) { 89 for (short y : VALUES) { 90 // Only compare the sign of the result of compareTo(). 91 int expected = Short.valueOf(x).compareTo(y); 92 int actual = Shorts.compare(x, y); 93 if (expected == 0) { 94 assertEquals(x + ", " + y, expected, actual); 95 } else if (expected < 0) { 96 assertTrue( 97 x + ", " + y + " (expected: " + expected + ", actual" + actual + ")", actual < 0); 98 } else { 99 assertTrue( 100 x + ", " + y + " (expected: " + expected + ", actual" + actual + ")", actual > 0); 101 } 102 } 103 } 104 } 105 testContains()106 public void testContains() { 107 assertFalse(Shorts.contains(EMPTY, (short) 1)); 108 assertFalse(Shorts.contains(ARRAY1, (short) 2)); 109 assertFalse(Shorts.contains(ARRAY234, (short) 1)); 110 assertTrue(Shorts.contains(new short[] {(short) -1}, (short) -1)); 111 assertTrue(Shorts.contains(ARRAY234, (short) 2)); 112 assertTrue(Shorts.contains(ARRAY234, (short) 3)); 113 assertTrue(Shorts.contains(ARRAY234, (short) 4)); 114 } 115 testIndexOf()116 public void testIndexOf() { 117 assertEquals(-1, Shorts.indexOf(EMPTY, (short) 1)); 118 assertEquals(-1, Shorts.indexOf(ARRAY1, (short) 2)); 119 assertEquals(-1, Shorts.indexOf(ARRAY234, (short) 1)); 120 assertEquals(0, Shorts.indexOf(new short[] {(short) -1}, (short) -1)); 121 assertEquals(0, Shorts.indexOf(ARRAY234, (short) 2)); 122 assertEquals(1, Shorts.indexOf(ARRAY234, (short) 3)); 123 assertEquals(2, Shorts.indexOf(ARRAY234, (short) 4)); 124 assertEquals( 125 1, Shorts.indexOf(new short[] {(short) 2, (short) 3, (short) 2, (short) 3}, (short) 3)); 126 } 127 testIndexOf_arrayTarget()128 public void testIndexOf_arrayTarget() { 129 assertEquals(0, Shorts.indexOf(EMPTY, EMPTY)); 130 assertEquals(0, Shorts.indexOf(ARRAY234, EMPTY)); 131 assertEquals(-1, Shorts.indexOf(EMPTY, ARRAY234)); 132 assertEquals(-1, Shorts.indexOf(ARRAY234, ARRAY1)); 133 assertEquals(-1, Shorts.indexOf(ARRAY1, ARRAY234)); 134 assertEquals(0, Shorts.indexOf(ARRAY1, ARRAY1)); 135 assertEquals(0, Shorts.indexOf(ARRAY234, ARRAY234)); 136 assertEquals(0, Shorts.indexOf(ARRAY234, new short[] {(short) 2, (short) 3})); 137 assertEquals(1, Shorts.indexOf(ARRAY234, new short[] {(short) 3, (short) 4})); 138 assertEquals(1, Shorts.indexOf(ARRAY234, new short[] {(short) 3})); 139 assertEquals(2, Shorts.indexOf(ARRAY234, new short[] {(short) 4})); 140 assertEquals( 141 1, 142 Shorts.indexOf( 143 new short[] {(short) 2, (short) 3, (short) 3, (short) 3, (short) 3}, 144 new short[] {(short) 3})); 145 assertEquals( 146 2, 147 Shorts.indexOf( 148 new short[] { 149 (short) 2, (short) 3, (short) 2, (short) 3, (short) 4, (short) 2, (short) 3 150 }, 151 new short[] {(short) 2, (short) 3, (short) 4})); 152 assertEquals( 153 1, 154 Shorts.indexOf( 155 new short[] { 156 (short) 2, (short) 2, (short) 3, (short) 4, (short) 2, (short) 3, (short) 4 157 }, 158 new short[] {(short) 2, (short) 3, (short) 4})); 159 assertEquals( 160 -1, 161 Shorts.indexOf( 162 new short[] {(short) 4, (short) 3, (short) 2}, 163 new short[] {(short) 2, (short) 3, (short) 4})); 164 } 165 testLastIndexOf()166 public void testLastIndexOf() { 167 assertEquals(-1, Shorts.lastIndexOf(EMPTY, (short) 1)); 168 assertEquals(-1, Shorts.lastIndexOf(ARRAY1, (short) 2)); 169 assertEquals(-1, Shorts.lastIndexOf(ARRAY234, (short) 1)); 170 assertEquals(0, Shorts.lastIndexOf(new short[] {(short) -1}, (short) -1)); 171 assertEquals(0, Shorts.lastIndexOf(ARRAY234, (short) 2)); 172 assertEquals(1, Shorts.lastIndexOf(ARRAY234, (short) 3)); 173 assertEquals(2, Shorts.lastIndexOf(ARRAY234, (short) 4)); 174 assertEquals( 175 3, Shorts.lastIndexOf(new short[] {(short) 2, (short) 3, (short) 2, (short) 3}, (short) 3)); 176 } 177 178 @GwtIncompatible testMax_noArgs()179 public void testMax_noArgs() { 180 try { 181 Shorts.max(); 182 fail(); 183 } catch (IllegalArgumentException expected) { 184 } 185 } 186 testMax()187 public void testMax() { 188 assertEquals(LEAST, Shorts.max(LEAST)); 189 assertEquals(GREATEST, Shorts.max(GREATEST)); 190 assertEquals( 191 (short) 9, 192 Shorts.max((short) 8, (short) 6, (short) 7, (short) 5, (short) 3, (short) 0, (short) 9)); 193 } 194 195 @GwtIncompatible testMin_noArgs()196 public void testMin_noArgs() { 197 try { 198 Shorts.min(); 199 fail(); 200 } catch (IllegalArgumentException expected) { 201 } 202 } 203 testMin()204 public void testMin() { 205 assertEquals(LEAST, Shorts.min(LEAST)); 206 assertEquals(GREATEST, Shorts.min(GREATEST)); 207 assertEquals( 208 (short) 0, 209 Shorts.min((short) 8, (short) 6, (short) 7, (short) 5, (short) 3, (short) 0, (short) 9)); 210 } 211 testConstrainToRange()212 public void testConstrainToRange() { 213 assertEquals((short) 1, Shorts.constrainToRange((short) 1, (short) 0, (short) 5)); 214 assertEquals((short) 1, Shorts.constrainToRange((short) 1, (short) 1, (short) 5)); 215 assertEquals((short) 3, Shorts.constrainToRange((short) 1, (short) 3, (short) 5)); 216 assertEquals((short) -1, Shorts.constrainToRange((short) 0, (short) -5, (short) -1)); 217 assertEquals((short) 2, Shorts.constrainToRange((short) 5, (short) 2, (short) 2)); 218 try { 219 Shorts.constrainToRange((short) 1, (short) 3, (short) 2); 220 fail(); 221 } catch (IllegalArgumentException expected) { 222 } 223 } 224 testConcat()225 public void testConcat() { 226 assertTrue(Arrays.equals(EMPTY, Shorts.concat())); 227 assertTrue(Arrays.equals(EMPTY, Shorts.concat(EMPTY))); 228 assertTrue(Arrays.equals(EMPTY, Shorts.concat(EMPTY, EMPTY, EMPTY))); 229 assertTrue(Arrays.equals(ARRAY1, Shorts.concat(ARRAY1))); 230 assertNotSame(ARRAY1, Shorts.concat(ARRAY1)); 231 assertTrue(Arrays.equals(ARRAY1, Shorts.concat(EMPTY, ARRAY1, EMPTY))); 232 assertTrue( 233 Arrays.equals( 234 new short[] {(short) 1, (short) 1, (short) 1}, Shorts.concat(ARRAY1, ARRAY1, ARRAY1))); 235 assertTrue( 236 Arrays.equals( 237 new short[] {(short) 1, (short) 2, (short) 3, (short) 4}, 238 Shorts.concat(ARRAY1, ARRAY234))); 239 } 240 241 @GwtIncompatible // Shorts.toByteArray testToByteArray()242 public void testToByteArray() { 243 assertTrue(Arrays.equals(new byte[] {0x23, 0x45}, Shorts.toByteArray((short) 0x2345))); 244 assertTrue( 245 Arrays.equals(new byte[] {(byte) 0xFE, (byte) 0xDC}, Shorts.toByteArray((short) 0xFEDC))); 246 } 247 248 @GwtIncompatible // Shorts.fromByteArray testFromByteArray()249 public void testFromByteArray() { 250 assertEquals((short) 0x2345, Shorts.fromByteArray(new byte[] {0x23, 0x45})); 251 assertEquals((short) 0xFEDC, Shorts.fromByteArray(new byte[] {(byte) 0xFE, (byte) 0xDC})); 252 } 253 254 @GwtIncompatible // Shorts.fromByteArray testFromByteArrayFails()255 public void testFromByteArrayFails() { 256 try { 257 Shorts.fromByteArray(new byte[] {0x01}); 258 fail(); 259 } catch (IllegalArgumentException expected) { 260 } 261 } 262 263 @GwtIncompatible // Shorts.fromBytes testFromBytes()264 public void testFromBytes() { 265 assertEquals((short) 0x2345, Shorts.fromBytes((byte) 0x23, (byte) 0x45)); 266 assertEquals((short) 0xFEDC, Shorts.fromBytes((byte) 0xFE, (byte) 0xDC)); 267 } 268 269 @GwtIncompatible // Shorts.fromByteArray, Shorts.toByteArray testByteArrayRoundTrips()270 public void testByteArrayRoundTrips() { 271 Random r = new Random(5); 272 byte[] b = new byte[Shorts.BYTES]; 273 274 // total overkill, but, it takes 0.1 sec so why not... 275 for (int i = 0; i < 10000; i++) { 276 short num = (short) r.nextInt(); 277 assertEquals(num, Shorts.fromByteArray(Shorts.toByteArray(num))); 278 279 r.nextBytes(b); 280 assertTrue(Arrays.equals(b, Shorts.toByteArray(Shorts.fromByteArray(b)))); 281 } 282 } 283 testEnsureCapacity()284 public void testEnsureCapacity() { 285 assertSame(EMPTY, Shorts.ensureCapacity(EMPTY, 0, 1)); 286 assertSame(ARRAY1, Shorts.ensureCapacity(ARRAY1, 0, 1)); 287 assertSame(ARRAY1, Shorts.ensureCapacity(ARRAY1, 1, 1)); 288 assertTrue( 289 Arrays.equals( 290 new short[] {(short) 1, (short) 0, (short) 0}, Shorts.ensureCapacity(ARRAY1, 2, 1))); 291 } 292 testEnsureCapacity_fail()293 public void testEnsureCapacity_fail() { 294 try { 295 Shorts.ensureCapacity(ARRAY1, -1, 1); 296 fail(); 297 } catch (IllegalArgumentException expected) { 298 } 299 try { 300 // notice that this should even fail when no growth was needed 301 Shorts.ensureCapacity(ARRAY1, 1, -1); 302 fail(); 303 } catch (IllegalArgumentException expected) { 304 } 305 } 306 testJoin()307 public void testJoin() { 308 assertEquals("", Shorts.join(",", EMPTY)); 309 assertEquals("1", Shorts.join(",", ARRAY1)); 310 assertEquals("1,2", Shorts.join(",", (short) 1, (short) 2)); 311 assertEquals("123", Shorts.join("", (short) 1, (short) 2, (short) 3)); 312 } 313 testLexicographicalComparator()314 public void testLexicographicalComparator() { 315 List<short[]> ordered = 316 Arrays.asList( 317 new short[] {}, 318 new short[] {LEAST}, 319 new short[] {LEAST, LEAST}, 320 new short[] {LEAST, (short) 1}, 321 new short[] {(short) 1}, 322 new short[] {(short) 1, LEAST}, 323 new short[] {GREATEST, GREATEST - (short) 1}, 324 new short[] {GREATEST, GREATEST}, 325 new short[] {GREATEST, GREATEST, GREATEST}); 326 327 Comparator<short[]> comparator = Shorts.lexicographicalComparator(); 328 Helpers.testComparator(comparator, ordered); 329 } 330 331 @GwtIncompatible // SerializableTester testLexicographicalComparatorSerializable()332 public void testLexicographicalComparatorSerializable() { 333 Comparator<short[]> comparator = Shorts.lexicographicalComparator(); 334 assertSame(comparator, SerializableTester.reserialize(comparator)); 335 } 336 testReverse()337 public void testReverse() { 338 testReverse(new short[] {}, new short[] {}); 339 testReverse(new short[] {1}, new short[] {1}); 340 testReverse(new short[] {1, 2}, new short[] {2, 1}); 341 testReverse(new short[] {3, 1, 1}, new short[] {1, 1, 3}); 342 testReverse(new short[] {-1, 1, -2, 2}, new short[] {2, -2, 1, -1}); 343 } 344 testReverse(short[] input, short[] expectedOutput)345 private static void testReverse(short[] input, short[] expectedOutput) { 346 input = Arrays.copyOf(input, input.length); 347 Shorts.reverse(input); 348 assertTrue(Arrays.equals(expectedOutput, input)); 349 } 350 testReverse( short[] input, int fromIndex, int toIndex, short[] expectedOutput)351 private static void testReverse( 352 short[] input, int fromIndex, int toIndex, short[] expectedOutput) { 353 input = Arrays.copyOf(input, input.length); 354 Shorts.reverse(input, fromIndex, toIndex); 355 assertTrue(Arrays.equals(expectedOutput, input)); 356 } 357 testReverseIndexed()358 public void testReverseIndexed() { 359 testReverse(new short[] {}, 0, 0, new short[] {}); 360 testReverse(new short[] {1}, 0, 1, new short[] {1}); 361 testReverse(new short[] {1, 2}, 0, 2, new short[] {2, 1}); 362 testReverse(new short[] {3, 1, 1}, 0, 2, new short[] {1, 3, 1}); 363 testReverse(new short[] {3, 1, 1}, 0, 1, new short[] {3, 1, 1}); 364 testReverse(new short[] {-1, 1, -2, 2}, 1, 3, new short[] {-1, -2, 1, 2}); 365 } 366 testSortDescending()367 public void testSortDescending() { 368 testSortDescending(new short[] {}, new short[] {}); 369 testSortDescending(new short[] {1}, new short[] {1}); 370 testSortDescending(new short[] {1, 2}, new short[] {2, 1}); 371 testSortDescending(new short[] {1, 3, 1}, new short[] {3, 1, 1}); 372 testSortDescending(new short[] {-1, 1, -2, 2}, new short[] {2, 1, -1, -2}); 373 } 374 testSortDescending(short[] input, short[] expectedOutput)375 private static void testSortDescending(short[] input, short[] expectedOutput) { 376 input = Arrays.copyOf(input, input.length); 377 Shorts.sortDescending(input); 378 assertTrue(Arrays.equals(expectedOutput, input)); 379 } 380 testSortDescending( short[] input, int fromIndex, int toIndex, short[] expectedOutput)381 private static void testSortDescending( 382 short[] input, int fromIndex, int toIndex, short[] expectedOutput) { 383 input = Arrays.copyOf(input, input.length); 384 Shorts.sortDescending(input, fromIndex, toIndex); 385 assertTrue(Arrays.equals(expectedOutput, input)); 386 } 387 testSortDescendingIndexed()388 public void testSortDescendingIndexed() { 389 testSortDescending(new short[] {}, 0, 0, new short[] {}); 390 testSortDescending(new short[] {1}, 0, 1, new short[] {1}); 391 testSortDescending(new short[] {1, 2}, 0, 2, new short[] {2, 1}); 392 testSortDescending(new short[] {1, 3, 1}, 0, 2, new short[] {3, 1, 1}); 393 testSortDescending(new short[] {1, 3, 1}, 0, 1, new short[] {1, 3, 1}); 394 testSortDescending(new short[] {-1, -2, 1, 2}, 1, 3, new short[] {-1, 1, -2, 2}); 395 } 396 397 @GwtIncompatible // SerializableTester testStringConverterSerialization()398 public void testStringConverterSerialization() { 399 SerializableTester.reserializeAndAssert(Shorts.stringConverter()); 400 } 401 testToArray()402 public void testToArray() { 403 // need explicit type parameter to avoid javac warning!? 404 List<Short> none = Arrays.<Short>asList(); 405 assertTrue(Arrays.equals(EMPTY, Shorts.toArray(none))); 406 407 List<Short> one = Arrays.asList((short) 1); 408 assertTrue(Arrays.equals(ARRAY1, Shorts.toArray(one))); 409 410 short[] array = {(short) 0, (short) 1, (short) 3}; 411 412 List<Short> three = Arrays.asList((short) 0, (short) 1, (short) 3); 413 assertTrue(Arrays.equals(array, Shorts.toArray(three))); 414 415 assertTrue(Arrays.equals(array, Shorts.toArray(Shorts.asList(array)))); 416 } 417 testToArray_threadSafe()418 public void testToArray_threadSafe() { 419 for (int delta : new int[] {+1, 0, -1}) { 420 for (int i = 0; i < VALUES.length; i++) { 421 List<Short> list = Shorts.asList(VALUES).subList(0, i); 422 Collection<Short> misleadingSize = Helpers.misleadingSizeCollection(delta); 423 misleadingSize.addAll(list); 424 short[] arr = Shorts.toArray(misleadingSize); 425 assertEquals(i, arr.length); 426 for (int j = 0; j < i; j++) { 427 assertEquals(VALUES[j], arr[j]); 428 } 429 } 430 } 431 } 432 testToArray_withNull()433 public void testToArray_withNull() { 434 List<Short> list = Arrays.asList((short) 0, (short) 1, null); 435 try { 436 Shorts.toArray(list); 437 fail(); 438 } catch (NullPointerException expected) { 439 } 440 } 441 testToArray_withConversion()442 public void testToArray_withConversion() { 443 short[] array = {(short) 0, (short) 1, (short) 2}; 444 445 List<Byte> bytes = Arrays.asList((byte) 0, (byte) 1, (byte) 2); 446 List<Short> shorts = Arrays.asList((short) 0, (short) 1, (short) 2); 447 List<Integer> ints = Arrays.asList(0, 1, 2); 448 List<Float> floats = Arrays.asList((float) 0, (float) 1, (float) 2); 449 List<Long> longs = Arrays.asList((long) 0, (long) 1, (long) 2); 450 List<Double> doubles = Arrays.asList((double) 0, (double) 1, (double) 2); 451 452 assertTrue(Arrays.equals(array, Shorts.toArray(bytes))); 453 assertTrue(Arrays.equals(array, Shorts.toArray(shorts))); 454 assertTrue(Arrays.equals(array, Shorts.toArray(ints))); 455 assertTrue(Arrays.equals(array, Shorts.toArray(floats))); 456 assertTrue(Arrays.equals(array, Shorts.toArray(longs))); 457 assertTrue(Arrays.equals(array, Shorts.toArray(doubles))); 458 } 459 testAsList_isAView()460 public void testAsList_isAView() { 461 short[] array = {(short) 0, (short) 1}; 462 List<Short> list = Shorts.asList(array); 463 list.set(0, (short) 2); 464 assertTrue(Arrays.equals(new short[] {(short) 2, (short) 1}, array)); 465 array[1] = (short) 3; 466 assertEquals(Arrays.asList((short) 2, (short) 3), list); 467 } 468 testAsList_toArray_roundTrip()469 public void testAsList_toArray_roundTrip() { 470 short[] array = {(short) 0, (short) 1, (short) 2}; 471 List<Short> list = Shorts.asList(array); 472 short[] newArray = Shorts.toArray(list); 473 474 // Make sure it returned a copy 475 list.set(0, (short) 4); 476 assertTrue(Arrays.equals(new short[] {(short) 0, (short) 1, (short) 2}, newArray)); 477 newArray[1] = (short) 5; 478 assertEquals((short) 1, (short) list.get(1)); 479 } 480 481 // This test stems from a real bug found by andrewk testAsList_subList_toArray_roundTrip()482 public void testAsList_subList_toArray_roundTrip() { 483 short[] array = {(short) 0, (short) 1, (short) 2, (short) 3}; 484 List<Short> list = Shorts.asList(array); 485 assertTrue( 486 Arrays.equals(new short[] {(short) 1, (short) 2}, Shorts.toArray(list.subList(1, 3)))); 487 assertTrue(Arrays.equals(new short[] {}, Shorts.toArray(list.subList(2, 2)))); 488 } 489 testAsListEmpty()490 public void testAsListEmpty() { 491 assertSame(Collections.emptyList(), Shorts.asList(EMPTY)); 492 } 493 494 @GwtIncompatible // NullPointerTester testNulls()495 public void testNulls() { 496 new NullPointerTester().testAllPublicStaticMethods(Shorts.class); 497 } 498 testStringConverter_convert()499 public void testStringConverter_convert() { 500 Converter<String, Short> converter = Shorts.stringConverter(); 501 assertEquals((Short) (short) 1, converter.convert("1")); 502 assertEquals((Short) (short) 0, converter.convert("0")); 503 assertEquals((Short) (short) (-1), converter.convert("-1")); 504 assertEquals((Short) (short) 255, converter.convert("0xff")); 505 assertEquals((Short) (short) 255, converter.convert("0xFF")); 506 assertEquals((Short) (short) (-255), converter.convert("-0xFF")); 507 assertEquals((Short) (short) 255, converter.convert("#0000FF")); 508 assertEquals((Short) (short) 438, converter.convert("0666")); 509 } 510 testStringConverter_convertError()511 public void testStringConverter_convertError() { 512 try { 513 Shorts.stringConverter().convert("notanumber"); 514 fail(); 515 } catch (NumberFormatException expected) { 516 } 517 } 518 testStringConverter_nullConversions()519 public void testStringConverter_nullConversions() { 520 assertNull(Shorts.stringConverter().convert(null)); 521 assertNull(Shorts.stringConverter().reverse().convert(null)); 522 } 523 testStringConverter_reverse()524 public void testStringConverter_reverse() { 525 Converter<String, Short> converter = Shorts.stringConverter(); 526 assertEquals("1", converter.reverse().convert((short) 1)); 527 assertEquals("0", converter.reverse().convert((short) 0)); 528 assertEquals("-1", converter.reverse().convert((short) -1)); 529 assertEquals("255", converter.reverse().convert((short) 0xff)); 530 assertEquals("255", converter.reverse().convert((short) 0xFF)); 531 assertEquals("-255", converter.reverse().convert((short) -0xFF)); 532 assertEquals("438", converter.reverse().convert((short) 0666)); 533 } 534 535 @GwtIncompatible // NullPointerTester testStringConverter_nullPointerTester()536 public void testStringConverter_nullPointerTester() throws Exception { 537 NullPointerTester tester = new NullPointerTester(); 538 tester.testAllPublicInstanceMethods(Shorts.stringConverter()); 539 } 540 } 541