-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unit_Testing.java
96 lines (81 loc) · 2.35 KB
/
Unit_Testing.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
package lab3;
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
class Lab3Tests {
@Test
void testException1() {
try{
RationalNumber r = new RationalNumber(1,0);
assertFalse(true);
}
catch(Lab3Exception ex) {
assertEquals(ex.message, "Denominator cannot be zero");
}
}
@Test
void testException2() {
try{
SpecialNumber.computeAverage(null);
assertFalse(true);
}
catch(Lab3Exception ex) {
assertEquals(ex.message, "List cannot be empty");
}
}
@Test
void testException3() {
try{
RationalNumber r = new RationalNumber(1,1);
r.add(new ComplexNumber(1,1));
assertFalse(true);
}
catch(Lab3Exception ex) {
assertEquals(ex.message, "Cannot add an incompatible type");
}
}
@Test
void testHashCode() throws Exception{
RationalNumber r1 = new RationalNumber(1,2);
RationalNumber r2 = new RationalNumber(2,4);
assertEquals(r1.hashCode(), r2.hashCode());
}
@Test
void testEquals() throws Exception{
RationalNumber r1 = new RationalNumber(1,2);
RationalNumber r2 = new RationalNumber(2,4);
assertEquals(r1, r2);
}
@Test
void testAverageRationalNumber() throws Exception{
List<SpecialNumber> A = new ArrayList<SpecialNumber>();
A.add(new RationalNumber(3,2));
A.add(new RationalNumber(1,1));
A.add(new RationalNumber(1,4));
assertEquals(SpecialNumber.computeAverage(A), new RationalNumber(11,12));
}
@Test
void testAverageComplexNumber() throws Exception{
List<SpecialNumber> A = new ArrayList<SpecialNumber>();
A.add(new ComplexNumber(3,2));
A.add(new ComplexNumber(1,1));
A.add(new ComplexNumber(1,4));
A.add(new ComplexNumber(5,1));
System.out.println(A);
assertEquals(SpecialNumber.computeAverage(A), new ComplexNumber(2.5,2));
}
@Test
void testSort() throws Exception{
List<SpecialNumber> A = new ArrayList<SpecialNumber>();
RationalNumber a = new RationalNumber(3,2);
RationalNumber b = new RationalNumber(1,1);
RationalNumber c = new RationalNumber(1,4);
List<SpecialNumber> L1 = new ArrayList<SpecialNumber>();
L1.add(a); L1.add(b); L1.add(c);
List<SpecialNumber> L2 = new ArrayList<SpecialNumber>();
L2.add(c); L2.add(b); L2.add(a);
L1.sort(null);
assertEquals(L1, L2);
}
}