From 3028915877f71e788325a93c00a84b46f6c47a54 Mon Sep 17 00:00:00 2001 From: Eddy Pedroni Date: Tue, 24 Sep 2024 07:55:55 +0200 Subject: Scheduler update implementation --- src/scheduler_brutal.py | 10 ++++++--- src/scheduler_brutal_unittest.py | 44 ++++++++++++++++------------------------ 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/src/scheduler_brutal.py b/src/scheduler_brutal.py index 0c7463f..d1c98b5 100644 --- a/src/scheduler_brutal.py +++ b/src/scheduler_brutal.py @@ -1,10 +1,12 @@ """ """ +from card import Card + HISTORY_DEPTH = 8 class SchedulerBrutal: - def __init__(self, cards: dict, state: dict): + def __init__(self, cards: dict[int, Card], state: dict): self._cards = cards self._state = {} @@ -26,8 +28,10 @@ class SchedulerBrutal: def test(self, size: int) -> dict: pass - def update(self, results: dict) -> None: - pass + def update(self, results: dict[int, int]) -> None: + # Add card result to sliding window, or None if card was not shown + self._state = {id: history[1:] + [results.get(id, None)] + for id, history in self._state.items()} def getState(self) -> dict: return self._state diff --git a/src/scheduler_brutal_unittest.py b/src/scheduler_brutal_unittest.py index 98de2f1..1ee6402 100644 --- a/src/scheduler_brutal_unittest.py +++ b/src/scheduler_brutal_unittest.py @@ -7,37 +7,29 @@ from card import Card scheduler_brutal.HISTORY_DEPTH = 3 #-------------------------------------------------------------------------- -# +# State update #-------------------------------------------------------------------------- +def test_stateUpdate(): + cards = {0: Card("f", "b"), 1: Card("a", "b"), 2: Card("c", "d")} + state = {0: [1, 0, 1], 1: [1, 0, 0], 2: [0, 0, 1]} -#-------------------------------------------------------------------------- -# -#-------------------------------------------------------------------------- - -#-------------------------------------------------------------------------- -# -#-------------------------------------------------------------------------- + uut = UUT(cards, state) -#-------------------------------------------------------------------------- -# -#-------------------------------------------------------------------------- + result = {0: 1, 1: 0} + expected_state = {0: [0, 1, 1], 1: [0, 0, 0], 2: [0, 1, None]} -#-------------------------------------------------------------------------- -# -#-------------------------------------------------------------------------- + uut.update(result) -#-------------------------------------------------------------------------- -# -#-------------------------------------------------------------------------- + assert uut.getState() == expected_state #-------------------------------------------------------------------------- # State corrections #-------------------------------------------------------------------------- def test_stateWhenCardsChanged(): - cards = {"0": Card("f", "b"), "1": Card("a", "b")} + cards = {0: Card("f", "b"), 1: Card("a", "b")} - initial_state = {"0": [1, 0, 1], "-1": [0, 0, 0]} - expected_state = {"0": [1, 0, 1], "1": [None, None, None]} + initial_state = {0: [1, 0, 1], -1: [0, 0, 0]} + expected_state = {0: [1, 0, 1], 1: [None, None, None]} uut = UUT(cards, initial_state) @@ -46,10 +38,10 @@ def test_stateWhenCardsChanged(): def test_stateWhenHistoryDepthIncreased(): scheduler_brutal.HISTORY_DEPTH = 5 - cards = {"0": Card("f", "b"), "1": Card("a", "b"), "2": Card("new", "new")} + cards = {0: Card("f", "b"), 1: Card("a", "b"), 2: Card("new", "new")} - initial_state = {"0": [1, 0, 1], "1": [0, 0, 0]} - expected_state = {"0": [None, None, 1, 0, 1], "1": [None, None, 0, 0, 0], "2": [None] * 5} + initial_state = {0: [1, 0, 1], 1: [0, 0, 0]} + expected_state = {0: [None, None, 1, 0, 1], 1: [None, None, 0, 0, 0], 2: [None] * 5} uut = UUT(cards, initial_state) @@ -58,10 +50,10 @@ def test_stateWhenHistoryDepthIncreased(): def test_stateWhenHistoryDepthDecreased(): scheduler_brutal.HISTORY_DEPTH = 1 - cards = {"0": Card("f", "b"), "1": Card("a", "b"), "2": Card("new", "new")} + cards = {0: Card("f", "b"), 1: Card("a", "b"), 2: Card("new", "new")} - initial_state = {"0": [1, 0, 0], "1": [0, 0, 1]} - expected_state = {"0": [0], "1": [1], "2": [None]} + initial_state = {0: [1, 0, 0], 1: [0, 0, 1]} + expected_state = {0: [0], 1: [1], 2: [None]} uut = UUT(cards, initial_state) -- cgit v1.2.3