import numpy as np
from test.assertions import QuaternionTest
import math
import unittest
from scipy import linalg

from .context import pq, pqr

class TestRandom(QuaternionTest):
    def test_uniform_random_axis(self):
        """A unit axis is generated by uniform_random_axis()"""
        v = pqr.uniform_random_axis()
        uv = v / linalg.norm(v)
        np.testing.assert_array_almost_equal(v, uv, decimal=12)

    def test_randu(self):
        """urand() produces random numbers which are inside the correct range (-1,+1). """
        for ii in range(0,100):
            v = pqr.randu()
            self.assertLessEqual(-1.0, v)
            self.assertGreaterEqual(1.0, v)

    def test_rand(self):
        """rand() produces a quaternion on the unit sphere"""
        qr = pqr.rand()
        self.assertEqual(type(qr), pq.Quat)

        v = qr.to_vector()
        self.assertAlmostEqual(linalg.norm(v), 1.0, places=15)

        for ii in range(0,4):
            self.assertLessEqual(-1.0, v[ii])
            self.assertGreaterEqual(1.0, v[ii])
        

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