Skip to content

Commit 8070aca

Browse files
authored
feat: add excpetions to LIC1, LIC7, LIC13 and change unit tests (#164) (#172)
close #164
1 parent eeba1ce commit 8070aca

2 files changed

Lines changed: 196 additions & 45 deletions

File tree

src/main/java/LIC.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ public static boolean LIC0(int numPoints, Point[] points, Parameters p) throws I
1313
return false;
1414
}
1515

16-
public static boolean LIC1(int numPoints, Point[] points, Parameters p) {
17-
if (numPoints < 3 || p.RADIUS1 < 0)
18-
return false;
16+
public static boolean LIC1(int numPoints, Point[] points, Parameters p) throws IllegalArgumentException {
17+
if (p.RADIUS1 < 0) {
18+
throw new IllegalArgumentException();
19+
}
20+
21+
if (numPoints < 3) return false;
22+
23+
1924
for (int i = 0; i < numPoints - 2; i++) {
2025
if (Point.circleRadius(points[i], points[i + 1], points[i + 2]) > p.RADIUS1)
2126
return true;
@@ -107,8 +112,16 @@ public static boolean LIC6(int numPoints, Point[] points, Parameters p) throws I
107112
return false;
108113
}
109114

110-
public static boolean LIC7(int numPoints, Point[] points, Parameters p) {
111-
if (numPoints < 3 || p.K_PTS < 1 || p.K_PTS > numPoints - 2 || p.LENGTH1 < 0) return false;
115+
public static boolean LIC7(int numPoints, Point[] points, Parameters p) throws IllegalArgumentException {
116+
117+
if (p.K_PTS < 1 || p.K_PTS > numPoints - 2) {
118+
throw new IllegalArgumentException();
119+
}
120+
if (p.LENGTH1 < 0) {
121+
throw new IllegalArgumentException();
122+
}
123+
124+
if (numPoints < 3) return false;
112125

113126
for (int i = 0; i < numPoints - p.K_PTS - 1; i++) {
114127
if (Point.distance(points[i], points[i + p.K_PTS + 1]) > p.LENGTH1)
@@ -210,8 +223,19 @@ public static boolean LIC12(int numPoints, Point[] points , Parameters p){
210223
return false;
211224
}
212225

213-
public static boolean LIC13(int numPoints, Point[] points, Parameters p) {
214-
if (numPoints < 5 || p.RADIUS2 < 0 || p.RADIUS1 < 0 || p.A_PTS < 1 || p.B_PTS < 1) return false;
226+
public static boolean LIC13(int numPoints, Point[] points, Parameters p) throws IllegalArgumentException {
227+
if (p.RADIUS2 < 0) {
228+
throw new IllegalArgumentException();
229+
}
230+
if (p.RADIUS1 < 0) {
231+
throw new IllegalArgumentException();
232+
}
233+
if (p.A_PTS < 1 || p.B_PTS < 1 || p.A_PTS + p.B_PTS > numPoints - 3) {
234+
throw new IllegalArgumentException();
235+
}
236+
237+
238+
if (numPoints < 5) return false;
215239

216240
boolean not_contained_flag = false, contained_flag = false;
217241
for (int i = 0; i < numPoints - p.A_PTS - p.B_PTS - 2; i++) {

src/test/java/LICTests.java

Lines changed: 165 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,26 @@ void LIC0_throws_with_length_too_small() {
163163
assertThrows(IllegalArgumentException.class, () -> LIC.LIC0(pts.length, pts, p));
164164
}
165165

166+
167+
@Test
168+
/*
169+
* Contract: Method throws exception due to RADIUS1 being invalid since it is negative
170+
*/
171+
void testLIC1InvalidRADIUS1() {
172+
Point[] points = new Point[] {
173+
new Point(2, 2),
174+
new Point(8, 2),
175+
new Point(6, 5)
176+
};
177+
Parameters p = new Parameters();
178+
p.RADIUS1 = -1;
179+
180+
assertThrows(IllegalArgumentException.class, () -> {
181+
LIC.LIC1(points.length, points, p);
182+
});
183+
184+
}
185+
166186
@Test
167187
/*
168188
* Contract: LIC1 is true iff there exists 3 consecutive points that
@@ -176,13 +196,8 @@ void testLIC1() {
176196
new Point(6, 5)
177197
};
178198
Parameters p = new Parameters();
179-
180-
p.RADIUS1 = -1;
181-
assertFalse(LIC.LIC1(points.length, points, p));
182199
p.RADIUS1 = 3;
183200

184-
assertFalse(LIC.LIC1(0, new Point[]{}, p));
185-
186201
assertTrue(LIC.LIC1(points.length, points, p));
187202
points[0].x = 2.1;
188203
assertFalse(LIC.LIC1(points.length, points, p));
@@ -556,6 +571,46 @@ public void LIC6_throws_exception_with_N_PTS_bigger_than_numpoints() {
556571
assertThrows(IllegalArgumentException.class, () -> LIC.LIC6(points2.length, points2, p));
557572
}
558573

574+
@Test
575+
public void testLIC7InvalidKPTS() {
576+
/*
577+
* Contract: Method throws exception due to K_PTS being invalid since it is sat 0
578+
*/
579+
Point[] points = new Point[] {
580+
new Point(2, 2),
581+
new Point(5, 5),
582+
new Point(8, 2)
583+
};
584+
585+
Parameters p = new Parameters();
586+
p.LENGTH1 = 1;
587+
p.K_PTS = 0;
588+
589+
assertThrows(IllegalArgumentException.class, () -> {
590+
LIC.LIC7(points.length, points, p);
591+
});
592+
}
593+
594+
@Test
595+
public void testLIC7InvalidLENGTH1() {
596+
/*
597+
* Contract: Method throws exception due to LENGTH1 being invalid since it is negtive
598+
*/
599+
Point[] points = new Point[] {
600+
new Point(2, 2),
601+
new Point(5, 5),
602+
new Point(8, 2)
603+
};
604+
605+
Parameters p = new Parameters();
606+
p.LENGTH1 = -1;
607+
p.K_PTS = 1;
608+
609+
assertThrows(IllegalArgumentException.class, () -> {
610+
LIC.LIC7(points.length, points, p);
611+
});
612+
}
613+
559614
@Test
560615
public void testLIC7() {
561616
/*
@@ -577,18 +632,9 @@ public void testLIC7() {
577632
new Point(-12, 5)
578633
};
579634

635+
580636
Parameters p = new Parameters();
581-
p.LENGTH1 = 1;
582-
p.K_PTS = 0;
583-
assertFalse(LIC.LIC7(points1.length, points1, p));
584637
p.K_PTS = 1;
585-
586-
assertFalse(LIC.LIC7(0, new Point[]{}, p));
587-
588-
p.LENGTH1 = -1;
589-
assertFalse(LIC.LIC7(points1.length, points1, p));
590-
591-
592638
p.LENGTH1 = 5.9;
593639
assertTrue(LIC.LIC7(points1.length, points1, p));
594640
p.LENGTH1 = 6;
@@ -1181,6 +1227,110 @@ public void LIC12_false_when_LENGTH1_is_Negative() {
11811227
assertFalse(LIC.LIC12(numPoints, points, p));
11821228
}
11831229

1230+
@Test
1231+
public void testLIC13InvalidAPTS() {
1232+
/*
1233+
* Contract: Method throws exception due to A_PTS being invalid since it is sat 0
1234+
*/
1235+
Point[] points = new Point[] {
1236+
new Point(2, 2),
1237+
new Point(99, 99),
1238+
new Point(8, 2),
1239+
new Point(100, 100),
1240+
new Point(6, 5),
1241+
new Point(101, 101),
1242+
new Point(5, 11)
1243+
};
1244+
1245+
Parameters p = new Parameters();
1246+
p.RADIUS1 = 3;
1247+
p.A_PTS = 0;
1248+
p.B_PTS = 1;
1249+
p.RADIUS2 = 1.5;
1250+
1251+
assertThrows(IllegalArgumentException.class, () -> {
1252+
LIC.LIC13(points.length, points, p);
1253+
});
1254+
}
1255+
1256+
@Test
1257+
public void testLIC13InvalidBPTS() {
1258+
/*
1259+
* Contract: Method throws exception due to B_PTS being invalid since it is sat 0
1260+
*/
1261+
Point[] points = new Point[] {
1262+
new Point(2, 2),
1263+
new Point(99, 99),
1264+
new Point(8, 2),
1265+
new Point(100, 100),
1266+
new Point(6, 5),
1267+
new Point(101, 101),
1268+
new Point(5, 11)
1269+
};
1270+
1271+
Parameters p = new Parameters();
1272+
p.RADIUS1 = 3;
1273+
p.A_PTS = 1;
1274+
p.B_PTS = 0;
1275+
p.RADIUS2 = 1.5;
1276+
1277+
assertThrows(IllegalArgumentException.class, () -> {
1278+
LIC.LIC13(points.length, points, p);
1279+
});
1280+
}
1281+
1282+
@Test
1283+
public void testLIC13InvalidRADIUS1() {
1284+
/*
1285+
* Contract: Method throws exception due to RADIUS1 being invalid since it is negative
1286+
*/
1287+
Point[] points = new Point[] {
1288+
new Point(2, 2),
1289+
new Point(99, 99),
1290+
new Point(8, 2),
1291+
new Point(100, 100),
1292+
new Point(6, 5),
1293+
new Point(101, 101),
1294+
new Point(5, 11)
1295+
};
1296+
1297+
Parameters p = new Parameters();
1298+
p.RADIUS1 = -3;
1299+
p.A_PTS = 1;
1300+
p.B_PTS = 1;
1301+
p.RADIUS2 = 1.5;
1302+
1303+
assertThrows(IllegalArgumentException.class, () -> {
1304+
LIC.LIC13(points.length, points, p);
1305+
});
1306+
}
1307+
1308+
@Test
1309+
public void testLIC13InvalidRADIUS2() {
1310+
/*
1311+
* Contract: Method throws exception due to RADIUS2 being invalid since it is negative
1312+
*/
1313+
Point[] points = new Point[] {
1314+
new Point(2, 2),
1315+
new Point(99, 99),
1316+
new Point(8, 2),
1317+
new Point(100, 100),
1318+
new Point(6, 5),
1319+
new Point(101, 101),
1320+
new Point(5, 11)
1321+
};
1322+
1323+
Parameters p = new Parameters();
1324+
p.RADIUS1 = 3;
1325+
p.A_PTS = 1;
1326+
p.B_PTS = 1;
1327+
p.RADIUS2 = -1.5;
1328+
1329+
assertThrows(IllegalArgumentException.class, () -> {
1330+
LIC.LIC13(points.length, points, p);
1331+
});
1332+
}
1333+
11841334
@Test
11851335
public void testLIC13() {
11861336
/*
@@ -1207,29 +1357,6 @@ public void testLIC13() {
12071357
System.out.println(radius3);
12081358

12091359
Parameters p = new Parameters();
1210-
p.RADIUS1 = -1;
1211-
p.A_PTS = 1;
1212-
p.B_PTS = 1;
1213-
p.RADIUS2 = 1.5;
1214-
1215-
assertFalse(LIC.LIC13(points.length, points, p));
1216-
p.RADIUS1 = 3;
1217-
1218-
p.A_PTS = 0;
1219-
assertFalse(LIC.LIC13(points.length, points, p));
1220-
p.A_PTS = 1;
1221-
1222-
p.B_PTS = 0;
1223-
assertFalse(LIC.LIC13(points.length, points, p));
1224-
p.B_PTS = 1;
1225-
1226-
p.RADIUS2 = -1;
1227-
assertFalse(LIC.LIC13(points.length, points, p));
1228-
p.RADIUS2 = 1.5;
1229-
1230-
assertFalse(LIC.LIC13(0, new Point[]{}, p));
1231-
1232-
12331360
p.RADIUS1 = 3;
12341361
p.A_PTS = 1;
12351362
p.B_PTS = 1;

0 commit comments

Comments
 (0)