JUnit
This commit is contained in:
BIN
DEV2.3/TP02/Ex2/Largest.class
Normal file
BIN
DEV2.3/TP02/Ex2/Largest.class
Normal file
Binary file not shown.
20
DEV2.3/TP02/Ex2/Largest.java
Normal file
20
DEV2.3/TP02/Ex2/Largest.java
Normal file
@@ -0,0 +1,20 @@
|
||||
public class Largest {
|
||||
|
||||
/**
|
||||
* Return the largest element in a list.
|
||||
*
|
||||
* @param list A list of integers
|
||||
* @return The largest number in the given list
|
||||
* @throws NullPointerException si le tableau est null
|
||||
*/
|
||||
public static int largest(int[] list) {
|
||||
int max = list[0];
|
||||
|
||||
for (int index = 0; index <= list.length-1; index++) {
|
||||
if (list[index] > max) {
|
||||
max = list[index];
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
}
|
BIN
DEV2.3/TP02/Ex2/TestLargest.class
Normal file
BIN
DEV2.3/TP02/Ex2/TestLargest.class
Normal file
Binary file not shown.
46
DEV2.3/TP02/Ex2/TestLargest.java
Normal file
46
DEV2.3/TP02/Ex2/TestLargest.java
Normal file
@@ -0,0 +1,46 @@
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import org.junit.Test;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class TestLargest {
|
||||
|
||||
@Test
|
||||
public void testSimple() {
|
||||
assertEquals(9, Largest.largest(new int[] {9,8,7}));
|
||||
}
|
||||
|
||||
@Test(expected = ArrayIndexOutOfBoundsException.class)
|
||||
public void testTableauVide() {
|
||||
Largest.largest(new int[] {});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFonctionCorrecte() {
|
||||
int[] t1 = {5, 4, 8, 1, 4};
|
||||
int[] t2 = {15, 4, 8, 49, 5, 89};
|
||||
int[] t3 = {4, -5, 8, 9, 4};
|
||||
|
||||
int maxt1 = Largest.largest(t1);
|
||||
int maxt2 = Largest.largest(t2);
|
||||
int maxt3 = Largest.largest(t3);
|
||||
Arrays.sort(t1);
|
||||
Arrays.sort(t2);
|
||||
Arrays.sort(t3);
|
||||
|
||||
assertEquals(t1[t1.length-1], maxt1);
|
||||
assertEquals(t2[t2.length-1], maxt2);
|
||||
assertEquals(t3[t3.length-1], maxt3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTableauNegatif() {
|
||||
assertEquals(-4, Largest.largest(new int[] {-15, -5, -4, -58}));
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void testTableauNull() {
|
||||
int[] t = null;
|
||||
Largest.largest(t);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user