1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package s2
16
17 import (
18 "math/rand"
19 "testing"
20 )
21
22 func TestPointVectorEmpty(t *testing.T) {
23 var shape PointVector
24
25 if got, want := shape.NumEdges(), 0; got != want {
26 t.Errorf("shape.NumEdges() = %v, want %v", got, want)
27 }
28 if got, want := shape.NumChains(), 0; got != want {
29 t.Errorf("shape.NumChains() = %v, want %v", got, want)
30 }
31 if got, want := shape.Dimension(), 0; got != want {
32 t.Errorf("shape.Dimension() = %v, want %v", got, want)
33 }
34 if !shape.IsEmpty() {
35 t.Errorf("shape.IsEmpty() = false, want true")
36 }
37 if shape.IsFull() {
38 t.Errorf("shape.IsFull() = true, want false")
39 }
40 if shape.ReferencePoint().Contained {
41 t.Errorf("shape.ReferencePoint().Contained = true, want false")
42 }
43 }
44
45 func TestPointVectorBasics(t *testing.T) {
46 const seed = 8675309
47 rand.Seed(seed)
48
49 const numPoints = 100
50 var p PointVector = make([]Point, numPoints)
51
52 for i := 0; i < numPoints; i++ {
53 p[i] = randomPoint()
54 }
55
56 shape := Shape(&p)
57 if got, want := shape.NumEdges(), numPoints; got != want {
58 t.Errorf("shape.NumEdges() = %v, want %v", got, want)
59 }
60 if got, want := shape.NumChains(), numPoints; got != want {
61 t.Errorf("shape.NumChains() = %v, want %v", got, want)
62 }
63 if got, want := shape.Dimension(), 0; got != want {
64 t.Errorf("shape.Dimension() = %v, want %v", got, want)
65 }
66 if shape.IsEmpty() {
67 t.Errorf("shape.IsEmpty() = true, want false")
68 }
69 if shape.IsFull() {
70 t.Errorf("shape.IsFull() = true, want false")
71 }
72
73 rand.Seed(seed)
74 for i := 0; i < numPoints; i++ {
75 if got, want := shape.Chain(i).Start, i; got != want {
76 t.Errorf("shape.Chain(%d).Start = %d, want %d", i, got, want)
77 }
78 if got, want := shape.Chain(i).Length, 1; got != want {
79 t.Errorf("shape.Chain(%d).Length = %v, want %d", i, got, want)
80 }
81 edge := shape.Edge(i)
82 pt := randomPoint()
83
84 if !pt.ApproxEqual(edge.V0) {
85 t.Errorf("shape.Edge(%d).V0 = %v, want %v", i, edge.V0, pt)
86 }
87 if !pt.ApproxEqual(edge.V1) {
88 t.Errorf("shape.Edge(%d).V1 = %v, want %v", i, edge.V1, pt)
89 }
90 }
91 }
92
View as plain text