課題1テストコード

test/MyHandTest.java


package test;
import static org.junit.Assert.*;
import java.util.Arrays;
import kadai.Hand;
import kotae.MyHand;
import org.junit.Test;
public class MyHandTest {
	@Test
	public void testWins() {
		int[][] result = {{0,-1,-1,-1},{1,0,1,-1},{1,-1,0,1},{1,1,-1,0}};
		for(int i=0;i<result.length;i++){
			Hand h = new MyHand(i);
			int[] test = new int[4];
			for(int j=0; j<test.length; j++){
				test[j]= h.wins(new MyHand(j));
			}
			assertEquals(Arrays.toString(result[i]),Arrays.toString(test));
		}
	}
	@Test
	public void testGetnum() {
		for(int i=0; i<4; i++){
			assertEquals(i,new MyHand(i).getNum());
		}
	}
	@Test
	public void testToString() {
		String[] te = new String[]{"Yasumi", "Gu", "Choki", "Pa"};
		for(int i=0; i<4; i++){
			assertEquals(te[i],new MyHand(i).toString());
		}
	}
	interface Expected {
		boolean expected(int i, int j);
	}
	class Expected0 implements Expected {
		@Override
		public boolean expected(int i, int j) {
			return i==1;
		}
	}
	class ExpectedTaikakusen implements Expected {
		@Override
		public boolean expected(int i, int j) {
			return i==j;
		}
	}
	interface Relation {
		boolean relation(Hand h1, Hand h2);
	}
	class EqualRelation implements Relation {
		@Override
		public boolean relation(Hand h1, Hand h2) {
			return h1.equals(h2);
		}
	}
	class HashRelation implements Relation {
		@Override
		public boolean relation(Hand h1, Hand h2) {
			return h1.hashCode()==h2.hashCode();
		}
	}
	class TestHand {
		private Hand[] hh1;
		private Hand[] hh2;
		private Relation r;
		private Expected e;
		public TestHand(Hand[] hh1, Hand[] hh2, Expected e, Relation r){
			this.hh1 = hh1;
			this.hh2 = hh2;
			this.e = e;
			this.r = r;
		}
		public void test() {
			for(int i=0; i<4; i++){
				for(int j=0; j<4; j++){
					if(e.expected(i,j)){
						assertTrue(r.relation(hh1[i],hh2[j]));
					}else{
						assertFalse(r.relation(hh1[i],hh2[j]));
					}
				}
			}
		}
	}
	@Test
	public void testEquals(){
		for(int i=0;i<4;i++){
			Hand h = new MyHand(i);
			assertFalse(h.equals(null));
			assertFalse(h.equals("abc"));
			assertFalse(h.equals("Gu"));
			assertFalse(h.equals("Choki"));
			assertFalse(h.equals("Pa"));
			assertFalse(h.equals("Yasumi"));
			assertFalse(h.equals(randomString()));
			assertFalse(h.equals(new Integer((int) (Math.random()*1000))));
			if(i==1){
				assertTrue(h.equals(new Hand0()));
			}else{
				assertFalse(h.equals(new Hand0()));
			}
		}
		TestHand h0 = new TestHand(getMyHandArray(), getHand0Array(),
				new Expected0(), new EqualRelation());
		h0.test();
		TestHand h1 = new TestHand(getMyHandArray(), getMyHandArray(),
				new ExpectedTaikakusen(), new EqualRelation());
		h1.test();
	}
	private String randomString() {
		StringBuilder sb = new StringBuilder();
		int length = (int) (Math.random()*10);
		for(int i=0; i< length; i++){
			sb.append(Math.random()*('Z'-'A')+'A');
		}
		return sb.toString();
	}
	public Hand[] getHand0Array() {
		return new Hand[]{new Hand0(), new Hand0(), new Hand0(), new Hand0()};
	}
	private Hand[] getMyHandArray() {
		return new Hand[]{new MyHand(0), new MyHand(1), new MyHand(2), new MyHand(3)};
	}
	@Test
	public void testHashCode(){	
		TestHand h0 = new TestHand(getMyHandArray(), getHand0Array(),
				new Expected0(), new HashRelation());
		h0.test();
		TestHand h1 = new TestHand(getMyHandArray(), getMyHandArray(),
				new ExpectedTaikakusen(), new HashRelation());
		h1.test();
	}
}

