@Test public void testHashCode2() { // ハッシュ値がばらけていることの確認 Set<Integer> hashCodes = Color.stream() .flatMap(c -> Point.stream().map(p -> Move.of(c, p).hashCode())) .collect(Collectors.toSet()); hashCodes.add(Move.passOf(Color.BLACK).hashCode()); hashCodes.add(Move.passOf(Color.WHITE).hashCode()); assertThat(hashCodes.size(), is(Color.values().length * (Point.values().length + 1))); }
@Test public void testPassOf() { Color.stream() .forEach( c -> { assertThat(Move.passOf(c), instanceOf(Move.class)); assertThat(Move.passOf(c).color, theInstance(c)); assertThat(Move.passOf(c).point, nullValue()); assertThat(Move.passOf(c), is(Move.of(c, null))); }); assertThat(of(() -> Move.passOf(null)), raise(NullPointerException.class)); }
@Test public void testEquals() { Color.stream() .forEach( c -> Point.stream() .forEach( p -> { Move move1 = Move.of(c, p); Move move2 = Move.of(c, p); assertThat(move1, not(sameInstance(move2))); assertThat(move1.equals(move1), is(true)); assertThat(move1.equals(move2), is(true)); assertThat(move1.equals(Move.of(c.opposite(), p)), is(false)); assertThat(move1.equals(Move.of(c, null)), is(false)); assertThat(Move.of(c, null).equals(move1), is(false)); assertThat( move1.equals( Move.of( c, p == Point.of(0, 0) ? Point.of(1, 1) : Point.of(0, 0))), is(false)); assertThat(move1.equals(null), is(false)); assertThat(move1.equals(c), is(false)); assertThat(move1.equals(p), is(false)); })); Color.stream() .forEach( c -> { assertThat(Move.of(c, null).equals(Move.of(c, null)), is(true)); assertThat(Move.passOf(c).equals(Move.passOf(c)), is(true)); assertThat(Move.of(c, null).equals(Move.passOf(c)), is(true)); assertThat(Move.passOf(c).equals(Move.of(c, null)), is(true)); }); }