-
Notifications
You must be signed in to change notification settings - Fork 0
/
p4_tests.py
131 lines (117 loc) · 4.63 KB
/
p4_tests.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import unittest
import random
from partial_persistence import Node
from partial_persistence import PartiallyPersistentPointerPackage as PPPP
from partial_persistence import p
class TestPartialPersistence(unittest.TestCase):
def setUp(self):
self.P4 = PPPP()
"""
TO TEST:
init / __str__
node starts empty
parent gets node
named correctly (using __str__)
formatted
prints reasonable things in a couple cases? (didn't implement this b/c annoying)
get_field
current version, old version, too-old version
get from starter node with no mods, starter node with mods,
node freshly copied, node copied and with mods, node copied > 1 time
set_field
value = None, integer, boolean, string, other node object
set on starter node with no mods, starter node with mods,
node freshly copied, node copied and with mods, node copied > 1 time
set to point at itself
"""
def test_init_and_str(self):
n0 = Node("n0", self.P4)
self.assertEqual(str(n0), "n0")
self.assertEqual(self.P4.get_nodes(), [n0])
self.assertEqual(n0.parent, self.P4)
self.assertEqual(n0.mods, [])
self.assertEqual(n0.is_active, True)
def test_get_set_field_current(self):
n0 = Node("n0", self.P4)
n0 = n0.set_field("val", 5)
self.assertEqual(n0.get_field("val"), 5)
def test_get_set_field_past(self):
n0 = Node("n0", self.P4)
n0 = n0.set_field("val", 5)
vers = self.P4.version
n0 = n0.set_field("val", "five")
self.assertEqual(n0.get_field("val"), "five")
self.assertEqual(n0.get_field("val", vers), 5)
def test_get_set_field_nonexistent(self):
n0 = Node("n0", self.P4)
n0 = n0.set_field("val", True)
self.assertEqual(n0.get_field("val", 0), None)
self.assertEqual(n0.get_field("value"), None)
self.assertEqual(n0.get_field("val"), True)
def test_get_set_overflow(self):
# overflow nodes with and without mods
n0 = Node("n0", self.P4)
n0 = n0.set_field("val", -1)
n0 = n0.set_field("truth", True)
for i in range(2*p-2):
n0 = n0.set_field("val", i)
self.assertEqual(n0.get_field("val"), 2*p-3)
for i in range(2*p-2, 2*p):
n0 = n0.set_field("val", i)
self.assertEqual(n0.get_field("val"), 2*p-1)
self.assertEqual(len(n0.mods), 2)
self.assertEqual(n0.get_field("val", 0), 2*p-3)
def test_get_set_overflow_multiple(self):
n0 = Node("n0", self.P4)
for i in range(4*p+1):
n0 = n0.set_field("val", i)
self.assertEqual(n0.get_field("val"), 4*p)
self.assertEqual(len(n0.mods), 1)
def test_get_set_node_fields(self):
# set, unset, check all the pointer chasing
n0 = Node("n0", self.P4)
n1 = Node("n1", self.P4)
n2 = Node("n2", self.P4)
n0 = n0.set_field("val", -1)
n0 = n0.set_field("ptr0", n1)
n1 = n1.set_field("ptr1", n2)
n2 = n2.set_field("ptr2", n1)
self.assertEqual(n1.get_field("ptr1"), n2)
self.assertEqual(n0._get_revptrs(), [])
self.assertEqual(n1._get_revptrs(), [(n0, "ptr0"), (n2, "ptr2")])
self.assertEqual(n2._get_revptrs(), [(n1, "ptr1")])
def test_get_set_node_fields_with_overflow(self):
n0 = Node("n0", self.P4)
n1 = Node("n1", self.P4)
n2 = Node("n2", self.P4)
for i in range(2*p+1):
n0 = n0.set_field("val", i)
n0 = n0.set_field("ptr0", n1)
n1 = n1.set_field("ptr1", n2)
n2 = n2.set_field("ptr2", n1)
self.assertEqual(n1.get_field("ptr1"), n2)
self.assertEqual(n0._get_revptrs(), [])
self.assertEqual(n1._get_revptrs(), [(n0, "ptr0"), (n2, "ptr2")])
self.assertEqual(n2._get_revptrs(), [(n1, "ptr1")])
self.assertEqual(len(n0.mods), 2)
def test_get_set_node_self_pointing(self):
n0 = Node("n0", self.P4)
n1 = Node("n1", self.P4)
n0 = n0.set_field("ptr0", n0)
self.assertEqual(n0.get_field("ptr0"), n0)
n0 = n0.set_field("ptr1", n1)
for i in range(2*p+1):
n0 = n0.set_field("val", i)
self.assertEqual(n0.get_field("ptr0"), n0)
self.assertEqual(n0.get_field("ptr1"), n1)
self.assertEqual(len(n0.mods), 4)
def test_cycle_of_death(self):
n0 = Node("n0", self.P4)
n1 = Node("n1", self.P4)
for i in range(p):
n0 = n0.set_field("ptr"+str(i), n1)
n1 = n1.set_field("ptr"+str(i), n0)
print(n0.formatted())
self.assertEqual(n0.get_field("ptr0"), n1)
if __name__ == '__main__':
unittest.main()