shapes: create a rectangle struct

This commit is contained in:
vinchent 2024-09-11 08:58:04 +02:00
parent 9381e4c92a
commit 5de98f3ac8
2 changed files with 13 additions and 6 deletions

View File

@ -1,9 +1,14 @@
package shapes package shapes
func Perimeter(width, height float64) float64 { type Rectangle struct {
return 2 * (width + height) Width float64
Height float64
} }
func Area(width, height float64) float64 { func Perimeter(r Rectangle) float64 {
return width * height return 2 * (r.Width + r.Height)
}
func Area(r Rectangle) float64 {
return r.Width * r.Height
} }

View File

@ -3,7 +3,8 @@ package shapes
import "testing" import "testing"
func TestPerimeter(t *testing.T) { func TestPerimeter(t *testing.T) {
got := Perimeter(30.5, 20.5) rectangle := Rectangle{30.5, 20.5}
got := Perimeter(rectangle)
exp := 102.0 exp := 102.0
if got != exp { if got != exp {
@ -12,7 +13,8 @@ func TestPerimeter(t *testing.T) {
} }
func TestArea(t *testing.T) { func TestArea(t *testing.T) {
got := Area(30.5, 20.5) rectangle := Rectangle{30.5, 20.5}
got := Area(rectangle)
exp := 625.25 exp := 625.25
if got != exp { if got != exp {