@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 testSerializable1() { Color.stream() .forEach( c -> { assertThat(writeAndRead(Move.of(c, null)), is(Move.of(c, null))); Point.stream() .forEach( p -> { assertThat(writeAndRead(Move.of(c, p)), is(Move.of(c, p))); }); }); }
@Test public void testOf() { Color.stream() .forEach( c -> { assertThat(Move.of(c, null), instanceOf(Move.class)); assertThat(Move.of(c, null).color, theInstance(c)); assertThat(Move.of(c, null).point, nullValue()); Point.stream() .forEach( p -> { assertThat(Move.of(c, p), instanceOf(Move.class)); assertThat(Move.of(c, p).color, theInstance(c)); assertThat(Move.of(c, p).point, theInstance(p)); }); }); Point.stream() .forEach( p -> { assertThat(of(() -> Move.of(null, p)), raise(NullPointerException.class)); }); }
@Test public void testHashCode1() { // 同じ内容のオブジェクトは常に同じ値を返すことの確認 Color.stream() .forEach( c -> { assertThat(Move.of(c, null).hashCode(), is(Move.of(c, null).hashCode())); Point.stream() .forEach( p -> { assertThat(Move.of(c, p).hashCode(), is(Move.of(c, p).hashCode())); }); }); }
@Test public void testToString() { Color.stream() .forEach( c -> { assertThat(Move.of(c, null).toString(), is(String.format("[%s : PASS]", c))); Point.stream() .forEach( p -> { assertThat( Move.of(c, p).toString(), is(String.format("[%s : %s]", c, p.toStringKindly()))); }); }); }
@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)); }); }