-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_main.py
More file actions
85 lines (64 loc) · 3.11 KB
/
test_main.py
File metadata and controls
85 lines (64 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from main import ALU
class TestALU:
def tests(self):
alu = ALU()
# TEST ADDITION
print("TEST ADDITION")
print("=" * 50)
result = alu.execute(0,10,15)
assert result == 25, f"Expected 25, got {result}"
print(f"Input A: 10 (0b{10:08b}),\nInput B : 15 (0b{15:08b}),\nResult: {result} (0b{result:08b})")
print(f"Flags: Z={alu.flags['Z']}, N={alu.flags['N']}, O={alu.flags['O']}")
assert alu.flags['Z'] == 0, f"Zero flag should be 0"
assert alu.flags['N'] == 0, f"Negative flag should be 0"
assert alu.flags['O'] == 0, f"Overflow flag should be 0"
print("=" * 50)
# TEST SUBTRACTION
print("TEST SUBTRACTION")
print("=" * 50)
result = alu.execute(1,10,3)
assert result == 7, f"Expected 7, got {result}"
print(f"Input A: 10 (0b{10:08b}),\nInput B : 3 (0b{3:08b}),\nResult: {result} (0b{result:08b})")
print(f"Flags: Z={alu.flags['Z']}, N={alu.flags['N']}, O={alu.flags['O']}")
assert alu.flags['Z'] == 0, f"Zero flag should be 0"
assert alu.flags['N'] == 0, f"Negative flag should be 0"
assert alu.flags['O'] == 0, f"Overflow flag should be 0"
print("=" * 50)
#TEST SIGNED OVERFLOW
print("TEST SIGNED OVERFLOW")
print("=" * 50)
result = alu.execute(0,127,1)
print(f"Input A: 127 (0b{127:08b}) [+127 in signed]")
print(f"Input B: 1 (0b{1:08b}) [+1 in signed]")
print(f"Result: {result} (0b{result:08b}) [-128 in signed - OVERFLOW!]")
print(f"Flags: Z={alu.flags['Z']}, N={alu.flags['N']}, O={alu.flags['O']}")
assert result == 128, f"Expected 128, got {result}"
assert alu.flags['Z'] == 0, "Zero flag should be 0"
assert alu.flags['N'] == 1, "Negative flag should be 1 (bit 7 is set)"
assert alu.flags['O'] == 1, "Overflow flag should be 1 (positive + positive = negative)"
print("=" * 50)
# TEST ZERO FLAG
print("TEST ZERO FLAG")
print("=" * 50)
result = alu.execute(0, 5, 251)
print(f"Input A: 5 (0b{5:08b}) [+5]")
print(f"Input B: 251 (0b{251:08b}) [-5 in two's complement]")
print(f"Result: {result} (0b{result:08b}) [Carry discarded]")
print(f"Flags: Z={alu.flags['Z']}, N={alu.flags['N']}, O={alu.flags['O']}")
assert result == 0, f"Expected 0, got {result}"
assert alu.flags['Z'] == 1, "Zero flag should be 1"
assert alu.flags['N'] == 0, "Negative flag should be 0"
print("=" * 50)
# Bonus Test: Edge case - Max unsigned addition
print("Unsigned Overflow: 255 + 1")
print("=" * 50)
result = alu.execute(0, 255, 1)
print(f"Input A: 255 (0b{255:08b})")
print(f"Input B: 1 (0b{1:08b})")
print(f"Result: {result} (0b{result:08b}) [Wrapped to 0]")
print(f"Flags: Z={alu.flags['Z']}, N={alu.flags['N']}, O={alu.flags['O']}")
assert result == 0, f"Expected 0 (wrapped), got {result}"
print("=" * 50)
print("PASSES ALL TESTS, SUCCESSFULLY")
if __name__ == "__main__":
TestALU().tests()