課題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

test/SuperRareScenario.java


package test;
import static org.junit.Assert.*;
import java.util.List;
import player.CardCollection;
import kotae.SuperRare;
import static test.CommonCardTest.*;
public class SuperRareScenario extends CardTestScenario<SuperRare>{
	private static final int baseRate = 100;
	private CardCollection cc;
	private boolean[] compArray;
	public SuperRareScenario(List<SuperRare> list1, CardCollection cc1) {
		super(list1);
		this.cc=cc1;		
		compArray = new boolean[list1.size()];
		int i=0;
		for(SuperRare card : list1){
			compArray[i++]=cc1.contains(card);
		}
	}
	public void drawCard(int j){
		compArray[j] = true;
		cc.add(list.get(j));
	}
	public void testVariableRate() {
		assertArrayEquals(expectedRate(compArray),toIntArray(list, new Rate()));
	}
	private static int[] expectedRate(boolean[] compArray) {
		int nokori = countFalse(compArray);
		int[] result = new int[compArray.length];
		int normal = baseRate;
		int special;
		switch(nokori){
		case 1: special = normal*100; break;
		case 2: special = normal*10; break;
		default: special = normal;
		}
		for(int i=0; i< result.length; i++){
			result[i] = compArray[i]? normal : special;
		}
		return result;
	}
	private static int countFalse(boolean[] compArray) {
		int result = 0;
		for(boolean b : compArray){
			if(!b) result++;
		}
		return result;
	}
}

test/SuperRareTest.java


package test;
import static org.junit.Assert.*;
import java.util.List;
import kotae.SuperRare;
import org.junit.Before;
import org.junit.Test;
import player.CardCollection;
import card.SuperRareFactory;
public class SuperRareTest extends CommonCardTest {
	private CardCollection cc1;
	private CardCollection cc2;
	private static final String[] name1 = {"SuperRareAka","SuperRareAo","SuperRareKuro"};
	private static final String[] name2 = {"SuperRareA","SuperRareB","SuperRareC","SuperRareD"};
	private List<SuperRare> list1;
	private List<SuperRare> list2;
	private SuperRareFactory factory1;
	private SuperRareFactory factory2;
	private SuperRareScenario cardTestScenario1;
	private SuperRareScenario cardTestScenario2;
	@Before
	public void setup(){
		cc1 = new CardCollection();
		cc2 = new CardCollection();
		factory1 = new SuperRareFactory(20,name1);
		factory2 = new SuperRareFactory(30,name2);
		list1 = factory1.getList(cc1);
		list2 = factory2.getList(cc2);
		cardTestScenario1 = new SuperRareScenario(list1,cc1);
		cardTestScenario2 = new SuperRareScenario(list2,cc2);
	}
	private static class SuperRareClassTest implements CardTestScenario.Tester<SuperRare>{
		@Override
		public boolean check(SuperRare card) {
			return card instanceof SuperRare;
		}
	}
	@Test
	public void testClass(){
		cardTestScenario1.setTest(new SuperRareClassTest());
		cardTestScenario1.execCheck();
		
		cardTestScenario2.setTest(new SuperRareClassTest());
		cardTestScenario2.execCheck();
	}
	@Test
	public void testVariableRate1(){
		cardTestScenario1.testVariableRate();
		cardTestScenario1.drawCard(1);
		cardTestScenario1.testVariableRate();
		cardTestScenario1.drawCard(1);
		cardTestScenario1.testVariableRate();
		cardTestScenario1.drawCard(2);
		cardTestScenario1.testVariableRate();
		cardTestScenario1.drawCard(1);
		cardTestScenario1.testVariableRate();
		cardTestScenario1.drawCard(0);
		cardTestScenario1.testVariableRate();	
	}
	@Test
	public void testVariableRate2(){
		cardTestScenario2.testVariableRate();
		cardTestScenario2.drawCard(1);
		cardTestScenario2.testVariableRate();
		cardTestScenario2.drawCard(1);
		cardTestScenario2.testVariableRate();
		cardTestScenario2.drawCard(2);
		cardTestScenario2.testVariableRate();
		cardTestScenario2.drawCard(1);
		cardTestScenario2.testVariableRate();
		cardTestScenario2.drawCard(0);
		cardTestScenario2.testVariableRate();	
		cardTestScenario2.drawCard(3);
		cardTestScenario2.testVariableRate();	
	}
	@Test
	public void testMultiplayer(){
		CardCollection cc12 = new CardCollection();
		List<SuperRare> list12 = factory1.getList(cc12);
		SuperRareScenario cardTestScenario12 = new SuperRareScenario(list12,cc12);
		cardTestScenario1.testVariableRate();
		cardTestScenario12.testVariableRate();
		
		cardTestScenario1.drawCard(0);
		cardTestScenario1.testVariableRate();
		cardTestScenario12.testVariableRate();
		
		cardTestScenario12.drawCard(1);
		cardTestScenario1.testVariableRate();
		cardTestScenario12.testVariableRate();
		
		cardTestScenario12.drawCard(2);
		cardTestScenario1.testVariableRate();
		cardTestScenario12.testVariableRate();
		
		cardTestScenario12.drawCard(0);
		cardTestScenario1.testVariableRate();
		cardTestScenario12.testVariableRate();
		
		cardTestScenario1.drawCard(1);
		cardTestScenario1.testVariableRate();
		cardTestScenario12.testVariableRate();
		
		cardTestScenario12.drawCard(0);
		cardTestScenario1.testVariableRate();
		cardTestScenario12.testVariableRate();
	}	
	@Test
	public void testToString(){
		assertArrayEquals(name1, toObjectArray(list1,new ToString()));
		assertArrayEquals(name2, toObjectArray(list2,new ToString()));
	}
	@Test
	public void testName(){
		assertArrayEquals(name1, toObjectArray(list1,new Name()));
		assertArrayEquals(name2, toObjectArray(list2,new Name()));
	}
	@Test
	public void testOrder() {
		int[] exp1 = {20,21,22};
		assertArrayEquals(exp1, toIntArray(list1,new Id()));
		int[] exp2 = {30,31,32,33};
		assertArrayEquals(exp2, toIntArray(list2,new Id()));
	}
	@Test
	public void testCompareTo() {
		cardTestScenario1.testCompareTo();
		cardTestScenario2.testCompareTo();
	}
}