-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFractionTest.java
315 lines (249 loc) · 8.72 KB
/
FractionTest.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package lab2;
import static org.junit.Assert.*;
import org.junit.Test;
import java.lang.reflect.Field;
import org.junit.rules.Timeout;
import org.junit.runners.MethodSorters;
import org.junit.FixMethodOrder;
import org.junit.Rule;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class FractionTest {
@Rule
public Timeout globalTimeout = Timeout.seconds(1);
private int getNumeratorValue(Fraction c) {
int v=-1;
try {
Field name = Fraction.class.getDeclaredField("numerator");
name.setAccessible(true);
v = (int)name.get(c);
} catch (Exception x) {
fail("exception occurred trying to get FractionTest this.getNumeratorValue");
System.out.println(x);
}
return v;
}
private int getdenominatorValue(Fraction c) {
int v=-1;
try {
Field name = Fraction.class.getDeclaredField("denominator");
name.setAccessible(true);
v = (int)name.get(c);
} catch (Exception x) {
fail("exception occurred trying to get FractionTest this.getdenominatorValue");
System.out.println(x);
}
return v;
}
@Test
public void test_0_0_FractionCtor() {
Fraction c = new Fraction();
int actual = this.getdenominatorValue(c);
int expected = 1;
String error = String.format(
"\n Test no-arg constructor failed Returned denominator value ( %d ) " + " but correct value is ( %d ). \n",
actual, expected);
assertEquals(error, expected, actual);
}
@Test
public void test_0_1_FractionCtor() {
Fraction c = new Fraction();
int actual = this.getNumeratorValue(c);
int expected = 0;
String error = String.format(
"\n Test no-arg constructor failed Returned numerator value ( %d ) " +
" but correct value is ( %d ). \n",
actual, expected);
assertEquals(error, expected, actual);
}
@Test
public void test_1_0_FractionCtor() {
Fraction c = new Fraction(10,10);
int actual = this.getNumeratorValue(c);
int expected = 10;
String error = String.format(
"\n Test arg constructor failed Returned numerator value ( %d ) " +
" but correct value is ( %d ). \n",
actual, expected);
assertEquals(error, expected, actual);
}
@Test
public void test_1_1_FractionCtor() {
Fraction c = new Fraction(10,150);
int actual = this.getdenominatorValue(c);
int expected = 150;
String error = String.format(
"\n Test arg constructor failed Returned denominator value ( %d ) " + " but correct value is ( %d ). \n",
actual, expected);
assertEquals(error, expected, actual);
}
@Test
public void test_2_0_FractiongetNumerator() {
Fraction c = new Fraction(145,10);
int actual = c.getNumerator();
int expected = 145;
String error = String.format(
"\n Test getDenominator failed Returned numerator value ( %d ) " + " but correct value is ( %d ). \n",
actual, expected);
assertEquals(error, expected, actual);
}
@Test
public void test_2_1_FractiongetDenominator() {
Fraction c = new Fraction(10,995);
int actual = c.getDenominator();
int expected = 995;
String error = String.format(
"\n Test getDenominator failed Returned denominator value ( %d ) " + " but correct value is ( %d ). \n",
actual, expected);
assertEquals(error, expected, actual);
}
@Test
public void test_3_0_FractionsetNumerator() {
Fraction c = new Fraction(145,10);
c.setNumerator(785);
int actual = this.getNumeratorValue(c);
int expected = 785;
String error = String.format(
"\n Test setNumerator failed Returned numerator value ( %d ) " + " but correct value is ( %d ). \n",
actual, expected);
assertEquals(error, expected, actual);
}
@Test
public void test_3_1_FractionsetDenominator() {
Fraction c = new Fraction(10,995);
c.setDenominator(854);
int actual = this.getdenominatorValue(c);
int expected = 854;
String error = String.format(
"\n Test setDenominator failed Returned denominator value ( %d ) " + " but correct value is ( %d ). \n",
actual, expected);
assertEquals(error, expected, actual);
}
@Test
public void test_4_0_FractionsetFraction() {
Fraction c = new Fraction(145,10);
c.setFraction(852, 458);
int actual = this.getNumeratorValue(c);
int expected = 852;
String error = String.format(
"\n Test setFraction failed Returned numerator value ( %d ) " + " but correct value is ( %d ). \n",
actual, expected);
assertEquals(error, expected, actual);
}
@Test
public void test_4_1_FractionsetFraction() {
Fraction c = new Fraction(10,995);
c.setFraction(852, 458);
int actual = this.getdenominatorValue(c);
int expected = 458;
String error = String.format(
"\n Test setFraction failed Returned denominator value ( %d ) " + " but correct value is ( %d ). \n",
actual, expected);
assertEquals(error, expected, actual);
}
@Test
public void test_5_0_FractiongetAbsValue() {
Fraction c = new Fraction(145,10);
double actual = c.getAbsValue();
double expected = 14.5;
String error = String.format(
"\n Test getAbsValue failed Returned numerator value ( %.7f ) " + " but correct value is ( %.7f ). \n",
actual, expected);
final double THRESHOLD = .0000005;
assertTrue(error, Math.abs(expected - actual) < THRESHOLD);
}
@Test
public void test_5_1_FractiongetAbsValue() {
Fraction c = new Fraction(10,995);
double actual = c.getAbsValue();
double expected = 0.0100503;
String error = String.format(
"\n Test getAbsValue failed Returned numerator value ( %.7f ) " + " but correct value is ( %.7f ). \n",
actual, expected);
final double THRESHOLD = .0000005;
assertTrue(error, Math.abs(expected - actual) < THRESHOLD);
}
@Test
public void test_5_2_FractiongetAbsValue() {
Fraction c = new Fraction(1,1995);
double actual = c.getAbsValue();
double expected = 0.0005013;
String error = String.format(
"\n Test getAbsValue failed Returned numerator value ( %.7f ) " + " but correct value is ( %.7f ). \n",
actual, expected);
final double THRESHOLD = .0000005;
assertTrue(error, Math.abs(expected - actual) < THRESHOLD);
}
@Test
public void test_5_3_FractiongetAbsValue() {
Fraction c = new Fraction(1,98995);
double actual = c.getAbsValue();
double expected = 0.0000101;
String error = String.format(
"\n Test getAbsValue failed Returned numerator value ( %.7f ) " + " but correct value is ( %.7f ). \n",
actual, expected);
final double THRESHOLD = .0000005;
assertTrue(error, Math.abs(expected - actual) < THRESHOLD);
}
@Test
public void test_6_0_FractionaddFraction() {
Fraction a = new Fraction(1,10);
Fraction b = new Fraction(1,10);
a.addFraction(b);
int actual = this.getNumeratorValue(a);
int expected = 20;
String error = String.format(
"\n Test addFraction failed Returned numerator value ( %d ) " + " but correct value is ( %d ). \n",
actual, expected);
assertEquals(error, expected, actual);
}
@Test
public void test_6_1_FractionaddFraction() {
Fraction a = new Fraction(1,10);
Fraction b = new Fraction(1,10);
a.addFraction(b);
int actual = this.getdenominatorValue(a);
int expected = 100;
String error = String.format(
"\n Test addFraction failed Returned denominator value ( %d ) " + " but correct value is ( %d ). \n",
actual, expected);
assertEquals(error, expected, actual);
}
@Test
public void test_6_2_FractionaddFraction() {
Fraction a = new Fraction(2,3);
Fraction b = new Fraction(5,4);
a.addFraction(b);
int actual = this.getNumeratorValue(a);
int expected = 23;
String error = String.format(
"\n Test addFraction failed Returned numerator value ( %d ) " + " but correct value is ( %d ). \n",
actual, expected);
assertEquals(error, expected, actual);
}
@Test
public void test_6_3_FractionaddFraction() {
Fraction a = new Fraction(2,4);
Fraction b = new Fraction(5,3);
a.addFraction(b);
int actual = this.getdenominatorValue(a);
int expected = 12;
String error = String.format(
"\n Test addFraction failed Returned denominator value ( %d ) " + " but correct value is ( %d ). \n",
actual, expected);
assertEquals(error, expected, actual);
}
@Test
public void test_7_0_FractiontoString() {
Fraction c = new Fraction(145,10);
String actual = c.toString();
String expected = "Fraction (145 / 10)";
assertEquals(expected, actual);
}
@Test
public void test_7_1_FractiontoString() {
Fraction c = new Fraction(1,10);
String actual = c.toString();
String expected = "Fraction (1 / 10)";
assertEquals(expected, actual);
}
}