This repository has been archived by the owner on Nov 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExerciseFour.cs
97 lines (84 loc) · 2.8 KB
/
ExerciseFour.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Globalization;
using NUnit.Framework;
namespace TDDSolo
{
public class ExerciseFour
{
enum Shape
{
Pyramid,
Cube,
Cylinder,
}
class ShapeVolumeCalc
{
private readonly double _coefficient;
public ShapeVolumeCalc(double coefficient)
{
_coefficient = coefficient;
}
public double CalculateVolume(double width, double height)
{
return _coefficient * width * width * height;
}
}
private static double CalculateVolume(double width, double height, Shape shape)
{
if (width < 0) throw new ArgumentOutOfRangeException(nameof(width));
var shapeVolumeCalc = CreateShapeVolumeCalc(shape);
return shapeVolumeCalc.CalculateVolume(width, height);
}
private static ShapeVolumeCalc CreateShapeVolumeCalc(Shape shape)
{
switch (shape)
{
case Shape.Pyramid:
return new ShapeVolumeCalc(1.0 / 3.0);
case Shape.Cylinder:
return new ShapeVolumeCalc(Math.PI / 4);
case Shape.Cube:
return new ShapeVolumeCalc(1);
default:
throw new ArgumentOutOfRangeException(nameof(shape));
}
}
[TestCase(0, 0)]
[TestCase(1, 1)]
[TestCase(2, 8)]
[TestCase(1.5, 3.375)]
public void CubeHasVolume(double size, double volume)
{
Assert.AreEqual(volume, CalculateVolume(size, size, Shape.Cube));
}
[Test]
public void CannotCalculateVolumeOfMissingShape()
{
Assert.Throws<ArgumentOutOfRangeException>(() => CalculateVolume(1, 1, (Shape)(-1)));
}
[Test]
public void NoNegativeCubeSides()
{
Assert.Throws<ArgumentOutOfRangeException>(() => CalculateVolume(-1, -1, Shape.Cube));
}
[TestCase(0, 0, 0)]
[TestCase(1, 1, Math.PI)]
[TestCase(1, 2, 2 * Math.PI)]
[TestCase(2, 1, 4 * Math.PI)]
[TestCase(2, 2, 8 * Math.PI)]
public void CylinderHasVolume(double radius, double height, double volume)
{
var diameter = 2 * radius;
Assert.AreEqual(volume, CalculateVolume(diameter, height, Shape.Cylinder));
}
[TestCase(0, 0, 0.0)]
[TestCase(1, 1, 1.0 / 3.0)]
[TestCase(1, 2, 2.0 / 3.0)]
[TestCase(2, 1, 4.0 / 3.0)]
[TestCase(2, 2, 8.0 / 3.0)]
public void PyramidHasVolume(double width, double height, double volume)
{
Assert.AreEqual(volume, CalculateVolume(width, height, Shape.Pyramid));
}
}
}