|
| 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