#!/usr/bin/env python
#-*- coding: utf-8 -*-

import sys
if len(sys.argv) > 1:
	sys.path.insert(0, sys.argv.pop(1))

import unittest
import math

import viennagrid.wrapper

from utils import equal, point_equal

class TestPointCartesian1D(unittest.TestCase):
	def setUp(self):
		self.addTypeEqualityFunc(viennagrid.wrapper.PointCartesian1D, point_equal)
	
	def test_init(self):
		"""Test constructor, and properties 'dim' and 'coords'."""
		point = viennagrid.wrapper.PointCartesian1D()
		self.assertEqual(point.dim, 1)
		self.assertEqual(point.coords[0], 0)
	
	def test_init_double(self):
		"""Test constructor with doubles, and properties 'dim' and 'coords'."""
		point = viennagrid.wrapper.PointCartesian1D(2.0)
		self.assertEqual(point.dim, 1)
		self.assertEqual(point.coords[0], 2.0)
	
	def test_coord_system(self):
		"""Test property 'coord_system'."""
		point = viennagrid.wrapper.PointCartesian1D()
		self.assertEqual(point.coord_system, 'cartesian')
	
	def test_get_coord(self):
		"""Test method 'get_coord'."""
		point = viennagrid.wrapper.PointCartesian1D()
		self.assertEqual(point.get_coord(0), 0)
		
		point = viennagrid.wrapper.PointCartesian1D(2)
		self.assertEqual(point.get_coord(0), 2)
	
	def test_set_coord(self):
		"""Test method 'set_coord'."""
		point = viennagrid.wrapper.PointCartesian1D()
		self.assertEqual(point.get_coord(0), 0)
		point.set_coord(0, 5)
		self.assertEqual(point.get_coord(0), 5)
	
	def test_equal(self):
		"""Test operator '=='."""
		p1 = viennagrid.wrapper.PointCartesian1D()
		p2 = viennagrid.wrapper.PointCartesian1D()
		self.assertEqual(p1, p2)
		self.assertEqual(p1.coords[0], 0.0)
		self.assertEqual(p2.coords[0], 0.0)
		
		p1 = viennagrid.wrapper.PointCartesian1D(2)
		p2 = viennagrid.wrapper.PointCartesian1D(2)
		self.assertEqual(p1, p2)
		self.assertEqual(p1.coords[0], 2.0)
		self.assertEqual(p2.coords[0], 2.0)
	
	def test_assign(self):
		"""Test operator '='."""
		p1 = viennagrid.wrapper.PointCartesian1D(2)
		p2 = viennagrid.wrapper.PointCartesian1D(1)
		self.assertNotEqual(p1, p2)
		self.assertEqual(p1.coords[0], 2.0)
		self.assertEqual(p2.coords[0], 1.0)
		p1 = p2
		self.assertEqual(p1, p2)
		self.assertEqual(p1.coords[0], 1.0)
		self.assertEqual(p2.coords[0], 1.0)
	
	def test_add(self):
		"""Test operator '+' (addition)."""
		p1 = viennagrid.wrapper.PointCartesian1D(2)
		p2 = viennagrid.wrapper.PointCartesian1D(1)
		self.assertNotEqual(p1, p2)
		self.assertEqual(p1.coords[0], 2.0)
		self.assertEqual(p2.coords[0], 1.0)
		p3 = p2 + p1
		self.assertEqual(p3.coords[0], 3.0)
	
	def test_sub(self):
		"""Test operator '-' (subtraction)."""
		p1 = viennagrid.wrapper.PointCartesian1D(2)
		p2 = viennagrid.wrapper.PointCartesian1D(1)
		self.assertNotEqual(p1, p2)
		self.assertEqual(p1.coords[0], 2.0)
		self.assertEqual(p2.coords[0], 1.0)
		p3 = p2 - p1
		self.assertEqual(p3.coords[0], -1.0)
	
	def test_mul(self):
		"""Test operator '*' (multiplication)."""
		p1 = viennagrid.wrapper.PointCartesian1D()
		p2 = viennagrid.wrapper.PointCartesian1D()
		p1 = p1 * 2
		self.assertEqual(p1, p2)
		self.assertEqual(p1.coords[0], 0.0)
		
		p1 = viennagrid.wrapper.PointCartesian1D(1)
		p2 = viennagrid.wrapper.PointCartesian1D(2)
		p1 = p1 * 2
		self.assertEqual(p1, p2)
		self.assertEqual(p1.coords[0], 2.0)
	
	def test_div(self):
		"""Test operator '/' (division)."""
		p1 = viennagrid.wrapper.PointCartesian1D()
		p2 = viennagrid.wrapper.PointCartesian1D()
		p1 = p1 / 2
		self.assertEqual(p1, p2)
		self.assertEqual(p1.coords[0], 0.0)
		
		p1 = viennagrid.wrapper.PointCartesian1D(1)
		p2 = viennagrid.wrapper.PointCartesian1D(0.5)
		p1 = p1 / 2
		self.assertEqual(p1, p2)
		self.assertEqual(p1.coords[0], 0.5)
	
	def test_neg(self):
		"""Test unary operator '-' (__neg__ in Python)."""
		p1 = viennagrid.wrapper.PointCartesian1D(1)
		p2 = -p1
		self.assertEqual(-p1.coords[0], p2.coords[0])

