1 """Shape OOP test for PythonVsJava
2 """
3 import unittest, math
4
5 class Shape:
6 """Abstract class for entire shape
7 """
8 def area(self):
9 raise NotImplementedError
10 def circumference(self):
11 raise NotImplementedError
12
13 class Circle(Shape):
14 def __init__(self, aRadius):
15 self.setRadius(aRadius)
16 def setRadius(self, aRadius):
17 assert type(aRadius) in (int, float) and aRadius >= 0
18 self.r = aRadius
19 def getRadius(self):
20 return self.r
21 def area(self):
22 return math.pi * self.r * self.r
23 def circumference(self):
24 return 2 * math.pi * self.r
25
26 class Rectangle(Shape):
27 def __init__(self, aWidth, aHeight):
28 self.setWidth(aWidth)
29 self.setHeight(aHeight)
30 def setWidth(self, aWidth):
31 assert type(aWidth) in (int, float) and aWidth >= 0
32 self.w = aWidth
33 def setHeight(self, aHeight):
34 assert type(aHeight) in (int, float) and aHeight >= 0
35 self.h = aHeight
36 def getWidth(self):
37 return self.w
38 def getHeight(self):
39 return self.h
40 def area(self):
41 return self.w * self.h
42 def circumference(self):
43 return 2 * (self.w + self.h)
44
45 class Square(Shape):
46 def __init__(self, aSide):
47 self.setSide(aSide)
48 def setSide(self, aSide):
49 assert type(aSide) in (int, float) and aSide>=0
50 self.s = aSide
51 def getSide(self):
52 return self.s
53 def area(self):
54 return self.s * self.s
55 def circumference(self):
56 return 4 * self.s
57
58 class Centered:
59 def __init__(self, aX, aY):
60 self.setCenter(aX, aY)
61 def setCenter(self, aX, aY):
62 self.x = aX
63 self.y = aY
64 def getCenterX(self):
65 return self.x
66 def getCenterY(self):
67 return self.y
68 def getDistanceFromOrigin(self):
69 return math.sqrt(self.x*self.x + self.y*self.y)
70
71 class CenteredCircle(Centered, Circle):
72 def __init__(self, aX, aY, aRadius):
73 Centered.__init__(self, aX, aY)
74 Circle.__init__(self, aRadius)
75
76 class CenteredRectangle(Centered, Rectangle):
77 def __init__(self, aX, aY, aWidth, aHeight):
78 Centered.__init__(self, aX, aY)
79 Rectangle.__init__(self, aWidth, aHeight)
80
81 class CenteredSquare(Centered, Square):
82 def __init__(self, aX, aY, aSide):
83 Centered.__init__(self, aX, aY)
84 Square.__init__(self, aSide)
85
86 class ShapeTest(unittest.TestCase):
87 def testShape(self):
88 myShape = Shape()
89 self.assertRaises(NotImplementedError, myShape.area)
90 def testCircle(self):
91 myCircle = Circle(5.5)
92 self.assertEquals(5.5, myCircle.getRadius())
93 self.assert_(myCircle.area())
94 self.assert_(myCircle.circumference())
95 self.assertRaises(AssertionError, myCircle.setRadius, -1)
96 self.assertRaises(AssertionError, myCircle.setRadius, 'a')
97 def testRectangle(self):
98 myRec = Rectangle(3,4)
99 self.assertEquals(3, myRec.getWidth())
100 def testAbstractClass(self):
101 myShapes = [Circle(2.0), Rectangle(1,3.0), Rectangle(4.0, 2.0)]
102 totalArea = 0
103 for myShape in myShapes:
104 totalArea += myShape.area()
105 self.assertEquals(23.566, round(totalArea,3))
106 def testSquare(self):
107 mySquare = Square(2.0)
108 self.assertEquals(8.0, mySquare.circumference())
109 def testMultipleInheritance(self):
110 myShapes = [CenteredCircle(1,1,1),
111 CenteredRectangle(2.3, 4.5, 3, 4),
112 CenteredSquare(2.5, 2, 3),
113 ]
114 totalArea = 0; totalDistance = 0
115 for myShape in myShapes:
116 totalArea += myShape.area()
117 totalDistance += myShape.getDistanceFromOrigin()
118 self.assertEquals(24.142, round(totalArea,3))
119 self.assertEquals(9.669, round(totalDistance,3))
120
121 if __name__=='__main__':
122 unittest.main(argv=('','-v'))