correction couverture de test Date
This commit is contained in:
@@ -1,21 +1,65 @@
|
|||||||
package fr.istic.vv;
|
package fr.istic.vv;
|
||||||
|
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.Deque;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Queue;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
public class StringUtils {
|
public class StringUtils {
|
||||||
|
|
||||||
private StringUtils() {}
|
private StringUtils() {}
|
||||||
|
|
||||||
public static boolean isBalanced(String str) {
|
static Set<Character> OPEN = Set.of('(', '[', '{');
|
||||||
|
static Set<Character> CLOSE = Set.of(')', ']', '}');
|
||||||
|
public static final Map<Character, Character> CONVERT;
|
||||||
|
|
||||||
return false;
|
static {
|
||||||
|
CONVERT = new HashMap<>();
|
||||||
|
CONVERT.put('(', ')');
|
||||||
|
CONVERT.put('{', '}');
|
||||||
|
CONVERT.put('[', ']');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static boolean isBalanced(String str) {
|
||||||
|
Deque<Character> balancedQueue = new ArrayDeque<Character>();
|
||||||
|
|
||||||
|
return reqIsBalanced(str, balancedQueue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String reqIsBalanced(String str){
|
public static boolean reqIsBalanced(String str,Deque<Character> balancedQueue){
|
||||||
String all = "[\\[\\(\\{\\]\\)\\}\"]";
|
int nbRemove = 1;
|
||||||
String openRegex = "[\\[\\(\\{\"]";
|
if(str.length()>0){
|
||||||
String closeRegex = "[\\]\\)\\}\"]";
|
char c=str.charAt(0);
|
||||||
String regex = "^[^"+all+"]*"+openRegex+"."+closeRegex+"^["+all+"]*";
|
while(true){
|
||||||
|
if(OPEN.contains(c)){
|
||||||
return str;
|
balancedQueue.push(c);
|
||||||
|
|
||||||
|
return reqIsBalanced(str.substring(nbRemove), balancedQueue);
|
||||||
|
}
|
||||||
|
else if(CLOSE.contains(c)){
|
||||||
|
if(balancedQueue.isEmpty()){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else if(CONVERT.get(balancedQueue.pollFirst()) == c){
|
||||||
|
return reqIsBalanced(str.substring(nbRemove),balancedQueue);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(nbRemove<str.length()){
|
||||||
|
c = str.charAt(nbRemove);
|
||||||
|
nbRemove++;
|
||||||
|
}
|
||||||
|
else return balancedQueue.isEmpty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return balancedQueue.isEmpty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,12 +8,102 @@ import static org.junit.jupiter.api.Assertions.*;
|
|||||||
class StringUtilsTest {
|
class StringUtilsTest {
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test2(){
|
public void testBalancedSimple(){
|
||||||
//assertEquals(num1,num2);
|
String prog1 = "()";
|
||||||
String s = "te";
|
String prog2 = "{}";
|
||||||
String s2 = "test";
|
String prog3 = "[]";
|
||||||
s += "st";
|
assertTrue(isBalanced(prog1));
|
||||||
assertSame(s2,s);
|
assertTrue(isBalanced(prog2));
|
||||||
|
assertTrue(isBalanced(prog3));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testnotBalancedSimple(){
|
||||||
|
String prog1 = "(]";
|
||||||
|
String prog2 = "{)";
|
||||||
|
String prog3 = "[}";
|
||||||
|
String prog4 = ")(";
|
||||||
|
assertFalse(isBalanced(prog1));
|
||||||
|
assertFalse(isBalanced(prog2));
|
||||||
|
assertFalse(isBalanced(prog3));
|
||||||
|
assertFalse(isBalanced(prog4));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEmpty(){
|
||||||
|
String prog1 = "";
|
||||||
|
assertTrue(isBalanced(prog1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOnlyText(){
|
||||||
|
String prog1 = "abc";
|
||||||
|
String prog2 = "abc'éhçfez_énrui";
|
||||||
|
assertTrue(isBalanced(prog1));
|
||||||
|
assertTrue(isBalanced(prog2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBalancedSimpleWithText(){
|
||||||
|
String prog1 = "(abc)";
|
||||||
|
String prog2 = "abc'[éhçfez]_énrui";
|
||||||
|
String prog3 = "abc'{éhçfez_énrui<}";
|
||||||
|
assertTrue(isBalanced(prog1));
|
||||||
|
assertTrue(isBalanced(prog2));
|
||||||
|
assertTrue(isBalanced(prog3));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNotBalancedSimpleWithText(){
|
||||||
|
String prog1 = "abc)";
|
||||||
|
String prog2 = "abc'éhçfez]_énrui";
|
||||||
|
String prog3 = "abc'éhçfez_énrui<}";
|
||||||
|
String prog4 = "{abc'éhçfez_énrui<";
|
||||||
|
assertFalse(isBalanced(prog1));
|
||||||
|
assertFalse(isBalanced(prog2));
|
||||||
|
assertFalse(isBalanced(prog3));
|
||||||
|
assertFalse(isBalanced(prog4));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBalancedFollow(){
|
||||||
|
String prog1 = "a()b[c]{}";
|
||||||
|
assertTrue(isBalanced(prog1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testnotBalancedFollow(){
|
||||||
|
String prog1 = "a()b[c]{)}";
|
||||||
|
assertFalse(isBalanced(prog1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBalancedFull(){
|
||||||
|
String prog1 = "for(int i = 0;i<10; i++){System.out.println(\"tab[i]\");}";
|
||||||
|
assertTrue(isBalanced(prog1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testnotBalancedFullForgotClose(){
|
||||||
|
String prog1 = "for(int i = 0;i<10; i++{System.out.println(\"tab[i]\");}";
|
||||||
|
assertFalse(isBalanced(prog1));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testnotBalancedFullFrogotOpen(){
|
||||||
|
String prog1 = "forint i = 0;i<10; i++){System.out.println(\"tab[i]\");}";
|
||||||
|
assertFalse(isBalanced(prog1));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testnotBalancedFullWrongClose(){
|
||||||
|
String prog1 = "for(int i = 0;i<10; i++){System.out.println(\"tab[i]\");]";
|
||||||
|
assertFalse(isBalanced(prog1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -35,14 +35,13 @@ class Date implements Comparable<Date> {
|
|||||||
int max_day = max_day(m, y);
|
int max_day = max_day(m, y);
|
||||||
if (d < max_day) {
|
if (d < max_day) {
|
||||||
return new Date(d + 1, m, y);
|
return new Date(d + 1, m, y);
|
||||||
} else if (d == max_day) {
|
} else {
|
||||||
if (m == 12) {
|
if (m == 12) {
|
||||||
return new Date(1, 1, y + 1);
|
return new Date(1, 1, y + 1);
|
||||||
} else {
|
} else {
|
||||||
return new Date(1, m + 1, y);
|
return new Date(1, m + 1, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null; // This line should never be reached
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Date previousDate() {
|
public Date previousDate() {
|
||||||
@@ -64,18 +63,16 @@ class Date implements Comparable<Date> {
|
|||||||
if (other == null) {
|
if (other == null) {
|
||||||
throw new NullPointerException();
|
throw new NullPointerException();
|
||||||
}
|
}
|
||||||
if (isValidDate(other.getDay(), other.getMonth(), other.getYear())) {
|
int cmpYear = Integer.compare(this.getYear(), other.getYear());
|
||||||
int cmpYear = Integer.compare(this.getYear(), other.getYear());
|
if (cmpYear != 0)
|
||||||
if (cmpYear != 0)
|
return cmpYear;
|
||||||
return cmpYear;
|
|
||||||
|
|
||||||
int cmpMonth = Integer.compare(this.getMonth(), other.getMonth());
|
int cmpMonth = Integer.compare(this.getMonth(), other.getMonth());
|
||||||
if (cmpMonth != 0)
|
if (cmpMonth != 0)
|
||||||
return cmpMonth;
|
return cmpMonth;
|
||||||
|
|
||||||
|
return Integer.compare(this.getDay(), other.getDay());
|
||||||
|
|
||||||
return Integer.compare(this.getDay(), other.getDay());
|
|
||||||
}
|
|
||||||
throw new IllegalArgumentException("The date to compare is not valid");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getDay() {
|
public int getDay() {
|
||||||
|
|||||||
@@ -204,4 +204,6 @@ class DateTest {
|
|||||||
Date d2 = new Date(5, 5, 2025);
|
Date d2 = new Date(5, 5, 2025);
|
||||||
assertEquals(0, d1.compareTo(d2));
|
assertEquals(0, d1.compareTo(d2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -26,3 +26,33 @@ Use the project in [tp3-balanced-strings](../code/tp3-balanced-strings) to compl
|
|||||||
|
|
||||||
## Answer
|
## Answer
|
||||||
|
|
||||||
|
1.
|
||||||
|
Test simple true:
|
||||||
|
"{}","[]","()","(a)","a[b]","" et "test"
|
||||||
|
|
||||||
|
des test simple false :
|
||||||
|
"{]","(}","[)","a{]","(b}"
|
||||||
|
|
||||||
|
des Test avec des parenthèsé qui se suivent :
|
||||||
|
"{}()a[a]"
|
||||||
|
|
||||||
|
La même chose avec des erreur :
|
||||||
|
"{}()a[a}"
|
||||||
|
|
||||||
|
des Test avec des parenthèse imbriqué :
|
||||||
|
"white(true){println("Tester c'est pas douter")}
|
||||||
|
|
||||||
|
des Test avec des oublies, d'ouverture et fermeture:
|
||||||
|
"whitetrue){println("Tester c'est pas douter")}",
|
||||||
|
"white(true{println("Tester c'est pas douter")}"
|
||||||
|
|
||||||
|
|
||||||
|
2.
|
||||||
|
avec ses tests la couverture est à 100%
|
||||||
|
|
||||||
|
3.
|
||||||
|
toutes les conditions n'ont qu'un seul opérateur boolean
|
||||||
|
|
||||||
|
4.
|
||||||
|
|
||||||
|
un mutant qui remplacé un return par un return true, il s'agit du cas ou le programme avec une erreur de parenthésage fini par des caractère, ex : "(a" on peut donc le rajouter dans les "test simple"
|
||||||
Reference in New Issue
Block a user