| 474 | } |
| 475 | |
| 476 | @Test void testCons() { |
| 477 | final List<String> abc0 = Arrays.asList("a", "b", "c"); |
| 478 | |
| 479 | final List<String> abc = ConsList.of("a", ImmutableList.of("b", "c")); |
| 480 | assertThat(abc, hasSize(3)); |
| 481 | assertThat(abc, is(abc0)); |
| 482 | |
| 483 | final List<String> bc = Lists.newArrayList("b", "c"); |
| 484 | final List<String> abc2 = ConsList.of("a", bc); |
| 485 | assertThat(abc2, hasSize(3)); |
| 486 | assertThat(abc2, is(abc0)); |
| 487 | bc.set(0, "z"); |
| 488 | assertThat(abc2, is(abc0)); |
| 489 | |
| 490 | final List<String> bc3 = ConsList.of("b", Collections.singletonList("c")); |
| 491 | final List<String> abc3 = ConsList.of("a", bc3); |
| 492 | assertThat(abc3, hasSize(3)); |
| 493 | assertThat(abc3, is(abc0)); |
| 494 | assertThat(abc3.indexOf("b"), is(1)); |
| 495 | assertThat(abc3.indexOf("z"), is(-1)); |
| 496 | assertThat(abc3.lastIndexOf("b"), is(1)); |
| 497 | assertThat(abc3.lastIndexOf("z"), is(-1)); |
| 498 | assertThat(abc3.hashCode(), is(abc0.hashCode())); |
| 499 | |
| 500 | assertThat(abc3.get(0), is("a")); |
| 501 | assertThat(abc3.get(1), is("b")); |
| 502 | assertThat(abc3.get(2), is("c")); |
| 503 | try { |
| 504 | final String z = abc3.get(3); |
| 505 | fail("expected error, got " + z); |
| 506 | } catch (IndexOutOfBoundsException e) { |
| 507 | // ok |
| 508 | } |
| 509 | try { |
| 510 | final String z = abc3.get(-3); |
| 511 | fail("expected error, got " + z); |
| 512 | } catch (IndexOutOfBoundsException e) { |
| 513 | // ok |
| 514 | } |
| 515 | try { |
| 516 | final String z = abc3.get(30); |
| 517 | fail("expected error, got " + z); |
| 518 | } catch (IndexOutOfBoundsException e) { |
| 519 | // ok |
| 520 | } |
| 521 | |
| 522 | final List<String> a = ConsList.of("a", ImmutableList.of()); |
| 523 | assertThat(a, hasSize(1)); |
| 524 | assertThat(a, isListOf("a")); |
| 525 | } |
| 526 | |
| 527 | @Test void testConsPerformance() { |
| 528 | final int n = 2000000; |