From f408b4dc813779222d2627670b1607c13e577587 Mon Sep 17 00:00:00 2001 From: ThaHobbyist Date: Sun, 2 Jun 2024 07:03:08 +0530 Subject: [PATCH 1/2] 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 From 716db54bc4787997f769db069de17a22fe6c9ac5 Mon Sep 17 00:00:00 2001 From: ThaHobbyist Date: Fri, 7 Jun 2024 18:11:52 +0530 Subject: [PATCH 2/2] Added writeOut and giveVal methods in vector data type --- manual_tests/test_pyvnt.py | 4 ++-- pyvnt/Reference/vector.py | 15 +++++++++++++++ testFile.txt | 17 ----------------- tests/test_vector.py | 6 +++++- 4 files changed, 22 insertions(+), 20 deletions(-) delete mode 100644 testFile.txt diff --git a/manual_tests/test_pyvnt.py b/manual_tests/test_pyvnt.py index f54e1d7..30945d9 100755 --- a/manual_tests/test_pyvnt.py +++ b/manual_tests/test_pyvnt.py @@ -11,7 +11,7 @@ # test for KeyData and Foam classes key1 = KeyData('solver', prop1, prop2) -print(key1.giveVal()) +# print(key1.giveVal()) head = Foam("test_head",None, None) # head.appendDict(args) @@ -33,7 +33,7 @@ # key1 = KeyData('solver', prop1) # print(key1) -writeTo(head, 'testFile.txt') +# writeTo(head, 'testFile.txt') diff --git a/pyvnt/Reference/vector.py b/pyvnt/Reference/vector.py index 05eb36f..8544c32 100644 --- a/pyvnt/Reference/vector.py +++ b/pyvnt/Reference/vector.py @@ -84,5 +84,20 @@ def normalise(self, tol: PropertyFloat) -> Self: 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 giveVal(self): + ''' + Returns the vector value + ''' + + res = (self.__x.giveVal(), self.__y.giveVal(), self.__z.giveVal()) + + return res + + def writeOut(self, file): + ''' + Returns the vector value in a string format + ''' + file.write( f"({self.__x.giveVal()} {self.__y.giveVal()} {self.__z.giveVal()})") + def __repr__(self): 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/testFile.txt b/testFile.txt deleted file mode 100644 index 5b57149..0000000 --- a/testFile.txt +++ /dev/null @@ -1,17 +0,0 @@ -test_head -{ - test_child - { - test_child2 - { - solver PCG PBiCG; - } - - test_child3 - { - solver PCG PBiCG; - } - - } - -} diff --git a/tests/test_vector.py b/tests/test_vector.py index 3064377..fd93507 100644 --- a/tests/test_vector.py +++ b/tests/test_vector.py @@ -49,4 +49,8 @@ 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 + assert self.vprop1.z() == 3/14**0.5 + + def test_vector_giveVal(self): + assert self.vprop1.giveVal() == (1, 2, 3) + assert self.vprop2.giveVal() == (4, 5, 6)