✏️ Exercices : Approche structurelle
1. Graphes de flot de contrôle (CFGs)
1.1. Exercice 1
Écrivez le graphe de flot de contrôle (CFG) de cette méthode :
public void printI() {
for(int i=0; i < 10; i++) {
System.out.println("I is: " + i);
}
}
1.2. Exercice 2
Écrivez le graphe de flot de contrôle (CFG) de cette méthode :
public boolean m(String[] strings) {
boolean found = false;
int i = 0;
while( i < strings.length ) {
if(! strings[i].startsWith("J")) {
i++;
continue;
}
if(! strings[i].startsWith("G")) {
found = true;
break;
}
i++;
}
return found;
}
1.3. Exercice 3
Écrivez le graphe de flot de contrôle (CFG) de cette méthode :
public int startsWithJ(String[] strings) {
int wordsStartingWithJ = 0;
for(String each : strings) {
if(! each.startsWith("J")) {
continue;
}
wordsStartingWithJ++;
}
return wordsStartingWithJ;
}
1.4. Exercice 4
Écrivez le graphe de flot de contrôle (CFG) de la méthode add :
import java.lang.ref.WeakReference;
public class Queue<E> {
protected Object[] values = new Object[10];
protected Entry<E>[] entries = new Entry[10];
protected int valuesLength;
public void add(E value, Entry<E> entry) {
int length = values.length;
if (valuesLength == length) {
Object[] newValues = new Object[2 * length];
System.arraycopy(values, 0, newValues, 0, valuesLength);
values = newValues;
}
if (entries == null || valuesLength == entries.length) {
Entry<E>[] newEntries = new Entry[2 * length];
if (entries != null) {System.arraycopy(entries, 0, newEntries, 0, valuesLength);}
entries = newEntries;
}
values[valuesLength] = value;
entries[valuesLength++] = entry;
}
protected static class Entry<E> extends WeakReference<E> {
public final int hashCode = 0;
private Entry() {
super(null);
}}}
2. Application de l’approche structurelle
2.1. Exercice 1 − indexOf
Considérez le code suivant :
/**
* Searches for the first occurrence of the given argument in list starting from
* a specified index. The equality is tested using both the operator '==' and
* the 'equals' method.
*
* @param list the list in which the object must be searched.
* @param o the object to search.
* @param fromIndex the index in 'list' from which the search must start.
* @return the index of the first occurrence of 'o' in 'list' (where
* index>=fromIndex); returns -1 if the object is not found.
*/
public static int indexOf(List<?> list, Object o, int fromIndex) {
if (fromIndex < 0) {
fromIndex = 0;
}
int size = list.size();
for (int i = fromIndex; i < size; i++) {
Object element = list.get(i);
if (o == null) {
if (element == null) {
return i;
}
} else if (o == element || o.equals(element)) {
return i;
}
}
return -1;
}
2.1.1. Question 1
Identifiez les paramètres d’entrée de la méthode (domaine d’entrée), ainsi les valeurs observables en résultat (domaine de sortie).
2.1.3. Question 3
Donnez le plus petit ensemble possible de chemins exécutables permettant de satisfaire le critère "tous les sommets".
Déduisez un tableau de scénarios de tests à partir des chemins obtenus.
2.2. Exercice 2 − BasicAlarmClock
Considérez le code suivant, qui fait partie d’une classe Java appelée BasicAlarmClock.
/**
* Construct a BasicAlarmClock.
*
* @param ring The value for 'ring'. Should be in [0; 10].
* @param hour The value for 'hour'. Should be in [0; 23].
* @param min The value for 'min'. Should be in [0; 59].
* @throws AlarmClockException If one parameter is out of bounds.
*/
protected BasicAlarmClock(int ring, int hour, int min) throws AlarmClockException {
if (hour < 0) {
throw new AlarmClockException("bad hour: inf value");
} else if (hour > 23) {
throw new AlarmClockException("bad hour: sup value");
} else if (min < 0) {
throw new AlarmClockException("bad min: inf value");
} else if (min > 59) {
throw new AlarmClockException("bad min: sup value");
} else if ((ring < 1) || (ring > 10)) {
throw new AlarmClockException("bad ringtone: out of limits");
} else {
this.ring = ring;
this.hour = hour;
this.min = min;
}
}
2.2.1. Question 1
Identifiez les paramètres d’entrée de la méthode (domaine d’entrée), ainsi les valeurs observables en résultat (domaine de sortie).
2.2.3. Question 3
Donnez le plus petit ensemble possible de chemins exécutables permettant de satisfaire le critère "tous les sommets".
Déduisez un tableau de scénarios de tests à partir des chemins obtenus.
2.3. Exercice 3 − attack
On considère une classe Pawn qui permet de représenter des pions dans un jeu.
Un pion est situé sur un plateau (board), peut se déplacer et attaquer d’autres pions.
Le plateau est géré via une classe Board dédiée, et la position des pions est gérée par une classe Position qui stocke des coordonnées (x,y).
On sait que la classe Pawn possède à minima les attributs et le constructeur suivant :
public class Pawn {
/**
* Position of the pawn in the board.
*/
private Position position;
/**
* The board on which the pawn is located
*/
private Board board;
/**
* The remaining number of hitpoints of the pawn
*/
private int hitpoints;
/**
* The collected gold of the pawn.
*/
private int gold;
/**
* The char that represents the pawn.
*/
private char letter;
/**
* Create a Pawn with 2 hitpoints and 0 gold.
*
* @param n The letter that represents the pawn.
* @param x Position on the x axis
* @param y Position on the y axis
* @param board The board on which the pawn is located
*/
public Pawn(char n, int x, int y, Board board) {
this.letter = n;
this.position = new Position(x, y);
this.board = board;
this.hitpoints = 2;
this.gold = 0;
}
…
}
Au sein de la classe Pawn, on s’intéresse à la méthode attack suivante que l’on souhaite tester :
/**
* Make the pawn attack another position. If any, the enemy pawn should
* suffer 1 damage, but it should suffer 2 damages if the current pawn
* is on a bonus square.
*
* If the enemy pawn dies, then the current pawn obtains 1 gold.
*
* @param enemy The attacked pawn.
* @throws ImpossibleActionException If the target position is too far or empty.
*/
public void attack(Position p) throws ImpossibleActionException {
if (this.position.isNextTo(p)) {
Pawn enemy = this.board.getSquareContent(p);
if (enemy == null) {
throw new ImpossibleActionException("There is no enemy pawn in that position, cannot attack.");
} else {
Logger.log(this.letter + " attacks!\n");
if (this.board.isBonusSquare(this.position))
enemy.suffer(2);
else
enemy.suffer(1);
if (enemy.isDead())
gold++;
}
} else {
throw new ImpossibleActionException("Square is too far, cannot attack.");
}
}
2.3.1. Question 1
Identifiez les paramètres d’entrée de la méthode (domaine d’entrée), ainsi les valeurs observables en résultat (domaine de sortie).
2.3.3. Question 3
Donnez le plus petit ensemble possible de chemins exécutables permettant de satisfaire le critère "tous les sommets".
Déduisez un tableau de scénarios de tests à partir des chemins obtenus.