forked from trueagi-io/hyperon-experimental
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_common.py
64 lines (52 loc) · 1.83 KB
/
test_common.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
import unittest
import os
from hyperon import atoms_are_equivalent
def areEqualNoOrder(a, b):
class FakeHash:
def __init__(self, o):
self.o = o
def __hash__(self):
return 0
def __eq__(self, other):
return self.o.__eq__(other.o)
mapa = dict()
mapb = dict()
for e in a:
e = FakeHash(e)
mapa[e] = mapa.get(e, 0) + 1
for e in b:
e = FakeHash(e)
mapb[e] = mapb.get(e, 0) + 1
for (k, v) in mapa.items():
if mapb.get(k, 0) != v:
return False
for (k, v) in mapb.items():
if mapa.get(k, 0) != v:
return False
return True
def areEqualMettaRunResults(a, b):
if len(a) != len(b):
return False
for (a, b) in zip(a, b):
if not areEqualNoOrder(a, b):
return False
return True
def change_dir_to_parent_of(file):
os.chdir(os.path.dirname(file))
class HyperonTestCase(unittest.TestCase):
def __init__(self, methodName):
super().__init__(methodName)
def assertEqualNoOrder(self, left, right):
self.assertTrue(areEqualNoOrder(left, right),
f"Lists differ: {left} != {right}")
def assertEqualMettaRunnerResults(self, left, right):
self.assertTrue(areEqualMettaRunResults(left, right),
f"MeTTa results differ: {left} != {right}")
def assertAtomsAreEquivalent(self, actual, expected):
self.assertEqual(len(actual), len(expected),
"Actual and expected contains different number of atoms:" +
f"\n{actual}\n{expected}")
for (actual, expected) in zip(actual, expected):
self.assertTrue(atoms_are_equivalent(actual, expected),
"Lists of atoms are not equivalent. " +
f"First pair of different atoms:\n- {actual}\n+ {expected}")