Skip to content

Commit fef5017

Browse files
author
Alexander Mannertorn
committed
feat: add exception for illegal arguments for LIC
2, 8, and 11, and created corresponding unit tests closes #169
1 parent 27d2600 commit fef5017

2 files changed

Lines changed: 45 additions & 46 deletions

File tree

src/main/java/LIC.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ public static boolean LIC1(int numPoints, Point[] points, Parameters p) throws I
3131
public static boolean LIC2(int numPoints, Point[] points, Parameters p) {
3232
double PI = 3.14159;
3333
if (!(0 <= p.EPSILON && p.EPSILON < PI)) {
34-
return false;
34+
throw new IllegalArgumentException();
3535
}
36-
if (numPoints == 2) {
37-
return false;
36+
if (numPoints < 3) {
37+
throw new IllegalArgumentException();
3838
}
3939
for (int i = 0; i < numPoints - 2; i++) {
4040
Point p1 = points[i];
@@ -134,8 +134,11 @@ public static boolean LIC7(int numPoints, Point[] points, Parameters p) throws I
134134
}
135135

136136
public static boolean LIC8(int numPoints, Point[] points, Parameters p) {
137-
if (p.A_PTS < 1 || p.B_PTS < 1 || numPoints < 5 || p.A_PTS+p.B_PTS > (numPoints-3) || 0>p.RADIUS1){
138-
return false;
137+
if (p.A_PTS < 1 || p.B_PTS < 1 || p.A_PTS+p.B_PTS > (numPoints-3) || 0>p.RADIUS1){
138+
throw new IllegalArgumentException();
139+
}
140+
if (numPoints < 5){
141+
return false;
139142
}
140143
for (int i = 0; i + p.A_PTS+p.B_PTS < numPoints-2; i++){
141144
Point p1 = points[i];
@@ -191,8 +194,8 @@ public static boolean LIC10(int numPoints, Point[] points, Parameters p) throws
191194
}
192195

193196
public static boolean LIC11(int numPoints, Point[] points , Parameters p){
194-
if (numPoints < 3 || 1 > p.G_PTS || p.G_PTS > numPoints-2){
195-
return false;
197+
if ( 1 > p.G_PTS || p.G_PTS > numPoints-2){
198+
throw new IllegalArgumentException();
196199
}
197200
for (int i = 0; i + p.G_PTS < numPoints-1; i++){
198201
Point p1 = points[i];

src/test/java/LICTests.java

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ point or the last point (or both) coincides with the vertex, the angle is undefi
244244
};
245245

246246
//Tests that false is returned when NUMPOINTS < 3
247-
assertFalse(LIC.LIC2(points1.length, points1, p));
247+
assertThrows(IllegalArgumentException.class, () -> {
248+
LIC.LIC2(points1.length, points1, p);
249+
});
248250

249251
//Tests that false is returned if any point coincides with the vertex
250252
assertFalse(LIC.LIC2(points2.length, points2, p));
@@ -255,11 +257,15 @@ point or the last point (or both) coincides with the vertex, the angle is undefi
255257

256258
//Tests that false is returned when EPSILON < 0
257259
p.EPSILON = -0.01;
258-
assertFalse(LIC.LIC2(points4.length, points4, p));
260+
assertThrows(IllegalArgumentException.class, () -> {
261+
LIC.LIC2(points4.length, points4, p);
262+
});
259263

260264
//Tests that false is returned when EPISILON > PI
261265
p.EPSILON = PI + 0.01;
262-
assertFalse(LIC.LIC2(points4.length, points4, p));
266+
assertThrows(IllegalArgumentException.class, () -> {
267+
LIC.LIC2(points4.length, points4, p);
268+
});
263269

264270
//Tests that false is returned when there does not exist at least one set of three
265271
//consecutive data points which form an angle such that: angle < (PI−EPSILON)
@@ -698,19 +704,6 @@ public void testLIC8() {
698704
* A PTS+B PTS ≤ (NUMPOINTS−3)
699705
*/
700706

701-
Point[] points1 = new Point[] {
702-
new Point(0, 10),
703-
new Point(0, 0),
704-
new Point(-10, 0),
705-
new Point(10, 0)
706-
};
707-
// Tests that false is returned when NUMPOINTS < 5
708-
Parameters p = new Parameters();
709-
p.RADIUS1 = 3;
710-
p.A_PTS = 1;
711-
p.B_PTS = 1;
712-
assertFalse(LIC.LIC8(points1.length, points1, p));
713-
714707
Point[] points2 = new Point[] {
715708
new Point(0, 10),
716709
new Point(0, 0),
@@ -719,23 +712,28 @@ public void testLIC8() {
719712
new Point(10, 0)
720713
};
721714

722-
715+
Parameters p = new Parameters();
723716
p.RADIUS1 = 1;
724717
p.A_PTS = 0;
725718
p.B_PTS = 1;
726719

727-
// Tests that false is returned when A PTS < 1
728-
assertFalse(LIC.LIC8(points2.length, points2, p));
729-
730-
// Tests that false is returned when B PTS < 1
720+
// Tests that IllegalArgumentException is thrown when A PTS < 1
721+
assertThrows(IllegalArgumentException.class, () -> {
722+
LIC.LIC8(points2.length, points2, p);
723+
});
724+
// Tests that IllegalArgumentException is thrown when B PTS < 1
731725
p.A_PTS = 1;
732726
p.B_PTS = 0;
733-
assertFalse(LIC.LIC8(points2.length, points2, p));
727+
assertThrows(IllegalArgumentException.class, () -> {
728+
LIC.LIC8(points2.length, points2, p);
729+
});
734730

735-
// Tests that false is returned when A PTS + B PTS > (NUMPOINTS-3)
731+
// Tests that IllegalArgumentException is thrown when A PTS + B PTS > (NUMPOINTS-3)
736732
p.A_PTS = 2;
737733
p.B_PTS = 1;
738-
assertFalse(LIC.LIC8(points2.length, points2, p));
734+
assertThrows(IllegalArgumentException.class, () -> {
735+
LIC.LIC8(points2.length, points2, p);
736+
});
739737

740738
// Tests that false is returned when there exists a set of three data points
741739
// separated by exactly A PTS and B PTS
@@ -763,9 +761,11 @@ public void testLIC8() {
763761
p.RADIUS1 = 1;
764762
assertFalse(LIC.LIC8(points3.length, points3, p));
765763

766-
// Tests that false is returned if RADIUS1 < 0:
764+
// Tests that IllegalArgumentException is thrown when RADIUS1 < 0:
767765
p.RADIUS1 = -1;
768-
assertFalse(LIC.LIC8(points2.length, points2, p));
766+
assertThrows(IllegalArgumentException.class, () -> {
767+
LIC.LIC8(points2.length, points2, p);
768+
});
769769

770770
p.RADIUS1 = 1;
771771
// Tests that true is returned for a valid input
@@ -1068,11 +1068,6 @@ public void testLIC11() {
10681068
new Point(-1, 1)
10691069
};
10701070

1071-
Point[] points2 = new Point[] {
1072-
new Point(1, -1),
1073-
new Point(-1, 1)
1074-
};
1075-
10761071
Point[] points3 = new Point[] {
10771072
new Point(-1, -1),
10781073
new Point(0, 0),
@@ -1086,22 +1081,23 @@ public void testLIC11() {
10861081

10871082
};
10881083
Parameters p = new Parameters();
1089-
p.G_PTS = 1;
1090-
1091-
//Tests that false is returned when NUMPOINTS < 3
1092-
assertFalse(LIC.LIC11(points2.length, points2, p));
10931084

1094-
//Tests that false is returned when G_PTS < 1
10951085
p.G_PTS = 0;
1096-
assertFalse(LIC.LIC11(points1.length, points1, p));
1086+
// Tests that IllegalArgumentException is thrown when G_PTS < 1
1087+
assertThrows(IllegalArgumentException.class, () -> {
1088+
LIC.LIC11(points1.length, points1, p);
1089+
});
10971090

1098-
//Tests that false is returned when NUMPOINTS-2 < G_PTS
10991091
p.G_PTS = 2;
1100-
assertFalse(LIC.LIC11(points1.length, points1, p));
1092+
// Tests that IllegalArgumentException is thrown when NUMPOINTS-2 < G_PTS
1093+
assertThrows(IllegalArgumentException.class, () -> {
1094+
LIC.LIC11(points1.length, points1, p);
1095+
});
11011096

11021097
//Tests that false is returned when there does not exist at least one set of two data points,
11031098
//(X[i],Y[i]) and (X[j],Y[j]), separated by
11041099
//exactly G PTS consecutive intervening points, such that X[j] - X[i] < 0. (where i < j )
1100+
p.G_PTS = 1;
11051101
assertFalse(LIC.LIC11(points3.length, points3, p));
11061102

11071103
//Tests that false is returned if there exists at least one set of two data points,

0 commit comments

Comments
 (0)