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

import mathlib

from customtest import *
import mathlib.intervalles as intervalles
from mathlib.intervalles import Intervalle
import sympy


class TestMathlibIntervalles(CustomTest):

    def assert_intervalle_preformater(self, x, y):
        y_ = intervalles.preformatage_ensemble(x)
        if y_ != y:
            print "/!\\ Formule: ", x
            print "ERREUR: ",  y_, " != ", y
        self.assert_(y_ == y)

    def test_preformater(self):
        self.assert_intervalle_preformater("R", "]-oo;+oo[")
        self.assert_intervalle_preformater("R+", "[0;+oo[")
        self.assert_intervalle_preformater("R*", "]-oo;+oo[-{0}")
        self.assert_intervalle_preformater("R-U{3}", "]-oo;0]+{3}")
        self.assert_intervalle_preformater("R-{3}", "]-oo;+oo[-{3}")
        self.assert_intervalle_preformater("R-{3}", "]-oo;+oo[-{3}")
        self.assert_intervalle_preformater("[0,1]U]1,2]", "[0;1]+]1;2]")

    def test_intervalle(self):
        self.assert_(str(Intervalle(8) + Intervalle(9)) == '[8;+oo[')

    def test_evalf(self):
        i = intervalles.conversion_chaine_ensemble(']-oo;-2]U[1/4;1/2[')
        self.assert_(str(i.evalf()) == ']-oo;-2.0]U[0.25;0.5[')
        i = Intervalle(-sympy.sqrt(2), sympy.pi)
        self.assert_(str(i.evalf()) == '[-1.41421356237310;3.14159265358979]')
        self.assert_(str(i.evalf(n = 3)) == '[-1.41;3.14]')

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