q2 et q4( à faire test et mutation)

This commit is contained in:
tuanvu
2025-12-05 12:46:46 +01:00
parent d6bd1db6bc
commit 001b3071c7
3 changed files with 354 additions and 7 deletions

View File

@@ -1,17 +1,126 @@
package fr.istic.vv;
class Date implements Comparable<Date> {
private int day;
private int month;
private int year;
public Date(int day, int month, int year) { }
public Date(int day, int month, int year) {
if (isValidDate(day, month, year)) {
this.day = day;
this.month = month;
this.year = year;
} else {
throw new IllegalArgumentException("Invalid date");
}
}
public static boolean isValidDate(int day, int month, int year) { return false; }
public static boolean isValidDate(int day, int month, int year) {
if (month < 1 || month > 12 || day < 1 || day > 31)
return false;
int max_day = max_day(month, year);
if (day > max_day)
return false;
return true;
}
public static boolean isLeapYear(int year) { return false; }
public static boolean isLeapYear(int year) {
return year % 4 == 0;
}
public Date nextDate() { return null; }
public Date nextDate() {
int d = this.getDay();
int m = this.getMonth();
int y = this.getYear();
int max_day = max_day(m, y);
if (d < max_day) {
return new Date(d + 1, m, y);
} else if (d == max_day) {
if (m == 12) {
return new Date(1, 1, y + 1);
} else {
return new Date(1, m + 1, y);
}
}
return null; // This line should never be reached
}
public Date previousDate() { return null; }
public Date previousDate() {
int d = this.getDay();
int m = this.getMonth();
int y = this.getYear();
if (d == 1) {
if (m == 1) {
return new Date(31, 12, y - 1);
} else {
return new Date(max_day(m - 1, y), m - 1, y);
}
} else {
return new Date(d - 1, m, y);
}
}
public int compareTo(Date other) { return 0; }
public int compareTo(Date other) {
if (other == null) {
throw new NullPointerException();
}
if (isValidDate(other.getDay(), other.getMonth(), other.getYear())) {
int cmpYear = Integer.compare(this.getYear(), other.getYear());
if (cmpYear != 0)
return cmpYear;
int cmpMonth = Integer.compare(this.getMonth(), other.getMonth());
if (cmpMonth != 0)
return cmpMonth;
return Integer.compare(this.getDay(), other.getDay());
}
throw new IllegalArgumentException("The date to compare is not valid");
}
public int getDay() {
return day;
}
public int getMonth() {
return month;
}
public int getYear() {
return year;
}
private static int max_day(int month, int year) {
int max_verstappen;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
max_verstappen = 31;
break;
case 4:
case 6:
case 9:
case 11:
max_verstappen = 30;
break;
case 2: {
if (isLeapYear(year)) {
max_verstappen = 29;
break;
} else {
max_verstappen = 28;
break;
}
}
default:
max_verstappen = 0;
break;
}
return max_verstappen;
}
}

View File

