課題1テストコード

課題1-2関係

Dollar2Test


package test;
import kadai.Dollar2;
import org.junit.Test;
public class Dollar2Test extends Ex2Test {
	@Test
	public void testCompareTo(){
		checkCompareTo(x->new Dollar2(x),x->Dollar2.setYenRate(x),110);
	}
	@Test
	public void testHashCode1(){
		Dollar2.setYenRate(80);
		checkHashCode(x->new Dollar2(x));
	}
	@Test
	public void testHashCode2(){
		Dollar2.setYenRate(120);
		checkHashCode(x->new Dollar2(x));
	}
	@Test 
	public void testEquals(){
		checkEquals(x->new Dollar2(x),x->Dollar2.setYenRate(x),110);
	}
	@Test
	public void testSetYenRate() throws ClassNotFoundException {
		checkSetYenRate(x->new Dollar2(x),x->Dollar2.setYenRate(x),
				3,"$3.0",80.0,"240.0円", 100.0, "300.0円");
	}
	@Test
	public void testToString() {
		checkToString(x->new Dollar2(x),3.0, "$3.0");
	}
}

Euro2Test


package test;
import kadai.Euro2;
import org.junit.Test;
public class Euro2Test extends Ex2Test {
	@Test
	public void testCompareTo(){
		checkCompareTo(x->new Euro2(x),x->Euro2.setYenRate(x),110);
	}
	@Test
	public void testHashCode1(){
		Euro2.setYenRate(80);
		checkHashCode(x->new Euro2(x));
	}
	@Test
	public void testHashCode2(){
		Euro2.setYenRate(120);
		checkHashCode(x->new Euro2(x));
	}
	@Test 
	public void testEquals(){
		checkEquals(x->new Euro2(x),x->Euro2.setYenRate(x),110);
	}
	@Test
	public void testSetYenRate() throws ClassNotFoundException {
		checkSetYenRate(x->new Euro2(x),x->Euro2.setYenRate(x),
				3,"3.0Euro",120.0,"360.0円", 130.0, "390.0円");
	}
	@Test
	public void testToString() {
		checkToString(x->new Euro2(x),3.0, "3.0Euro");
	}
}

Yen2Test


package test;
import java.util.function.DoubleConsumer;
import java.util.function.DoubleFunction;
import kadai.Money2;
import kadai.Yen2;
import org.junit.Test;
public class Yen2Test  extends Ex2Test {
	@Test
	public void testCompareTo(){
		checkCompareTo(x->new Yen2(x),x->Yen2.setYenRate(x),110);
	}
	@Test
	public void testHashCode1(){
		Yen2.setYenRate(80);
		checkHashCode(x->new Yen2(x));
	}
	@Test
	public void testHashCode2(){
		Yen2.setYenRate(120);
		checkHashCode(x->new Yen2(x));
	}
	@Test 
	public void testEquals(){
		checkEquals(x->new Yen2(x),x->Yen2.setYenRate(x),110);
	}
	@Test
	public void testSetYenRate() throws ClassNotFoundException {
		checkSetYenRate(x->new Yen2(x),x->Yen2.setYenRate(x),
				3,"3.0円",80.0,"3.0円", 100.0, "3.0円");
	}
	@Test
	public void testToString() {
		checkToString(x->new Yen2(x),3.0, "3.0円");
	}
	@Override
	protected <T extends Money2> void checkCompareTo(DoubleFunction<T> constructor,
			DoubleConsumer setYenRate, double i){
		setYenRate.accept(80);
		Money2 m1 = constructor.apply(i);
		Money2 m2 = constructor.apply(i+10);
		Money2 m3 = constructor.apply(i);
		Money2 m4 = new Yen2(i);
		checkSimilar(m1, m2, m3, m4);
		setYenRate.accept(1.0);
		checkSimilar(m1, m2, m3, m4);
	}
	@Override
	protected <T extends Money2> void checkEquals(DoubleFunction<T> constructor,
			DoubleConsumer setYenRate, double i) {
		setYenRate.accept(80);
		Money2 m1 = constructor.apply(i);
		Money2 m2 = constructor.apply(i+10);
		Money2 m3 = constructor.apply(i);
		Money2 m4 = new Yen2(i);
		String string = m1.toString();
		checkEqSimilar(m1, m2, m3, m4, string);		
		setYenRate.accept(1.0);
		checkEqSimilar(m1, m2, m3, m4, string);		
	}
}


