Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for PageManager class #163

Merged
merged 7 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/game_objects/page_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def create_page(self, pid, idx):
self._pages[(pid, idx)] = page
return page

def swap_page(self, page : Page, swap_whole_row : bool):
def swap_page(self, page : Page, swap_whole_row : bool = False):
source_slots = self._swap_slots if page.in_swap else self._ram_slots
target_slots = self._ram_slots if page.in_swap else self._swap_slots

Expand Down
198 changes: 198 additions & 0 deletions src/tests/game_objects/test_page_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
import pytest

from game_objects.page import Page
from game_objects.page_slot import PageSlot
from game_objects.page_manager import PageManager
from stage_config import StageConfig

class TestPageManager:
@pytest.fixture
def stage_config(self):
return StageConfig(
num_cpus=4,
num_ram_rows=1,
)

@pytest.fixture
def stage(self, stage_custom_config, stage_config):
return stage_custom_config(stage_config)

@pytest.fixture
def page_manager(self, stage):
page_manager = PageManager(stage)
page_manager.setup()
return page_manager

def test_create_page_in_ram(self, page_manager):
num_pages = len([child for child in page_manager.children if isinstance(child, Page)])
assert num_pages == 0

page1 = page_manager.create_page(5, 0)
assert page1.pid == 5
assert page1.idx == 0
assert not page1.in_swap
num_pages = len([child for child in page_manager.children if isinstance(child, Page)])
assert num_pages == 1
assert page1 == next(child for child in page_manager.children if isinstance(child, Page))

page2 = page_manager.create_page(5, 1)
assert page2.pid == 5
assert page2.idx == 1
assert not page2.in_swap
num_pages = len([child for child in page_manager.children if isinstance(child, Page)])
assert num_pages == 2
assert page1 in [child for child in page_manager.children if isinstance(child, Page)]
assert page2 in [child for child in page_manager.children if isinstance(child, Page)]

page3 = page_manager.create_page(6, 0)
assert page3.pid == 6
assert page3.idx == 0
assert not page3.in_swap
num_pages = len([child for child in page_manager.children if isinstance(child, Page)])
assert num_pages == 3
assert page1 in [child for child in page_manager.children if isinstance(child, Page)]
assert page2 in [child for child in page_manager.children if isinstance(child, Page)]
assert page3 in [child for child in page_manager.children if isinstance(child, Page)]

assert page1.view.y == page2.view.y == page3.view.y
assert page3.view.x > page2.view.x > page1.view.x

def test_create_page_in_swap(self, page_manager):
pages_in_ram = []
for i in range(PageManager.get_num_cols()):
pages_in_ram.append(page_manager.create_page(1, i))

num_pages = len([child for child in page_manager.children if isinstance(child, Page)])
assert num_pages == PageManager.get_num_cols()

page1 = page_manager.create_page(2, 0)
assert page1.pid == 2
assert page1.idx == 0
assert page1.in_swap
num_pages = len([child for child in page_manager.children if isinstance(child, Page)])
assert num_pages == PageManager.get_num_cols() + 1
assert page1 == next(child for child in page_manager.children if isinstance(child, Page) and child.in_swap)

page2 = page_manager.create_page(2, 1)
assert page2.pid == 2
assert page2.idx == 1
assert page2.in_swap
num_pages = len([child for child in page_manager.children if isinstance(child, Page)])
assert num_pages == PageManager.get_num_cols() + 2
assert page1 in [child for child in page_manager.children if isinstance(child, Page) and child.in_swap]
assert page2 in [child for child in page_manager.children if isinstance(child, Page) and child.in_swap]

for ram_page in pages_in_ram:
assert ram_page.view.y < page1.view.y
assert ram_page.view.y < page2.view.y

assert page1.view.y == page2.view.y
assert page2.view.x > page1.view.x

def test_swap_page_when_can_swap(self, page_manager):
pages = []