@@ -1,10 +1,207 @@
package fr.istic.vv;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class DateTest {
// ---------- isLeapYear ----------
@Test
public void testIsLeapYear_Year1900() {
assertTrue(Date.isLeapYear(1900));
}
@Test
public void testIsLeapYear_Year2024() {
assertTrue(Date.isLeapYear(2024));
}
@Test
public void testIsLeapYear_Year2019() {
assertFalse(Date.isLeapYear(2019));
}
// ---------- isValidDate ----------
@Test
public void testIsValidDate_Feb29_Leap() {
assertTrue(Date.isValidDate(29, 2, 2024));
}
@Test
public void testIsValidDate_Feb29_NonLeap() {
assertFalse(Date.isValidDate(29, 2, 2025));
}
@Test
public void testIsValidDate_April31() {
assertFalse(Date.isValidDate(31, 4, 2025));
}
@Test
public void testIsValidDate_Dec31() {
assertTrue(Date.isValidDate(31, 12, 1999));
}
@Test
public void testIsValidDate_InvalidMonthZero() {
assertFalse(Date.isValidDate(1, 0, 2025));
}
@Test
public void testIsValidDate_InvalidMonth13() {
assertFalse(Date.isValidDate(1, 13, 2025));
}
@Test
public void testIsValidDate_DayZero() {
assertFalse(Date.isValidDate(0, 1, 2025));
}
// ---------- constructor ----------
@Test
public void testConstructor_ValidDate() {
Date d = new Date(15, 6, 2025);
assertEquals(15, d.getDay());
assertEquals(6, d.getMonth());
assertEquals(2025, d.getYear());
}
@Test
public void testConstructor_InvalidDateThrows() {
assertThrows(IllegalArgumentException.class, () -> new Date(31, 4, 2025));
}
// ---------- nextDate ----------
@Test
public void testNextDate_NormalIncrement() {
Date d = new Date(10, 6, 2025);
Date expected = new Date(11, 6, 2025);
// System.out.print(d.nextDate().getDay() + "/" + d.nextDate().getMonth() + "/"
// + d.nextDate().getYear());
assertEquals(expected.getDay(), d.nextDate().getDay());
assertEquals(expected.getMonth(), d.nextDate().getMonth());
assertEquals(expected.getYear(), d.nextDate().getYear());
}
@Test
public void testNextDate_EndOf30DayMonth() {
Date d = new Date(30, 4, 2025);
Date expected = new Date(1, 5, 2025);
assertEquals(expected.getDay(), d.nextDate().getDay());
assertEquals(expected.getMonth(), d.nextDate().getMonth());
assertEquals(expected.getYear(), d.nextDate().getYear());
}
@Test
public void testNextDate_FebNonLeap() {
Date d = new Date(28, 2, 2025);
Date expected = new Date(1, 3, 2025);
assertEquals(expected.getDay(), d.nextDate().getDay());
assertEquals(expected.getMonth(), d.nextDate().getMonth());
assertEquals(expected.getYear(), d.nextDate().getYear());
}
@Test
public void testNextDate_FebLeap() {
Date d = new Date(28, 2, 2024);
Date expected = new Date(29, 2, 2024);
assertEquals(expected.getDay(), d.nextDate().getDay());
assertEquals(expected.getMonth(), d.nextDate().getMonth());
assertEquals(expected.getYear(), d.nextDate().getYear());
Date d2 = new Date(29, 2, 2024);
Date expected2 = new Date(1, 3, 2024);
assertEquals(expected2.getDay(), d2.nextDate().getDay());
assertEquals(expected2.getMonth(), d2.nextDate().getMonth());
assertEquals(expected2.getYear(), d2.nextDate().getYear());
}
@Test
public void testNextDate_EndOfYear() {
Date d = new Date(31, 12, 1999);
Date expected = new Date(1, 1, 2000);
assertEquals(expected.getDay(), d.nextDate().getDay());
assertEquals(expected.getMonth(), d.nextDate().getMonth());
assertEquals(expected.getYear(), d.nextDate().getYear());
}
// ---------- previousDate ----------
@Test
public void testPreviousDate_NormalDecrement() {
Date d = new Date(10, 6, 2025);
Date expected = new Date(9, 6, 2025);
assertEquals(expected.getDay(), d.previousDate().getDay());
assertEquals(expected.getMonth(), d.previousDate().getMonth());
assertEquals(expected.getYear(), d.previousDate().getYear());
}
@Test
public void testPreviousDate_StartOfMonthToPrev() {
Date d = new Date(1, 5, 2025);
Date expected = new Date(30, 4, 2025);
assertEquals(expected.getDay(), d.previousDate().getDay());
assertEquals(expected.getMonth(), d.previousDate().getMonth());
assertEquals(expected.getYear(), d.previousDate().getYear());
}
@Test
public void testPreviousDate_Mar1NonLeap() {
Date d = new Date(1, 3, 2025);
Date expected = new Date(28, 2, 2025);
assertEquals(expected.getDay(), d.previousDate().getDay());
assertEquals(expected.getMonth(), d.previousDate().getMonth());
assertEquals(expected.getYear(), d.previousDate().getYear());
}
@Test
public void testPreviousDate_Mar1Leap() {
Date d = new Date(1, 3, 2024);
Date expected = new Date(29, 2, 2024);
assertEquals(expected.getDay(), d.previousDate().getDay());
assertEquals(expected.getMonth(), d.previousDate().getMonth());
assertEquals(expected.getYear(), d.previousDate().getYear());
}
@Test
public void testPreviousDate_StartOfYear() {
Date d = new Date(1, 1, 2000);
Date expected = new Date(31, 12, 1999);
assertEquals(expected.getDay(), d.previousDate().getDay());
assertEquals(expected.getMonth(), d.previousDate().getMonth());
assertEquals(expected.getYear(), d.previousDate().getYear());
}
// ---------- compareTo ----------
@Test
public void testCompareTo_NullThrows() {
Date d = new Date(1, 1, 2025);
assertThrows(NullPointerException.class, () -> d.compareTo(null));
}
@Test
public void testCompareTo_ByYear() {
Date d1 = new Date(1, 1, 2026);
Date d2 = new Date(1, 1, 2025);
assertTrue(d1.compareTo(d2) > 0);
}
@Test
public void testCompareTo_ByMonth() {
Date d1 = new Date(1, 2, 2025);
Date d2 = new Date(1, 1, 2025);
assertTrue(d1.compareTo(d2) > 0);
}
@Test
public void testCompareTo_ByDay() {
Date d1 = new Date(2, 1, 2025);
Date d2 = new Date(1, 1, 2025);
assertTrue(d1.compareTo(d2) > 0);
}
@Test
public void testCompareTo_Equal() {
Date d1 = new Date(5, 5, 2025);
Date d2 = new Date(5, 5, 2025);
assertEquals(0, d1.compareTo(d2));
}
}

