Skip to content
Snippets Groups Projects
Select Git revision
  • 7571e9510743cc50a82d54be521d9c47f0e98ac8
  • master default protected
2 results

ExercicesConditionnelleTest.java

Blame
  • Forked from NAVES Guyslain / ProgAvExercices
    Source project has a limited visibility.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    ExercicesConditionnelleTest.java 4.82 KiB
    package fr.univamu.progav.td1;
    
    import org.junit.jupiter.api.Test;
    
    import java.util.List;
    
    import static org.junit.jupiter.api.Assertions.assertEquals;
    import static org.junit.jupiter.api.Assertions.assertTrue;
    
    class ExercicesConditionnelleTest {
       static final class MockCar implements ExercicesConditionnelle.Car {
        int nbGo = 0;
        int nbStop = 0;
        public void go() { nbGo++; }
        public void stop() { nbStop++; }
      }
      enum Light implements ExercicesConditionnelle.TrafficLight {
        RED("rouge"), GREEN("vert"), YELLOW("orange");
        private final String name;
        Light(String name) { this.name = name; }
        public boolean isGreen() { return this == GREEN; }
        public boolean isRed() { return this == RED; }
        public boolean isYellow() { return this == YELLOW; }
      }
    
      @Test
      void testDecideGo() {
        MockCar car = new MockCar();
        ExercicesConditionnelle.decideStopOrGo(car, Light.GREEN,()->true);
        String situation = "(" + Light.GREEN.name + ",  carrefour libre)";
        assertTrue(1 == car.nbGo + car.nbStop,
          () -> "la voiture doit prendre exactement une décision " + situation);
        assertTrue(0 == car.nbStop,() -> "La voiture ne doit pas s'arrêter au vert " + situation);
        assertTrue(1 == car.nbGo,() -> "La voiture doit s'avancer au vert " + situation);
      }
    
      @Test
      void testDecideStop() {
         for (Light light : Light.values()) {
           MockCar car = new MockCar();
           ExercicesConditionnelle.decideStopOrGo(car, light,()-> false);
           String situation = "(" + light.name + ",  carrefour occupé)";
           assertTrue(1 == car.nbGo + car.nbStop,
             () -> "la voiture doit prendre exactement une décision " + situation);
           assertTrue(0 == car.nbGo,()-> "La voiture ne doit pas avancer " + situation);
           assertTrue(1 == car.nbStop, () -> "La voiture doit s'arrêter " + situation);
         }
        for (Light light : List.of(Light.YELLOW,Light.RED)) {
          MockCar car = new MockCar();
          ExercicesConditionnelle.decideStopOrGo(car, light,()-> true);
          String situation = "(" + light.name + ",  carrefour libre)";
          assertTrue(car.nbGo + car.nbStop == 1, () -> "la voiture doit prendre exactement une " +
            "décision" +
            "situation");
          assertTrue(car.nbGo == 0,()-> "La voiture ne doit pas avancer " + situation);
          assertTrue(car.nbStop == 1, () -> "La voiture doit s'arrêter " + situation);
        }
      }
    
      record MockCustomer(
        int age,
        boolean isUnemployed,
        boolean hasAnnualSubscription,
        double price
      ) implements ExercicesConditionnelle.Customer {
        @Override
        public String toString() {
          return "Âge " + age + " "
            + (isUnemployed? "sans emploi ": "")
            + (hasAnnualSubscription? "avec abonnement annuel": "");