diff --git a/structs/shapes.go b/structs/shapes.go new file mode 100644 index 0000000..49ceceb --- /dev/null +++ b/structs/shapes.go @@ -0,0 +1,37 @@ +package structs + +import "math" + +type Shape interface { + Area() float64 +} + +type Rectangle struct { + Width float64 + Height float64 +} + +type Circle struct { + Radius float64 +} + +type Triangle struct { + Base float64 + Height float64 +} + +func Perimiter(r Rectangle) float64 { + return 2 * (r.Width + r.Height) +} + +func (c Circle) Area() float64 { + return math.Pi * c.Radius * c.Radius +} + +func (r Rectangle) Area() float64 { + return r.Width * r.Height +} + +func (t Triangle) Area() float64 { + return t.Base * t.Height / 2 +} diff --git a/structs/shapes_test.go b/structs/shapes_test.go new file mode 100644 index 0000000..75663bd --- /dev/null +++ b/structs/shapes_test.go @@ -0,0 +1,38 @@ +package structs + +import "testing" + +func TestPerimiter(t *testing.T) { + rectangle := Rectangle{10.0, 10.0} + got := Perimiter(rectangle) + want := 40.0 + + if got != want { + t.Errorf("got %.2f want %.2f", got, want) + } +} + +func TestArea(t *testing.T) { + + areaTests := []struct { + name string + shape Shape + hasArea float64 + }{ + {name: "Rectangle", shape: Rectangle{Width: 12, Height: 6}, hasArea: 72.0}, + {name: "Circle", shape: Circle{Radius: 10}, hasArea: 314.1592653589793}, + {name: "Triangle", shape: Triangle{Base: 12, Height: 6}, hasArea: 36.0}, + } + + for _, tt := range areaTests { + // using tt.name from the case to use it as the `t.Run` test name + t.Run(tt.name, func(t *testing.T) { + got := tt.shape.Area() + if got != tt.hasArea { + t.Errorf("%#v got %g want %g", tt.shape, got, tt.hasArea) + } + }) + + } + +}