import unittest

from fancymath.vector import Vector


class TestVector(unittest.TestCase):
    def test_cross_product(self):
        # Dictionary of (v1, v2): v3
        values = {
            (Vector(1, 2, 3), Vector(3, 2, 1)): Vector(-4, 8, -4),
            (Vector(0, 0, 0), Vector(1, 2, 3)): Vector(0, 0, 0),
            (Vector(1, 0, 0), Vector(1, 0, 0)): Vector(0, 0, 0),
        }

        for inputs, expected in values.items():
            v1, v2 = inputs
            with self.subTest(v1=v1, v2=v2):
                v3 = v1 * v2

                self.assertIsInstance(v3, Vector)
                self.assertEqual(v3.x, expected.x)
                self.assertEqual(v3.y, expected.y)
                self.assertEqual(v3.z, expected.z)

    def test_cross_product_failure(self):
        v1 = Vector(1, 2, 3)
        v2 = (3, 2, 1)

        with self.assertRaises(TypeError):
            v1 * v2


    def test_add_vector(self):
        v1 = Vector(1, 2, 3)
        v2 = Vector(-3, -2, -1)
        v3 = v1 + v2
        self.assertIsInstance(v3, Vector)
        self.assertEqual(v3.x, v1.x + v2.x)

    def test_abs(self):
        v1 = Vector(0, 0, 0)
        v2 = Vector(1, 2, 2)
        v3 = Vector(1, -2, 2)
        self.assertEqual(v1.abs(), 0)
        self.assertEqual(v2.abs(), 3)
        self.assertEqual(v2.abs(), v3.abs())

    def test_dot_failure(self):
        with self.assertRaises(ValueError):
            v1 = Vector(1, 2, 3)
            v1.dot(3)

    def test_dot_product(self):
        v1 = Vector(1, 2, 3)
        v2 = Vector(-3, -2, -1)
        v3 = v1.dot(v2)
        self.assertEqual(v3,v1.x * v2.x + v1.y * v2.y + v1.z * v2.z)
        vzero = Vector(0, 0, 0)
        self.assertEqual(v1.dot(vzero),0)
