From ad0455f12753bef12f2e6247d114f8323b127f88 Mon Sep 17 00:00:00 2001 From: daDevBoat Date: Thu, 29 Jan 2026 11:20:26 +0100 Subject: [PATCH] feat: add excpetions to LIC1, LIC7, LIC13 and change unit tests (#164) close #164 --- src/main/java/LIC.java | 38 +++++-- src/test/java/LICTests.java | 203 +++++++++++++++++++++++++++++------- 2 files changed, 196 insertions(+), 45 deletions(-) diff --git a/src/main/java/LIC.java b/src/main/java/LIC.java index e3ee3c5..227d636 100644 --- a/src/main/java/LIC.java +++ b/src/main/java/LIC.java @@ -13,9 +13,14 @@ public static boolean LIC0(int numPoints, Point[] points, Parameters p) throws I return false; } - public static boolean LIC1(int numPoints, Point[] points, Parameters p) { - if (numPoints < 3 || p.RADIUS1 < 0) - return false; + public static boolean LIC1(int numPoints, Point[] points, Parameters p) throws IllegalArgumentException { + if (p.RADIUS1 < 0) { + throw new IllegalArgumentException(); + } + + if (numPoints < 3) return false; + + for (int i = 0; i < numPoints - 2; i++) { if (Point.circleRadius(points[i], points[i + 1], points[i + 2]) > p.RADIUS1) return true; @@ -107,8 +112,16 @@ public static boolean LIC6(int numPoints, Point[] points, Parameters p) throws I return false; } - public static boolean LIC7(int numPoints, Point[] points, Parameters p) { - if (numPoints < 3 || p.K_PTS < 1 || p.K_PTS > numPoints - 2 || p.LENGTH1 < 0) return false; + public static boolean LIC7(int numPoints, Point[] points, Parameters p) throws IllegalArgumentException { + + if (p.K_PTS < 1 || p.K_PTS > numPoints - 2) { + throw new IllegalArgumentException(); + } + if (p.LENGTH1 < 0) { + throw new IllegalArgumentException(); + } + + if (numPoints < 3) return false; for (int i = 0; i < numPoints - p.K_PTS - 1; i++) { 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){ return false; } - public static boolean LIC13(int numPoints, Point[] points, Parameters p) { - if (numPoints < 5 || p.RADIUS2 < 0 || p.RADIUS1 < 0 || p.A_PTS < 1 || p.B_PTS < 1) return false; + public static boolean LIC13(int numPoints, Point[] points, Parameters p) throws IllegalArgumentException { + if (p.RADIUS2 < 0) { + throw new IllegalArgumentException(); + } + if (p.RADIUS1 < 0) { + throw new IllegalArgumentException(); + } + if (p.A_PTS < 1 || p.B_PTS < 1 || p.A_PTS + p.B_PTS > numPoints - 3) { + throw new IllegalArgumentException(); + } + + + if (numPoints < 5) return false; boolean not_contained_flag = false, contained_flag = false; for (int i = 0; i < numPoints - p.A_PTS - p.B_PTS - 2; i++) { diff --git a/src/test/java/LICTests.java b/src/test/java/LICTests.java index f2f2426..ab1e150 100644 --- a/src/test/java/LICTests.java +++ b/src/test/java/LICTests.java @@ -163,6 +163,26 @@ void LIC0_throws_with_length_too_small() { assertThrows(IllegalArgumentException.class, () -> LIC.LIC0(pts.length, pts, p)); } + + @Test + /* + * Contract: Method throws exception due to RADIUS1 being invalid since it is negative + */ + void testLIC1InvalidRADIUS1() { + Point[] points = new Point[] { + new Point(2, 2), + new Point(8, 2), + new Point(6, 5) + }; + Parameters p = new Parameters(); + p.RADIUS1 = -1; + + assertThrows(IllegalArgumentException.class, () -> { + LIC.LIC1(points.length, points, p); + }); + + } + @Test /* * Contract: LIC1 is true iff there exists 3 consecutive points that @@ -176,13 +196,8 @@ void testLIC1() { new Point(6, 5) }; Parameters p = new Parameters(); - - p.RADIUS1 = -1; - assertFalse(LIC.LIC1(points.length, points, p)); p.RADIUS1 = 3; - assertFalse(LIC.LIC1(0, new Point[]{}, p)); - assertTrue(LIC.LIC1(points.length, points, p)); points[0].x = 2.1; assertFalse(LIC.LIC1(points.length, points, p)); @@ -556,6 +571,46 @@ public void LIC6_throws_exception_with_N_PTS_bigger_than_numpoints() { assertThrows(IllegalArgumentException.class, () -> LIC.LIC6(points2.length, points2, p)); } + @Test + public void testLIC7InvalidKPTS() { + /* + * Contract: Method throws exception due to K_PTS being invalid since it is sat 0 + */ + Point[] points = new Point[] { + new Point(2, 2), + new Point(5, 5), + new Point(8, 2) + }; + + Parameters p = new Parameters(); + p.LENGTH1 = 1; + p.K_PTS = 0; + + assertThrows(IllegalArgumentException.class, () -> { + LIC.LIC7(points.length, points, p); + }); + } + + @Test + public void testLIC7InvalidLENGTH1() { + /* + * Contract: Method throws exception due to LENGTH1 being invalid since it is negtive + */ + Point[] points = new Point[] { + new Point(2, 2), + new Point(5, 5), + new Point(8, 2) + }; + + Parameters p = new Parameters(); + p.LENGTH1 = -1; + p.K_PTS = 1; + + assertThrows(IllegalArgumentException.class, () -> { + LIC.LIC7(points.length, points, p); + }); + } + @Test public void testLIC7() { /* @@ -577,18 +632,9 @@ public void testLIC7() { new Point(-12, 5) }; + Parameters p = new Parameters(); - p.LENGTH1 = 1; - p.K_PTS = 0; - assertFalse(LIC.LIC7(points1.length, points1, p)); p.K_PTS = 1; - - assertFalse(LIC.LIC7(0, new Point[]{}, p)); - - p.LENGTH1 = -1; - assertFalse(LIC.LIC7(points1.length, points1, p)); - - p.LENGTH1 = 5.9; assertTrue(LIC.LIC7(points1.length, points1, p)); p.LENGTH1 = 6; @@ -1181,6 +1227,110 @@ public void LIC12_false_when_LENGTH1_is_Negative() { assertFalse(LIC.LIC12(numPoints, points, p)); } + @Test + public void testLIC13InvalidAPTS() { + /* + * Contract: Method throws exception due to A_PTS being invalid since it is sat 0 + */ + Point[] points = new Point[] { + new Point(2, 2), + new Point(99, 99), + new Point(8, 2), + new Point(100, 100), + new Point(6, 5), + new Point(101, 101), + new Point(5, 11) + }; + + Parameters p = new Parameters(); + p.RADIUS1 = 3; + p.A_PTS = 0; + p.B_PTS = 1; + p.RADIUS2 = 1.5; + + assertThrows(IllegalArgumentException.class, () -> { + LIC.LIC13(points.length, points, p); + }); + } + + @Test + public void testLIC13InvalidBPTS() { + /* + * Contract: Method throws exception due to B_PTS being invalid since it is sat 0 + */ + Point[] points = new Point[] { + new Point(2, 2), + new Point(99, 99), + new Point(8, 2), + new Point(100, 100), + new Point(6, 5), + new Point(101, 101), + new Point(5, 11) + }; + + Parameters p = new Parameters(); + p.RADIUS1 = 3; + p.A_PTS = 1; + p.B_PTS = 0; + p.RADIUS2 = 1.5; + + assertThrows(IllegalArgumentException.class, () -> { + LIC.LIC13(points.length, points, p); + }); + } + + @Test + public void testLIC13InvalidRADIUS1() { + /* + * Contract: Method throws exception due to RADIUS1 being invalid since it is negative + */ + Point[] points = new Point[] { + new Point(2, 2), + new Point(99, 99), + new Point(8, 2), + new Point(100, 100), + new Point(6, 5), + new Point(101, 101), + new Point(5, 11) + }; + + Parameters p = new Parameters(); + p.RADIUS1 = -3; + p.A_PTS = 1; + p.B_PTS = 1; + p.RADIUS2 = 1.5; + + assertThrows(IllegalArgumentException.class, () -> { + LIC.LIC13(points.length, points, p); + }); + } + + @Test + public void testLIC13InvalidRADIUS2() { + /* + * Contract: Method throws exception due to RADIUS2 being invalid since it is negative + */ + Point[] points = new Point[] { + new Point(2, 2), + new Point(99, 99), + new Point(8, 2), + new Point(100, 100), + new Point(6, 5), + new Point(101, 101), + new Point(5, 11) + }; + + Parameters p = new Parameters(); + p.RADIUS1 = 3; + p.A_PTS = 1; + p.B_PTS = 1; + p.RADIUS2 = -1.5; + + assertThrows(IllegalArgumentException.class, () -> { + LIC.LIC13(points.length, points, p); + }); + } + @Test public void testLIC13() { /* @@ -1207,29 +1357,6 @@ public void testLIC13() { System.out.println(radius3); Parameters p = new Parameters(); - p.RADIUS1 = -1; - p.A_PTS = 1; - p.B_PTS = 1; - p.RADIUS2 = 1.5; - - assertFalse(LIC.LIC13(points.length, points, p)); - p.RADIUS1 = 3; - - p.A_PTS = 0; - assertFalse(LIC.LIC13(points.length, points, p)); - p.A_PTS = 1; - - p.B_PTS = 0; - assertFalse(LIC.LIC13(points.length, points, p)); - p.B_PTS = 1; - - p.RADIUS2 = -1; - assertFalse(LIC.LIC13(points.length, points, p)); - p.RADIUS2 = 1.5; - - assertFalse(LIC.LIC13(0, new Point[]{}, p)); - - p.RADIUS1 = 3; p.A_PTS = 1; p.B_PTS = 1;