Mapytex/pymath/calculus/test/test_random_expression.py

110 lines
3.0 KiB
Python
Raw Normal View History

2014-02-28 13:11:33 +00:00
#!/usr/bin/env python
# encoding: utf-8
import unittest
2016-01-07 16:56:30 +00:00
from pymath.calculus.random_expression import RdExpression
2014-02-28 13:11:33 +00:00
class TestRandomExpression(unittest.TestCase):
2016-01-07 16:56:30 +00:00
"""Testing functions from pymath.calculus.random_expression"""
2014-02-28 13:11:33 +00:00
def test_only_form(self):
form = "{a} + 2"
rdExp = RdExpression(form)
self.assertEqual(rdExp._letters, {'a'})
self.assertEqual(rdExp._2replaced, {'a'})
rdExp()
self.assertEqual(set(rdExp._gene_varia.keys()), {'a'})
self.assertEqual(set(rdExp._gene_2replaced.keys()), {'a'})
def test_only_form_calc(self):
form = "{a+b} + 2"
2014-02-28 13:11:33 +00:00
rdExp = RdExpression(form)
self.assertEqual(rdExp._letters, {'a', 'b'})
self.assertEqual(rdExp._2replaced, {'a+b'})
2014-02-28 13:11:33 +00:00
rdExp()
self.assertEqual(set(rdExp._gene_varia.keys()), {'a', 'b'})
self.assertEqual(set(rdExp._gene_2replaced.keys()), {'a+b'})
2014-02-28 13:11:33 +00:00
def test_only_form_cond(self):
form = "{a} + 2"
cond = ["{a} == 3"]
rdExp = RdExpression(form, cond)
self.assertEqual(rdExp._letters, {'a'})
self.assertEqual(rdExp._2replaced, {'a'})
rdExp()
self.assertEqual(set(rdExp._gene_varia.keys()), {'a'})
self.assertEqual(set(rdExp._gene_2replaced.keys()), {'a'})
self.assertEqual(rdExp._gene_varia['a'], 3)
def test_only_form_conds(self):
form = "{a} + 2"
cond = ["{a} in list(range(5))", "{a} % 2 == 1"]
rdExp = RdExpression(form, cond)
self.assertEqual(rdExp._letters, {'a'})
self.assertEqual(rdExp._2replaced, {'a'})
rdExp()
self.assertEqual(set(rdExp._gene_varia.keys()), {'a'})
self.assertEqual(set(rdExp._gene_2replaced.keys()), {'a'})
self.assertTrue(rdExp._gene_varia['a'] in list(range(5)))
self.assertTrue(rdExp._gene_varia['a'] % 2 == 1)
def test_only_form_calc_cond(self):
form = "{a*3} * {b}"
cond = ["{a} == 3"]
rdExp = RdExpression(form, cond)
self.assertEqual(rdExp._letters, {'a', 'b'})
self.assertEqual(rdExp._2replaced, {'a', 'b', 'a*3'})
rdExp()
self.assertEqual(set(rdExp._gene_varia.keys()), {'a', 'b'})
self.assertEqual(set(rdExp._gene_2replaced.keys()), {'a', 'b', 'a*3'})
self.assertEqual(rdExp._gene_varia['a'], 3)
def test_only_form_calc_cond_calc(self):
2014-11-23 07:29:02 +00:00
form = "{a*3} * {b}"
cond = ["{a+b} == 3"]
2014-11-23 07:29:02 +00:00
rdExp = RdExpression(form, cond)
self.assertEqual(rdExp._letters, {'a', 'b'})
self.assertEqual(rdExp._2replaced, {'b', 'a*3', 'a+b'})
2014-11-23 07:29:02 +00:00
rdExp()
self.assertEqual(set(rdExp._gene_varia.keys()), {'a', 'b'})
self.assertEqual(set(rdExp._gene_2replaced.keys()), {'b', 'a*3', 'a+b'})
2014-02-28 13:11:33 +00:00
2014-11-23 07:29:02 +00:00
self.assertEqual((rdExp._gene_varia['a'] + rdExp._gene_varia['b']), 3)
2014-02-28 13:11:33 +00:00
if __name__ == '__main__':
unittest.main()
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del