古いコードは別ページへ
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(new Gagyo(2)));
assertFalse(h.equals(new Chaki(4)));
assertFalse(h.equals(new Pagyo(0)));
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;
public abstract class NiseClass {
protected int num;
protected String[] array;
protected abstract int getOffset();
public NiseClass(int i, String[] a) {
super();
num=i;
array=a;
}
@Override
public int hashCode() {
return num+getOffset();
}
@Override
public String toString() {
return array[num];
}
}
package test;
public class Gagyo extends NiseClass {
public Gagyo(int i) {
super(i, new String[]{"Ga", "Gi", "Gu", "Ge", "Go"});
}
@Override
protected int getOffset(){
return -1;
}
}
package test;
public class Chaki extends NiseClass {
public Chaki(int i) {
super(i, new String[]{"Chaki", "Chiki", "Chuki", "Cheki", "Choki"});
}
@Override
protected int getOffset(){
return -2;
}
}
package test;
public class Pagyo extends NiseClass {
public Pagyo(int i) {
super(i, new String[]{"Pa", "Pi", "Pu", "Pe", "Po"});
}
@Override
protected int getOffset(){
return 3;
}
}
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 "グー";
}
}
package test;
import kadai.Hand;
import kadai.HandBuilder;
public class HandBuilder0 implements HandBuilder {
public HandBuilder0(){}
@Override
public Hand getHand(int type) {
return new Hand0();
}
}
package test;
import org.junit.Test;
import kadai.HandBuilder;
import kadai.HandBuilder1;
public class HandBuilder1Test {
@Test
public void testGetHand() {
HandBuilder hb = new HandBuilder1();
HandBuilderTest.examineGetHand(hb);
HandBuilderTest.examineEquivalence(hb);
}
}
package test;
import kadai.HandBuilder;
import kotae.HandBuilder2;
import org.junit.Test;
public class HandBuilder2Test {
@Test
public void testGetHand() {
HandBuilder hb = new HandBuilder2();
HandBuilderTest.examineGetHand(hb);
HandBuilderTest.examineEquivalence(hb);
HandBuilderTest.examineSameness(hb);
}
}
package test;
import static org.junit.Assert.*;
import kadai.HandBuilder;
public class HandBuilderTest {
public static void examineGetHand(HandBuilder hb) {
for(int i=0; i<4; i++){
assertEquals(i,hb.getHand(i).getNum());
}
}
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));
}
}
}
package test;
import kadai.Hand;
import kadai.HandBuilder;
import kadai.Tactics;
public class Tactics0 implements Tactics {
private HandBuilder hb;
public Tactics0(HandBuilder hb) {
this.hb = hb;
}
@Override
public Hand getHand() {
return hb.getHand(1);
}
}
package test;
import kadai.HandBuilder;
import kadai.HandBuilder1;
import kotae.Tactics1;
import org.junit.Test;
public class Tactics1Test {
@Test
public void testGetHand() {
HandBuilder hb = new HandBuilder1();
TestTactics.testGetHand(new Tactics1(hb));
}
@Test
public void testHandBuilder() {
HandBuilder hb = new HandBuilder0();
TestTactics.testGetHand(new Tactics1(hb));
}
}
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());
}
TestTactics.testGetHand(t);
}
@Test
public void testGetHand2(){
HandBuilder hb = new HandBuilder1();
Tactics 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());
}
}
}
private int next() {
int result = count;
count = count%3+1;
return result;
}
}
package test;
import java.util.HashMap;
import kadai.Hand;
public class Counter extends HashMap<Hand, Integer> {
private static final long serialVersionUID = 1L;
public Counter(){
super();
}
public void inc(Hand hand){
if(containsKey(hand)){
put(hand,get(hand)+1);
}else{
put(hand,1);
}
}
public int[] toIntArray() {
int[] result = new int[4];
for(Hand hand : keySet()){
result[hand.getNum()]=get(hand);
}
return result;
}
}
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("CurePiece");
assertEquals("CurePiece: 0 wins, 0 looses and 0 draws.",p.toString());
p = new Player("Sazae");
assertEquals("Sazae: 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 kadai.HandBuilder;
import kadai.Janken;
import kotae.Player;
import org.junit.Test;
public class JankenTest {
@Test
public void testPon() {
HandBuilder hb = new HandBuilder0();
Player[] dummy = {new Player("Dummy0"), new Player("Dummy1")};
dummy[0].setTactics(new Tactics0(hb));
dummy[1].setTactics(new Tactics0(hb));
Janken j = new Janken(dummy[0], dummy[1]);
assertEquals("Dummy0: 0 wins, 0 looses and 0 draws.",dummy[0].toString());
assertEquals("Dummy1: 0 wins, 0 looses and 0 draws.",dummy[1].toString());
j.pon();
assertEquals("Dummy0: 0 wins, 0 looses and 1 draws.",dummy[0].toString());
assertEquals("Dummy1: 0 wins, 0 looses and 1 draws.",dummy[1].toString());
}
}