tensor-sensor

Форк
0
/
test_tree_eval.py 
113 строк · 2.5 Кб
1
"""
2
MIT License
3

4
Copyright (c) 2021 Terence Parr
5

6
Permission is hereby granted, free of charge, to any person obtaining a copy
7
of this software and associated documentation files (the "Software"), to deal
8
in the Software without restriction, including without limitation the rights
9
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
copies of the Software, and to permit persons to whom the Software is
11
furnished to do so, subject to the following conditions:
12

13
The above copyright notice and this permission notice shall be included in all
14
copies or substantial portions of the Software.
15

16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
SOFTWARE.
23
"""
24
from tsensor.parsing import *
25
import tsensor.ast
26
import sys
27
import numpy as np
28

29

30
def check(s,expected):
31
    frame = sys._getframe()
32
    caller = frame.f_back
33
    p = PyExprParser(s)
34
    t = p.parse()
35
    result = t.eval(caller)
36
    assert str(result)==str(expected)
37

38

39
def test_int():
40
    check("34", 34)
41

42
def test_assign():
43
    check("a = 34", 34)
44

45
def test_var():
46
    a = 34
47
    check("a", 34)
48

49
def test_member_var():
50
    class A:
51
        def __init__(self):
52
            self.a = 34
53
    x = A()
54
    check("x.a", 34)
55

56
def test_member_func():
57
    class A:
58
        def f(self, a):
59
            return a+4
60
    x = A()
61
    check("x.f(30)", 34)
62

63
def test_index():
64
    a = [1,2,3]
65
    check("a[2]", 3)
66

67
def test_add():
68
    a = 3
69
    b = 4
70
    c = 5
71
    check("a+b+c", 12)
72

73
def test_add_mul():
74
    a = 3
75
    b = 4
76
    c = 5
77
    check("a+b*c", 23)
78

79
def test_pow():
80
    a = 3
81
    b = 4
82
    check("a**b", 3**4)
83

84
def test_pow2():
85
    a = 3
86
    b = 4
87
    c = 5
88
    check("a**(b+1)**c", 3**5**5)
89

90
def test_parens():
91
    a = 3
92
    b = 4
93
    c = 5
94
    check("(a+b)*c", 35)
95

96
def test_list_literal():
97
    a = [[1,2,3],[4,5,6]]
98
    check("a", """[[1, 2, 3], [4, 5, 6]]""")
99

100

101
def test_np_literal():
102
    a = np.array([[1,2,3],[4,5,6]])
103
    check("a*2", """[[ 2  4  6]\n [ 8 10 12]]""")
104

105

106
def test_np_add():
107
    a = np.array([[1,2,3],[4,5,6]])
108
    check("a+a", """[[ 2  4  6]\n [ 8 10 12]]""")
109

110

111
def test_np_add2():
112
    a = np.array([[1,2,3],[4,5,6]])
113
    check("a+a+a", """[[ 3  6  9]\n [12 15 18]]""")
114

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.