for i in range(PageManager.get_num_cols() * 2):
pages.append(page_manager.create_page(1, i))

page_manager.swap_page(pages[0])
assert pages[0].in_swap
assert pages[0].view.y > pages[PageManager.get_num_cols()].view.y
assert pages[0].view.x == pages[PageManager.get_num_cols()].view.x

page_manager.swap_page(pages[2])
assert pages[2].in_swap
assert pages[2].view.y > pages[PageManager.get_num_cols()].view.y
assert pages[2].view.x == pages[PageManager.get_num_cols() + 1].view.x

page_manager.swap_page(pages[2])
assert not pages[2].in_swap
assert pages[2].view.y == pages[1].view.y
assert pages[2].view.x < pages[1].view.x

def test_swap_page_when_cannot_swap(self, page_manager):
pages = []

for i in range(PageManager.get_num_cols() * 2):
pages.append(page_manager.create_page(1, i))

assert pages[PageManager.get_num_cols()].in_swap

(old_x, old_y) = (pages[PageManager.get_num_cols()].view.x, pages[PageManager.get_num_cols()].view.y)

page_manager.swap_page(pages[PageManager.get_num_cols()])

assert pages[PageManager.get_num_cols()].in_swap
assert pages[PageManager.get_num_cols()].view.x == old_x
assert pages[PageManager.get_num_cols()].view.y == old_y

def test_swap_whole_row(self, page_manager):
pages = []

for i in range(PageManager.get_num_cols() * 2):
pages.append(page_manager.create_page(1, i))

page_manager.swap_page(pages[0], swap_whole_row=True)

for i in range(PageManager.get_num_cols()):
assert pages[i].in_swap
assert pages[i].view.x == pages[PageManager.get_num_cols() + i].view.x
assert pages[i].view.y > pages[PageManager.get_num_cols() + i].view.y

new_page = page_manager.create_page(2, 0)
assert not new_page.in_swap

old_y = pages[0].view.y
page_manager.swap_page(pages[0], swap_whole_row=True)

for i in range(PageManager.get_num_cols() - 1):
assert not pages[i].in_swap
assert pages[i].view.x == pages[PageManager.get_num_cols() + i + 1].view.x
assert pages[i].view.y == new_page.view.y

assert pages[PageManager.get_num_cols() - 1].in_swap
assert pages[PageManager.get_num_cols() - 1].view.x == pages[PageManager.get_num_cols() - 1].view.x
assert pages[PageManager.get_num_cols() - 1].view.y == old_y

def test_delete_page_in_ram(self, page_manager):
pages = []

for i in range(PageManager.get_num_cols() * 2):
pages.append(page_manager.create_page(1, i))

page_to_delete = pages[1]
assert not page_to_delete.in_swap

containing_slot = next((
child for child in page_manager.children if isinstance(child, PageSlot) and child.page == page_to_delete
), None)
assert containing_slot is not None

page_manager.delete_page(page_to_delete)

containing_slot = next((
child for child in page_manager.children if isinstance(child, PageSlot) and child.page == page_to_delete
), None)
assert containing_slot is None

def test_delete_page_in_swap(self, page_manager):
pages = []

for i in range(PageManager.get_num_cols() * 2):
pages.append(page_manager.create_page(1, i))

page_to_delete = pages[PageManager.get_num_cols() + 1]
assert page_to_delete.in_swap

containing_slot = next((
child for child in page_manager.children if isinstance(child, PageSlot) and child.page == page_to_delete
), None)
assert containing_slot is not None

page_manager.delete_page(page_to_delete)

containing_slot = next((
child for child in page_manager.children if isinstance(child, PageSlot) and child.page == page_to_delete
), None)
assert containing_slot is None

1 change: 0 additions & 1 deletion src/tests/game_objects/test_process_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,4 +728,3 @@ def test_cpu_hotkeys(self, ready_process_manager_custom_config):

assert process_manager.cpu_list[i - 1].process is None
assert process.cpu is None

Loading