graded assignment 2a done (with assumption on JaCoCo)

This commit is contained in:
Claudio Maggioni 2023-03-08 11:38:11 +01:00
parent 01c833b44f
commit 75448022dd
7 changed files with 348 additions and 0 deletions

137
SQ-2021-A4/pom.xml Normal file
View File

@ -0,0 +1,137 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.usi.sq</groupId>
<artifactId>sq-2021-a4</artifactId>
<version>1.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.platform.version>1.7.0-M1</junit.platform.version>
<junit.jupiter.version>5.6.2</junit.jupiter.version>
<junit.vintage.version>5.6.2</junit.vintage.version>
<junit.version>4.13</junit.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<runOrder>alphabetical</runOrder>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- attached to Maven test phase -->
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>1.5.2</version>
<configuration>
<targetClasses>
<param>org.usi.sq*</param>
</targetClasses>
<targetTests>
<param>org.usi.sq*</param>
<param>*</param>
</targetTests>
</configuration>
<dependencies>
<dependency>
<groupId>org.pitest</groupId>
<artifactId>pitest-junit5-plugin</artifactId>
<version>0.12</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.vintage.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<reporting>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<reportSets>
<reportSet>
<reports>
<!-- select non-aggregate reports -->
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<!-- place configuration here if required-->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.1</version>
</plugin>
</plugins>
</reporting>
</project>

View File

@ -0,0 +1,35 @@
package org.usi.sq.util.numbers;
public interface INumberChecker {
/**
*
* The method takes a string as input and checks if it is a valid hexadecimal number.
* It returns a boolean value that is:
* - true, if the string is a valid hexadecimal number
* - false, if the string is not a valid hexadecimal number
*
* A valid hexadecimal number is given by a string that satisfies the following conditions:
* - the string can start with a sign (+/- are the only signs accepted as starting char)
* - the first character of a hexadecimal number is a '0'
* - the second character of a hexadecimal number is an 'x' or an 'X'
* - the following characters of a hexadecimal number are decimal digits (from 0 to 9) or
* a letter of the alphabet between 'a' and 'f' (both lowercase and uppercase letters are valid)
*
* Examples of valid hexadecimal strings:
* - +0x47f8eca1d
* - 0Xadc4790da
* - 0xAB78cd3Ab
*
* Examples of invalid hexadecimal strings:
* - +07934abc (the pattern '0x' is not respected)
* - +-0xa45cb (the string can contain only '+' or '-', but not both)
* - xabCd73aC (the pattern '0x' is not respected)
* - 0xABd19Vc ('V' is not a valid character)
*
* @param str the string that needs to be checked
* @return the boolean result of the checking, that is, true if {@code str}
* is a valid hexadecimal number and false otherwise
*
* */
boolean isHexNumber(final String str);
}

View File

@ -0,0 +1,62 @@
package org.usi.sq.util.numbers;
public class NumberChecker implements INumberChecker {
/**
* The NumberChecker constructor. Do not use this constructor directly in
* test cases.
*/
public NumberChecker() {
}
/**
* {@inheritDoc}
* See documentation of interface method {@code INumberChecker.isHexNumber}.
* The implementation of this method is an adapted excerpt of the method
* org.apache.commons.lang3.math.NumberUtils.isCreatable in the Apache
* Commons Lang library.
*
* @param str the string that needs to be checked
* @return true if {@code str} is a valid hexadecimal number and false
* otherwise
*/
public boolean isHexNumber(final String str) {
if (str == null || "".equals(str)){
return false;
}
boolean isHex = true;
final char[] chars = str.toCharArray();
int sz = chars.length;
int start = 0;
// leading sign
if(chars[0] == '-' || chars[0] == '+')
start = 1;
if (sz > start + 1){
if(chars[start] == '0') {
if (chars[start + 1] == 'x' || chars[start + 1] == 'X') {
int i = start + 2;
if (i == sz) {
isHex = false; // str == "0x"
} else {
// checking hex digits (it can't be anything else)
for (; i < chars.length; i++) {
if ((chars[i] < '0' || chars[i] > '9')
&& (chars[i] < 'a' || chars[i] > 'f')
&& (chars[i] < 'A' || chars[i] > 'F')) {
isHex = false;
}
}
}
} else {
isHex = false;
}
} else {
isHex = false;
}
} else {
isHex = false;
}
return isHex;
}
}

View File

@ -0,0 +1,18 @@
package org.usi.sq.util.numbers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
class NumberCheckerBasicConditionCoverageTest {
INumberChecker nc = null;
@BeforeEach
public void createNumberChecker(){
nc = NumberCheckerFactory.create();
}
@Test
public void test(){
// create tests using nc
}
}

View File

@ -0,0 +1,70 @@
package org.usi.sq.util.numbers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class NumberCheckerBranchCoverageTest {
INumberChecker nc = null;
@BeforeEach
public void createNumberChecker(){
nc = NumberCheckerFactory.create();
}
@Test
public void emptyStringNotHex() {
assertFalse(nc.isHexNumber(""));
}
@Test
public void plusNotHex() {
assertFalse(nc.isHexNumber("+"));
}
@Test
public void oneOneNotHex() {
assertFalse(nc.isHexNumber("11"));
}
@Test
public void zeroBNotHex() {
assertFalse(nc.isHexNumber("0B"));
}
@Test
public void zeroXNotHex() {
assertFalse(nc.isHexNumber("0x"));
}
@Test
public void zeroXGNotHex() {
assertFalse(nc.isHexNumber("0xG"));
}
@Test
public void zeroXZeroHex() {
assertTrue(nc.isHexNumber("0x0"));
}
// NOTE: JaCoCo does not really measure branch coverage, but modified condition coverage (MCC). The following tests
// are needed to satisfy MCC, remove them for a minimal 100% branch coverage test suite
@Test
public void nullIsNotHex() {
assertFalse(nc.isHexNumber(null));
}
@Test
public void offByOneNotHex() {
assertFalse(nc.isHexNumber("-0X!"));
assertFalse(nc.isHexNumber("-0Xg"));
}
@Test
public void zeroXBIsHex() {
assertTrue(nc.isHexNumber("0xB"));
assertTrue(nc.isHexNumber("0xb"));
}
}

View File

@ -0,0 +1,7 @@
package org.usi.sq.util.numbers;
public class NumberCheckerFactory {
public static INumberChecker create(){
return new NumberChecker();
}
}

View File

@ -0,0 +1,19 @@
package org.usi.sq.util.numbers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class NumberCheckerMCDCCoverageTest {
INumberChecker nc = null;
@BeforeEach
public void createNumberChecker(){
nc = NumberCheckerFactory.create();
}
@Test
public void test(){
// create tests using nc
}
}