-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_std.py
More file actions
154 lines (130 loc) · 4.22 KB
/
test_std.py
File metadata and controls
154 lines (130 loc) · 4.22 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
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
# Test suite for "std" callbacks in MaExPa.
#
# Copyright 2020 Alexandre Emsenhuber
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import math
import unittest
import maexpa
class StdTestCase( unittest.TestCase ):
def setUp( self ):
maexpa.lib( "std" )
def test_var( self ):
tests = [
( "e", math.e ),
( "pi", math.pi ),
( "tau", math.tau ),
]
for expr, comp in tests:
with self.subTest( "Constants", expr = expr ):
res = maexpa.Expression( expr )()
self.assertIs( type( res ), float )
self.assertAlmostEqual( res, comp )
def test_no_var( self ):
for text in [ "xi", "lambda", "a" ]:
with self.subTest( "Undefined constants", text = text ):
with self.assertRaises( maexpa.exception.NoVarException ):
maexpa.Expression( text )()
def test_func_builtin_int( self ):
tests = [
( "min(1,2)", 1 ),
( "max(1,2)", 2 ),
( "min(-1,1)", -1 ),
( "max(-1,1)", 1 ),
( "pow(1,2)", 1 ),
( "pow(2,3)", 8 ),
]
for expr, comp in tests:
with self.subTest( "Builting functions on integers", expr = expr ):
res = maexpa.Expression( expr )()
self.assertIs( type( res ), int )
self.assertEqual( res, comp )
def test_func_builtin_float( self ):
tests = [
( "min(0.9,1.1)", 0.9 ),
( "max(0.9,1.1)", 1.1 ),
( "pow(2.0,4)", 16. ),
]
for expr, comp in tests:
with self.subTest( "Builting functions on floats", expr = expr ):
res = maexpa.Expression( expr )()
self.assertIs( type( res ), float )
self.assertAlmostEqual( res, comp )
def test_func_type_float( self ):
tests = [
( "floor(1.7648)", 1 ),
( "ceil(1.7648)", 2 ),
( "floor(-1.7648)", -2 ),
( "ceil(-1.7648)", -1 ),
]
for expr, comp in tests:
with self.subTest( "Conversion from float to integer", expr = expr ):
res = maexpa.Expression( expr )()
self.assertIs( type( res ), int )
self.assertEqual( res, comp )
def test_func_abs_float( self ):
tests = [
( "abs(1.7648)", 1.7648 ),
( "abs(-1.7648)", 1.7648 ),
]
for expr, comp in tests:
with self.subTest( "Absoltue value on float", expr = expr ):
res = maexpa.Expression( expr )()
self.assertIs( type( res ), float )
self.assertAlmostEqual( res, comp )
def test_func_explog_float( self ):
tests = [
( "exp(1)", math.e ),
( "log(e)", 1. ),
( "log2(2)", 1. ),
( "log10(10)", 1. ),
]
for expr, comp in tests:
with self.subTest( "Exponential and logarithms on float", expr = expr ):
res = maexpa.Expression( expr )()
self.assertIs( type( res ), float )
self.assertAlmostEqual( res, comp )
def test_func_sqrt( self ):
tests = [
( "sqrt(2)", math.sqrt( 2. ) ),
( "sqrt(2.)", math.sqrt( 2. ) ),
( "sqrt(45**2)", 45. ),
]
for expr, comp in tests:
with self.subTest( "Square root", expr = expr ):
res = maexpa.Expression( expr )()
self.assertIs( type( res ), float )
self.assertAlmostEqual( res, comp )
def test_func_cbrt( self ):
tests = [
( "cbrt(8.)", 2. ),
( "cbrt(-7)", -7**( 1. / 3. ) ),
( "cbrt(45**3)", 45. ),
]
for expr, comp in tests:
with self.subTest( "Cube root", expr = expr ):
res = maexpa.Expression( expr )()
self.assertIs( type( res ), float )
self.assertAlmostEqual( res, comp )
def test_func_args_num( self ):
for text in [ "min(1)", "ceil(1,2)", "sqrt(9,16)" ]:
with self.subTest( "Passing incorrect number of arguments", text = text ):
with self.assertRaises( maexpa.exception.FuncArgsNumException ):
maexpa.Expression( text )()
def test_func_no_var( self ):
for text in [ "e(1)", "pi(1)", "tau(1)" ]:
with self.subTest( "Using variables as functions", text = text ):
with self.assertRaises( maexpa.exception.NoFuncException ):
maexpa.Expression( text )()
if __name__ == '__main__':
unittest.main()