...
1 package geo
2
3 import (
4 "testing"
5 )
6
7 func TestPointDistanceTo(t *testing.T) {
8 p1 := &Point{0, 0}
9 p2 := &Point{100, 0}
10
11 p := &Point{50, 70}
12
13 d := p.DistanceToLine(p1, p2)
14
15 if d != 70.0 {
16 t.Fatalf("Expected 70.0 and got %v", d)
17 }
18 }
19
20 func TestAddVector(t *testing.T) {
21 start := &Point{1.5, 5.3}
22 c := NewVector(-3.5, -2.3)
23 p2 := start.AddVector(c)
24
25 if p2.X != -2 || p2.Y != 3 {
26 t.Fatalf("Expected resulting point to be (-2, 3), got %+v", p2)
27 }
28 }
29
30 func TestToVector(t *testing.T) {
31 p := &Point{3.5, 6.7}
32 v := p.ToVector()
33
34 if v[0] != p.X || v[1] != p.Y {
35 t.Fatalf("Expected Vector (%v) coordinates to match the point (%v)", p, v)
36 }
37
38 if len(v) != 2 {
39 t.Fatal("Expected the Vector to have 2 components")
40 }
41 }
42
43 func TestVectorTo(t *testing.T) {
44 p1 := &Point{1.5, 5.3}
45 p2 := &Point{-2, 3}
46 c := p1.VectorTo(p2)
47 if !c.equals(NewVector(-3.5, -2.3)) {
48 t.Fatalf("Expected Vector to be (-3.5, -2.3), got %v", c)
49 }
50
51 p1 = &Point{1.5, 5.3}
52 p2 = &Point{-2, 3}
53 c = p2.VectorTo(p1)
54 if !c.equals(NewVector(3.5, 2.3)) {
55 t.Fatalf("Expected Vector to be (3.5, 2.3), got %v", c)
56 }
57 }
58
View as plain text