2014-11-08 16:40:02 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
2016-01-07 16:56:30 +00:00
|
|
|
from pymath.calculus.str2tokens import str2tokens, str2in_tokens, in2post_fix
|
|
|
|
from pymath.calculus.polynom import Polynom
|
2014-11-08 16:40:02 +00:00
|
|
|
|
2016-02-13 03:49:37 +00:00
|
|
|
|
2014-11-08 16:40:02 +00:00
|
|
|
class TestStr2tokens(unittest.TestCase):
|
2016-01-07 16:56:30 +00:00
|
|
|
"""Testing functions from pymath.calculus.str2tokens"""
|
2014-11-08 16:40:02 +00:00
|
|
|
|
|
|
|
def test_str2intokens(self):
|
|
|
|
ans = str2in_tokens("2+3*4")
|
|
|
|
self.assertEqual(ans, [2, "+", 3, "*", 4])
|
2016-02-13 03:29:26 +00:00
|
|
|
|
2014-11-08 16:40:02 +00:00
|
|
|
ans = str2in_tokens("2*3+4")
|
|
|
|
self.assertEqual(ans, [2, "*", 3, "+", 4])
|
2016-02-13 03:29:26 +00:00
|
|
|
|
2016-02-13 17:10:51 +00:00
|
|
|
ans = str2in_tokens("2.34")
|
|
|
|
self.assertEqual(ans, [2.34])
|
|
|
|
|
2014-11-08 16:40:02 +00:00
|
|
|
def test_in2post_fix(self):
|
|
|
|
in_tokens = str2in_tokens("2+3*4")
|
|
|
|
ans = in2post_fix(in_tokens)
|
|
|
|
self.assertEqual(ans, [2, 3, 4, "*", "+"])
|
2016-02-13 03:29:26 +00:00
|
|
|
|
2014-11-08 16:40:02 +00:00
|
|
|
in_tokens = str2in_tokens("2*3+4")
|
|
|
|
ans = in2post_fix(in_tokens)
|
2016-02-13 03:49:37 +00:00
|
|
|
self.assertEqual(ans, [2, 3, "*", 4, "+"])
|
2014-11-08 16:40:02 +00:00
|
|
|
|
2014-11-08 17:15:04 +00:00
|
|
|
def test_str2in_tokens_big_num(self):
|
|
|
|
exp = "123 + 3"
|
|
|
|
tok = str2in_tokens(exp)
|
|
|
|
self.assertEqual(tok, [123, "+", 3])
|
|
|
|
|
|
|
|
def test_str2in_tokens_beg_minus(self):
|
|
|
|
exp = "-123 + 3"
|
|
|
|
tok = str2in_tokens(exp)
|
|
|
|
self.assertEqual(tok, [-123, "+", 3])
|
|
|
|
|
|
|
|
def test_str2in_tokens_time_lack(self):
|
|
|
|
exp = "(-3)(2)"
|
|
|
|
tok = str2in_tokens(exp)
|
2016-02-13 03:49:37 +00:00
|
|
|
self.assertEqual(tok, ["(", -3, ")", "*", "(", 2, ")"])
|
2014-11-08 17:15:04 +00:00
|
|
|
|
2014-12-22 10:03:44 +00:00
|
|
|
def test_str2tokens_poly(self):
|
|
|
|
exp = "2x + 4"
|
|
|
|
post = str2in_tokens(exp)
|
2014-12-23 13:29:39 +00:00
|
|
|
self.assertEqual(post, [2, "*", Polynom([0, 1]), '+', 4])
|
2014-12-22 10:03:44 +00:00
|
|
|
|
|
|
|
def test_str2tokens_poly_double_x(self):
|
|
|
|
exp = "xx + 4"
|
|
|
|
post = str2in_tokens(exp)
|
2014-12-23 13:29:39 +00:00
|
|
|
self.assertEqual(post, [Polynom([0, 1]), "*", Polynom([0, 1]), '+', 4])
|
2014-12-22 10:03:44 +00:00
|
|
|
|
|
|
|
def test_str2tokens_poly(self):
|
|
|
|
exp = "x(2+1) + 4"
|
|
|
|
post = str2in_tokens(exp)
|
2016-02-13 03:49:37 +00:00
|
|
|
self.assertEqual(post, [Polynom([0, 1]), "*",
|
|
|
|
"(", 2, "+", 1, ')', '+', 4])
|
2014-12-22 10:03:44 +00:00
|
|
|
|
2014-11-08 17:15:04 +00:00
|
|
|
def test_str2in_tokens_time_lack2(self):
|
|
|
|
exp = "-3(2)"
|
|
|
|
tok = str2in_tokens(exp)
|
2016-02-13 03:49:37 +00:00
|
|
|
self.assertEqual(tok, [-3, "*", "(", 2, ")"])
|
2014-11-08 17:15:04 +00:00
|
|
|
|
|
|
|
def test_str2tokens_error(self):
|
|
|
|
exp = "1 + $"
|
|
|
|
self.assertRaises(ValueError, str2tokens, exp)
|
2014-11-08 16:40:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
2016-02-13 03:29:26 +00:00
|
|
|
# cursor: 16 del
|