test jqwik
This commit is contained in:
@@ -19,18 +19,18 @@ public class RomanNumeraUtils {
|
|||||||
public static boolean isValidRomanNumeral(String value) {
|
public static boolean isValidRomanNumeral(String value) {
|
||||||
boolean isValid = true;
|
boolean isValid = true;
|
||||||
|
|
||||||
char lastChar = ' ';
|
char lastChar = ' ';//X
|
||||||
int nbLastChar = 0;
|
int nbLastChar = 0;//1
|
||||||
int valMax = 1001;
|
int valMax = 1000;//9
|
||||||
|
|
||||||
for(int i = 0; i<value.length() && isValid; i++){
|
for(int i = 0; i<value.length() && isValid; i++){
|
||||||
|
|
||||||
char val = value.charAt(i);
|
char val = value.charAt(i);
|
||||||
|
|
||||||
if(mapToNum.containsKey(val) && mapToNum.get(val)<valMax){
|
if(mapToNum.containsKey(val)){
|
||||||
if(lastChar!=' '){
|
if(lastChar!=' '){
|
||||||
if(mapToNum.get(val)>mapToNum.get(lastChar)){
|
if(mapToNum.get(val)>mapToNum.get(lastChar)){
|
||||||
//seul I X C peuvent soustraire un symbole (ex : IX XC CM)
|
//règle 5. : seul I X C peuvent soustraire un symbole (ex : IX XC CM)
|
||||||
if(lastChar == 'I' || lastChar == 'X' || lastChar == 'C'){
|
if(lastChar == 'I' || lastChar == 'X' || lastChar == 'C'){
|
||||||
valMax = mapToNum.get(lastChar)-1;
|
valMax = mapToNum.get(lastChar)-1;
|
||||||
//si plus de 1 soustraction (ex IIX) ou que la soustraction est ni 1/10 ni 1/5 du chiffre le plus grand
|
//si plus de 1 soustraction (ex IIX) ou que la soustraction est ni 1/10 ni 1/5 du chiffre le plus grand
|
||||||
@@ -40,6 +40,7 @@ public class RomanNumeraUtils {
|
|||||||
}
|
}
|
||||||
else isValid = false;
|
else isValid = false;
|
||||||
}
|
}
|
||||||
|
else if((mapToNum.get(val)>valMax)) isValid = false; //règles 6.
|
||||||
|
|
||||||
}
|
}
|
||||||
switch (val){
|
switch (val){
|
||||||
@@ -85,7 +86,6 @@ public class RomanNumeraUtils {
|
|||||||
char lastChar = ' ';
|
char lastChar = ' ';
|
||||||
|
|
||||||
for(int i = 0; i<numeral.length(); i++){
|
for(int i = 0; i<numeral.length(); i++){
|
||||||
|
|
||||||
char val = numeral.charAt(i);
|
char val = numeral.charAt(i);
|
||||||
int value = mapToNum.get(val);
|
int value = mapToNum.get(val);
|
||||||
// -
|
// -
|
||||||
@@ -102,12 +102,13 @@ public class RomanNumeraUtils {
|
|||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
else throw new IllegalArgumentException("Invalid date");
|
else{
|
||||||
|
throw new IllegalArgumentException("not valid Roman numeral");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String toRomanNumeral(int number) {
|
public static String toRomanNumeral(int number) {
|
||||||
System.out.println(number);
|
|
||||||
String res = "";
|
String res = "";
|
||||||
char[] order = {'M','D','C','L','X','V','I'};
|
char[] order = {'M','D','C','L','X','V','I'};
|
||||||
char[] sub = {'I','X','C'};
|
char[] sub = {'I','X','C'};
|
||||||
@@ -121,7 +122,6 @@ public class RomanNumeraUtils {
|
|||||||
if(number>=val){
|
if(number>=val){
|
||||||
number -= val;
|
number -= val;
|
||||||
res = res + symb;
|
res = res + symb;
|
||||||
System.out.println(res);
|
|
||||||
notFound = false;
|
notFound = false;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
@@ -133,7 +133,6 @@ public class RomanNumeraUtils {
|
|||||||
if(number>=val-valSub){
|
if(number>=val-valSub){
|
||||||
number -= val-valSub;
|
number -= val-valSub;
|
||||||
res = res + symbSub + symb;
|
res = res + symbSub + symb;
|
||||||
System.out.println(res);
|
|
||||||
notFoundSub = false;
|
notFoundSub = false;
|
||||||
notFound = false;
|
notFound = false;
|
||||||
}
|
}
|
||||||
@@ -144,7 +143,6 @@ public class RomanNumeraUtils {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println("Return : " + res);
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
else throw new IllegalArgumentException("max 4000");
|
else throw new IllegalArgumentException("max 4000");
|
||||||
|
|||||||
@@ -3,120 +3,64 @@ import net.jqwik.api.*;
|
|||||||
import net.jqwik.api.constraints.IntRange;
|
import net.jqwik.api.constraints.IntRange;
|
||||||
import static fr.istic.vv.RomanNumeraUtils.*;
|
import static fr.istic.vv.RomanNumeraUtils.*;
|
||||||
|
|
||||||
|
|
||||||
public class RomanNumeralTest {
|
public class RomanNumeralTest {
|
||||||
|
|
||||||
|
char[] chars = {'M','D','C','L','X','V','I'};
|
||||||
|
|
||||||
@Example
|
|
||||||
void simpleValidRomanNumerals() {
|
|
||||||
assert isValidRomanNumeral("I");
|
|
||||||
assert isValidRomanNumeral("II");
|
|
||||||
assert isValidRomanNumeral("III");
|
|
||||||
assert isValidRomanNumeral("IV");
|
|
||||||
assert isValidRomanNumeral("VI");
|
|
||||||
assert isValidRomanNumeral("VIII");
|
|
||||||
assert isValidRomanNumeral("IX");
|
|
||||||
assert isValidRomanNumeral("X");
|
|
||||||
assert isValidRomanNumeral("XI");
|
|
||||||
assert isValidRomanNumeral("MI");
|
|
||||||
assert isValidRomanNumeral("XIII");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Example
|
|
||||||
void nonValidRomanNumeralsBadLetter() {
|
|
||||||
assert !isValidRomanNumeral("IIIA");
|
|
||||||
assert !isValidRomanNumeral("A");
|
|
||||||
assert !isValidRomanNumeral("CAI");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Example
|
|
||||||
void nonValidRomanNumeralsRepeated() {
|
|
||||||
assert !isValidRomanNumeral("IIII");
|
|
||||||
assert !isValidRomanNumeral("XXXX");
|
|
||||||
assert !isValidRomanNumeral("CCCC");
|
|
||||||
assert !isValidRomanNumeral("MMMM");
|
|
||||||
assert !isValidRomanNumeral("DD");
|
|
||||||
assert !isValidRomanNumeral("LL");
|
|
||||||
assert !isValidRomanNumeral("VV");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Example
|
|
||||||
void validRomanNumeralsLeftSub() {
|
|
||||||
assert isValidRomanNumeral("IV");
|
|
||||||
assert isValidRomanNumeral("IX");
|
|
||||||
assert isValidRomanNumeral("XL");
|
|
||||||
assert isValidRomanNumeral("XC");
|
|
||||||
assert isValidRomanNumeral("CD");
|
|
||||||
assert isValidRomanNumeral("CM");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Example
|
|
||||||
void nonValidRomanNumeralsLeftSub() {
|
|
||||||
assert !isValidRomanNumeral("VX");
|
|
||||||
assert !isValidRomanNumeral("VL");
|
|
||||||
assert !isValidRomanNumeral("IL");
|
|
||||||
assert !isValidRomanNumeral("IC");
|
|
||||||
assert !isValidRomanNumeral("VC");
|
|
||||||
assert !isValidRomanNumeral("LC");
|
|
||||||
assert !isValidRomanNumeral("ID");
|
|
||||||
assert !isValidRomanNumeral("VD");
|
|
||||||
assert !isValidRomanNumeral("XD");
|
|
||||||
assert !isValidRomanNumeral("LD");
|
|
||||||
assert !isValidRomanNumeral("IM");
|
|
||||||
assert !isValidRomanNumeral("VM");
|
|
||||||
assert !isValidRomanNumeral("XM");
|
|
||||||
assert !isValidRomanNumeral("LM");
|
|
||||||
assert !isValidRomanNumeral("DM");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Example
|
|
||||||
void completValidRomanNumerals() {
|
|
||||||
assert isValidRomanNumeral("MCDXXXIV");
|
|
||||||
assert isValidRomanNumeral("DCCLXXXIX");
|
|
||||||
assert isValidRomanNumeral("LXIX");
|
|
||||||
assert isValidRomanNumeral("MCCXXXIV");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Example
|
|
||||||
void completNonValidRomanNumerals() {
|
|
||||||
assert !isValidRomanNumeral("IXX");
|
|
||||||
assert !isValidRomanNumeral("IXIXIX");
|
|
||||||
assert !isValidRomanNumeral("MMMDCCCLIXXXVIII");
|
|
||||||
assert !isValidRomanNumeral("XIXV");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
//1.
|
||||||
|
|
||||||
@Example
|
@Provide
|
||||||
void testParseRomanNumeral() {
|
Arbitrary<String> invalidCharsGenerator() {
|
||||||
assert parseRomanNumeral("XX")==20;
|
return Arbitraries.strings()
|
||||||
assert parseRomanNumeral("XIX")==19;
|
.withChars('A', 'Z')
|
||||||
assert parseRomanNumeral("XXI")==21;
|
.ofMaxLength(6)
|
||||||
assert parseRomanNumeral("IX")==9;
|
.filter(s -> s.matches(".*[^MDCLXVI].*"));
|
||||||
assert parseRomanNumeral("MCCXXXIV")==1234;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Example
|
|
||||||
void testToRomanNumeral() {
|
|
||||||
assert toRomanNumeral(20).compareTo("XX")==0;
|
|
||||||
assert toRomanNumeral(19).compareTo("XIX")==0;
|
|
||||||
assert toRomanNumeral(21).compareTo("XXI")==0;
|
|
||||||
assert toRomanNumeral(9).compareTo("IX")==0;
|
|
||||||
assert toRomanNumeral(1234).compareTo("MCCXXXIV")==0;
|
|
||||||
assert toRomanNumeral(3999).compareTo("MMMCMXCIX")==0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Property
|
@Property
|
||||||
boolean absoluteValueOfAllNumbersIsPositive(@ForAll @IntRange(min=Integer.MIN_VALUE +1) int anInteger) {
|
void invalidCharacters(@ForAll("invalidCharsGenerator") String s) {
|
||||||
return Math.abs(anInteger) >= 0;
|
assert !isValidRomanNumeral(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//2.3.
|
||||||
|
@Provide
|
||||||
|
Arbitrary<String> invalidRepeatGenerator() {
|
||||||
|
return Arbitraries.strings()
|
||||||
|
.withChars(chars)
|
||||||
|
.ofMaxLength(10)
|
||||||
|
.filter(s ->
|
||||||
|
s.contains("MMMM") || s.contains("CCCC")||
|
||||||
|
s.contains("XXXX") || s.contains("IIII")||
|
||||||
|
s.contains("DD") || s.contains("LL") || s.contains("VV")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Property(tries = 1000) //1000 pour être sure de tester tout les cas
|
||||||
|
void invalidRepeat(@ForAll("invalidRepeatGenerator") String s) {
|
||||||
|
assert !isValidRomanNumeral(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//4.5.6.
|
||||||
|
|
||||||
|
@Provide
|
||||||
|
Arbitrary<Integer> romanRange() {
|
||||||
|
return Arbitraries.integers().between(1, 3999);
|
||||||
|
}
|
||||||
|
|
||||||
|
//ps : Ce test a permis de corriger un manque à la règle 6. (cas XLIX)
|
||||||
|
@Property
|
||||||
|
void refindIntAfterConvert(@ForAll("romanRange") int val) {
|
||||||
|
String str = toRomanNumeral(val);
|
||||||
|
int res = parseRomanNumeral(str);
|
||||||
|
System.out.println(val);
|
||||||
|
assert res==val;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Property
|
||||||
|
void isValid(@ForAll("romanRange") int val) {
|
||||||
|
assert isValidRomanNumeral(toRomanNumeral(val));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user