Skip to content

Commit

Permalink
Add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsy committed Jan 6, 2017
1 parent 7597e15 commit 049ca86
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
9 changes: 9 additions & 0 deletions pygeop/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import os

current_dir = os.path.dirname(os.path.realpath(__file__))
modules = [ m for m in os.listdir(current_dir) if os.path.isfile(os.path.join(current_dir, m)) ]
modules = filter(lambda m : m != '__init__.py' and not m.startswith('.'), modules)
for m in modules:
base = m.split('.')[0]
cmd = 'from .%s import *' % base
exec(cmd)
16 changes: 12 additions & 4 deletions pygeop/geom3d/vector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import math
from ..exception import PygpException

class Vector(object):
def __init__(self, x, y, z):
Expand All @@ -15,12 +16,19 @@ def cross(self, v):
z = self.z * v.y - self.y * v.x
return Vector(x, y, z)

@staticmethod
def normalize(v):
return v / v.norm()
def normalize(self):
try:
self = self / self.norm()
except ZeroDivisionError:
raise PygpException('Vector length is zero!')

return self

def norm(self):
return math.sqrt(Vector.dot(self, self))
return math.sqrt(self.norm2())

def norm2(self):
return self.dot(self)

def __add__(self, v):
return Vector(self.x + v.x, self.y + v.y, self.z + v.z)
Expand Down
48 changes: 48 additions & 0 deletions tests/geom3d/test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import unittest

from random import *
from pygeop import PygpException
from pygeop.geom3d import Vector

class TestVector(unittest.TestCase):
Expand All @@ -22,6 +23,26 @@ def test_dot(self):
ans = v1.dot(v2)
self.assertEqual(ans, v1.x * v2.x + v1.y * v2.y + v1.z * v2.z)

def test_cross(self):
v1 = Vector(random(), random(), random())
v2 = Vector(random(), random(), random())
v3 = v1.cross(v2)
x = v1.y * v2.z - v1.z * v2.y
y = v1.x * v2.x - v1.x * v2.z
z = v1.z * v2.y - v1.y * v2.x
self.assertEqual(v3.x, x)
self.assertEqual(v3.y, y)
self.assertEqual(v3.z, z)

def test_normalize(self):
v0 = Vector(0.0, 0.0, 0.0)
with self.assertRaises(PygpException):
v0.normalize()

v0 = Vector(1.0, 2.0, 3.0)
v0 = v0.normalize()
self.assertAlmostEqual(v0.norm(), 1.0)

def test_add(self):
v1 = Vector(random(), random(), random())
v2 = Vector(random(), random(), random())
Expand All @@ -37,6 +58,28 @@ def test_neg(self):
self.assertEqual(u.y, -v.y)
self.assertEqual(u.z, -v.z)

def test_sub(self):
v1 = Vector(random(), random(), random())
v2 = Vector(random(), random(), random())
v3 = v1 - v2
self.assertEqual(v3.x, v1.x - v2.x)
self.assertEqual(v3.y, v1.y - v2.y)
self.assertEqual(v3.z, v1.z - v2.z)

def test_mul(self):
v = Vector(random(), random(), random())
s = random() + 1.0e-8

u = v * s
self.assertEqual(u.x, v.x * s)
self.assertEqual(u.y, v.y * s)
self.assertEqual(u.z, v.z * s)

u = s * v
self.assertEqual(u.x, v.x * s)
self.assertEqual(u.y, v.y * s)
self.assertEqual(u.z, v.z * s)

def test_truediv(self):
v = Vector(random(), random(), random())
s = random() + 1.0e-8
Expand All @@ -48,6 +91,11 @@ def test_truediv(self):
with self.assertRaises(ZeroDivisionError):
v / 0.0

def test_repr(self):
try:
print(self)
except ExceptionType:
self.fail("Vector.__repr__ raised Exception unexpectedly!")

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

0 comments on commit 049ca86

Please sign in to comment.