import org.junit.Test; import static org.junit.Assert.*; public class Esercizio3 { public static boolean checkIdentica(int[][] m) { for (int i = 0; i < m.length; i++) { for (int j = 0; j < m[i].length; j++) { if (i == j && m[i][j] != 1) return false; else if (i != j && m[i][j] != 0) return false; } } return true; } @Test public void test1() { int[] a = {2, 3, 5}; int[][] m = {{2, 0, 0}, {0, 3, 0}, {0, 0, 5}}; assertArrayEquals(m, Esercizio1.diag(a)); } @Test public void test2() { int[][] m = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}; // Uso il metodo fill per creare l'array {1,1,1} senza specificarlo manualmente. // Per questo test non è molo utile, ma sarà essenziale nel prossimo test. assertArrayEquals(m, Esercizio1.diag(Esercizio1.fill(3, 1))); } @Test public void test3() { // Uso il for per fare più test senza dover ripetere tutte le volete le istruzioni for (int n = 1; n <= 30; n++) { // Fondamenatale qui è l'uso del metodo fill per generare l'array {1,1,1,...,1} // di lunghezza n, e l'uso di checkIdentica per controllare se la matrice risultante // dal metodo diaf è la matrice identità. assertTrue(checkIdentica(Esercizio1.diag(Esercizio1.fill(n, 1)))); } } }