/** * Questa classe collezione tutti i test per gli esercizi 1, 2 e 3 della * lezione di laboratorio del 21/11/2022. Si ricorda che, perché funzionino, * è necessario prima attivare il sistema di gestione test di VSCode * (vedi lezione https://fad.unich.it/mod/page/view.php?id=19810) */ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; public class TestMetodi { @Test public void testMassimo() { // Questo è un modo per creare un array specificandone contestualmente il // contenuto. int[] v1 = new int[] { 2 }; assertEquals(2, Metodi.massimo(v1)); // Questo è un altro modo per creare un array specificandone contestualmente il // contenuto. A differenza che nel caso di sopra, ometto "new int[]". int[] v2 = { 2, 3, 4 }; assertEquals(4, Metodi.massimo(v2)); // Posso passare direttamente un vettore come argomento del metodo massimo, // senza // prima metterelo nella variabile, con la sintassi vista per v1. assertEquals(4, Metodi.massimo(new int[] { 2, 4, 3 })); // Notare che, in quest'ultimo caso, il "new int[]" è obbligatorio. La versione // semplificate commentata qui sotto non funziona. // assertEquals(4, Metodi.massimo({2, 4, 3})); assertEquals(-2, Metodi.massimo(new int[] { -2, -4, -3 })); assertEquals(4, Metodi.massimo(new int[] { 4, 4, 4 })); } @Test public void testCreaPieno() { assertArrayEquals(new int[] { -3 }, Metodi.creaPieno(1, -3)); assertArrayEquals(new int[] {}, Metodi.creaPieno(-5, 4)); assertArrayEquals(new int[] { 4, 4, 4, 4, 4 }, Metodi.creaPieno(5, 4)); assertArrayEquals(new int[] { 0, 0 }, Metodi.creaPieno(2, 0)); assertArrayEquals(new int[] {}, Metodi.creaPieno(0, 5)); } @Test public void testConfronta() { assertTrue(Metodi.confronta(new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 })); assertTrue(Metodi.confronta(new int[] {}, new int[] {})); assertFalse(Metodi.confronta(new int[] { 1, 2 }, new int[] { 1, 2, 3 })); assertFalse(Metodi.confronta(new int[] { 1, 2, 3 }, new int[] { 1, 2 })); assertFalse(Metodi.confronta(new int[] { 1, 2, 3 }, new int[] { 1, 2, 4 })); } @Test public void testSommaVettori() { assertArrayEquals(new int[] { 2, 5, 7 }, Metodi.sommaVettori(new int[] { 1, 2, 3, }, new int[] { 1, 3, 4 })); // sono pigro.. dovrei scrivere altri test ma non ne ho voglia } }