Skip to content

Commit 553dd19

Browse files
committed
Refine Polygon unit tests
1 parent 62196d8 commit 553dd19

File tree

2 files changed

+52
-56
lines changed

2 files changed

+52
-56
lines changed

README.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ type Point[T Number] struct{
4848
func (p Point[T]) XY() (T, T)
4949

5050
// Mathematical operations
51-
func (p Point[T]) Add(vector Vector[T]) Point[T
52-
func (p Point[T]) AddXY(deltaX, deltaY T) Point[T
51+
func (p Point[T]) Add(vector Vector[T]) Point[T]
52+
func (p Point[T]) AddXY(deltaX, deltaY T) Point[T]
5353
func (p Point[T]) Subtract(other Point[T]) Vector[T]
5454
func (p Point[T]) Multiply(scale float64) Point[T]
5555
func (p Point[T]) MultiplyXY(scaleX, scaleY float64) Point[T]
@@ -90,20 +90,20 @@ func (v Vector[T]) DivideXY(scaleX, scaleY float64) Vector[T]
9090
func (v Vector[T]) Negate() Vector[T]
9191

9292
// Vector operations
93-
func (v Vector) Dot(vector Vector[T]) float64
94-
func (v Vector) Cross(vector Vector[T]) float64
95-
func (v Vector) Length() float64
96-
func (v Vector) LengthSquared() T
97-
func (v Vector) Normalize() Vector[T]
98-
func (v Vector) Angle() float64
93+
func (v Vector[T]) Dot(vector Vector[T]) float64
94+
func (v Vector[T]) Cross(vector Vector[T]) float64
95+
func (v Vector[T]) Length() float64
96+
func (v Vector[T]) LengthSquared() T
97+
func (v Vector[T]) Normalize() Vector[T]
98+
func (v Vector[T]) Angle() float64
9999

100100
// Transformations
101-
func (v Vector) Rotate(angle float64) Vector[T]
102-
func (v Vector) Normal() Vector[T]
101+
func (v Vector[T]) Rotate(angle float64) Vector[T]
102+
func (v Vector[T]) Normal() Vector[T]
103103

104104
// Utilities
105-
func (v Vector) Equal(vector Vector) bool
106-
func (v Vector) Zero() bool
105+
func (v Vector[T]) Equal(vector Vector) bool
106+
func (v Vector[T]) Zero() bool
107107
```
108108

109109
### Size
@@ -124,11 +124,11 @@ func (s Size[T]) AspectRatio() float64
124124
func (s Size[T]) Scaled(factor float64) Size[T]
125125
func (s Size[T]) ScaledXY(scaleX, scaleY float64) Size[T]
126126
func (s Size[T]) Expand(deltaWidth, deltaHeight T) Size[T]
127-
func (s Size[T]) Shrunk(deltaWidth, deltaHeight T) Size [T]
127+
func (s Size[T]) Shrunk(deltaWidth, deltaHeight T) Size[T]
128128

129129
// Utilities
130-
func (s Size) Equal(other Size[T]) bool
131-
func (s Size) Zero() bool
130+
func (s Size[T]) Equal(other Size[T]) bool
131+
func (s Size[T]) Zero() bool
132132
```
133133

134134
### Circle
@@ -146,8 +146,8 @@ func (c Circle[T]) Diameter() T
146146
func (c Circle[T]) Bounds() Rectangle[T]
147147

148148
// Transformations
149-
func (c Circle[T]) Translate(vector Vector) Circle[T]
150-
func (c Circle[T]) MoveTo(center Point) Circle[T]
149+
func (c Circle[T]) Translate(vector Vector[T]) Circle[T]
150+
func (c Circle[T]) MoveTo(center Point[T]) Circle[T]
151151
func (c Circle[T]) Scale(factor float64) Circle[T]
152152

153153
// Size operations
@@ -159,7 +159,7 @@ func (c Circle[T]) Shrunk(amount T) Circle[T]
159159
func (c Circle[T]) Contains(point Point[T]) bool
160160

161161
// Utilities
162-
func (c Circle) Equal(circle Circle) bool
162+
func (c Circle[T]) Equal(circle Circle) bool
163163
```
164164

165165
### Rectangle
@@ -203,13 +203,13 @@ type RegularPolygon[T Number] struct {
203203
Short constructors for convenience
204204

205205
```go
206-
func P(x, y T) Point[T]
207-
func V(x, y T) Vector[T]
208-
func S(w, h T) Size[T]
209-
func C(center Point[T], radius T) Circle[T]
210-
func R(center Point[T], size Size[T]) Rectangle[T]
211-
func L(start, end Point[T]) Line[T]
212-
func RP(center Point[T], size Size[T], n int) RegularPolygon[T]
206+
func P[T Number](x, y T) Point[T]
207+
func V[T Number](x, y T) Vector[T]
208+
func S[T Number](w, h T) Size[T]
209+
func C[T Number](center Point[T], radius T) Circle[T]
210+
func R[T Number](center Point[T], size Size[T]) Rectangle[T]
211+
func L[T Number](start, end Point[T]) Line[T]
212+
func RP[T Number](center Point[T], size Size[T], n int) RegularPolygon[T]
213213
```
214214

215215

polygon_test.go

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,40 @@ import (
66
"github.com/gravitton/assert"
77
)
88

9-
func TestPolygon_Center(t *testing.T) {
10-
p := Polygon[int]{Vertices: []Point[int]{P(0, 0), P(2, 0), P(2, 2), P(0, 2)}}
11-
testPoint(t, p.Center(), 1, 1)
9+
func TestPolygon_New(t *testing.T) {
10+
vertices := []Point[int]{P(0, 0), P(2, 0), P(2, 2), P(0, 2)}
1211

13-
pf := Polygon[float64]{Vertices: []Point[float64]{P(0.0, 0.0), P(2.0, 0.0), P(2.0, 1.0)}}
14-
cx, cy := pf.Center().XY()
15-
assert.EqualDelta(t, cx, 4.0/3.0, Delta)
16-
assert.EqualDelta(t, cy, 1.0/3.0, Delta)
12+
testPolygon(t, Polygon[int]{vertices}, vertices)
1713
}
1814

19-
func TestPolygon_Translate_MoveTo(t *testing.T) {
20-
p := Polygon[int]{Vertices: []Point[int]{P(0, 0), P(2, 0)}}
21-
p2 := p.Translate(V(1, -1))
22-
testPoint(t, p2.Vertices[0], 1, -1)
23-
testPoint(t, p2.Vertices[1], 3, -1)
24-
25-
p3 := p.MoveTo(P(10, 10))
26-
testPoint(t, p3.Center(), 10, 10)
15+
func TestPolygon_Center(t *testing.T) {
16+
testPoint(t, Polygon[int]{[]Point[int]{P(0, 0), P(2, 0), P(2, 2), P(0, 2)}}.Center(), 1, 1)
17+
testPoint(t, Polygon[float64]{[]Point[float64]{P(0.0, 0.0), P(2.0, 0.0), P(2.0, 1.0)}}.Center(), 4.0/3.0, 1.0/3.0)
2718
}
2819

29-
func TestPolygon_Scale(t *testing.T) {
30-
p := Polygon[float64]{Vertices: []Point[float64]{P(1.0, 0.0), P(0.0, 1.0), P(-1.0, 0.0), P(0.0, -1.0)}}
31-
center := p.Center()
32-
assert.True(t, center.Equal(P(0.0, 0.0)))
20+
func TestPolygon_Translate(t *testing.T) {
21+
testPolygon(t, Polygon[int]{[]Point[int]{P(0, 0), P(2, 0)}}.Translate(V(1, -1)), []Point[int]{
22+
P(1, -1),
23+
P(3, -1),
24+
})
25+
}
3326

34-
p2 := p.Scale(2)
35-
// Every point should double its distance from center (0,0).
36-
assert.EqualDelta(t, p2.Vertices[0].X, 2.0, Delta)
37-
assert.EqualDelta(t, p2.Vertices[0].Y, 0.0, Delta)
38-
assert.EqualDelta(t, p2.Vertices[1].X, 0.0, Delta)
39-
assert.EqualDelta(t, p2.Vertices[1].Y, 2.0, Delta)
27+
func TestPolygon_MoveTo(t *testing.T) {
28+
testPolygon(t, Polygon[int]{Vertices: []Point[int]{P(0, 0), P(2, 0)}}.MoveTo(P(10, 10)), []Point[int]{
29+
P(9, 10),
30+
P(11, 10),
31+
})
32+
}
4033

41-
p3 := p.ScaleXY(2, 0.5)
42-
assert.EqualDelta(t, p3.Vertices[0].X, 2.0, Delta)
43-
assert.EqualDelta(t, p3.Vertices[0].Y, 0.0, Delta)
44-
assert.EqualDelta(t, p3.Vertices[1].X, 0.0, Delta)
45-
assert.EqualDelta(t, p3.Vertices[1].Y, 0.5, Delta)
34+
func TestPolygon_Scale(t *testing.T) {
35+
testPolygon(t, Polygon[int]{Vertices: []Point[int]{P(0, 0), P(2, 0)}}.Scale(2), []Point[int]{
36+
P(-1, 0),
37+
P(3, 0),
38+
})
39+
testPolygon(t, Polygon[float64]{Vertices: []Point[float64]{P(0.0, 0.0), P(2.0, 1.0)}}.ScaleXY(0.5, 2.5), []Point[float64]{
40+
P(0.5, -0.75),
41+
P(1.5, 1.75),
42+
})
4643
}
4744

4845
func TestPolygon_Immutable(t *testing.T) {
@@ -53,7 +50,6 @@ func TestPolygon_Immutable(t *testing.T) {
5350
p.Scale(2)
5451
p.ScaleXY(2, 3)
5552

56-
// unchanged
5753
testPoint(t, p.Vertices[0], 0, 0)
5854
testPoint(t, p.Vertices[1], 2, 0)
5955
}

0 commit comments

Comments
 (0)