日期:2014-05-19  浏览次数:20754 次

菜鸟跪求问关于猜拳的java问题
kadai/Hand.java
package kadai;
public interface Hand {
int wins(Hand hand);
int getNum();
}

*****************************************************************************
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(new Gagyo(2)));
assertFalse(h.equals(new Chaki(4)));
assertFalse(h.equals(new Pagyo(0)));