From ee57e1dfe6ccf8c7ab0f909c53f4918313f324a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnau=20Pelechano=20Garc=C3=ADa?= Date: Thu, 29 Jan 2026 11:38:43 +0100 Subject: [PATCH] feat: Add IllegalArgumentException in LICs 3, 9, 12 and change unit test (#168) closes #168 --- src/main/java/LIC.java | 25 ++++++----- src/test/java/LICTests.java | 86 ++++++++++++++++--------------------- 2 files changed, 50 insertions(+), 61 deletions(-) diff --git a/src/main/java/LIC.java b/src/main/java/LIC.java index 43246cd..fcdba0a 100644 --- a/src/main/java/LIC.java +++ b/src/main/java/LIC.java @@ -50,9 +50,10 @@ public static boolean LIC2(int numPoints, Point[] points, Parameters p) { return false; } - public static boolean LIC3(int numPoints, Point[] points, Parameters p) { - if (p.AREA1 < 0) return false; - if(numPoints == 2) return false; + public static boolean LIC3(int numPoints, Point[] points, Parameters p) throws IllegalArgumentException { + if (p.AREA1 < 0 || numPoints < 3) { + throw new IllegalArgumentException(); + } for (int i = 0; i < numPoints - 2; i++){ if(Point.triangleArea(points[i], points[i+1], points[i+2]) > p.AREA1){ return true; @@ -148,11 +149,10 @@ public static boolean LIC8(int numPoints, Point[] points, Parameters p) { return false; } - public static boolean LIC9(int numPoints, Point[] points, Parameters p){ - if(numPoints < 5) return false; - if(p.C_PTS < 1 || p.D_PTS < 1) return false; - if(p.C_PTS + p.D_PTS > numPoints - 3) return false; - if(p.EPSILON < 0 || p.EPSILON >= Math.PI) return false; + public static boolean LIC9(int numPoints, Point[] points, Parameters p) throws IllegalArgumentException { + if((p.C_PTS < 1 || p.D_PTS < 1) || (p.EPSILON < 0 || p.EPSILON >= Math.PI) || (p.C_PTS + p.D_PTS > numPoints - 3) || (numPoints < 5)){ + throw new IllegalArgumentException(); + } for(int i = 0; i + p.C_PTS + p.D_PTS + 2 < numPoints; i++){ @@ -204,11 +204,10 @@ public static boolean LIC11(int numPoints, Point[] points , Parameters p){ return false; } - public static boolean LIC12(int numPoints, Point[] points , Parameters p){ - if(numPoints < 3) return false; - if(p.LENGTH2 < 0) return false; - if(p.LENGTH1 < 0) return false; - if(p.K_PTS < 1 || p.K_PTS > numPoints -2) return false; + public static boolean LIC12(int numPoints, Point[] points , Parameters p) throws IllegalArgumentException { + if((p.LENGTH1 < 0) || (p.LENGTH2 < 0) || (p.K_PTS < 1 || p.K_PTS > numPoints -2) || (numPoints < 3)){ + throw new IllegalArgumentException(); + } boolean condA = false; /* Exists pair with distance > LENGTH1 */ boolean condB = false; /* Exists pair with distance < LENGTH2 */ diff --git a/src/test/java/LICTests.java b/src/test/java/LICTests.java index bb85277..45b1ce8 100644 --- a/src/test/java/LICTests.java +++ b/src/test/java/LICTests.java @@ -294,8 +294,8 @@ public void LIC3_true_Triangle_GreaterThan_AREA1() { } @Test - public void LIC3_false_InputInvalid_AREA1_Negative() { - /* Contract: If AREA1 < 0 (invalid input), LIC3 must return false */ + public void LIC3_InputInvalid_AREA1_Negative() { + /* Contract: If AREA1 < 0 (invalid input), the method must throw IllegalArgumentException */ int numPoints = 3; Point[] points = new Point[] { new Point(0, 0), new Point(4,0), new Point(0,3) @@ -304,7 +304,7 @@ public void LIC3_false_InputInvalid_AREA1_Negative() { Parameters p = new Parameters(); p.AREA1 = -1.0; - assertFalse(LIC.LIC3(numPoints, points, p)); + assertThrows(IllegalArgumentException.class, () -> { LIC.LIC3(numPoints, points, p); }); } @Test @@ -329,12 +329,9 @@ public void LIC3_false_Triangle_NotGreaterThan_AREA1() { } @Test - public void LIC3_false_InputInvalid_numPoints_Equals_2() { - /* - * Contract: If there are 2 points, LIC3 has no valid triple consecutive points - * to evaluate, - * therefore it must return false. - */ + public void LIC3_InputInvalid_numPoints_LessThan_3() { + /* Contract: If there are 2 points, LIC3 has no valid triple consecutive points + to evaluate, therefore it must throw an IllegalArgumentException */ int numPoints = 2; @@ -346,7 +343,7 @@ public void LIC3_false_InputInvalid_numPoints_Equals_2() { Parameters p = new Parameters(); p.AREA1 = 0.0; - assertFalse(LIC.LIC3(numPoints, points, p)); + assertThrows(IllegalArgumentException.class, () -> { LIC.LIC3(numPoints, points, p); }); } @Test @@ -775,8 +772,8 @@ public void testLIC8() { // LIC9 Unit Tests @Test - public void LIC9_false_when_numPoint_LessThan_5() { - /* Contract: If numPoints < 5, returns false (invalid input) */ + public void LIC9_invalidInput_when_numPoint_LessThan_5() { + /* Contract: If numPoints < 5, must throw IllegalArgumentException */ int numPoints = 4; Point[] points = new Point[] { @@ -788,12 +785,12 @@ public void LIC9_false_when_numPoint_LessThan_5() { p.D_PTS = 1; p.EPSILON = 0.1; - assertFalse(LIC.LIC9(numPoints, points, p)); + assertThrows(IllegalArgumentException.class, () -> { LIC.LIC9(numPoints, points, p); }); } @Test - public void LIC9_false_whenC_PTSorD_PTS_Invalid() { - /* Contract: If C_PTS < 1 or D_PTS < 1, LCI9 returns false (invalid input) */ + public void LIC9_whenC_PTSorD_PTS_InvalidInput() { + /* Contract: If C_PTS < 1 or D_PTS < 1, LCI9 (invalid input) must throw IllegalArgumentException */ int numPoints = 5; Point[] points = new Point[] { @@ -805,18 +802,18 @@ public void LIC9_false_whenC_PTSorD_PTS_Invalid() { p1.D_PTS = 1; p1.EPSILON = 0.1; - assertFalse(LIC.LIC9(numPoints, points, p1)); + assertThrows(IllegalArgumentException.class, () -> { LIC.LIC9(numPoints, points, p1); }); Parameters p2 = new Parameters(); p2.C_PTS = 1; p2.D_PTS = 0; p2.EPSILON = 0.1; - assertFalse(LIC.LIC9(numPoints, points, p2)); + assertThrows(IllegalArgumentException.class, () -> { LIC.LIC9(numPoints, points, p2); }); } @Test - public void LIC9_false_when_EPSILON_OutOfRange() { - /* Contract: Given EPSILON outside [0, PI), LIC9 must return FALSE */ + public void LIC9_InvalidInput_when_EPSILON_OutOfRange() { + /* Contract: Given EPSILON outside [0, PI), LIC9 must return throw IllegalArgumentException */ Point[] points = new Point[] { new Point(0,0), new Point(1,0), new Point(2,0), new Point(2,1), new Point(3,1) }; @@ -827,22 +824,19 @@ public void LIC9_false_when_EPSILON_OutOfRange() { p1.C_PTS = 1; p1.D_PTS = 1; p1.EPSILON = -0.1; - assertFalse(LIC.LIC9(numPoints, points, p1)); + assertThrows(IllegalArgumentException.class, () -> { LIC.LIC9(numPoints, points, p1); }); /* Case 2: EPSILON >= 1 */ Parameters p2 = new Parameters(); p2.C_PTS = 1; p2.D_PTS = 1; p2.EPSILON = Math.PI; - assertFalse(LIC.LIC9(numPoints, points, p2)); + assertThrows(IllegalArgumentException.class, () -> { LIC.LIC9(numPoints, points, p2); }); } @Test - public void LIC9_false_When_C_PTSplusD_PTS_GreaterThan_numPoints_minus_three() { - /* - * Contract: If C_PTS + D_PTS > numPoints - 3, LIC9 returns false (invalid - * input) - */ + public void LIC9_invalidInput_When_C_PTSplusD_PTS_GreaterThan_numPoints_minus_three() { + /* Contract: If C_PTS + D_PTS > numPoints - 3, LIC9 (invalid input) must throw IllegallArgumentException */ int numPoints = 5; Point[] points = new Point[] { @@ -854,16 +848,13 @@ public void LIC9_false_When_C_PTSplusD_PTS_GreaterThan_numPoints_minus_three() { p.D_PTS = 1; p.EPSILON = 0.1; - assertFalse(LIC.LIC9(numPoints, points, p)); + assertThrows(IllegalArgumentException.class, () -> { LIC.LIC9(numPoints, points, p); }); } @Test public void LIC9_true_When_AngleOutside_PIPlusMinusEpsilon() { - /* - * Contract: LIC9 returns true iff there exists points A,B,C with the required - * spacing - * such that the angle at B is < (PI - EPSILON) or > (PI + EPSILON) - */ + /* Contract: LIC9 returns true iff there exists points A,B,C with the required spacing + such that the angle at B is < (PI - EPSILON) or > (PI + EPSILON) */ int numPoints = 5; @@ -885,10 +876,8 @@ public void LIC9_true_When_AngleOutside_PIPlusMinusEpsilon() { @Test public void LIC9_false_AllEvaluatingAngles_AreWithin_PIPlusMinusEpsilon() { - /* - * Contract: If for every valid triple A,B,C (with required spacing), - * the angle at B lies within [PI - EPSILON, PI + EPSILON], LIC9 returns false - */ + /* Contract: If for every valid triple A,B,C (with required spacing), + the angle at B lies within [PI - EPSILON, PI + EPSILON], LIC9 returns false */ int numPoints = 5; @@ -1145,8 +1134,8 @@ B. There exists at least one (possibly different) pair of points (j, j+K_PTS+1) } @Test - public void LIC12_false_K_pts_OutOfRange() { - /* Contract: Given invalid K_PTS (outside [1, numPoints -2]), LIC12 must return false */ + public void LIC12_invalid_K_pts_OutOfRange() { + /* Contract: Given invalid K_PTS (outside [1, numPoints -2]), LIC12 (invalid input) must throw IllegalArgumentException */ Parameters p = new Parameters(); p.K_PTS = 0; @@ -1158,7 +1147,7 @@ public void LIC12_false_K_pts_OutOfRange() { }; int numPoints = points.length; - assertFalse(LIC.LIC12(numPoints, points, p)); + assertThrows(IllegalArgumentException.class, () -> { LIC.LIC12(numPoints, points, p); }); } @Test @@ -1211,8 +1200,8 @@ If condition B holds (exists pair with distance < LENGTH2) but condition A does } @Test - public void LIC12_false_when_numPoints_lessThan_3() { - /* Contract: When numPoints < 3, the condition is not met and must return false. */ + public void LIC12_invalidInput_when_numPoints_lessThan_3() { + /* Contract: When numPoints < 3, the condition is not met and must throw IllegalArgumentException. */ int numPoints = 2; @@ -1226,12 +1215,13 @@ public void LIC12_false_when_numPoints_lessThan_3() { new Point(1, 0) }; - assertFalse(LIC.LIC12(numPoints, points, p)); + assertThrows(IllegalArgumentException.class, () -> { LIC.LIC12(numPoints, points, p); }); + } @Test - public void LIC12_false_when_LENGTH2_IsNegative() { - /* Contract: LENGTH2 must be non-negative. If LENGTH2 < 0 (invalid input), LIC12 must return false */ + public void LIC12_invalidInput_when_LENGTH2_IsNegative() { + /* Contract: LENGTH2 must be non-negative. If LENGTH2 < 0 (invalid input), LIC12 must throw IllegalArgumentException */ int numPoints = 3; @@ -1246,13 +1236,13 @@ public void LIC12_false_when_LENGTH2_IsNegative() { new Point(6, 0) }; - assertFalse(LIC.LIC12(numPoints, points, p)); + assertThrows(IllegalArgumentException.class, () -> { LIC.LIC12(numPoints, points, p); }); } @Test - public void LIC12_false_when_LENGTH1_is_Negative() { - /* Contract: Given LENGTH1 < 0, LIC12 must return false */ + public void LIC12_invalid_when_LENGTH1_is_Negative() { + /* Contract: Given LENGTH1 < 0, LIC12 must show IllegalArgumentException */ Parameters p = new Parameters(); p.K_PTS = 1; p.LENGTH1 = -0.5; @@ -1263,7 +1253,7 @@ public void LIC12_false_when_LENGTH1_is_Negative() { }; int numPoints = points.length; - assertFalse(LIC.LIC12(numPoints, points, p)); + assertThrows(IllegalArgumentException.class, () -> { LIC.LIC12(numPoints, points, p); }); } @Test