package test;
import static org.junit.Assert.*;
import java.util.Arrays;
import kadai.Hand;
import kotae.MyHand;
import org.junit.Test;
public class MyHandTest {
	@Test
	public void testWins() {
		int[][] result = {{0,-1,-1,-1},{1,0,1,-1},{1,-1,0,1},{1,1,-1,0}};
		for(int i=0;i<result.length;i++){
			Hand h = new MyHand(i);
			int[] test = new int[4];
			for(int j=0; j<test.length; j++){
				test[j]= h.wins(new MyHand(j));
			}
			assertEquals(Arrays.toString(result[i]),Arrays.toString(test));
		}
	}
	@Test
	public void testGetnum() {
		for(int i=0; i<4; i++){
			assertEquals(i,new MyHand(i).getNum());
		}
	}
	@Test
	public void testToString() {
		String[] te = new String[]{"Yasumi", "Gu", "Choki", "Pa"};
		for(int i=0; i<4; i++){
			assertEquals(te[i],new MyHand(i).toString());
		}
	}
	interface Expected {
		boolean expected(int i, int j);
	}
	class Expected0 implements Expected {
		@Override
		public boolean expected(int i, int j) {
			return i==1;
		}
	}
	class ExpectedTaikakusen implements Expected {
		@Override
		public boolean expected(int i, int j) {
			return i==j;
		}
	}
	interface Relation {
		boolean relation(Hand h1, Hand h2);
	}
	class EqualRelation implements Relation {
		@Override
		public boolean relation(Hand h1, Hand h2) {
			return h1.equals(h2);
		}
	}
	class HashRelation implements Relation {
		@Override
		public boolean relation(Hand h1, Hand h2) {
			return h1.hashCode()==h2.hashCode();
		}
	}
	class TestHand {
		private Hand[] hh1;
		private Hand[] hh2;
		private Relation r;
		private Expected e;
		public TestHand(Hand[] hh1, Hand[] hh2, Expected e, Relation r){
			this.hh1 = hh1;
			this.hh2 = hh2;
			this.e = e;
			this.r = r;
		}
		public void test() {
			for(int i=0; i<4; i++){
				for(int j=0; j<4; j++){
					if(e.expected(i,j)){
						assertTrue(r.relation(hh1[i],hh2[j]));
					}else{
						assertFalse(r.relation(hh1[i],hh2[j]));
					}
				}
			}
		}
	}
	@Test
	public void testEquals(){
		for(int i=0;i<4;i++){
			Hand h = new MyHand(i);
			assertFalse(h.equals(null));
			assertFalse(h.equals("abc"));
			if(i==1){
				assertTrue(h.equals(new Hand0()));
			}else{
				assertFalse(h.equals(new Hand0()));
			}
		}
		TestHand h0 = new TestHand(getMyHandArray(), getHand0Array(),
				new Expected0(), new EqualRelation());
		h0.test();
		TestHand h1 = new TestHand(getMyHandArray(), getMyHandArray(),
				new ExpectedTaikakusen(), new EqualRelation());
		h1.test();
	}
	public Hand[] getHand0Array() {
		return new Hand[]{new Hand0(), new Hand0(), new Hand0(), new Hand0()};
	}
	private Hand[] getMyHandArray() {
		return new Hand[]{new MyHand(0), new MyHand(1), new MyHand(2), new MyHand(3)};
	}
	@Test
	public void testHashCode(){	
		TestHand h0 = new TestHand(getMyHandArray(), getHand0Array(),
				new Expected0(), new HashRelation());
		h0.test();
		TestHand h1 = new TestHand(getMyHandArray(), getMyHandArray(),
				new ExpectedTaikakusen(), new HashRelation());
		h1.test();
	}
}

