shapes: add Triangle area test
This commit is contained in:
parent
595ed65745
commit
47a831ab33
@ -1,6 +1,8 @@
|
|||||||
package shapes
|
package shapes
|
||||||
|
|
||||||
import "math"
|
import (
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
type Shape interface {
|
type Shape interface {
|
||||||
Area() float64
|
Area() float64
|
||||||
@ -11,10 +13,6 @@ type Rectangle struct {
|
|||||||
Height float64
|
Height float64
|
||||||
}
|
}
|
||||||
|
|
||||||
type Circle struct {
|
|
||||||
Radius float64
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r Rectangle) Perimeter() float64 {
|
func (r Rectangle) Perimeter() float64 {
|
||||||
return 2 * (r.Width + r.Height)
|
return 2 * (r.Width + r.Height)
|
||||||
}
|
}
|
||||||
@ -23,6 +21,19 @@ func (r Rectangle) Area() float64 {
|
|||||||
return r.Width * r.Height
|
return r.Width * r.Height
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Circle struct {
|
||||||
|
Radius float64
|
||||||
|
}
|
||||||
|
|
||||||
func (c Circle) Area() float64 {
|
func (c Circle) Area() float64 {
|
||||||
return c.Radius * c.Radius * math.Pi
|
return c.Radius * c.Radius * math.Pi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Triangle struct {
|
||||||
|
Width float64
|
||||||
|
Height float64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Triangle) Area() float64 {
|
||||||
|
return t.Width * t.Height * 0.5
|
||||||
|
}
|
||||||
|
@ -19,6 +19,7 @@ func TestArea(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{Rectangle{30.5, 20.5}, 625.25},
|
{Rectangle{30.5, 20.5}, 625.25},
|
||||||
{Circle{10}, 314.1592653589793},
|
{Circle{10}, 314.1592653589793},
|
||||||
|
{Triangle{12, 6}, 36.0},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range areaTests {
|
for _, test := range areaTests {
|
||||||
|
Loading…
Reference in New Issue
Block a user