Mapytex/pymath/calculus/test/test_random_expression.py
2016-02-14 21:00:16 +03:00

109 lines
2.6 KiB
Python

#!/usr/bin/env python
# encoding: utf-8
from pymath.calculus.random_expression import RdExpression
def test_only_form():
form = "{a} + 2"
rdExp = RdExpression(form)
assert rdExp._letters == {'a'}
assert rdExp._2replaced == {'a'}
rdExp()
assert set(rdExp._gene_varia.keys()) == {'a'}
assert set(rdExp._gene_2replaced.keys()) == {'a'}
def test_form_with_underscores():
form = "_ + 2*_"
rdExp = RdExpression(form)
assert rdExp._letters == {'A', 'B'}
assert rdExp._2replaced == {'A', 'B'}
rdExp()
assert set(rdExp._gene_varia.keys()) == {'A', 'B'}
assert set(rdExp._gene_2replaced.keys()) == {'A', 'B'}
def test_only_form_calc():
form = "{a+b} + 2"
rdExp = RdExpression(form)
assert rdExp._letters, {'a' == 'b'}
assert rdExp._2replaced == {'a+b'}
rdExp()
assert set(rdExp._gene_varia.keys()), {'a' == 'b'}
assert set(rdExp._gene_2replaced.keys()) == {'a+b'}
def test_only_form_cond():
form = "{a} + 2"
cond = ["{a} == 3"]
rdExp = RdExpression(form, cond)
assert rdExp._letters == {'a'}
assert rdExp._2replaced == {'a'}
rdExp()
assert set(rdExp._gene_varia.keys()) == {'a'}
assert set(rdExp._gene_2replaced.keys()) == {'a'}
assert rdExp._gene_varia['a'] == 3
def test_only_form_conds():
form = "{a} + 2"
cond = ["{a} in list(range(5))", "{a} % 2 == 1"]
rdExp = RdExpression(form, cond)
assert rdExp._letters == {'a'}
assert rdExp._2replaced == {'a'}
rdExp()
assert set(rdExp._gene_varia.keys()) == {'a'}
assert set(rdExp._gene_2replaced.keys()) == {'a'}
assert rdExp._gene_varia['a'] in list(range(5))
assert rdExp._gene_varia['a'] % 2 == 1
def test_only_form_calc_cond():
form = "{a*3} * {b}"
cond = ["{a} == 3"]
rdExp = RdExpression(form, cond)
assert rdExp._letters, {'a' == 'b'}
assert rdExp._2replaced, {'a', 'b' == 'a*3'}
rdExp()
assert set(rdExp._gene_varia.keys()), {'a' == 'b'}
assert set(rdExp._gene_2replaced.keys()), {'a', 'b' == 'a*3'}
assert rdExp._gene_varia['a'] == 3
def test_only_form_calc_cond_calc():
form = "{a*3} * {b}"
cond = ["{a+b} == 3"]
rdExp = RdExpression(form, cond)
assert rdExp._letters, {'a' == 'b'}
assert rdExp._2replaced, {'b', 'a*3' == 'a+b'}
rdExp()
assert set(rdExp._gene_varia.keys()), {'a' == 'b'}
assert set(rdExp._gene_2replaced.keys()), {'b', 'a*3' == 'a+b'}
assert (rdExp._gene_varia['a'] + rdExp._gene_varia['b']) == 3
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del