test/Ex2Test.java


package test;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.function.DoubleConsumer;
import java.util.function.DoubleFunction;
import kadai.Money2;
import kadai.Yen2;
public class Ex2Test extends Ex1Test{
	public Ex2Test() {
		super();
	}
	protected <T extends Money2> void checkHashCode(DoubleFunction<T> constructor) {
		double[] sample = {1.0, 10.0, 1.0, 0.1, 2.0, 10.0, 500.0};
		Boolean[][] expect={
				{true, false, true, false, false, false, false},
				{false, true, false, false, false, true, false},
				{true, false, true, false, false, false, false},
				{false, false, false, true, false, false, false},
				{false, false, false, false, true, false, false},
				{false, true, false, false, false, true, false},
				{false, false, false, false, false, false, true}
		};
		int[] hashCodes = Arrays.stream(sample).mapToInt(x->constructor.apply(x).hashCode()).toArray();
		for(int i=0; i<hashCodes.length; i++){
			for(int j=0; i<hashCodes.length; i++){
				if(expect[i][j]){
					assertTrue(i+":"+j,hashCodes[i]==hashCodes[j]);
				}else{
					assertFalse(i+":"+j,hashCodes[i]==hashCodes[j]);
				}				
			}
		}
	}
	protected <T extends Money2> void checkCompareTo(DoubleFunction<T> constructor,
			DoubleConsumer setYenRate, double i){
		setYenRate.accept(80);
		Money2 m1 = constructor.apply(i);
		Money2 m2 = constructor.apply(i+10);
		Money2 m3 = constructor.apply(i);
		Money2 m4 = new Yen2(i);
		checkLarger(m1, m2, m3, m4);
		setYenRate.accept(1.0);
		checkSimilar(m1, m2, m3, m4);
	}
	protected void checkSimilar(Money2 m1, Money2 m2, Money2 m3, Money2 m4) {
		assertTrue(m1.compareTo(m1)==0);
		assertTrue(m1.compareTo (m2)<0);
		assertTrue(m1.compareTo (m3)==0);
		assertTrue(m1.compareTo (m4)==0);
		assertTrue(m2.compareTo(m1)>0);
		assertTrue(m2.compareTo (m2)==0);
		assertTrue(m2.compareTo (m3)>0);
		assertTrue(m2.compareTo (m4)>0);
		assertTrue(m3.compareTo(m1)==0);
		assertTrue(m3.compareTo (m2)<0);
		assertTrue(m3.compareTo (m3)==0);
		assertTrue(m3.compareTo (m4)==0);
		assertTrue(m4.compareTo(m1)==0);
		assertTrue(m4.compareTo (m2)<0);
		assertTrue(m4.compareTo (m3)==0);
		assertTrue(m4.compareTo (m4)==0);
	}
	protected void checkLarger(Money2 m1, Money2 m2, Money2 m3, Money2 m4) {
		assertTrue(m1.compareTo(m1)==0);
		assertTrue(m1.compareTo (m2)<0);
		assertTrue(m1.compareTo (m3)==0);
		assertTrue(m1.compareTo (m4)>0);
		assertTrue(m2.compareTo(m1)>0);
		assertTrue(m2.compareTo (m2)==0);
		assertTrue(m2.compareTo (m3)>0);
		assertTrue(m2.compareTo (m4)>0);
		assertTrue(m3.compareTo(m1)==0);
		assertTrue(m3.compareTo (m2)<0);
		assertTrue(m3.compareTo (m3)==0);
		assertTrue(m3.compareTo (m4)>0);
		assertTrue(m4.compareTo(m1)<0);
		assertTrue(m4.compareTo (m2)<0);
		assertTrue(m4.compareTo (m3)<0);
		assertTrue(m4.compareTo (m4)==0);
	}
	protected <T extends Money2> void checkEquals(DoubleFunction<T> constructor,
			DoubleConsumer setYenRate, double i) {
		setYenRate.accept(80);
		Money2 m1 = constructor.apply(i);
		Money2 m2 = constructor.apply(i+10);
		Money2 m3 = constructor.apply(i);
		Money2 m4 = new Yen2(i);
		String string = m1.toString();
		checkEqLarger(m1, m2, m3, m4, string);
		setYenRate.accept(1.0);
		checkEqSimilar(m1, m2, m3, m4, string);		
	}
	protected void checkEqSimilar(Money2 m1, Money2 m2, Money2 m3, Money2 m4,
			String string) {
		assertTrue(m1.equals(m1));
		assertFalse(m1.equals(null));
		assertFalse(m1.equals(string));
		assertFalse(m1.equals(m2));
		assertTrue(m1.equals(m3));
		assertTrue(m1.equals(m4));
	}
	protected void checkEqLarger(Money2 m1, Money2 m2, Money2 m3, Money2 m4,
			String string) {
		assertTrue(m1.equals(m1));
		assertFalse(m1.equals(null));
		assertFalse(m1.equals(string));
		assertFalse(m1.equals(m2));
		assertTrue(m1.equals(m3));
		assertFalse(m1.equals(m4));
	}
	protected <T extends Money2> void checkSetYenRate(DoubleFunction<T> constructor,
			DoubleConsumer setYenRate, double i, String string, double d, String string2,
			double e, String string3) throws ClassNotFoundException {
		Money2 m = constructor.apply(i);
		assertEquals(string, m.toString());
		setYenRate.accept(d);
		Money2 m2 = m.getYen();
		assertEquals(Class.forName("kadai.Yen2"),m2.getClass());
		assertEquals(string2,m2.toString());
		setYenRate.accept(e);
		Money2 m3 = m.getYen();
		assertEquals(string3,m3.toString());
	}
}

Ex2supplement(2014年7月25日追加,2014年8月21日改訂)


package test;

import static org.junit.Assert.*;
import kadai.Dollar2;
import kadai.Euro2;
import kadai.Money2;

public abstract class Ex2supplement {
	/**
	 * Yen2, Dollar2, Euro2 の順で、二つが等しくなるような組を
	 * 並べる。また、組同士はだんだん円換算の金額が大きくなるようにする。
	 */
	abstract protected  Money2[] getArray100140();
	private boolean[][] getDiagonal11(int n){
		boolean[][] e = new boolean[n][n];
		for(int i=0;i<n;i++){
			for(int j=0; j<n; j++){
				e[i][j]=(i==j);
			}
		}
		return e;
	}
	protected boolean[][] getDiagonal22(int n){
		boolean[][] e = new boolean[n][n];
		for(int i=0;i<n;i++){
			for(int j=0; j<n; j++){
				e[i][j]=((i/2)==(j/2));
			}
		}
		return e;
	}
	public void testEquals100140() {
		Money2[] a = getArray100140();
		boolean[][] expected=getDiagonal22(a.length);
		Dollar2.setYenRate(100);
		Euro2.setYenRate(140);
		for(int i=0; i<a.length; i++){
			for(int j=0; j<a.length; j++){
				String message=i+","+j;
				assertEquals(message,expected[i][j],a[i].equals(a[j]));
			}
		}
	}
	public void testEquals110160() {
		Money2[] a = getArray100140();
		boolean[][] expected=getDiagonal11(a.length);
		Dollar2.setYenRate(110);
		Euro2.setYenRate(160);
		for(int i=0; i<a.length; i++){
			for(int j=0; j<a.length; j++){
				String message=i+","+j;
				assertEquals(message,expected[i][j],a[i].equals(a[j]));
			}
		}
	}
	public void testHash100140() {
		Dollar2.setYenRate(100);
		Euro2.setYenRate(140);
		Money2[] a = getArray100140();
		for(int i=0; i<a.length; i++){
			for(int j=0; j<a.length; j++){
				String message=i+","+j;
				assertEquals(message,a[i].equals(a[j]),a[i].hashCode()==a[j].hashCode());
			}
		}
	}
	public void testHash110160() {
		Money2[] a = getArray100140();
		Dollar2.setYenRate(110);
		Euro2.setYenRate(160);
		for(int i=0; i<a.length; i++){
			for(int j=0; j<a.length; j++){
				String message=i+","+j;
				assertEquals(message,a[i].equals(a[j]),a[i].hashCode()==a[j].hashCode());
			}
		}
	}
	public void testCompareTo100140() {
		Money2[] a = getArray100140();
		Dollar2.setYenRate(100);
		Euro2.setYenRate(140);
		Eval[][] test = getDiagonal22Test(a.length);
		for(int i=0; i<a.length; i++){
			for(int j=0; j<a.length; j++){
				String message=i+","+j;
				test[i][j].assertion(message, a[i], a[j]);
			}
		}
	}
	public void testCompareTo110160() {
		Money2[] a = getArray100140();
		Dollar2.setYenRate(110);
		Euro2.setYenRate(160);
		Eval[][] test = getDiagonal11Test(a.length);
		for(int i=0; i<a.length; i++){
			for(int j=0; j<a.length; j++){
				String message=i+","+j;
				test[i][j].assertion(message, a[i], a[j]);
			}
		}
	}
	private Eval[][] getDiagonal11Test(int n){
		Eval[][] e = new Eval[n][n];
		for(int i=0;i<n;i++){
			for(int j=0; j<n; j++){
				e[i][j]=(i==j)? Eval.EVEN
						:(i<j)?Eval.LARGE
								: Eval.SMALL;
			}
		}
		return e;
	}
	private Eval[][] getDiagonal22Test(int n){
		Eval[][] e = new Eval[n][n];
		for(int i=0;i<n;i++){
			for(int j=0; j<n; j++){
				e[i][j]=((i/2)==(j/2))? Eval.EVEN
						:((i/2)<(j/2))?Eval.LARGE
								: Eval.SMALL;
			}
		}
		return e;
	}
	enum Eval {
		SMALL {
			@Override
			public void assertion(String message, Money2 a, Money2 b) {
				assertTrue(message,a.compareTo(b)>0);
			}
		},
		EVEN {
			@Override
			public void assertion(String message, Money2 a, Money2 b) {
				assertTrue(message,a.compareTo(b)==0);
			}
		},
		LARGE {
			@Override
			public void assertion(String message, Money2 a, Money2 b) {
				assertTrue(message,a.compareTo(b)<0);
			}
		};
		abstract public void assertion(String message,Money2 a, Money2 b);
	}
}

