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

from customtest import *



class testVariables(CustomTest):

    def test_operations(self):
        for i in xrange(10):
            a = randint(50) - randint(50)
            b = randint(50) - randint(50)
            if i >= 5:
                a += random()
                b += random()
            u = Variable(a)
            v = Variable(b)
            # Test opérations :
            if i < 5:
                self.assertEqual(u+v, a+b)
                self.assertEqual(u-v, a-b)
                self.assertEqual(u*v, a*b)
                if a !=0:
                    self.assertEqual(u**v, a**b)
                if  b != 0:
                    self.assertEqual(u/v, a/b)
                    self.assertEqual(u//v, a//b)
                    self.assertEqual(u%v, a%b)
            else:
                self.assertAlmostEqual(u+v, a+b)
                self.assertAlmostEqual(u-v, a-b)
                self.assertAlmostEqual(u*v, a*b)
                if a != 0:
                    self.assertAlmostEqual(abs(u)**v, abs(a)**b)
           # Test assignations :
            u += v
            a += b
            self.assertEqual(u, a)
            u *= v
            a *= b
            self.assertEqual(u, a)

    def test_erreurs_mathematiques(self):
        u = Variable(randint(50) - randint(50)+random())
        def diviser(x,  y):
            return x/y
        self.assertRaises(ZeroDivisionError,  diviser,  u,  0)
        def puissance(x,  y):
            return x**y
        self.assertRaises(OverflowError,  puissance,  Variable(25.17),  10000)

    def test_simple_compose(self):
        self.assert_(Variable(5)._type == "simple")
        self.assert_(Variable("-5.3")._type == "simple")
        self.assert_(Variable("a")._type == "compose")


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