forked from trueagi-io/hyperon-experimental
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_stdlib.py
More file actions
120 lines (95 loc) · 5.95 KB
/
Copy pathtest_stdlib.py
File metadata and controls
120 lines (95 loc) · 5.95 KB
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
import unittest
from hyperon import *
from hyperon.stdlib import Char
from test_common import *
class StdlibTest(HyperonTestCase):
def test_text_ops(self):
metta = MeTTa(env_builder=Environment.test_env())
# Check that (repr (my atom)) == "(my atom)"
self.assertEqualMettaRunnerResults(metta.run("!(repr (my atom))"),
[[ValueAtom("(my atom)")]])
# Check that (parse "(my atom)") == (my atom)
self.assertEqualMettaRunnerResults(metta.run("!(parse \"(my atom)\")"),
[[E(S("my"), S("atom"))]])
#unstable renaming of variables causes random failures of the test
#self.assertEqualMettaRunnerResults(metta.run('!(parse "$X")'),
# [[(V("X"))]])
self.assertEqualMettaRunnerResults(metta.run('!(parse "\\"A\\"")'),
[[(ValueAtom("A"))]])
#self.assertEqualMettaRunnerResults(metta.run('!(parse "(func (Cons $x (Cons $xs $xss))) ")'),
# [[E(S("func"), E(S("Cons"), V("x"), E(S("Cons"), V("xs"), V("xss"))))]])
self.assertEqualMettaRunnerResults(metta.run('!(parse "(A 2 \'S\')")'),
[[E(S("A"), ValueAtom(2), ValueAtom(Char("S")))]])
# Check that (stringToChars "ABC") == ('A' 'B' 'C')
self.assertEqualMettaRunnerResults(metta.run('!(stringToChars "ABC")'),
[[E(ValueAtom(Char("A")), ValueAtom(Char("B")), ValueAtom(Char("C")))]])
# Check that (charsToString ('A' 'B' 'C')) == "ABC"
self.assertEqualMettaRunnerResults(metta.run("!(charsToString ('A' 'B' 'C'))"),
[[ValueAtom("ABC")]])
def test_py_atoms(self):
metta = MeTTa(env_builder=Environment.test_env())
# Basic tests
# py-atom can give as both a module and a function in the module
# py-dot works for the module returned by py-atom
metta.run('''
! (bind! &pow (py-atom math.pow (-> Number Number Number)))
! (bind! &math (py-atom math))
''')
self.assertEqual(metta.run('! (&pow 2 3)'),
metta.run('! ((py-dot &math pow) 2 3)'))
self.assertEqual([[ValueAtom(3)]],
metta.run('! ((py-dot &math floor) (py-dot &math pi))'))
# Additional tests
# py-atom can actually execute some Python code
# py-dot works for dicts (and other classes) - not only modules
self.assertEqual([[ValueAtom('f')]],
metta.run('! ((py-dot (py-atom "{5: \'f\'}") get) 5)'))
# py-atom can return some built-in functions without modules
self.assertEqual([[ValueAtom('5')]],
metta.run('! ((py-atom str) 5)'))
# Check that py-dot accepts a nested path as a second argument
self.assertEqual([[ValueAtom("/usr")]],
metta.run('''
! ((py-dot (py-atom os) path.commonpath)
(py-atom "['/usr/lib', '/usr/local/lib']"))
'''))
def test_number_parsing(self):
metta = MeTTa(env_builder=Environment.test_env())
self.assertEqualMettaRunnerResults(metta.run("!(+ 1 2)"), [[ValueAtom(3)]])
self.assertEqualMettaRunnerResults(metta.run("!(+ 5.0 -2.0)"), [[ValueAtom(3.0)]])
self.assertEqualMettaRunnerResults(metta.run("!(+ 1.0e3 2.0e3)"), [[ValueAtom(3e3)]])
self.assertEqualMettaRunnerResults(metta.run("!(+ 5e-3 -2e-3)"), [[ValueAtom(3e-3)]])
def test_regex(self):
metta = MeTTa(env_builder=Environment.test_env())
metta.run('''(= (intent regex:"^Hello[[\.|!]]?$") (Intent Hello))
(= (intent regex:"Good~morning.*[[\\.|!]]?") (Intent Hello))
(= (intent $x) (empty))''')
self.assertEqual(metta.run('!(intent "hello")', True), [E(S("Intent"), S("Hello"))])
self.assertEqual(metta.run('!(intent "Good morning my friend!")', True),
[E(S("Intent"), S("Hello"))])
self.assertEqual(metta.run('!(intent "Hi")', True), [])
def test_py_list_tuple(self):
metta = MeTTa(env_builder=Environment.test_env())
self.assertEqual(metta.run('!(py-list ())'), [[ValueAtom( [] )]])
self.assertEqual(metta.run('!(py-tuple ())'), [[ValueAtom( () )]])
self.assertEqual(metta.run('!(py-dict ())'), [[ValueAtom( {} )]])
self.assertEqual(metta.run('!(py-tuple (1 (2 (3 "3")) (py-atom list)))'), [[ValueAtom((1,(2,(3, "3")), list))]])
self.assertEqual(metta.run('!(py-list (1 2 (4.5 3)))'), [[ValueAtom( [1,2,[4.5,3]] )]])
self.assertEqual(metta.run('!(py-list (1 2 (py-tuple (3 4))))'), [[ValueAtom( [1,2, (3,4)] )]])
self.assertEqual(metta.run('!(py-dict ((a "b") ("b" "c")))'), [[ValueAtom( {"a":"b", "b":"c"} )]])
self.assertEqual(str(metta.run('!(py-list (a b c))')[0][0].get_object().content[2]), "c")
# We need py-chain for langchain, but we test with bitwise operation | (1 | 2 | 3 | 4 = 7)
self.assertEqual(metta.run('!(py-chain (1 2 3 4))'), [[ValueAtom( 7 )]])
# test when we except errors (just in case we reset metta after each exception)
self.assertRaises(Exception, metta.run('!(py-dict (("a" "b" "c") ("b" "c")))'))
metta = MeTTa(env_builder=Environment.test_env())
self.assertRaises(Exception, metta.run('!(py-dict (("a") ("b" "c")))'))
metta = MeTTa(env_builder=Environment.test_env())
self.assertRaises(Exception, metta.run('!(py-dict ("a" "b") ("b" "c"))'))
metta = MeTTa(env_builder=Environment.test_env())
self.assertRaises(Exception, metta.run('!(py-list 1 2)'))
metta = MeTTa(env_builder=Environment.test_env())
self.assertRaises(Exception, metta.run('!(py-list 1)'))
metta = MeTTa(env_builder=Environment.test_env())
if __name__ == "__main__":
unittest.main()