View File

@@ -14,4 +14,45 @@ Discuss the test smell you found with the help of PMD and propose here an improv
Include the improved test code in this file.
## Answer
On lance la commande
`pmd check -d ./test_folder/commons-math/ -R category/java/errorprone.xml/DetachedTestCase -r results_q2_tp3.txt -f text`
---
`
./test_folder/commons-math/commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/MathArraysTest.java:730: DetachedTestCase: Probable detached JUnit test case.
./test_folder/commons-math/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/SingularValueDecompositionTest.java:169: DetachedTestCase: Probable detached JUnit test case.
./test_folder/commons-math/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/SingularValueDecompositionTest.java:202: DetachedTestCase: Probable detached JUnit test case.
./test_folder/commons-math/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/regression/MillerUpdatingRegressionTest.java:559: DetachedTestCase: Probable detached JUnit test case.
`
---
On a pris le 1ere erreur:
```
public void testConcatenateEmptyArguments() {
final double[] x = new double[] {0, 1, 2};
final double[] y = new double[] {3};
final double[] z = new double[] {};
final double[] u = new double[] {0, 1, 2, 3};
Assert.assertArrayEquals(u, MathArrays.concatenate(x, z, y), 0);
Assert.assertArrayEquals(u, MathArrays.concatenate(x, y, z), 0);
Assert.assertArrayEquals(u, MathArrays.concatenate(z, x, y), 0);
Assert.assertEquals(0, MathArrays.concatenate(z, z, z).length);
}
```
On peut voir quand il n'a pas l'annotation `@Test` au début de class test donc normalement le test
qu'il a fait mais JUnit n'execute pas.
Voici la correction:
```
@Test
public void testConcatenateEmptyArguments() {
final double[] x = new double[] {0, 1, 2};
final double[] y = new double[] {3};
final double[] z = new double[] {};
final double[] u = new double[] {0, 1, 2, 3};
Assert.assertArrayEquals(u, MathArrays.concatenate(x, z, y), 0);
Assert.assertArrayEquals(u, MathArrays.concatenate(x, y, z), 0);
Assert.assertArrayEquals(u, MathArrays.concatenate(z, x, y), 0);
Assert.assertEquals(0, MathArrays.concatenate(z, z, z).length);
}
```