/
QuadraticEquationTest.java
112 lines (100 loc) · 3.62 KB
/
QuadraticEquationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.github.dakusui.quadratic.tests.session3;
import com.github.dakusui.jcunit.runners.standard.JCUnit;
import com.github.dakusui.jcunit.runners.standard.annotations.Condition;
import com.github.dakusui.jcunit.runners.standard.annotations.FactorField;
import com.github.dakusui.jcunit.runners.standard.annotations.When;
import com.github.dakusui.quadratic.suts.session1.QuadraticEquation;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.hamcrest.number.IsCloseTo.closeTo;
import static org.junit.Assert.assertThat;
/**
* <pre>
* QuadraticEquationクラス仕様 version 2
* - 入力:三つの整数a,b,c
* -- aが0の場合、例外が送出される。(問題4:a==0)
* -- a,b,cのいずれかの絶対値が100より大きい場合、例外が創出される。(問題3:巨大な係数)
* -- a,b,cがb * b - 4 * c * a >= 0を満たさない場合、例外が送出される。(問題1:虚数解)
* - 出力:二次方程式、a x^2 + b x^2 + c = 0を満たす実数x1とx2
* -- 出力される解x1またはx2を上述の2次方程式に代入した時、その誤差は0.01を越えない。(問題2:丸め誤差)
* </pre>
*/
@RunWith(JCUnit.class)
public class QuadraticEquationTest {
@FactorField(intLevels = {1, 0, -1, 100, 101, -100, -101, Integer.MAX_VALUE, Integer.MIN_VALUE})
public int a;
@FactorField(intLevels = {1, 0, -1, 100, 101, -100, -101, Integer.MAX_VALUE, Integer.MIN_VALUE})
public int b;
@FactorField(intLevels = {1, 0, -1, 100, 101, -100, -101, Integer.MAX_VALUE, Integer.MIN_VALUE})
public int c;
/**
* 制約1: ```a```が0の場合、例外が送出される。(**問題4:a==0**)
*/
@Condition
public boolean aIsNonZero() {
return this.a != 0;
}
/**
* 制約2: ```a```,```b```,```c```のいずれかの絶対値が100より大きい場合、例外が創出される。(**問題3:巨大な係数**)
*/
@Condition
public boolean coefficientsAreValid() {
return
-100 <= a && a <= 100 &&
-100 <= b && b <= 100 &&
-100 <= c && c <= 100;
}
/**
* 制約3: ```a```,```b```,```c```が```b * b - 4 * c * a >= 0```を満たさない場合、例外が送出される。(**問題1:虚数解**)
*/
@Condition
public boolean discriminantIsNonNegative() {
int a = this.a;
int b = this.b;
int c = this.c;
return b * b - 4 * c * a >= 0;
}
@Test(expected = IllegalArgumentException.class)
@When({ "!aIsNonZero" })
public void solveEquation1$thenThrowIllegalArgumentException() {
new QuadraticEquation(
a,
b,
c).solve();
}
@Test(expected = IllegalArgumentException.class)
@When({ "!coefficientsAreValid" })
public void solveEquation3$thenThrowIllegalArgumentException() {
new QuadraticEquation(
a,
b,
c).solve();
}
@Test(expected = IllegalArgumentException.class)
@When({ "!discriminantIsNonNegative" })
public void solveEquation2$thenThrowIllegalArgumentException() {
new QuadraticEquation(
a,
b,
c).solve();
}
/**
* 出力される解x1またはx2を上述の2次方程式に代入した時、その誤差は0.01を越えない。(問題2:丸め誤差)
*/
@Test
@When({ "*" })
public void solveEquation$thenSolved() {
QuadraticEquation.Solutions s = new QuadraticEquation(a, b,
c).solve();
assertThat(
String.format("(a,b,c)=(%d,%d,%d)", a, b, c),
Math.abs(a * s.x1 * s.x1 + b * s.x1 + c),
closeTo(0, 0.01)
);
assertThat(
String.format("(a,b,c)=(%d,%d,%d)", a, b, c),
a * s.x2 * s.x2 + b * s.x2 + c,
closeTo(0, 0.01)
);
}
}