Skip to content

Instantly share code, notes, and snippets.

@monzou
Created July 3, 2011 15:27
Show Gist options
  • Save monzou/1062317 to your computer and use it in GitHub Desktop.
Save monzou/1062317 to your computer and use it in GitHub Desktop.
JUnit4
package sandbox;
/**
* 与えられた数値を 2 倍にして返す関数です。
*
* @author monzou
*/
public enum DoubleFunction implements Function<Number, Number>, Predicate<Number> {
/** Integer 型を倍にする関数 */
INTEGER {
@Override
public boolean isTarget(Number input) {
return input != null && (Integer.class.isAssignableFrom(input.getClass()) || Integer.TYPE.isAssignableFrom(input.getClass()));
}
@Override
public Number apply(Number input) {
if (input == null) {
return null;
}
return input.intValue() * 2;
}
},
/** Double 型を倍にする関数 */
DOUBLE {
@Override
public boolean isTarget(Number input) {
return input != null && (Double.class.isAssignableFrom(input.getClass()) || Double.TYPE.isAssignableFrom(input.getClass()));
}
@Override
public Number apply(Number input) {
if (input == null) {
return null;
}
return input.doubleValue() * 2d;
}
}
}
package sandbox;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
/**
* {@link DoubleFunction} のテストです。
*
* @author monzou
*/
@RunWith(Enclosed.class)
public class DouleFunctionTest {
private static class Fixtures {
static Integer[] INTEGERS = new Integer[] { Integer.valueOf(-1), Integer.valueOf(0), Integer.valueOf(1), };
static Double[] DOUBLES = new Double[] { Double.valueOf(-1d), Double.valueOf(0d), Double.valueOf(1d), };
static int[] PRIMITIVE_INTEGERS = new int[] { -1, 0, 1, };
static double[] PRIMITIVE_DOUBLES = new double[] { -1d, 0d, 1d, };
}
@RunWith(Enclosed.class)
public static class Integer型に関するテスト {
private static DoubleFunction SUBJECT = DoubleFunction.INTEGER;
@RunWith(Theories.class)
public static class 事前条件判定の確認 {
@DataPoints
public static Integer[] INTEGERS = Fixtures.INTEGERS;
@DataPoints
public static Double[] DOUBLES = Fixtures.DOUBLES;
@DataPoints
public static int[] PRIMITIVE_INTEGERS = Fixtures.PRIMITIVE_INTEGERS;
@DataPoints
public static double[] PRIMITIVE_DOUBLES = Fixtures.PRIMITIVE_DOUBLES;
@Test
public void Nullの場合は評価しない() {
assertThat(SUBJECT.isTarget(null), is(false));
}
@Theory
public void Integer型なら評価する(Integer integerValue) {
assertThat(SUBJECT.isTarget(integerValue), is(true));
}
@Theory
public void int型なら評価する(int intValue) {
assertThat(SUBJECT.isTarget(intValue), is(true));
}
@Theory
public void Double型なら評価しない(Double doubleValue) {
assertThat(SUBJECT.isTarget(Double.valueOf(doubleValue)), is(false));
}
@Theory
public void double型なら評価しない(double doubleValue) {
assertThat(SUBJECT.isTarget(doubleValue), is(false));
}
}
@RunWith(Theories.class)
public static class 関数評価結果の確認 {
@DataPoints
public static Integer[] INTEGERS = Fixtures.INTEGERS;
@DataPoints
public static Double[] DOUBLES = Fixtures.DOUBLES;
@DataPoints
public static int[] PRIMITIVE_INTEGERS = Fixtures.PRIMITIVE_INTEGERS;
@DataPoints
public static double[] PRIMITIVE_DOUBLES = Fixtures.PRIMITIVE_DOUBLES;
@Test
public void Nullの場合はNull() {
assertNull(SUBJECT.apply(null));
}
@Theory
public void Integer型(Integer integerValue) {
assertThat(SUBJECT.apply(integerValue).intValue(), is(integerValue * 2));
}
@Theory
public void int型(int intValue) {
assertThat(SUBJECT.apply(intValue).intValue(), is(intValue * 2));
}
}
}
}
package sandbox;
/**
* 関数の定義です。
*
* @author monzou
*/
public interface Function<I, O> {
/**
* 関数を評価します。
*
* @param input インプット
* @return 関数評価結果
*/
O apply(I input);
}
package sandbox;
/**
* 条件述語の定義です。
*
* @author monzou
*/
public interface Predicate<I> {
/**
* 条件を評価します。
*
* @param input インプット
* @return 評価結果
*/
boolean isTarget(I input);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment