# -*- coding: iso-8859-1 -*-
from __future__ import division # 1/2 == .5 (par defaut, 1/2 == 0)

import re
import sympy

from customtest import *
from mathlib import traduire_formule
import custom_functions
#from mathlib.custom_functions import resoudre

VERBOSE = False

class TestMathlibCustomFunctions(CustomTest):

    def assertEqual(self, x, y):
        if x != y:
            print "ERREUR:", repr(x), "!=", repr(y)
        self.assert_(x == y)

    def assert_resoudre(self, x, y):
        self.assertEqual(str(custom_functions.resoudre(x)), y)

    def assert_positif(self, x, y):
        self.assertEqual(unicode(custom_functions.positif(x)), y)

    def assert_ens_def(self, x, y):
        self.assertEqual(unicode(custom_functions.ensemble_definition(x)), y)

    def test_resoudre(self):
        self.assert_resoudre("2*x=0", "{0}")
        self.assert_resoudre("2*x>0", u"]0;+oo[")
        self.assert_resoudre("(x>4 et x<7) ou (x/2-1>=3)", u"]4;7[U[8;+oo[")
        self.assert_resoudre("(2)*x>0", u"]0;+oo[")
        self.assert_resoudre("2*x+3>5*x-4 et 3*x+1>=4*x-4", u']-oo;7/3[')
        self.assert_resoudre("2*x+3*y=4 et 4*x-2*y=1", u"{x: 11/16, y: 7/8}")
        self.assert_resoudre("ln(x)<ln(-2*x+1)", "]0;1/3[")
        self.assert_resoudre("exp(x^2)-x^2-1>=0", "]-oo;+oo[")


    def test_positif(self):
        x = sympy.Symbol("x")
        self.assert_positif(x**7, "[0;+oo[")
        self.assert_positif((x+1)**5, "[-1;+oo[")
        self.assert_positif(sympy.sqrt(x+1), "[-1;+oo[")
        self.assert_positif((x+1)**6, "]-oo;+oo[")
#        self.assert_positif(x + exp(x), "[0;+oo[")
        self.assert_positif(-x**2-x, "[-1;0]")
        self.assert_positif(sympy.sqrt(5), "]-oo;+oo[")
        self.assert_positif((x-1)/(x+1), ']-oo;-1[U[1;+oo[')
        self.assert_positif(x*sympy.exp(3)-1, '[exp(-3);+oo[')


    def test_ensemble_definition(self):
        x = sympy.Symbol("x")
        self.assert_ens_def((2 - x)/(6 - 5*x + x**2), ']-oo;2[U]2;3[U]3;+oo[')


if __name__ == '__main__':
    unittest.main()