Ex2supplement1(2014年8月21日追加)


package test;

import kadai.Dollar2;
import kadai.Euro2;
import kadai.Money2;
import kadai.Yen2;

import org.junit.Test;

public class Ex2supplement1 extends Ex2supplement {
	@Override
	protected  Money2[] getArray100140(){
		Money2[] result = {
				new Yen2(100),
				new Dollar2(1),
				new Yen2(280),
				new Euro2(2),
				new Dollar2(7),
				new Euro2(5)
		};
		return result;
	}
	@Test
	public void testEquals100140() {
		super.testEquals100140();
	}
	@Test
	public void testEquals110160() {
		super.testEquals110160();
	}
	@Test
	public void testHash100140() {
		super.testHash100140();
	}
	@Test
	public void testHash110160() {
		super.testHash110160();
	}
	@Test
	public void testCompareTo100140() {
		super.testCompareTo100140();
	}
	@Test
	public void testCompareTo110160() {
		super.testCompareTo110160();
	}
}

Ex2supplement2(2014年8月21日追加)


package test;

import kadai.Dollar2;
import kadai.Euro2;
import kadai.Money2;
import kadai.Yen2;

import org.junit.Test;

public class Ex2supplement2 extends Ex2supplement{
	/**
	 * Yen2, Dollar2, Euro2 の順で、二つが等しくなるような組を
	 * 並べる。また、組同士はだんだん円換算の金額が大きくなるようにする。
	 */
	@Override
	protected Money2[] getArray100140(){
		Money2[] result = {
				new Yen2(100.1),
				new Dollar2(1.001),
				new Yen2(100.2),
				new Dollar2(1.002),
				new Yen2(140.14),
				new Euro2(1.001)
		};
		return result;
	}
	@Test
	public void testEquals100140() {
		super.testEquals100140();
	}
	@Test
	public void testHash100140() {
		super.testHash100140();
	}
	@Test
	public void testCompareTo100140() {
		super.testCompareTo100140();
	}
}

坂本直志 <sakamoto@c.dendai.ac.jp>
東京電機大学工学部情報通信工学科