From f408b4dc813779222d2627670b1607c13e577587 Mon Sep 17 00:00:00 2001 From: ThaHobbyist Date: Sun, 2 Jun 2024 07:03:08 +0530 Subject: [PATCH] Added testcases --- pyvnt/Reference/vector.py | 12 +++++---- pyvnt/__init__.py | 2 ++ tests/test_vector.py | 52 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 tests/test_vector.py diff --git a/pyvnt/Reference/vector.py b/pyvnt/Reference/vector.py index 5757ded..05eb36f 100644 --- a/pyvnt/Reference/vector.py +++ b/pyvnt/Reference/vector.py @@ -1,4 +1,6 @@ from pyvnt.Reference.basic import * +from typing import Self +import math class PropertyVector(ValueProperty): ''' @@ -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 @@ -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 @@ -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()})" \ No newline at end of file + return f"PropertyVector(name = {self._ValueProperty__name}, x = {self.__x.giveVal()}, y = {self.__y.giveVal()}, z = {self.__z.giveVal()})" \ No newline at end of file diff --git a/pyvnt/__init__.py b/pyvnt/__init__.py index e147810..af6fd89 100755 --- a/pyvnt/__init__.py +++ b/pyvnt/__init__.py @@ -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 * diff --git a/tests/test_vector.py b/tests/test_vector.py new file mode 100644 index 0000000..3064377 --- /dev/null +++ b/tests/test_vector.py @@ -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 \ No newline at end of file