forked from trueagi-io/hyperon-experimental
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_bindings.py
155 lines (114 loc) · 5.19 KB
/
test_bindings.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import unittest
from hyperon import *
from copy import deepcopy
class BindingsTest(unittest.TestCase):
def setUp(self):
self.emptyBindings = Bindings()
self.bindings = Bindings()
self.bindings.add_var_binding(V("a"), S("b"))
self.bindings.add_var_binding(V("x"), S("y"))
def tearDown(self) -> None:
pass
def test_bindings_match_display(self):
pass
'''
commented till sort inside bindings become stable
bindings = hp.bindings_new()
# uncomment this and assert on line 22 fails
#self.assertEqual(hp.bindings_to_str(bindings), "{ }")
hp.bindings_add_var_binding(bindings, "a", hp.atom_sym("b"))
hp.bindings_add_var_binding(bindings, "x", hp.atom_sym("y"))
bindings_as_str = hp.bindings_to_str(bindings)
# sometimes fails
self.assertEqual(bindings_as_str, "{ $x = y, $a = b }")
hp.bindings_free(bindings)
'''
def test_bindings_equality_and_clone(self):
with deepcopy(self.emptyBindings) as cloned_empty:
self.assertEqual(self.emptyBindings, cloned_empty)
with deepcopy(self.bindings) as cloned:
with self.bindings.clone() as cloned_explicitly:
self.assertEqual(self.bindings, cloned)
self.assertEqual(cloned, cloned_explicitly)
# should be after deletion of cloned to ensure that clone is real
self.assertEqual(self.bindings, self.bindings)
self.assertEqual(self.emptyBindings, self.emptyBindings)
self.assertNotEqual(self.emptyBindings, self.bindings)
def test_bindings_merge(self):
merged_empty = Bindings.merge(self.emptyBindings, self.emptyBindings)
self.assertEqual(BindingsSet(self.emptyBindings), merged_empty)
merged_with_empty_lr = Bindings.merge(self.emptyBindings, self.bindings)
merged_with_empty_rl = Bindings.merge(self.bindings, self.emptyBindings)
self.assertEqual(merged_with_empty_rl, merged_with_empty_lr)
self.assertEqual(merged_with_empty_rl, BindingsSet(self.bindings))
merged_self = Bindings.merge(self.bindings, self.bindings)
self.assertEqual(merged_self, BindingsSet(self.bindings))
def test_bindings_is_empty(self):
self.assertTrue(self.emptyBindings.is_empty())
self.assertFalse(self.bindings.is_empty())
def test_bindings_resolve(self):
self.assertIsNone(self.emptyBindings.resolve(V("a")))
self.assertIsNone(self.bindings.resolve(V("XYXY")))
atom_expected = S("b")
atom_resolved = self.bindings.resolve(V("a"))
self.assertEqual(atom_expected, atom_resolved)
def test_bindings_iterator(self):
pass
# uncomment below as sort in bindings become stable.
'''
it = self.bindings.iterator()
expected_names = ["x", "a"]
expected_atoms = [S("y"), S("b")]
for x, expected_name, expected_atom in zip(it, expected_names, expected_atoms):
self.assertEqual(expected_name, x[0])
self.assertEqual(expected_atom, x[1])
'''
def test_bindings_set(self):
empty_set = BindingsSet.empty()
self.assertFalse(empty_set.is_single())
self.assertTrue(empty_set.is_empty())
new_bindings = Bindings()
new_bindings.add_var_binding(V("a"), S("A"))
empty_set.push(new_bindings)
no_longer_empty_set = empty_set
set = BindingsSet()
self.assertTrue(set.is_single())
self.assertFalse(set.is_empty())
set.add_var_binding(V("a"), S("A"))
self.assertFalse(set.is_single())
self.assertFalse(set.is_empty())
self.assertEqual(set, no_longer_empty_set);
new_bindings = Bindings()
new_bindings.add_var_binding(V("a"), S("A"))
set_2 = BindingsSet(new_bindings)
self.assertEqual(set, set_2)
cloned_set = deepcopy(set)
self.assertEqual(set, cloned_set)
cloned_set.add_var_binding(V("a"), S("B"))
self.assertTrue(cloned_set.is_empty())
set.add_var_equality(V("a"), V("a_prime"))
bindings_counter = 0
for _ in set.iterator():
bindings_counter += 1
self.assertEqual(1, bindings_counter)
new_bindings = Bindings()
new_bindings.add_var_binding(V("b"), S("B"))
set.merge_into(new_bindings)
new_bindings = Bindings()
new_bindings.add_var_binding(V("c"), S("C"))
new_set = BindingsSet(new_bindings)
set.merge_into(new_set)
new_bindings = Bindings()
new_bindings.add_var_binding(V("d"), S("D"))
set_bindings_list = list(set.iterator())
self.assertEqual(len(set_bindings_list), 1)
new_set = new_bindings.merge(set_bindings_list[0])
expected_bindings_set = BindingsSet()
expected_bindings_set.add_var_binding(V("a"), S("A"))
expected_bindings_set.add_var_binding(V("b"), S("B"))
expected_bindings_set.add_var_binding(V("c"), S("C"))
expected_bindings_set.add_var_binding(V("d"), S("D"))
expected_bindings_set.add_var_equality(V("a"), V("a_prime"))
self.assertEqual(new_set, expected_bindings_set)
if __name__ == "__main__":
unittest.main()