package test;
import static org.junit.Assert.*;
import java.util.Arrays;
import kadai.Hand;
import kotae.MyHand;
import org.junit.Test;
public class MyHandTest {
	@Test
	public void testWins() {
		int[][] result = {{0,-1,-1,-1},{1,0,1,-1},{1,-1,0,1},{1,1,-1,0}};
		for(int i=0;i<result.length;i++){
			Hand h = new MyHand(i);
			int[] test = new int[4];
			for(int j=0; j<test.length; j++){
				test[j]= h.wins(new MyHand(j));
			}
			assertEquals(Arrays.toString(result[i]),Arrays.toString(test));
		}
	}
	@Test
	public void testGetnum() {
		for(int i=0; i<4; i++){
			assertEquals(i,new MyHand(i).getNum());
		}
	}
	@Test
	public void testToString() {
		String[] te = new String[]{"Yasumi", "Gu", "Choki", "Pa"};
		for(int i=0; i<4; i++){
			assertEquals(te[i],new MyHand(i).toString());
		}
	}
	abstract class TestRelation {
		abstract boolean relation(int i, int j);
	}
	void taikakusen(TestRelation r){
		for(int i=0;i<4;i++){
			for(int j=0; j<4; j++){
				if(i==j){
					assertTrue(r.relation(i,j));
				}else{
					assertFalse(r.relation(i,j));
				}
			}
		}
	}
	class TestEquals extends TestRelation{
		public TestEquals(){}
		@Override
		boolean relation(int i, int j) {
			return (new MyHand(i)).equals(new MyHand(j));
		}
	}
	@Test
	public void testEquals(){
		taikakusen(new TestEquals());
	}
	class TestHashCode extends TestRelation{
		public TestHashCode(){}
		@Override
		boolean relation(int i, int j) {
			return (new MyHand(i)).hashCode()==(new MyHand(j)).hashCode();
		}
	}
	@Test
	public void testHashCode(){
		taikakusen(new TestHashCode());
	}
}

test/Hand0.java


package test;
import kadai.Hand;
class Hand0 implements Hand {
	public Hand0(){}
	@Override
	public int wins(Hand hand) {
		return 0;
	}

	@Override
	public int hashCode(){
		return 1;
	}

	@Override
	public int getNum() {
		return 1;
	}
	@Override
	public String toString(){
		return "Gu";
	}
}

test/HandBuilder0.java

test/HandBuilder1Test.java


package test;
import org.junit.Test;
import kadai.HandBuilder1;
public class HandBuilder1Test {
	@Test
	public void testGetHand() {
		HandBuilderTest.examineEquivalence(new HandBuilder1());
	}
}

test/HandBuilder2Test.java


package test;
import kadai.HandBuilder;
import kotae.HandBuilder2;
import org.junit.Test;
public class HandBuilder2Test {
	@Test
	public void testGetHand() {
		HandBuilder hb = new HandBuilder2();
		HandBuilderTest.examineEquivalence(hb);
		HandBuilderTest.examineSameness(hb);
	}
}

test/HandBuilderTest.java


package test;
import static org.junit.Assert.*;
import kadai.HandBuilder;
public class HandBuilderTest {
	public static void examineSameness(HandBuilder hb) {
		for(int i=0; i<4; i++){
			assertSame(hb.getHand(i),hb.getHand(i));
		}
	}
	public static void examineEquivalence(HandBuilder hb) {
		for(int i=0; i<4; i++){
			assertEquals(hb.getHand(i),hb.getHand(i));
		}
	}
}

test/Tactics0.java

test/Tactics1Test.java


package test;
import kadai.HandBuilder1;
import kotae.Tactics1;
import org.junit.Test;
public class Tactics1Test {
	@Test
	public void testGetHand() {
		TestTactics.testGetHand(new Tactics1(new HandBuilder1()));
	}
	@Test
	public void testHandBuilder() {
		TestTactics.testHandBuilder(new Tactics1(new HandBuilder0()));
	}
}

test/Tactics2Test.java


package test;
import static org.junit.Assert.*;
import kadai.HandBuilder;
import kadai.HandBuilder1;
import kadai.Tactics;
import kotae.Tactics2;
import org.junit.Test;
public class Tactics2Test {
	private int count=1;
	@Test
	public void testHandBuilder() {
		HandBuilder hb = new HandBuilder0();
		TestTactics.testHandBuilder(new Tactics2(hb));
	}
	@Test
	public void testGetHand() {
		HandBuilder hb = new HandBuilder1();
		Tactics t = new Tactics2(hb);
		for(int i=0; i<100; i++){
			assertEquals(i%3+1,t.getHand().getNum());
		}
		 t = new Tactics2(hb);
		for(int j=0;j<2;j++){
			for(int i=0; i<Integer.MAX_VALUE; i++){
				assertEquals(next(),t.getHand().getNum());
			}
		}
		TestTactics.testGetHand(t);
	}
	private int next() {
		int result = count;
		count = count%3+1;		
		return result;
	}
}

