Skip to content

Commit 3b3f45d

Browse files
committed
feat: implement LIC 4,5 and 14 throw exception on bad input with respective unit tests #167
closes #167
1 parent eeba1ce commit 3b3f45d

2 files changed

Lines changed: 134 additions & 10 deletions

File tree

src/main/java/LIC.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ public static boolean LIC3(int numPoints, Point[] points, Parameters p) {
5858

5959
public static boolean LIC4(int numPoints, Point[] points, Parameters p) {
6060
if (p.Q_PTS > numPoints || p.Q_PTS < 2) {
61-
return false;
61+
throw new IllegalArgumentException();
6262
}
6363
if (p.QUADS < 1 || p.QUADS > 3) {
64-
return false;
64+
throw new IllegalArgumentException();
6565
}
6666

6767
for (int i = 0; i <= numPoints - p.Q_PTS; i++) {
@@ -79,6 +79,9 @@ public static boolean LIC4(int numPoints, Point[] points, Parameters p) {
7979
}
8080

8181
public static boolean LIC5(int numPoints, Point[] points, Parameters p) {
82+
if (numPoints < 2) {
83+
throw new IllegalArgumentException();
84+
}
8285
for (int i = 0; i < numPoints - 1; i++) {
8386
if(points[i+1].x - points[i].x < 0) {
8487
return true;
@@ -226,11 +229,14 @@ public static boolean LIC13(int numPoints, Point[] points, Parameters p) {
226229
}
227230

228231
public static boolean LIC14(int numPoints, Point[] points, Parameters p) {
229-
if (numPoints < 5 || p.AREA1 < 0 || p.AREA2 < 0 || p.E_PTS < 1 || p.F_PTS < 1) {
232+
if (numPoints < 5) {
230233
return false;
234+
}
235+
if (p.AREA1 < 0 || p.AREA2 < 0 || p.E_PTS < 1 || p.F_PTS < 1) {
236+
throw new IllegalArgumentException();
231237
}
232238
if (p.E_PTS + p.F_PTS + 3 > numPoints) {
233-
return false;
239+
throw new IllegalArgumentException();
234240
}
235241

236242
boolean moreThanArea1 = false;

src/test/java/LICTests.java

Lines changed: 124 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,34 @@ void LIC4_true_five_consecutive_points_three_quadrants() {
356356
}
357357

358358
@Test
359-
void LIC4_false_on_bad_input() {
359+
void LIC4_throws_on_too_few_points() {
360360
/*
361361
* Contract: LIC4 has constraints: 2 <= Q_PTS <= NUMPOINTS, 1 <= QUADS <= 3.
362-
Set Q_PTS = 5, QUADS = 4, with 4 points.
362+
* Set Q_PTS = 5, QUADS = 3, with 4 points.
363363
*/
364364
Parameters p = new Parameters();
365365
p.Q_PTS = 5;
366+
p.QUADS = 3;
367+
368+
Point[] points = new Point[] {
369+
new Point(0, 0),
370+
new Point(1, 0),
371+
new Point(-1, 0),
372+
new Point(0, -2),
373+
};
374+
assertThrows(IllegalArgumentException.class, () -> {
375+
LIC.LIC4(points.length, points, p);
376+
});
377+
}
378+
379+
@Test
380+
void LIC4_throws_on_too_many_quads() {
381+
/*
382+
* Contract: LIC4 has constraints: 2 <= Q_PTS <= NUMPOINTS, 1 <= QUADS <= 3.
383+
* Set Q_PTS = 3, QUADS = 4, with 4 points.
384+
*/
385+
Parameters p = new Parameters();
386+
p.Q_PTS = 3;
366387
p.QUADS = 4;
367388

368389
Point[] points = new Point[] {
@@ -371,11 +392,13 @@ void LIC4_false_on_bad_input() {
371392
new Point(-1, 0),
372393
new Point(0, -2),
373394
};
374-
assertFalse(LIC.LIC4(points.length, points, p));
395+
assertThrows(IllegalArgumentException.class, () -> {
396+
LIC.LIC4(points.length, points, p);
397+
});
375398
}
376399

377400
@Test
378-
void LIC4_false_five_consecutive_points_too_few_quadrants() {
401+
void LIC4_throws_on_too_few_quadrants() {
379402
/*
380403
* Contract: LIC4 is false when the are not Q_PTS number of consecutive points that are contained in more than QUADS quadrants.
381404
Set Q_PTS=5, QUADS=2, with an array of five points that are contained in two quadrants.
@@ -428,6 +451,22 @@ void LIC5_false_two_consecutive_points_x_difference_larger_than_zero() {
428451
assertFalse(LIC.LIC5(points.length, points, p));
429452
}
430453

454+
@Test
455+
void LIC5_throws_on_too_few_points() {
456+
/*
457+
* Contract: LIC5 needs at least two points to evaluate.
458+
* Use only 1 point.
459+
*/
460+
Parameters p = new Parameters();
461+
462+
Point[] points = new Point[] {
463+
new Point(0, 0)
464+
};
465+
assertThrows(IllegalArgumentException.class, () -> {
466+
LIC.LIC5(points.length, points, p);
467+
});
468+
}
469+
431470
@Test
432471
public void LIC6_false_with_points_too_close() {
433472
/*
@@ -1316,10 +1355,10 @@ void LIC14_false_too_few_points() {
13161355
}
13171356

13181357
@Test
1319-
void LIC14_false_on_bad_input() {
1358+
void LIC14_false_on_too_few_points() {
13201359
/*
13211360
* Contract: LIC14 is false when NUMPOINTS < 5
1322-
* Use 4 points to test.
1361+
* Use 4 points to test, with otherwise correct parameter setup.
13231362
*/
13241363
Parameters p = new Parameters();
13251364
p.E_PTS = 1;
@@ -1336,4 +1375,83 @@ void LIC14_false_on_bad_input() {
13361375

13371376
assertFalse(LIC.LIC14(points.length, points, p));
13381377
}
1378+
1379+
@Test
1380+
void LIC14_throws_on_negative_area() {
1381+
/*
1382+
* Contract: Negative area is not a correct input.
1383+
* Set negative areas, with otherwise correct parameters and point setup.
1384+
*/
1385+
Parameters p = new Parameters();
1386+
p.E_PTS = 1;
1387+
p.F_PTS = 1;
1388+
p.AREA1 = -0.9;
1389+
p.AREA2 = -0.5;
1390+
1391+
Point[] points = new Point[]{
1392+
new Point(0,0),
1393+
new Point(2,0),
1394+
new Point(1,2),
1395+
new Point(2.5,1),
1396+
new Point(1,0),
1397+
new Point(2.5,0)
1398+
};
1399+
1400+
assertThrows(IllegalArgumentException.class, () -> {
1401+
LIC.LIC14(points.length, points, p);
1402+
});
1403+
}
1404+
1405+
@Test
1406+
void LIC14_throws_on_too_large_E_PTS_and_F_PTS() {
1407+
/*
1408+
* Contract: We require a minimum of E_PTS+F_PTS+3 points to evaluate LIC14.
1409+
* Set E_PTS=F_PTS=2, which requires a minimum of 7 points to evaluate, use
1410+
* 6 points. Otherwise use correct parameters and point setup.
1411+
*/
1412+
Parameters p = new Parameters();
1413+
p.E_PTS = 2;
1414+
p.F_PTS = 2;
1415+
p.AREA1 = 0.9;
1416+
p.AREA2 = 0.5;
1417+
1418+
Point[] points = new Point[]{
1419+
new Point(0,0),
1420+
new Point(2,0),
1421+
new Point(1,2),
1422+
new Point(2.5,1),
1423+
new Point(1,0),
1424+
new Point(2.5,0)
1425+
};
1426+
1427+
assertThrows(IllegalArgumentException.class, () -> {
1428+
LIC.LIC14(points.length, points, p);
1429+
});
1430+
}
1431+
1432+
@Test
1433+
void LIC14_throws_on_too_small_E_PTS_and_F_PTS() {
1434+
/*
1435+
* Contract: E_PTS >= 1 AND F_PTS >= 1.
1436+
* Set E_PTS=F_PTS=0, with otherwise correct parameters and point setup.
1437+
*/
1438+
Parameters p = new Parameters();
1439+
p.E_PTS = 0;
1440+
p.F_PTS = 0;
1441+
p.AREA1 = 0.9;
1442+
p.AREA2 = 0.5;
1443+
1444+
Point[] points = new Point[]{
1445+
new Point(0,0),
1446+
new Point(2,0),
1447+
new Point(1,2),
1448+
new Point(2.5,1),
1449+
new Point(1,0),
1450+
new Point(2.5,0)
1451+
};
1452+
1453+
assertThrows(IllegalArgumentException.class, () -> {
1454+
LIC.LIC14(points.length, points, p);
1455+
});
1456+
}
13391457
}

0 commit comments

Comments
 (0)