...

Source file src/github.com/golang/geo/s2/metric_test.go

Documentation: github.com/golang/geo/s2

     1  // Copyright 2015 Google Inc. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    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  	// Check that the maximum aspect ratio of an individual cell is consistent
    28  	// with the global minimums and maximums.
    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  	// Check that area is consistent with edge and width.
    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  		// Check boundary cases (exactly equal to a threshold value).
    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  		// Also check non-boundary cases.
    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  	// check that min <= avg <= max for each metric.
    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