1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package s2
16
17 import (
18 "math"
19 "testing"
20 )
21
22 func TestMetric(t *testing.T) {
23 if got := MinWidthMetric.MaxLevel(0.001256); got != 9 {
24 t.Errorf("MinWidthMetric.MaxLevel(0.001256) = %d, want 9", got)
25 }
26
27
28
29 if MaxEdgeAspect < 1 {
30 t.Errorf("MaxEdgeAspect = %v, want >= 1", MaxEdgeAspect)
31 }
32 if got := MaxEdgeMetric.Deriv / MinEdgeMetric.Deriv; MaxEdgeAspect > got {
33 t.Errorf("Edge Aspect: %v/%v = %v, want <= %v", MaxEdgeMetric.Deriv, MinEdgeMetric.Deriv, got, MaxDiagAspect)
34 }
35 if MaxDiagAspect < 1 {
36 t.Errorf("MaxDiagAspect = %v, want >= 1", MaxDiagAspect)
37 }
38 if got := MaxDiagMetric.Deriv / MinDiagMetric.Deriv; MaxDiagAspect > got {
39 t.Errorf("Diag Aspect: %v/%v = %v, want <= %v", MaxDiagMetric.Deriv, MinDiagMetric.Deriv, got, MaxDiagAspect)
40 }
41
42
43 if got := MinWidthMetric.Deriv*MinEdgeMetric.Deriv - 1e-15; MinAreaMetric.Deriv < got {
44 t.Errorf("Min Area: %v*%v = %v, want >= %v", MinWidthMetric.Deriv, MinEdgeMetric.Deriv, got, MinAreaMetric.Deriv)
45 }
46 if got := MaxWidthMetric.Deriv*MaxEdgeMetric.Deriv + 1e-15; MaxAreaMetric.Deriv > got {
47 t.Errorf("Max Area: %v*%v = %v, want <= %v", MaxWidthMetric.Deriv, MaxEdgeMetric.Deriv, got, MaxAreaMetric.Deriv)
48 }
49
50 for level := -2; level <= MaxLevel+3; level++ {
51 width := MinWidthMetric.Deriv * math.Pow(2, float64(-level))
52 if level >= MaxLevel+3 {
53 width = 0
54 }
55
56
57 expected := int(math.Max(0, math.Min(MaxLevel, float64(level))))
58
59 if MinWidthMetric.MinLevel(width) != expected {
60 t.Errorf("MinWidthMetric.MinLevel(%v) = %v, want %v", width, MinWidthMetric.MinLevel(width), expected)
61 }
62 if MinWidthMetric.MaxLevel(width) != expected {
63 t.Errorf("MinWidthMetric.MaxLevel(%v) = %v, want %v", width, MinWidthMetric.MaxLevel(width), expected)
64 }
65 if MinWidthMetric.ClosestLevel(width) != expected {
66 t.Errorf("MinWidthMetric.ClosestLevel(%v) = %v, want %v", width, MinWidthMetric.ClosestLevel(width), expected)
67 }
68
69
70 if got := MinWidthMetric.MinLevel(1.2 * width); got != expected {
71 t.Errorf("non-boundary MinWidthMetric.MinLevel(%v) = %v, want %v", 1.2*width, got, expected)
72 }
73 if got := MinWidthMetric.MaxLevel(0.8 * width); got != expected {
74 t.Errorf("non-boundary MinWidthMetric.MaxLevel(%v) = %v, want %v", 0.8*width, got, expected)
75 }
76 if got := MinWidthMetric.ClosestLevel(1.2 * width); got != expected {
77 t.Errorf("non-boundary larger MinWidthMetric.ClosestLevel(%v) = %v, want %v", 1.2*width, got, expected)
78 }
79 if got := MinWidthMetric.ClosestLevel(0.8 * width); got != expected {
80 t.Errorf("non-boundary smaller MinWidthMetric.ClosestLevel(%v) = %v, want %v", 0.8*width, got, expected)
81 }
82 }
83 }
84
85 func TestMetricSizeRelations(t *testing.T) {
86
87 tests := []struct {
88 min Metric
89 avg Metric
90 max Metric
91 }{
92 {MinAngleSpanMetric, AvgAngleSpanMetric, MaxAngleSpanMetric},
93 {MinWidthMetric, AvgWidthMetric, MaxWidthMetric},
94 {MinEdgeMetric, AvgEdgeMetric, MaxEdgeMetric},
95 {MinDiagMetric, AvgDiagMetric, MaxDiagMetric},
96 {MinAreaMetric, AvgAreaMetric, MaxAreaMetric},
97 }
98
99 for _, test := range tests {
100 if test.min.Deriv > test.avg.Deriv {
101 t.Errorf("Min %v > Avg %v", test.min.Deriv, test.avg.Deriv)
102 }
103 if test.avg.Deriv > test.max.Deriv {
104 t.Errorf("Avg %v > Max %v", test.avg.Deriv, test.max.Deriv)
105 }
106 }
107 }
108
View as plain text