Skip to content
Snippets Groups Projects
Commit bac1a093 authored by Guyslain's avatar Guyslain
Browse files

Ajout TD2 (utilisation du debugger)

parent 12c7911f
No related branches found
No related tags found
No related merge requests found
package fr.univamu.progav.td2;
import org.junit.jupiter.api.Test;
import static fr.univamu.progav.td2.GuessingGame.LOWER_BOUND;
import static fr.univamu.progav.td2.GuessingGame.UPPER_BOUND;
import static org.junit.jupiter.api.Assertions.*;
class GuessingGameTest {
@Test
void solve() {
int max_allowed = // a bound on the minimum number of guesses by the best strategy.
(int) Math.ceil(log2(GuessingGame.UPPER_BOUND - GuessingGame.LOWER_BOUND + 2,1e-1));
for (int i = GuessingGame.LOWER_BOUND; i <= GuessingGame.UPPER_BOUND; i++) {
int r = GuessingGame.solve(i);
assertTrue(r <= max_allowed,
"Guessing " + i + " in " + r + "/" + max_allowed + " attempts");
}
}
// compute log(x) in base 2, with given precision, for instance log2(x,1e-6)
// is at most 0.000001 away from the exact value.
private static double log2(double x, double precision) {
return
(x >= 2)? 1 + log2(x/2, precision):
(x == 1)? 0:
(x < 1)? - log2(1/x, precision):
(precision > 1) ? 0:
0.5 * log2(x * x, precision*2);
}
}
\ No newline at end of file
package fr.univamu.progav.td2;
import org.junit.jupiter.api.Test;
import java.util.List;
import static fr.univamu.progav.td2.People.whereIsCharlie;
import static org.junit.jupiter.api.Assertions.*;
class PeopleTest {
private final List<People> peopleList =
List.of(
new People("Alfa", 12),
new People("Bravo", 18),
new People("Charlie", 25),
new People("Delta", 36),
new People("Echo", 8),
new People("Foxtrot", 11),
new People("Golf", 21),
new People("Hotel", 53),
new People("India", 42),
new People("Juliett", 28)
);
@Test
void whereIsCharlieTest() {
int charliePosition = whereIsCharlie(peopleList);
assertEquals(2, charliePosition);
List<People> noCharlie =
peopleList.stream().filter(p -> !p.getName().equals("Charlie")).toList();
int noCharliePosition = whereIsCharlie(noCharlie);
assertEquals(-1, noCharliePosition);
}
}
\ No newline at end of file
package fr.univamu.progav.td2;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class RatioTest {
@Test
void plusTest() {
assertEquals(Ratio.unsafeOf(2,1),Ratio.of(1,1).plus(Ratio.of(1,1)));
assertEquals(Ratio.unsafeOf(5,6),Ratio.of(1,3).plus(Ratio.of(1,2)));
assertEquals(Ratio.unsafeOf(1,6),Ratio.of(-1,3).plus(Ratio.of(1,2)));
assertEquals(Ratio.unsafeOf(-1,6),Ratio.of(1,3).plus(Ratio.of(-1,2)));
assertEquals(Ratio.unsafeOf(-5,6),Ratio.of(-1,3).plus(Ratio.of(-1,2)));
assertEquals(Ratio.unsafeOf(-5,6),Ratio.of(1,-3).plus(Ratio.of(1,-2)));
assertEquals(Ratio.unsafeOf(5,6),Ratio.of(-1,-3).plus(Ratio.of(1,2)));
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment