Skip to content

Commit 4a1eed2

Browse files
authored
Fix testcode.py formatting and import statements
1 parent 43aab43 commit 4a1eed2

1 file changed

Lines changed: 6 additions & 16 deletions

File tree

testcode.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
2-
Tests for Snake RL + RewardGuard integration (testcode.py).
3-
Run with: pytest test_snake.py -v
2+
Tests for Snake RL + RewardGuard integration.
3+
Run with: pytest testcode.py -v
44
"""
55

66
import os
@@ -10,13 +10,11 @@
1010
import pytest
1111
import numpy as np
1212
from collections import defaultdict
13-
from unittest.mock import patch, MagicMock
1413

1514
os.environ["SDL_VIDEODRIVER"] = "dummy"
1615
os.environ["SDL_AUDIODRIVER"] = "dummy"
1716

18-
# ── Import the modules under test ──────────────────────────────────────────────
19-
from testcode import (
17+
from snake_env import (
2018
SnakeEnv,
2119
QLearningAgent,
2220
Direction,
@@ -122,12 +120,11 @@ def test_survival_reward_when_alive(self, env):
122120
assert rewards["survival"] == 1.0
123121

124122
def test_death_reward_on_collision(self, env):
125-
"""Force a wall collision and verify death reward."""
126123
env.reset()
127124
env.direction = Direction.LEFT
128125
env.head = Point(0, GRID_H // 2)
129126
env.snake = [env.head]
130-
_, rewards, done, _ = env.step(0) # straight → hits wall
127+
_, rewards, done, _ = env.step(0)
131128
assert done
132129
assert rewards["death"] == -50.0
133130

@@ -150,7 +147,6 @@ def test_set_reward_weights(self, env):
150147
assert env.reward_weights["food"] == 5.0
151148

152149
def test_weighted_total_reward(self, env):
153-
"""total_reward must equal sum of weighted components."""
154150
env.reset()
155151
env.set_reward_weights({"survival": 2.0, "food": 3.0, "death": 1.0, "proximity": 1.0})
156152
_, rewards, done, info = env.step(0)
@@ -198,7 +194,7 @@ def test_epsilon_one_always_random(self):
198194
ag = QLearningAgent(epsilon=1.0)
199195
state = (0,) * 11
200196
actions = {ag.act(state) for _ in range(100)}
201-
assert len(actions) > 1, "With epsilon=1 should explore"
197+
assert len(actions) > 1
202198

203199
def test_epsilon_zero_greedy(self):
204200
ag = QLearningAgent(epsilon=0.0)
@@ -218,7 +214,6 @@ def test_learn_done_ignores_next_state(self, agent):
218214
s2 = (1,) * 11
219215
agent.q_table[s2] = np.array([999.0, 999.0, 999.0])
220216
agent.learn(s, 0, -50.0, s2, done=True)
221-
# target = reward only, so q shouldn't approach 999
222217
assert agent.q_table[s][0] < 100
223218

224219
def test_epsilon_decay(self, agent):
@@ -292,8 +287,7 @@ def test_reset_clears_step_count(self, monitor):
292287

293288
def test_expected_percentages_sum_to_100(self):
294289
total = sum(EXPECTED.values())
295-
assert abs(total - 1.0) < 1e-9 or abs(total - 100.0) < 1e-9, \
296-
"EXPECTED values must sum to 1.0 or 100.0"
290+
assert abs(total - 1.0) < 1e-9 or abs(total - 100.0) < 1e-9
297291

298292

299293
# ══════════════════════════════════════════════════════════════════════════════
@@ -306,7 +300,6 @@ def test_single_episode_runs_without_error(self):
306300
env = SnakeEnv(render=False)
307301
agent = QLearningAgent()
308302
monitor = rg.Monitor(expected=EXPECTED, tolerance=5.0, window=200)
309-
310303
state = env.reset()
311304
done = False
312305
steps = 0
@@ -320,7 +313,6 @@ def test_single_episode_runs_without_error(self):
320313
)
321314
state = next_state
322315
steps += 1
323-
324316
assert steps > 0
325317
assert monitor.step_count == steps
326318
env.close()
@@ -329,7 +321,6 @@ def test_multiple_episodes_accumulate_steps(self):
329321
env = SnakeEnv(render=False)
330322
agent = QLearningAgent()
331323
monitor = rg.Monitor(expected=EXPECTED, tolerance=5.0, window=200)
332-
333324
total_steps = 0
334325
for _ in range(3):
335326
state = env.reset()
@@ -346,6 +337,5 @@ def test_multiple_episodes_accumulate_steps(self):
346337
state = next_state
347338
ep_steps += 1
348339
total_steps += ep_steps
349-
350340
assert monitor.step_count == total_steps
351341
env.close()

0 commit comments

Comments
 (0)