Skip to content

Commit b0823c2

Browse files
Cherry-picking changes from AndrewHUNGNguyen branch to new branch (#83)
1 parent cf5a974 commit b0823c2

4 files changed

Lines changed: 268 additions & 8 deletions

File tree

easyPythonpi/easyPythonpi.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ def __init__(self, n, message="n is not valid, must be greater than or equal to
1919
self.n = n
2020
self.message = message
2121

22+
class InvalidMeasurementShapeException(Exception):
23+
def __init__(self, message="invalid measurement, must be greater than or equal to 0"):
24+
self.message = message

easyPythonpi/methods/basics.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ def factors(n:'int')->'int':
3737
factors.append(i)
3838
return factors
3939

40-
def Area_circle(r:'float')->'float': # To find the area of a circle using the radius r
41-
PI = 3.142
42-
return PI * (r * r)
43-
44-
def Perimeter_circle(r:'float')->'float': # To find the perimeter of a circle using the radius r
45-
PI = 3.142
46-
return 2 * PI * r
47-
4840
def fibonacci(n:'int')->'int': #To find the nth fibonacci series
4941
"""Finds the fibonacci of the nth sequence.
5042

easyPythonpi/methods/shape.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env python
2+
#-*- coding: utf-8 -*-
3+
4+
import easyPythonpi as pi
5+
import regex as re
6+
import math
7+
8+
# To find the perimeter of a circle using the radius r
9+
def perimeter_circle(r:'float')->'float':
10+
if r <= 0:
11+
raise pi.InvalidMeasurementShapeException(r)
12+
PI = 3.142
13+
return 2 * PI * r
14+
15+
# To find the area of a circle using the radius r
16+
def area_circle(r:'float')->'float':
17+
if r <= 0:
18+
raise pi.InvalidMeasurementShapeException()
19+
PI = 3.142
20+
return PI * (r * r)
21+
22+
# To find the volume of a sphere using the radius r
23+
def volume_sphere(r:'float')->'float':
24+
if r <= 0:
25+
raise pi.InvalidMeasurementShapeException()
26+
PI = 3.142
27+
return 4 * PI * (r**3)
28+
29+
# To find the perimeter of a rectangle using the length and width
30+
def perimeter_rectangle_square(length:'float', width:'float')->'float':
31+
if length <= 0 or width <= 0:
32+
raise pi.InvalidMeasurementShapeException()
33+
return (2 * length) + (2 * width)
34+
35+
def area_rectangle_square(length:'float', width:'float')->'float':
36+
if length <= 0 or width <= 0:
37+
raise pi.InvalidMeasurementShapeException()
38+
return length * width
39+
40+
# To find the perimeter of a rectangle using the length and width
41+
def perimeter_triangle(side1:'float', side2:'float', side3:'float')->'float':
42+
if side1 <= 0 or side2 <= 0 or side3 <= 0:
43+
raise pi.InvalidMeasurementShapeException()
44+
return side1 + side2 + side3
45+
46+
def area_triangle(base:'float', height:'float')->'float':
47+
if base <= 0 or height <= 0:
48+
raise pi.InvalidMeasurementShapeException()
49+
return (base * height) / 2
50+
51+
# To find the volume of a pyramid with length and width of base and height of pyramid
52+
def volume_pyramid(length:'float', width:'float', height:'float')->'float':
53+
if length <= 0 or width <= 0 or height <= 0:
54+
raise pi.InvalidMeasurementShapeException()
55+
return (length * width * height) / 3
56+
57+
# To find the volume of a cylinder with radius of the cylinder base and height of the cylinder
58+
def volume_cylinder(radius:'float', height:'float')->'float':
59+
if radius <= 0 or height <= 0:
60+
raise pi.InvalidMeasurementShapeException()
61+
PI = 3.142
62+
return PI * (radius**2) * height
63+
64+
# To find the area of a cone with length and width of base and height of pyramid
65+
def area_cone(radius:'float', height:'float')->'float':
66+
if radius <= 0 or height <= 0:
67+
raise pi.InvalidMeasurementShapeException()
68+
PI = 3.142
69+
return ((PI * radius) * (radius + (math.sqrt(height**2 + radius**2))))
70+
71+
# To find the volume of a cone with length and width of base and height of pyramid
72+
def volume_cone(radius:'float', height:'float')->'float':
73+
if radius <= 0 or height <= 0:
74+
raise pi.InvalidMeasurementShapeException()
75+
PI = 3.142
76+
return ( PI * (radius**2) * height ) / 3
77+

easyPythonpi/test/test_shape.py

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
import unittest
2+
import math
3+
4+
# Import the function you want to test
5+
from methods.shape import *
6+
from easyPythonpi import InvalidMeasurementShapeException
7+
8+
class TestShape(unittest.TestCase):
9+
# Test cases to calculate the perimeter of a circle
10+
def test_perimeter_circle_radius_zero(self):
11+
with self.assertRaises(InvalidMeasurementShapeException):
12+
perimeter_circle(0)
13+
def test_perimeter_circle_radius_invalid(self):
14+
with self.assertRaises(InvalidMeasurementShapeException):
15+
perimeter_circle(-1)
16+
def test_perimeter_circle_radius_greater_than_zero(self):
17+
result = perimeter_circle(10)
18+
self.assertEqual(round(result,3), 62.840)
19+
20+
# Test cases to calculate the area of a circle
21+
def test_area_circle_radius_zero(self):
22+
with self.assertRaises(InvalidMeasurementShapeException):
23+
area_circle(0)
24+
def test_area_circle_radius_invalid(self):
25+
with self.assertRaises(InvalidMeasurementShapeException):
26+
area_circle(-1)
27+
def test_area_circle_radius_greater_than_zero(self):
28+
result = area_circle(1)
29+
self.assertEqual(result, 3.142)
30+
31+
# Test cases to calculate the volume of a sphere
32+
def test_volume_sphere_radius_zero(self):
33+
with self.assertRaises(InvalidMeasurementShapeException):
34+
volume_sphere(0)
35+
def test_volume_sphere_radius_negative(self):
36+
with self.assertRaises(InvalidMeasurementShapeException):
37+
volume_sphere(-1)
38+
def test_volume_sphere_radius_greater_than_zero(self):
39+
result = volume_sphere(2)
40+
self.assertEqual(round(result,3), 100.544)
41+
42+
# Test cases to calculate the perimeter of a rectangle
43+
def test_perimeter_rectangle_square_length_and_width_negative(self):
44+
with self.assertRaises(InvalidMeasurementShapeException):
45+
perimeter_rectangle_square(-1, -9.9)
46+
def test_perimeter_rectangle_square_length_negative(self):
47+
with self.assertRaises(InvalidMeasurementShapeException):
48+
perimeter_rectangle_square(-1,5.51)
49+
def test_perimeter_rectangle_square_width_negative(self):
50+
with self.assertRaises(InvalidMeasurementShapeException):
51+
perimeter_rectangle_square(63,-8)
52+
def test_perimeter_rectangle_square_length_and_width_positive_num(self):
53+
result = perimeter_rectangle_square(5, 3)
54+
self.assertEqual(result, 16)
55+
56+
# Test cases to calculate the area of a rectangle
57+
def test_area_rect_square_with_positive_length_and_width(self):
58+
self.assertEqual(area_rectangle_square(9.9, 3.7), 36.63)
59+
def test_area_rect_square_with_zero_length(self):
60+
with self.assertRaises(InvalidMeasurementShapeException):
61+
area_rectangle_square(0, 3)
62+
def test_area_rect_square_with_zero_width(self):
63+
with self.assertRaises(InvalidMeasurementShapeException):
64+
area_rectangle_square(5, 0)
65+
def test_area_rect_square_with_negative_length(self):
66+
with self.assertRaises(InvalidMeasurementShapeException):
67+
area_rectangle_square(-5, 3)
68+
def test_area_with_negative_width(self):
69+
with self.assertRaises(InvalidMeasurementShapeException):
70+
area_rectangle_square(5, -3)
71+
72+
# Test cases to calculate the perimeter of a triangle
73+
def test_perimeter_triangle_with_positive_sides(self):
74+
self.assertEqual(perimeter_triangle(3, 4, 5), 12)
75+
def test_perimeter_triangle_with_zero_side1(self):
76+
with self.assertRaises(InvalidMeasurementShapeException):
77+
perimeter_triangle(0, 4, 5)
78+
def test_perimeter_triangle_with_zero_side2(self):
79+
with self.assertRaises(InvalidMeasurementShapeException):
80+
perimeter_triangle(3, 0, 5)
81+
def test_perimeter_triangle_with_zero_side3(self):
82+
with self.assertRaises(InvalidMeasurementShapeException):
83+
perimeter_triangle(3, 4, 0)
84+
def test_perimeter_triangle_with_negative_side1(self):
85+
with self.assertRaises(InvalidMeasurementShapeException):
86+
perimeter_triangle(-3, 4, 5)
87+
def test_perimeter_triangle_with_negative_side2(self):
88+
with self.assertRaises(InvalidMeasurementShapeException):
89+
perimeter_triangle(3, -4, 5)
90+
def test_perimeter_triangle_with_negative_side3(self):
91+
with self.assertRaises(InvalidMeasurementShapeException):
92+
perimeter_triangle(3, 4, -5)
93+
94+
#test cases to find area of a triangle
95+
def test_area_triangle_valid_lengths(self):
96+
# Test for an equilateral triangle
97+
result = area_triangle(67, 4)
98+
self.assertEqual(result, (67 * 4) / 2)
99+
def test_area_triangle_zero_base(self):
100+
# Test with a zero side length
101+
with self.assertRaises(InvalidMeasurementShapeException):
102+
area_triangle(0, 4)
103+
def test_area_triangle_negative_base_length(self):
104+
# Test with a negative side length
105+
with self.assertRaises(InvalidMeasurementShapeException):
106+
area_triangle(-99, 4)
107+
def test_area_triangle_zero_height_length(self):
108+
# Test with a zero side length
109+
with self.assertRaises(InvalidMeasurementShapeException):
110+
area_triangle(4, 0)
111+
def test_area_triangle_negative_height_length(self):
112+
# Test with a negative side length
113+
with self.assertRaises(InvalidMeasurementShapeException):
114+
area_triangle(5, -47)
115+
116+
# Test cases to find the volume of a pyramid
117+
def test_volume_pyramid_valid_dimensions(self):
118+
result = volume_pyramid(4, 6, 3)
119+
self.assertEqual(result, 24.0)
120+
def test_volume_pyramid_zero_length(self):
121+
with self.assertRaises(InvalidMeasurementShapeException):
122+
volume_pyramid(0, 6, 3)
123+
def test_volume_pyramid_negative_length(self):
124+
with self.assertRaises(InvalidMeasurementShapeException):
125+
volume_pyramid(-76, 6, 3)
126+
def test_volume_pyramid_zero_width(self):
127+
with self.assertRaises(InvalidMeasurementShapeException):
128+
volume_pyramid(55, 0, 12)
129+
def test_volume_pyramid_negative_width(self):
130+
with self.assertRaises(InvalidMeasurementShapeException):
131+
volume_pyramid(55, -99, 12)
132+
def test_volume_pyramid_zero_height(self):
133+
with self.assertRaises(InvalidMeasurementShapeException):
134+
volume_pyramid(55, 49, 0)
135+
def test_volume_pyramid_negative_height(self):
136+
with self.assertRaises(InvalidMeasurementShapeException):
137+
volume_pyramid(55, 49, -77)
138+
139+
# test cases to find the volume of a cone
140+
def test_volume_cone_valid_dimensions(self):
141+
result = volume_cone(2, 9.5)
142+
self.assertEqual(round(result,3), 39.799)
143+
def test_volume_cone_zero_radius(self):
144+
with self.assertRaises(InvalidMeasurementShapeException):
145+
volume_cone(0, 49)
146+
def test_volume_cone_negative_radius(self):
147+
with self.assertRaises(InvalidMeasurementShapeException):
148+
volume_cone(-10, 49)
149+
def test_volume_cone_negative_height(self):
150+
with self.assertRaises(InvalidMeasurementShapeException):
151+
volume_cone(77, -33)
152+
def test_volume_cone_zero_height(self):
153+
with self.assertRaises(InvalidMeasurementShapeException):
154+
volume_cone(212, 0)
155+
156+
# test cases to find the volume of a cylinder
157+
def test_volume_cylinder_valid_dimensions(self):
158+
result = volume_cylinder(8, 10.99)
159+
self.assertEqual(round(result,3), 2209.957)
160+
def test_volume_cylinder_zero_radius(self):
161+
with self.assertRaises(InvalidMeasurementShapeException):
162+
volume_cylinder(0, 49)
163+
def test_volume_cylinder_negative_radius(self):
164+
with self.assertRaises(InvalidMeasurementShapeException):
165+
volume_cylinder(-10, 49)
166+
def test_volume_cylinder_negative_height(self):
167+
with self.assertRaises(InvalidMeasurementShapeException):
168+
volume_cylinder(77, -33)
169+
def test_volume_cylinder_zero_height(self):
170+
with self.assertRaises(InvalidMeasurementShapeException):
171+
volume_cylinder(212, 0)
172+
173+
# test cases to find the area of a cone
174+
def test_area_cone_valid_dimensions(self):
175+
result = area_cone(25, 4.99)
176+
self.assertEqual(round(result,3), 3966.236)
177+
def test_area_cone_radius_zero(self):
178+
with self.assertRaises(InvalidMeasurementShapeException):
179+
area_cone(0, 5)
180+
def test_area_cone_radius_negative(self):
181+
with self.assertRaises(InvalidMeasurementShapeException):
182+
area_cone(4, -5)
183+
def test_area_cone_height_zero(self):
184+
with self.assertRaises(InvalidMeasurementShapeException):
185+
area_cone(5, 0)
186+
def test_area_cone_height_negative(self):
187+
with self.assertRaises(InvalidMeasurementShapeException):
188+
area_cone(4, -99)

0 commit comments

Comments
 (0)