constants, Sequenzen

This commit is contained in:
Oliver Hofmann 2025-03-25 19:20:32 +01:00
parent 8702bd847e
commit 09d81f1348
7 changed files with 11193 additions and 10 deletions

14
data/seq0.txt Normal file
View File

@ -0,0 +1,14 @@
-59
52
46
14
-50
58
-87
-77
34
15
50
47
51
48

100
data/seq1.txt Normal file
View File

@ -0,0 +1,100 @@
56
13
97
14
5
40
33
20
51
98
52
89
31
33
47
59
47
75
11
39
0
79
33
57
5
1
28
87
77
54
35
21
24
97
96
94
6
31
-45
53
-98
-44
85
-76
-48
-90
-99
47
-11
16
-98
98
-23
57
27
-35
23
65
-54
96
71
99
81
30
-7
76
-22
43
62
-49
73
59
75
36
39
15
51
-51
63
69
1
1
-25
-18
88
86
93
33
71
-95
56
2
4
11
-55
28
60
-55
-69
-97

10000
data/seq2.txt Normal file

File diff suppressed because it is too large Load Diff

1000
data/seq3.txt Normal file

File diff suppressed because it is too large Load Diff

41
utils/constants.py Normal file
View File

@ -0,0 +1,41 @@
from literal import Literal
class MinValue(Literal):
def __init__(self):
super().__init__(0)
def __gt__(self, other):
return False
def __ge__(self, other):
return False
def __lt__(self, other):
return True
def __le__(self, other):
return True
def __eq__(self, other):
return False
class MaxValue(Literal):
def __init__(self):
super().__init__(0)
def __gt__(self, other):
return True
def __ge__(self, other):
return True
def __lt__(self, other):
return False
def __le__(self, other):
return False
def __eq__(self, other):
return False

View File

@ -1,6 +1,7 @@
from literal import Literal
from memory_cell import MemoryCell
from memory_manager import MemoryManager
from pathlib import Path
class MemoryArray:
@ -48,6 +49,33 @@ class MemoryArray:
for cell in self.cells:
cell.reset_counters()
@staticmethod
def create_random_array(count, min_value, max_value):
"""Erzeugt ein zufälliges Speicherarray."""
size = Literal(count)
a = MemoryArray(size)
for i in a.indices():
a[i] = Literal(random.randint(min_value, max_value))
a.reset_counters()
return a
@staticmethod
def create_array_from_file(filename, limit=None):
"""Erzeugt ein Speicherarray aus einer Datei."""
this_dir = Path(__file__).resolve().parent
project_dir = this_dir.parent
filename = project_dir / filename
with open(filename) as f:
lines = f.readlines()
if limit is not None:
lines = lines[:limit]
size = Literal(len(lines))
a = MemoryArray(size)
for i, line in enumerate(lines):
a[Literal(i)] = Literal(int(line))
a.reset_counters()
return a
if __name__ == "__main__":
import random

View File

@ -1,8 +1,8 @@
from unittest import TestCase
from literal import Literal
from memory_cell import MemoryCell
from memory_array import MemoryArray
import random
from pathlib import Path
class TestMemoryArray(TestCase):
@ -10,18 +10,15 @@ class TestMemoryArray(TestCase):
l = random.randint(5,10)
size = Literal(l)
a = MemoryArray(size)
self.assertEqual(len(a), l)
def test_set_item(self):
l = random.randint(5,10)
size = Literal(l)
a = MemoryArray(size)
i = Literal(random.randint(0,l-1))
v = Literal(random.randint(1,100))
a[i] = v
self.assertEqual(a[i].value, v.value)
def test_get_item(self):
@ -29,29 +26,32 @@ class TestMemoryArray(TestCase):
size = Literal(l)
a = MemoryArray(size)
values = []
for i in a.indices():
v = Literal(random.randint(1,100))
values.append(v.value)
a[i] = v
for pos, i in enumerate(a.indices()):
self.assertEqual(a[i].value, values[pos])
def test_reset_counters(self):
l = random.randint(5,10)
size = Literal(l)
a = MemoryArray(size)
for i in a.indices():
v = Literal(random.randint(1,100))
a[i] = v
for i in a.indices():
self.assertEqual(a[i].write_count, 1)
a.reset_counters()
for i in a.indices():
self.assertEqual(a[i].write_count, 0)
def test_create_random_array(self):
a = MemoryArray.create_random_array(10, 1, 100)
self.assertEqual(len(a), 10)
def test_create_array_from_file(self):
a = MemoryArray.create_array_from_file("data/seq0.txt")
self.assertEqual(len(a), 14)