Skip to content

Commit

Permalink
Fix bugs in geometry module + write tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ruancomelli committed Jun 11, 2021
1 parent ed436b3 commit 58627ad
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
41 changes: 21 additions & 20 deletions boiling_learning/utils/geometry.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
import numpy as np
import pint
import math

from dataclassy import dataclass
from pint import Quantity


@dataclass(kwargs=True)
class Solid:
surface_area: pint.Quantity = None
volume: pint.Quantity = None
surface_area: Quantity = None
volume: Quantity = None


class Prism(Solid):
length: pint.Quantity
length: Quantity

cross_section_perimeter: pint.Quantity
cross_section_area: pint.Quantity
cross_section_perimeter: Quantity
cross_section_area: Quantity

lateral_area: pint.Quantity = None
lateral_area: Quantity = None

def __init__(self) -> None:
def __post_init__(self) -> None:
self.lateral_area = self.cross_section_perimeter * self.length
self.surface_area = self.lateral_area + 2 * self.cross_section_area
self.volume = self.cross_section_area * self.length


class Cylinder(Prism):
diameter: pint.Quantity
diameter: Quantity

cross_section_perimeter: pint.Quantity = None
cross_section_area: pint.Quantity = None
cross_section_perimeter: Quantity = None
cross_section_area: Quantity = None

def __init__(self) -> None:
def __post_init__(self) -> None:
self.radius = self.diameter / 2
self.cross_section_perimeter = np.pi * self.diameter
self.cross_section_area = np.pi * self.radius ** 2
self.cross_section_perimeter = math.pi * self.diameter
self.cross_section_area = math.pi * self.radius ** 2


class RectangularPrism(Prism):
width: pint.Quantity
thickness: pint.Quantity
width: Quantity
thickness: Quantity

cross_section_perimeter: pint.Quantity = None
cross_section_area: pint.Quantity = None
cross_section_perimeter: Quantity = None
cross_section_area: Quantity = None

def __init__(self) -> None:
def __post_init__(self) -> None:
self.cross_section_perimeter = 2 * (self.width + self.thickness)
self.cross_section_area = self.width * self.thickness
28 changes: 28 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from unittest.case import TestCase

from boiling_learning.utils.collections import KeyedSet
from boiling_learning.utils.geometry import Cylinder, Prism, RectangularPrism
from boiling_learning.utils.utils import indexify


Expand All @@ -26,3 +27,30 @@ def test_KeyedSet(self):

keyed_set.discard('hello')
self.assertNotIn('hello', keyed_set)


class geometry_test(TestCase):
def test_Prism(self):
# consider a triangular prism

side = 3
length = 10

prism = Prism(
cross_section_perimeter=3 * side,
cross_section_area=side ** 2 * 3 ** 0.5 / 4,
length=length,
)

self.assertAlmostEqual(prism.lateral_area, 90, delta=0.1)

def test_Cylinder(self):
cylinder = Cylinder(length=10, diameter=2)

self.assertEqual(cylinder.radius, 1)

def test_RectangularPrism(self):
prism = RectangularPrism(width=5, thickness=3, length=10)

self.assertEqual(prism.cross_section_area, 15)
self.assertEqual(prism.cross_section_perimeter, 16)

0 comments on commit 58627ad

Please sign in to comment.