-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtest_memory.py
107 lines (75 loc) · 3.26 KB
/
test_memory.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import unittest
import os
import ctypes
from VM.Memory import Memory
class TestMemory(unittest.TestCase):
MEM_SIZE = 512
MAX_RANDOM_REPEAT = 10_000
def setUp(self):
self.mem = Memory(self.MEM_SIZE)
self.random_data = os.urandom(self.MEM_SIZE)
ctypes.memmove(self.mem.mem, self.random_data, self.MEM_SIZE)
def test_1_size(self):
self.assertEqual(self.mem.size, self.MEM_SIZE)
def test_get_8(self):
for offset in range(self.mem.size):
ret = self.mem.get(offset, 1)
correct = self.random_data[offset]
self.assertEqual(ret, correct)
def do_test_get(self, size: int):
for offset in range(self.mem.size - size):
ret = self.mem.get(offset, size)
correct = int.from_bytes(self.random_data[offset:offset + size], 'little')
self.assertEqual(ret, correct)
for offset in range(1, self.mem.size - size):
ret = self.mem.get(offset, size)
correct = int.from_bytes(self.random_data[offset:offset + size], 'little')
self.assertEqual(ret, correct)
if size == 4:
# test all possible permutations
for offset in range(2, self.mem.size - size):
ret = self.mem.get(offset, size)
correct = int.from_bytes(self.random_data[offset:offset + size], 'little')
self.assertEqual(ret, correct)
for offset in range(3, self.mem.size - size):
ret = self.mem.get(offset, size)
correct = int.from_bytes(self.random_data[offset:offset + size], 'little')
self.assertEqual(ret, correct)
def test_get_16(self):
self.do_test_get(2)
def test_get_32(self):
self.do_test_get(4)
def test_set_8(self):
for offset in range(self.mem.size):
correct, = os.urandom(1)
self.mem.set(offset, 1, correct)
ret = self.mem.get(offset, 1)
self.assertEqual(ret, correct)
def do_test_set(self, size: int):
for offset in range(self.mem.size - size):
correct = int.from_bytes(os.urandom(size), 'little')
self.mem.set(offset, size, correct)
ret = self.mem.get(offset, size)
self.assertEqual(ret, correct)
for offset in range(1, self.mem.size - size):
correct = int.from_bytes(os.urandom(size), 'little')
self.mem.set(offset, size, correct)
ret = self.mem.get(offset, size)
self.assertEqual(ret, correct)
if size == 4:
for offset in range(2, self.mem.size - size):
correct = int.from_bytes(os.urandom(size), 'little')
self.mem.set(offset, size, correct)
ret = self.mem.get(offset, size)
self.assertEqual(ret, correct)
for offset in range(3, self.mem.size - size):
correct = int.from_bytes(os.urandom(size), 'little')
self.mem.set(offset, size, correct)
ret = self.mem.get(offset, size)
self.assertEqual(ret, correct)
def test_set_16(self):
self.do_test_set(2)
def test_set_32(self):
self.do_test_set(4)
if __name__ == '__main__':
unittest.main(verbosity=2)