class TestPointCartesian2D(unittest.TestCase):
	def setUp(self):
		self.addTypeEqualityFunc(viennagrid.wrapper.PointCartesian2D, point_equal)
	
	def test_init(self):
		"""Test constructor, and properties 'dim' and 'coords'."""
		point = viennagrid.wrapper.PointCartesian2D()
		self.assertEqual(point.dim, 2)
		self.assertEqual(point.coords[0], 0)
		self.assertEqual(point.coords[1], 0)
	
	def test_init_double(self):
		"""Test constructor with doubles, and properties 'dim' and 'coords'."""
		point = viennagrid.wrapper.PointCartesian2D(2.0, 5.0)
		self.assertEqual(point.dim, 2)
		self.assertEqual(point.coords[0], 2.0)
		self.assertEqual(point.coords[1], 5.0)
	
	def test_coord_system(self):
		"""Test property 'coord_system'."""
		point = viennagrid.wrapper.PointCartesian2D()
		self.assertEqual(point.coord_system, 'cartesian')
	
	def test_get_coord(self):
		"""Test method 'get_coord'."""
		point = viennagrid.wrapper.PointCartesian2D()
		self.assertEqual(point.get_coord(0), 0)
		self.assertEqual(point.get_coord(1), 0)
		
		point = viennagrid.wrapper.PointCartesian2D(2, 5)
		self.assertEqual(point.get_coord(0), 2)
		self.assertEqual(point.get_coord(1), 5)
	
	def test_set_coord(self):
		"""Test method 'set_coord'."""
		point = viennagrid.wrapper.PointCartesian2D()
		self.assertEqual(point.get_coord(0), 0)
		self.assertEqual(point.get_coord(1), 0)
		point.set_coord(0, 5)
		point.set_coord(1, 8)
		self.assertEqual(point.get_coord(0), 5)
		self.assertEqual(point.get_coord(1), 8)
	
	def test_equal(self):
		"""Test operator '=='."""
		p1 = viennagrid.wrapper.PointCartesian2D()
		p2 = viennagrid.wrapper.PointCartesian2D()
		self.assertEqual(p1, p2)
		self.assertEqual(p1.coords[0], 0.0)
		self.assertEqual(p1.coords[1], 0.0)
		self.assertEqual(p2.coords[0], 0.0)
		self.assertEqual(p2.coords[1], 0.0)
		
		p1 = viennagrid.wrapper.PointCartesian2D(2, 1)
		p2 = viennagrid.wrapper.PointCartesian2D(2, 1)
		self.assertEqual(p1, p2)
		self.assertEqual(p1.coords[0], 2.0)
		self.assertEqual(p1.coords[1], 1.0)
		self.assertEqual(p2.coords[0], 2.0)
		self.assertEqual(p2.coords[1], 1.0)
	
	def test_assign(self):
		"""Test operator '='."""
		p1 = viennagrid.wrapper.PointCartesian2D(2, 1)
		p2 = viennagrid.wrapper.PointCartesian2D(1, 2)
		self.assertNotEqual(p1, p2)
		self.assertEqual(p1.coords[0], 2.0)
		self.assertEqual(p1.coords[1], 1.0)
		self.assertEqual(p2.coords[0], 1.0)
		self.assertEqual(p2.coords[1], 2.0)
		p1 = p2
		self.assertEqual(p1, p2)
		self.assertEqual(p1.coords[0], 1.0)
		self.assertEqual(p1.coords[1], 2.0)
		self.assertEqual(p2.coords[0], 1.0)