From 3b3f45d6269cdeb86ef392e6623e2bb4ad08ed47 Mon Sep 17 00:00:00 2001 From: Jonte Date: Thu, 29 Jan 2026 10:58:27 +0100 Subject: [PATCH] feat: implement LIC 4,5 and 14 throw exception on bad input with respective unit tests #167 closes #167 --- src/main/java/LIC.java | 14 ++-- src/test/java/LICTests.java | 130 ++++++++++++++++++++++++++++++++++-- 2 files changed, 134 insertions(+), 10 deletions(-) diff --git a/src/main/java/LIC.java b/src/main/java/LIC.java index e3ee3c5..490a2e1 100644 --- a/src/main/java/LIC.java +++ b/src/main/java/LIC.java @@ -58,10 +58,10 @@ public static boolean LIC3(int numPoints, Point[] points, Parameters p) { public static boolean LIC4(int numPoints, Point[] points, Parameters p) { if (p.Q_PTS > numPoints || p.Q_PTS < 2) { - return false; + throw new IllegalArgumentException(); } if (p.QUADS < 1 || p.QUADS > 3) { - return false; + throw new IllegalArgumentException(); } for (int i = 0; i <= numPoints - p.Q_PTS; i++) { @@ -79,6 +79,9 @@ public static boolean LIC4(int numPoints, Point[] points, Parameters p) { } public static boolean LIC5(int numPoints, Point[] points, Parameters p) { + if (numPoints < 2) { + throw new IllegalArgumentException(); + } for (int i = 0; i < numPoints - 1; i++) { if(points[i+1].x - points[i].x < 0) { return true; @@ -226,11 +229,14 @@ public static boolean LIC13(int numPoints, Point[] points, Parameters p) { } public static boolean LIC14(int numPoints, Point[] points, Parameters p) { - if (numPoints < 5 || p.AREA1 < 0 || p.AREA2 < 0 || p.E_PTS < 1 || p.F_PTS < 1) { + if (numPoints < 5) { return false; + } + if (p.AREA1 < 0 || p.AREA2 < 0 || p.E_PTS < 1 || p.F_PTS < 1) { + throw new IllegalArgumentException(); } if (p.E_PTS + p.F_PTS + 3 > numPoints) { - return false; + throw new IllegalArgumentException(); } boolean moreThanArea1 = false; diff --git a/src/test/java/LICTests.java b/src/test/java/LICTests.java index f2f2426..3924e93 100644 --- a/src/test/java/LICTests.java +++ b/src/test/java/LICTests.java @@ -356,13 +356,34 @@ void LIC4_true_five_consecutive_points_three_quadrants() { } @Test - void LIC4_false_on_bad_input() { + void LIC4_throws_on_too_few_points() { /* * Contract: LIC4 has constraints: 2 <= Q_PTS <= NUMPOINTS, 1 <= QUADS <= 3. - Set Q_PTS = 5, QUADS = 4, with 4 points. + * Set Q_PTS = 5, QUADS = 3, with 4 points. */ Parameters p = new Parameters(); p.Q_PTS = 5; + p.QUADS = 3; + + Point[] points = new Point[] { + new Point(0, 0), + new Point(1, 0), + new Point(-1, 0), + new Point(0, -2), + }; + assertThrows(IllegalArgumentException.class, () -> { + LIC.LIC4(points.length, points, p); + }); + } + + @Test + void LIC4_throws_on_too_many_quads() { + /* + * Contract: LIC4 has constraints: 2 <= Q_PTS <= NUMPOINTS, 1 <= QUADS <= 3. + * Set Q_PTS = 3, QUADS = 4, with 4 points. + */ + Parameters p = new Parameters(); + p.Q_PTS = 3; p.QUADS = 4; Point[] points = new Point[] { @@ -371,11 +392,13 @@ void LIC4_false_on_bad_input() { new Point(-1, 0), new Point(0, -2), }; - assertFalse(LIC.LIC4(points.length, points, p)); + assertThrows(IllegalArgumentException.class, () -> { + LIC.LIC4(points.length, points, p); + }); } @Test - void LIC4_false_five_consecutive_points_too_few_quadrants() { + void LIC4_throws_on_too_few_quadrants() { /* * Contract: LIC4 is false when the are not Q_PTS number of consecutive points that are contained in more than QUADS quadrants. 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() { assertFalse(LIC.LIC5(points.length, points, p)); } + @Test + void LIC5_throws_on_too_few_points() { + /* + * Contract: LIC5 needs at least two points to evaluate. + * Use only 1 point. + */ + Parameters p = new Parameters(); + + Point[] points = new Point[] { + new Point(0, 0) + }; + assertThrows(IllegalArgumentException.class, () -> { + LIC.LIC5(points.length, points, p); + }); + } + @Test public void LIC6_false_with_points_too_close() { /* @@ -1316,10 +1355,10 @@ void LIC14_false_too_few_points() { } @Test - void LIC14_false_on_bad_input() { + void LIC14_false_on_too_few_points() { /* * Contract: LIC14 is false when NUMPOINTS < 5 - * Use 4 points to test. + * Use 4 points to test, with otherwise correct parameter setup. */ Parameters p = new Parameters(); p.E_PTS = 1; @@ -1336,4 +1375,83 @@ void LIC14_false_on_bad_input() { assertFalse(LIC.LIC14(points.length, points, p)); } + + @Test + void LIC14_throws_on_negative_area() { + /* + * Contract: Negative area is not a correct input. + * Set negative areas, with otherwise correct parameters and point setup. + */ + Parameters p = new Parameters(); + p.E_PTS = 1; + p.F_PTS = 1; + p.AREA1 = -0.9; + p.AREA2 = -0.5; + + Point[] points = new Point[]{ + new Point(0,0), + new Point(2,0), + new Point(1,2), + new Point(2.5,1), + new Point(1,0), + new Point(2.5,0) + }; + + assertThrows(IllegalArgumentException.class, () -> { + LIC.LIC14(points.length, points, p); + }); + } + + @Test + void LIC14_throws_on_too_large_E_PTS_and_F_PTS() { + /* + * Contract: We require a minimum of E_PTS+F_PTS+3 points to evaluate LIC14. + * Set E_PTS=F_PTS=2, which requires a minimum of 7 points to evaluate, use + * 6 points. Otherwise use correct parameters and point setup. + */ + Parameters p = new Parameters(); + p.E_PTS = 2; + p.F_PTS = 2; + p.AREA1 = 0.9; + p.AREA2 = 0.5; + + Point[] points = new Point[]{ + new Point(0,0), + new Point(2,0), + new Point(1,2), + new Point(2.5,1), + new Point(1,0), + new Point(2.5,0) + }; + + assertThrows(IllegalArgumentException.class, () -> { + LIC.LIC14(points.length, points, p); + }); + } + + @Test + void LIC14_throws_on_too_small_E_PTS_and_F_PTS() { + /* + * Contract: E_PTS >= 1 AND F_PTS >= 1. + * Set E_PTS=F_PTS=0, with otherwise correct parameters and point setup. + */ + Parameters p = new Parameters(); + p.E_PTS = 0; + p.F_PTS = 0; + p.AREA1 = 0.9; + p.AREA2 = 0.5; + + Point[] points = new Point[]{ + new Point(0,0), + new Point(2,0), + new Point(1,2), + new Point(2.5,1), + new Point(1,0), + new Point(2.5,0) + }; + + assertThrows(IllegalArgumentException.class, () -> { + LIC.LIC14(points.length, points, p); + }); + } }