package test;
import static org.junit.Assert.*;
import kadai.HandBuilder;
import kadai.HandBuilder1;
import kadai.Tactics;
import kotae.Tactics2;
import org.junit.Test;
public class Tactics2Test {
	@Test
	public void testHandBuilder() {
	    HandBuilder hb = new HandBuilder0();
	    TestTactics.testGetHand(new Tactics2(hb));
	}
	@Test
	public void testGetHand() {
	    HandBuilder hb = new HandBuilder1();
		Tactics t = new Tactics2(hb);
		for(int i=0; i<100; i++){
			assertEquals(i%3+1,t.getHand().getNum());
		}
		TestTactics.testGetHand(t);
	}
}

package test;
import static org.junit.Assert.*;
import kadai.HandBuilder1;
import kadai.Tactics;
import kotae.Tactics2;
import org.junit.Test;
public class Tactics2Test {
	@Test
	public void testHandBuilder() {
		TestTactics.testHandBuilder(new Tactics2(new HandBuilder0()));
	}
	@Test
	public void testGetHand() {
		Tactics t = new Tactics2(new HandBuilder1());
		for(int i=0; i<100; i++){
			assertEquals(i%3+1,t.getHand().getNum());
		}
		TestTactics.testGetHand(t);
	}
}

test/Counter.java

test/PlayerTest.java


package test;
import static org.junit.Assert.*;
import kadai.Hand;
import kadai.Tactics;
import kotae.Player;
import org.junit.Test;
public class PlayerTest {
	@Test
	public void testToString() {
		Player p = new Player("Test");
		assertEquals("Test:	0 wins, 0 looses and 0 draws.",p.toString());
	}
	@Test
	public void testGetHand() {
		Player p = new Player("Test");
		Tactics t = new Tactics0(new HandBuilder0());
		p.setTactics(t);
		Hand h = p.getHand();
		assertEquals(1,h.getNum());
	}
	@Test
	public void testDraw() {
		Player p = new Player("Test");
		p.draw();
		assertEquals("Test:	0 wins, 0 looses and 1 draws.",p.toString());
		p.draw();
		assertEquals("Test:	0 wins, 0 looses and 2 draws.",p.toString());
	}
	@Test
	public void testWin() {
		Player p = new Player("Test");
		p.win();
		assertEquals("Test:	1 wins, 0 looses and 0 draws.",p.toString());
		p.win();
		assertEquals("Test:	2 wins, 0 looses and 0 draws.",p.toString());
	}
	@Test
	public void testLoose() {
		Player p = new Player("Test");
		p.loose();
		assertEquals("Test:	0 wins, 1 looses and 0 draws.",p.toString());
		p.loose();
		assertEquals("Test:	0 wins, 2 looses and 0 draws.",p.toString());
	}
}

package test;
import static org.junit.Assert.*;
import kotae.Player;
import org.junit.Test;
public class PlayerTest {
	@Test
	public void testToString() {
		Player p = new Player("Test");
		assertEquals("Test:	0 wins, 0 looses and 0 draws.",p.toString());
	}
	@Test
	public void testGetHand() {
		Player p = new Player("Test");
		p.setTactics(new Tactics0(new HandBuilder0()));
		assertEquals(1,p.getHand().getNum());
	}
	@Test
	public void testDraw() {
		Player p = new Player("Test");
		p.draw();
		assertEquals("Test:	0 wins, 0 looses and 1 draws.",p.toString());
		p.draw();
		assertEquals("Test:	0 wins, 0 looses and 2 draws.",p.toString());
	}
	@Test
	public void testWin() {
		Player p = new Player("Test");
		p.win();
		assertEquals("Test:	1 wins, 0 looses and 0 draws.",p.toString());
		p.win();
		assertEquals("Test:	2 wins, 0 looses and 0 draws.",p.toString());
	}
	@Test
	public void testLoose() {
		Player p = new Player("Test");
		p.loose();
		assertEquals("Test:	0 wins, 1 looses and 0 draws.",p.toString());
		p.loose();
		assertEquals("Test:	0 wins, 2 looses and 0 draws.",p.toString());
	}
}

test/JankenTest.java