Skip to content

Commit

Permalink
Added testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaHobbyist committed Jun 2, 2024
1 parent f96babc commit f408b4d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
12 changes: 7 additions & 5 deletions pyvnt/Reference/vector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from pyvnt.Reference.basic import *
from typing import Self
import math

class PropertyVector(ValueProperty):
'''
Expand All @@ -11,7 +13,7 @@ class PropertyVector(ValueProperty):
z: PropertyFloat object to store z value of the vector
'''

__slots__ = ('_PropertyVector__name', '_PropertyVector__x', '_PropertyVector__y', '_PropertyVector__z')
__slots__ = ('_ValueProperty__name', '_PropertyVector__x', '_PropertyVector__y', '_PropertyVector__z')

def instance_restricted(self):
pass
Expand Down Expand Up @@ -68,7 +70,7 @@ def magnitude(self) -> float:
'''
return math.sqrt(self.__x.giveVal()**2 + self.__y.giveVal()**2 + self.__z.giveVal()**2)

def normalise(self, tol: PropertyFloat) -> PropertyVector:
def normalise(self, tol: PropertyFloat) -> Self:
'''
Normalises the vector
Expand All @@ -77,10 +79,10 @@ def normalise(self, tol: PropertyFloat) -> PropertyVector:
'''
s = self.magnitude()
if s < tol.giveVal():
self.setProperties(self.__name, PropertyFloat(self.__name + "_x", 0), PropertyFloat(self.__name + "_y", 0), PropertyFloat(self.__name + "_z", 0))
self.setProperties(self._ValueProperty__name, PropertyFloat(self._ValueProperty__name + "_x", 0), PropertyFloat(self._ValueProperty__name + "_y", 0), PropertyFloat(self._ValueProperty__name + "_z", 0))
else:
self.setProperties(self.__name, PropertyFloat(self.__name + "_x", self.__x.giveVal()/s), PropertyFloat(self.__name + "_y", self.__y.giveVal()/s), PropertyFloat(self.__name + "_z", self.__z.giveVal()/s))
self.setProperties(self._ValueProperty__name, PropertyFloat(self._ValueProperty__name + "_x", self.__x.giveVal()/s), PropertyFloat(self._ValueProperty__name + "_y", self.__y.giveVal()/s), PropertyFloat(self._ValueProperty__name + "_z", self.__z.giveVal()/s))
return self

def __repr__(self):
return f"PropertyVector(name = {self.__name}, x = {self.__x.giveVal()}, y = {self.__y.giveVal()}, z = {self.__z.giveVal()})"
return f"PropertyVector(name = {self._ValueProperty__name}, x = {self.__x.giveVal()}, y = {self.__y.giveVal()}, z = {self.__z.giveVal()})"
2 changes: 2 additions & 0 deletions pyvnt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from pyvnt.Reference.basic import *
from pyvnt.Reference.vector import *
from pyvnt.Reference.tensor import *
from pyvnt.DictionaryElement.foamDS import *
from pyvnt.DictionaryElement.keyData import *
from pyvnt.Converter.Writer.writer import *
Expand Down
52 changes: 52 additions & 0 deletions tests/test_vector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pytest

from pyvnt import *

class TestVector:
def setup_method(self, method):
self.hprop1 = PropertyFloat('val1', default=1)
self.hprop2 = PropertyFloat('val2', default=2)
self.hprop3 = PropertyFloat('val3', default=3)
self.hprop4 = PropertyFloat('val4', default=4)
self.hprop5 = PropertyFloat('val5', default=5)
self.hprop6 = PropertyFloat('val6', default=6)


self.vprop1 = PropertyVector('val1', self.hprop1, self.hprop2, self.hprop3)
self.vprop2 = PropertyVector('val2', self.hprop4, self.hprop5, self.hprop6)

def teardown_method(self, method):
del self.vprop1
del self.vprop2
del self.hprop1
del self.hprop2
del self.hprop3
del self.hprop4
del self.hprop5
del self.hprop6

def test_vector_print(self):
assert str(self.vprop1) == f"PropertyVector(name = val1, x = {self.hprop1.giveVal()}, y = {self.hprop2.giveVal()}, z = {self.hprop3.giveVal()})"
assert str(self.vprop2) == f"PropertyVector(name = val2, x = {self.hprop4.giveVal()}, y = {self.hprop5.giveVal()}, z = {self.hprop6.giveVal()})"

def test_vector_x(self):
assert self.vprop1.x() == 1
assert self.vprop2.x() == 4

def test_vector_y(self):
assert self.vprop1.y() == 2
assert self.vprop2.y() == 5

def test_vector_z(self):
assert self.vprop1.z() == 3
assert self.vprop2.z() == 6

def test_vector_magnitude(self):
assert self.vprop1.magnitude() == 14**0.5
assert self.vprop2.magnitude() == 77**0.5

def test_vector_normalise(self):
self.vprop1.normalise(PropertyFloat('tol', 0.1))
assert self.vprop1.x() == 1/14**0.5
assert self.vprop1.y() == 2/14**0.5
assert self.vprop1.z() == 3/14**0.5

0 comments on commit f408b4d

Please sign in to comment.