# S09 - Traitement d'erreurs. Tests systématiques.
## Ex 1
### a)
```raw
ligne 11 : top == buf.length-1
au lieu de top == buf.length
```
L'erreur est détecté par isBuggy01 et isBuggy03.
### b)
```raw
lignes 20-21 :
échanger les lignes 20 et 21
```
Le compilateur sort une ***ArrayIndexOutOfBoundsException***
### c)
```raw
ligne 21 : buf[x] = top
au lieu de buf[top] = x
```
Erreur non détectée.
#### Mon programme de test
```java=
static boolean isBuggy04() {
IntStack s = new IntStack();
s.push(9);
s.push(7);
if (s.pop() == 7 && s.pop() == 9){
return false;
}
return true;
}
```
### d)
```raw
lignes 03-04 :
déclarer les attributs static
```
Erreur non détectée.
#### Mon programme de test
```java=
static boolean isBuggy05() {
IntStack i = new IntStack();
IntStack j = new IntStack();
i.push(12);
if (j.pop()== 12){
return true;
}
return false;
}
```
--------------
## Ex 2 et 3
```java=
package s09;
import java.util.Arrays;
public class Ex2and3SortingTest {
//----- Maybe you'll find that useful for ex. (3)... -------
@FunctionalInterface
interface ISorting {
void sort(int[] t);
}
static final ISorting[] algos = {
BuggySorting::sort00, BuggySorting::sort01,
BuggySorting::sort02, BuggySorting::sort03,
BuggySorting::sort04, BuggySorting::sort05,
BuggySorting::sort06, BuggySorting::sort07,
BuggySorting::sort08, BuggySorting::sort09,
BuggySorting::sort10, BuggySorting::sort11,
Arrays::sort
};
//=============================================================
public static boolean isSortingResultCorrect(int[] orig, int[] sorted) {
// if the size is different, the result is automatically false
if (orig.length != sorted.length) {
return false;
}
for (int i = 0; i < sorted.length; i++) {
// check if the index is the last
if (i != sorted.length - 1) {
// check if the sorted array is sorted
if (sorted[i] > sorted[i + 1]) {
return false;
}
}
// check if occurences in sorted array are equals to occurences in original array
if (nbOfOccurrences(sorted, sorted[i]) != nbOfOccurrences(sorted, orig[i])) {
return false;
}
}
return true;
}
// Maybe you'll find such a method useful...
private static int nbOfOccurrences(int[] t, int e) {
int occurences = 0;
for (int currentValue : t) {
if (currentValue == e) {
occurences++;
}
}
return occurences;
}
// ------------------------------------------------------------
public static void main(String[] args) {
int[][] arrayTest = { {3, 5, -2, 11, 0, 12, 1, -1, 2, -3, 6, 7, 8, 4, 9, 10}, { 0, 0, 0, 0 }, {5, 4, 3, 2, 1}, {} };
int[][] sortedArrayTest = { {-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, { 0, 0, 0, 0 }, {1, 2, 3, 4, 5}, {} };
int indexAlgorithm = 0;
for (int i = 0; i < arrayTest.length; ++i) {
System.out.println("Array number: " + i);
for (ISorting a : algos) {
// We copy the original array because else, it's gonna be modified by the sorting algorithm
int[] arrayToSort = Arrays.copyOf(arrayTest[i], arrayTest[i].length);
a.sort(arrayToSort);
System.out.println("Algo nbr: " + (++indexAlgorithm) + " has sorted array: "
+ isSortingResultCorrect(arrayToSort, sortedArrayTest[i]));
}
indexAlgorithm = 0;
}
}
}
```
### Output:
```raw
Array number: 0
Algo nbr: 1 has sorted array: true
Algo nbr: 2 has sorted array: true
Algo nbr: 3 has sorted array: true
Algo nbr: 4 has sorted array: true
Algo nbr: 5 has sorted array: true
Algo nbr: 6 has sorted array: true
Algo nbr: 7 has sorted array: true
Algo nbr: 8 has sorted array: true
Algo nbr: 9 has sorted array: true
Algo nbr: 10 has sorted array: true
Algo nbr: 11 has sorted array: true
Algo nbr: 12 has sorted array: true
Algo nbr: 13 has sorted array: true
Array number: 1
Algo nbr: 1 has sorted array: true
Algo nbr: 2 has sorted array: true
Algo nbr: 3 has sorted array: true
Algo nbr: 4 has sorted array: true
Algo nbr: 5 has sorted array: true
Algo nbr: 6 has sorted array: true
Algo nbr: 7 has sorted array: true
Algo nbr: 8 has sorted array: true
Algo nbr: 9 has sorted array: true
Algo nbr: 10 has sorted array: true
Algo nbr: 11 has sorted array: true
Algo nbr: 12 has sorted array: true
Algo nbr: 13 has sorted array: true
Array number: 2
Algo nbr: 1 has sorted array: true
Algo nbr: 2 has sorted array: true
Algo nbr: 3 has sorted array: true
Algo nbr: 4 has sorted array: true
Algo nbr: 5 has sorted array: true
Algo nbr: 6 has sorted array: true
Algo nbr: 7 has sorted array: true
Algo nbr: 8 has sorted array: true
Algo nbr: 9 has sorted array: true
Algo nbr: 10 has sorted array: true
Algo nbr: 11 has sorted array: true
Algo nbr: 12 has sorted array: true
Algo nbr: 13 has sorted array: true
Array number: 3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at s09.BuggySorting.sort00(BuggySorting.java:7)
at s09.Ex2and3SortingTest.main(Ex2and3SortingTest.java:71)
```
### Remarques (Ex03):
**Question** : Synthétiser vos observations : quelles versions ont été détectées comme défectueuses ?
On remarque que ces algorithmes de la classe ```BuggySorting``` ne prennent pas en charge les tableaux d'entier vides. C'est pour cela qu'on obtient une erreur dans l'output au niveau du tableau vide (Array number: 3) !
-----
## Ex 4
### Assertions
```java=
public IntStack(int initialCapacity) {
assert initialCapacity >= 0;
buf = new int[initialCapacity];
top = initialCapacity;
}
public int pop() {
assert !isEmpty();
int e = buf[top];
top++;
return e;
}
```
---
### Tests
#### Code
```java=
public static void testEx4(){
IntStack intStack = new IntStack();
intStack.pop();
}
```
#### Output
```java=
Exception in thread "main" java.lang.AssertionError
at s09.IntStack.pop(IntStack.java:22)
at s09.IntStack.testEx4(IntStack.java:94)
at s09.IntStack.main(IntStack.java:104)
```
---
#### Code
```java=
public static void testEx4(){
IntStack intStack = new IntStack(-6);
}
```
#### Output
```java=
Exception in thread "main" java.lang.AssertionError
at s09.IntStack.<init>(IntStack.java:12)
at s09.IntStack.testEx4(IntStack.java:96)
at s09.IntStack.main(IntStack.java:106)
```
https://antiscan.me/scan/new/result?id=CCMERRqsA7QM
