Skip to content

Commit 3aa5361

Browse files
committed
test: add unit tests and allow test files in repo
1 parent 51e6da7 commit 3aa5361

3 files changed

Lines changed: 63 additions & 5 deletions

File tree

.gitignore

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
test_assembler.py
2-
test_gpu_core.py
3-
test_gpu.py
4-
test_programs.py
5-
61
# Byte-compiled / optimized / DLL files
72
__pycache__/
83
*.py[cod]

tests/test_assembler.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
import tempfile
3+
import os
4+
from tinygpu.assembler import assemble_file
5+
6+
def test_assemble_file_basic():
7+
code = """
8+
start:
9+
LOAD R1, 42
10+
ADD R2, R1, 1
11+
JMP start
12+
"""
13+
with tempfile.NamedTemporaryFile("w", delete=False, suffix=".tgpu") as f:
14+
f.write(code)
15+
fname = f.name
16+
try:
17+
program, labels = assemble_file(fname)
18+
assert labels["start"] == 0
19+
assert program[0][0] == "LOAD"
20+
assert program[1][0] == "ADD"
21+
assert program[2][0] == "JMP"
22+
assert program[2][1][0] == "start" or program[2][1][0] == 0
23+
finally:
24+
os.remove(fname)

tests/test_gpu.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
import numpy as np
3+
from tinygpu.gpu import TinyGPU
4+
5+
def test_tinygpu_init():
6+
gpu = TinyGPU(num_threads=4, num_registers=8, mem_size=16)
7+
assert gpu.num_threads == 4
8+
assert gpu.num_registers == 8
9+
assert gpu.mem_size == 16
10+
assert gpu.registers.shape == (4, 8)
11+
assert gpu.memory.shape == (16,)
12+
assert gpu.pc.shape == (4,)
13+
assert gpu.active.shape == (4,)
14+
15+
def test_tinygpu_step_and_run():
16+
gpu = TinyGPU(num_threads=2, num_registers=4, mem_size=8)
17+
# Fake program: increment R0, halt after 2 steps
18+
gpu.program = [
19+
("LOAD", [("R", 0), 1]),
20+
("ADD", [("R", 0), ("R", 0), 1]),
21+
]
22+
gpu.pc[:] = 0
23+
gpu.active[:] = True
24+
gpu.sync_waiting[:] = False
25+
gpu.history_registers = []
26+
gpu.history_memory = []
27+
gpu.history_pc = []
28+
# Patch INSTRUCTIONS for test
29+
from tinygpu import instructions
30+
def fake_load(self, tid, reg, val):
31+
self.registers[tid, reg[1]] = val
32+
def fake_add(self, tid, reg, reg2, val):
33+
self.registers[tid, reg[1]] = self.registers[tid, reg2[1]] + val
34+
instructions.INSTRUCTIONS["LOAD"] = fake_load
35+
instructions.INSTRUCTIONS["ADD"] = fake_add
36+
# After first step, both instructions are executed for both threads
37+
gpu.step()
38+
# LOAD sets R0=1, then ADD sets R0=2 in the same step
39+
assert np.all(gpu.registers[:,0] == 2)

0 commit comments

Comments
 (0)