-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolygon_test.go
More file actions
120 lines (98 loc) · 2.65 KB
/
polygon_test.go
File metadata and controls
120 lines (98 loc) · 2.65 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package polygon_test
import (
"math"
"testing"
"github.com/hamonangann/polygon"
"github.com/stretchr/testify/assert"
)
func TestPolygon(t *testing.T) {
t.Run("should handle polygon creation with less than 3 points", func(t *testing.T) {
_, err := polygon.NewPolygon(
polygon.Point{0, 0},
polygon.Point{1, 2},
)
assert.EqualError(t, err, polygon.ErrInsufficientPoints.Error())
})
t.Run("should handle polygon creation with collinear points", func(t *testing.T) {
_, err := polygon.NewPolygon(
polygon.Point{1, 3},
polygon.Point{2, 6},
polygon.Point{3, 9},
)
assert.EqualError(t, err, polygon.ErrCollinearPoints.Error())
})
t.Run("should handle polygon creation with self-intersecting sides", func(t *testing.T) {
_, err := polygon.NewPolygon(
polygon.Point{0, 3},
polygon.Point{3, 3},
polygon.Point{0, 0},
polygon.Point{3, 0},
)
assert.EqualError(t, err, polygon.ErrSelfIntersecting.Error())
})
t.Run("should return created points from valid polygon", func(t *testing.T) {
pts := []polygon.Point{
{0, 0},
{4, 3},
{4, 0},
}
p, _ := polygon.NewPolygon(pts...)
assert.Equal(t, pts, p.Points())
})
t.Run("should return correct sides length", func(t *testing.T) {
p, _ := polygon.NewPolygon(
polygon.Point{0, 0},
polygon.Point{0, 3},
polygon.Point{4, 3},
polygon.Point{4, 0},
)
assert.Equal(t, []float64{3, 4, 3, 4}, p.Sides())
})
t.Run("should return correct perimeter", func(t *testing.T) {
p, _ := polygon.NewPolygon(
polygon.Point{0, 0},
polygon.Point{0, 3},
polygon.Point{4, 3},
polygon.Point{4, 0},
)
assert.Equal(t, 14.0, p.Perimeter())
})
t.Run("should return correct area", func(t *testing.T) {
p, _ := polygon.NewPolygon(
polygon.Point{0, 0},
polygon.Point{0, 3},
polygon.Point{4, 0},
)
assert.Equal(t, 6.0, p.Area())
})
t.Run("should return correct number of sides", func(t *testing.T) {
p, _ := polygon.NewPolygon(
polygon.Point{0, 0},
polygon.Point{0, 3},
polygon.Point{3, 4},
polygon.Point{5, 3},
polygon.Point{5, 0},
)
assert.Equal(t, 5, p.NumberOfSides())
})
t.Run("should return correct number of vertices", func(t *testing.T) {
p, _ := polygon.NewPolygon(
polygon.Point{0, 0},
polygon.Point{0, 3},
polygon.Point{3, 4},
polygon.Point{5, 3},
polygon.Point{5, 0},
)
assert.Equal(t, 5, p.NumberOfVertices())
})
t.Run("should return correct sum of interior angles", func(t *testing.T) {
p, _ := polygon.NewPolygon(
polygon.Point{0, 0},
polygon.Point{0, 3},
polygon.Point{3, 4},
polygon.Point{5, 3},
polygon.Point{5, 0},
)
assert.Equal(t, math.Pi*3.0, p.SumOfInteriorAngles())
})
}