Skip to content

Commit

Permalink
close #1 Convertiti i primi 3 numeri interi in romani
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcGiu committed May 17, 2024
1 parent 82df6f1 commit 726015a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 10 deletions.
31 changes: 28 additions & 3 deletions src/main/java/it/unipd/mtss/IntegerToRoman.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,33 @@

public class IntegerToRoman {

// Array di valori interi corrispondenti ai simboli romani
private static final int[] values = {1};

// Array di simboli romani corrispondenti ai valori interi
private static final String[] symbols = {"I"};

// Converte i numeri interi nel range [1,3] in numeri romani
public static String convert(int number){
// TODO
return null;

// Controlla se il numero è fuori dal range [1,3]
if (number<1 || number>3) {
throw new IllegalArgumentException(number + " is not in range [1,3]");
}

// Variabile per memorizzare il risultato della conversione
StringBuilder roman = new StringBuilder();

// Itera su tutti i valori e simboli
for (int i = 0; i < values.length; i++) {
// Aggiunge i simboli romani corrispondenti fino a ridurre completamente il numero
while (number >= values[i]) {
roman.append(symbols[i]);
number -= values[i];
}
}

// Restituisce il risultato finale della conversione
return roman.toString();
}
}
}
59 changes: 52 additions & 7 deletions src/test/java/it/unipd/mtss/IntegerToRomanTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,64 @@

package it.unipd.mtss;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

import org.junit.Test;
import java.util.Arrays;
import java.util.Collection;

//Unit test per IntegerToRoman
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

// Utilizza il runner parametrizzato di JUnit per eseguire test con diverse combinazioni di parametri
@RunWith(Parameterized.class)
public class IntegerToRomanTests {

//TODO
// Variabili di istanza per l'input e l'output attesi
private String input;
private String expectedOutput;

// Costruttore che inizializza le variabili di istanza con i valori dei parametri
public IntegerToRomanTests(String input, String expectedOutput) {
this.input = input;
this.expectedOutput = expectedOutput;
}

// Metodo che fornisce i dati di test come una collezione di array di stringhe
@Parameters
public static Collection<String[]> testConditions() {

// Array bidimensionale con i valori di input e gli output attesi
String[][] expectedOutputs = {
{ "1", "I" },
{ "2", "II" },
{ "3", "III" }};

// Ritorna come lista
return Arrays.asList(expectedOutputs);
}

// Test parametrizzato per verificare la conversione dei numeri in numeri romani
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
public void testConvertNumbers() {
assertEquals(expectedOutput,
IntegerToRoman.convert(Integer.parseInt(input)));
}

// Test che si aspetta un'eccezione IllegalArgumentException per un valore
// di input troppo basso (minore di 1)
@Test(expected=IllegalArgumentException.class)
public void TestExeption_IllegalMinArgument() {
int number = 0;
String expected = IntegerToRoman.convert(number);
}

// Test che si aspetta un'eccezione IllegalArgumentException per un valore
// di input troppo alto (maggiore di 3)
@Test(expected=IllegalArgumentException.class)
public void TestExeption_IllegalMaxArgument() {
int number = 4;
String expected = IntegerToRoman.convert(number);
}
}

0 comments on commit 726015a

Please sign in to comment.