From 9cd3c86391402cf2ddd0c0fe4b566d58b02a9f4a Mon Sep 17 00:00:00 2001
From: Dipam Chakraborty <dipam@aicrowd.com>
Date: Wed, 27 Oct 2021 15:44:16 +0530
Subject: [PATCH] add test for malfunction phase through

---
 tests/test_flatland_malfunction.py       |  2 +-
 tests/test_flatland_states_edge_cases.py | 34 +++++++++++++++++++++++-
 2 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/tests/test_flatland_malfunction.py b/tests/test_flatland_malfunction.py
index d1598d0d..278941b3 100644
--- a/tests/test_flatland_malfunction.py
+++ b/tests/test_flatland_malfunction.py
@@ -119,7 +119,7 @@ def test_malfunction_process():
         total_down_time += env.agents[0].malfunction_handler.malfunction_down_counter
     # Check that the appropriate number of malfunctions is achieved
     # Dipam: The number of malfunctions varies by seed
-    assert env.agents[0].malfunction_handler.num_malfunctions == 46, "Actual {}".format(
+    assert env.agents[0].malfunction_handler.num_malfunctions == 28, "Actual {}".format(
        env.agents[0].malfunction_handler.num_malfunctions)
 
     # Check that malfunctioning data was standing around
diff --git a/tests/test_flatland_states_edge_cases.py b/tests/test_flatland_states_edge_cases.py
index c0d53838..d1d22f26 100644
--- a/tests/test_flatland_states_edge_cases.py
+++ b/tests/test_flatland_states_edge_cases.py
@@ -80,4 +80,36 @@ def test_ready_to_depart_to_stopped():
         env.step({0: RailEnvActions.STOP_MOVING})
 
     
-    assert env.agents[0].state == TrainState.STOPPED
\ No newline at end of file
+    assert env.agents[0].state == TrainState.STOPPED
+
+def test_malfunction_no_phase_through():
+    """
+    A moving train shouldn't phase through a malfunctioning train
+    """
+    stochastic_data = MalfunctionParameters(malfunction_rate=0,  # Rate of malfunction occurence
+                                        min_duration=0,  # Minimal duration of malfunction
+                                        max_duration=0  # Max duration of malfunction
+                                        )
+
+    rail, _, optionals = make_simple_rail()
+
+    env = RailEnv(width=25,
+                  height=30,
+                  rail_generator=rail_from_grid_transition_map(rail, optionals),
+                  line_generator=sparse_line_generator(seed=10),
+                  number_of_agents=2,
+                  malfunction_generator_and_process_data=malfunction_from_params(stochastic_data),
+                  )
+    
+    env.reset(False, False, random_seed=10)
+
+    for _ in range(5):
+        env.step({0: RailEnvActions.MOVE_FORWARD, 1: RailEnvActions.MOVE_FORWARD})
+    
+    env.agents[1].malfunction_handler._set_malfunction_down_counter(10)
+
+    for _ in range(3):
+        env.step({0: RailEnvActions.MOVE_FORWARD, 1: RailEnvActions.DO_NOTHING})
+
+    assert env.agents[0].state == TrainState.STOPPED
+    assert env.agents[0].position == (3, 5)
\ No newline at end of file
-- 
GitLab