correction couverture de test Date
This commit is contained in:
@@ -1,21 +1,65 @@
|
||||
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 {
|
||||
|
||||
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){
|
||||
String all = "[\\[\\(\\{\\]\\)\\}\"]";
|
||||
String openRegex = "[\\[\\(\\{\"]";
|
||||
String closeRegex = "[\\]\\)\\}\"]";
|
||||
String regex = "^[^"+all+"]*"+openRegex+"."+closeRegex+"^["+all+"]*";
|
||||
|
||||
return str;
|
||||
public static boolean reqIsBalanced(String str,Deque<Character> balancedQueue){
|
||||
int nbRemove = 1;
|
||||
if(str.length()>0){
|
||||
char c=str.charAt(0);
|
||||
while(true){
|
||||
if(OPEN.contains(c)){
|
||||
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 {
|
||||
|
||||
|
||||
@Test
|
||||
public void test2(){
|
||||
//assertEquals(num1,num2);
|
||||
String s = "te";
|
||||
String s2 = "test";
|
||||
s += "st";
|
||||
assertSame(s2,s);
|
||||
@Test
|
||||
public void testBalancedSimple(){
|
||||
String prog1 = "()";
|
||||
String prog2 = "{}";
|
||||
String prog3 = "[]";
|
||||
assertTrue(isBalanced(prog1));
|
||||
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);
|
||||
if (d < max_day) {
|
||||
return new Date(d + 1, m, y);
|
||||
} else if (d == max_day) {
|
||||
} else {
|
||||
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() {
|
||||
@@ -64,18 +63,16 @@ class Date implements Comparable<Date> {
|
||||
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 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;
|
||||
int cmpMonth = Integer.compare(this.getMonth(), other.getMonth());
|
||||
if (cmpMonth != 0)
|
||||
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() {
|
||||
|
||||
@@ -204,4 +204,6 @@ class DateTest {
|
||||
Date d2 = new Date(5, 5, 2025);
|
||||
assertEquals(0, d1.compareTo(d2));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user