...

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

Documentation: github.com/golang/geo/s2

     1  // Copyright 2020 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  	"reflect"
    20  	"testing"
    21  )
    22  
    23  func TestSequenceLexiconAdd(t *testing.T) {
    24  	tests := []struct {
    25  		have []int32
    26  		want int32
    27  	}{
    28  		{have: []int32{}, want: 0},
    29  		{have: []int32{5}, want: 1},
    30  		{have: []int32{}, want: 0},
    31  		{have: []int32{5, 5}, want: 2},
    32  		{have: []int32{5, 0, -3}, want: 3},
    33  		{have: []int32{5}, want: 1},
    34  		{have: []int32{0x7fffffff}, want: 4},
    35  		{have: []int32{5, 0, -3}, want: 3},
    36  		{have: []int32{}, want: 0},
    37  	}
    38  
    39  	lex := newSequenceLexicon()
    40  	for _, test := range tests {
    41  		if got := lex.add(test.have); got != test.want {
    42  			t.Errorf("lexicon.add(%v) = %v, want %v", test.have, got, test.want)
    43  		}
    44  
    45  	}
    46  
    47  	if lex.size() != 5 {
    48  		t.Errorf("lexicon.size() = %v, want 5", lex.size())
    49  	}
    50  
    51  	for _, test := range tests {
    52  		if got := lex.sequence(test.want); !reflect.DeepEqual(got, test.have) {
    53  			t.Errorf("lexicon.sequence(%v) = %v, want %v", test.want, got, test.have)
    54  		}
    55  	}
    56  }
    57  
    58  func TestSequenceLexiconClear(t *testing.T) {
    59  	lex := newSequenceLexicon()
    60  
    61  	if got, want := lex.add([]int32{1}), int32(0); got != want {
    62  		t.Errorf("lex.add([]int32{1}) = %v, want %v", got, want)
    63  	}
    64  	if got, want := lex.add([]int32{2}), int32(1); got != want {
    65  		t.Errorf("lex.add(sequence{2}) = %v, want %v", got, want)
    66  	}
    67  	lex.clear()
    68  	if got, want := lex.add([]int32{2}), int32(0); got != want {
    69  		t.Errorf("lex.add([]int32{2}) = %v, want %v", got, want)
    70  	}
    71  	if got, want := lex.add([]int32{1}), int32(1); got != want {
    72  		t.Errorf("lex.add([]int32{1}) = %v, want %v", got, want)
    73  	}
    74  }
    75  
    76  func TestIDSetLexiconSingletonSets(t *testing.T) {
    77  	var m int32 = math.MaxInt32
    78  	tests := []struct {
    79  		have int32
    80  		want int32
    81  	}{
    82  		{5, 5},
    83  		{0, 0},
    84  		{1, 1},
    85  		{m, m},
    86  	}
    87  
    88  	lex := newIDSetLexicon()
    89  	// Test adding
    90  	for _, test := range tests {
    91  		if got := lex.add(test.have); got != test.want {
    92  			t.Errorf("lexicon.add(%v) = %v, want %v", test.have, got, test.want)
    93  		}
    94  	}
    95  
    96  	// Test recall
    97  	for _, test := range tests {
    98  		if got := lex.idSet(test.want); !reflect.DeepEqual(got, []int32{test.have}) {
    99  			t.Errorf("lexicon.idSet(%v) = %v, want %v", test.want, got, test.have)
   100  		}
   101  	}
   102  }
   103  
   104  func TestIDSetLexiconSetsAreSorted(t *testing.T) {
   105  	tests := []struct {
   106  		have []int32
   107  		want int32
   108  	}{
   109  		// This test relies on order of test cases to get the expected IDs.
   110  		{
   111  			have: []int32{2, 5},
   112  			want: ^0,
   113  		},
   114  		{
   115  			have: []int32{3, 2, 5},
   116  			want: ^1,
   117  		},
   118  		{
   119  			have: []int32{2, 2, 2, 2, 5, 2, 5},
   120  			want: ^0,
   121  		},
   122  		{
   123  			have: []int32{2, 5},
   124  			want: ^0,
   125  		},
   126  		{
   127  			have: []int32{5, 3, 2, 5},
   128  			want: ^1,
   129  		},
   130  	}
   131  
   132  	lexicon := newIDSetLexicon()
   133  	for _, test := range tests {
   134  		if got := lexicon.add(test.have...); got != test.want {
   135  			t.Errorf("lexicon.addSet(%v) = %v, want %v", test.have, got, test.want)
   136  		}
   137  	}
   138  
   139  	recallTests := []struct {
   140  		have int32
   141  		want []int32
   142  	}{
   143  		{
   144  			have: ^0,
   145  			want: []int32{2, 5},
   146  		},
   147  		{
   148  			have: ^1,
   149  			want: []int32{2, 3, 5},
   150  		},
   151  	}
   152  
   153  	for _, test := range recallTests {
   154  		if got := lexicon.idSet(test.have); !reflect.DeepEqual(got, test.want) {
   155  			t.Errorf("lexicon.idSet(%v) = %+v, want %+v", test.have, got, test.want)
   156  		}
   157  	}
   158  }
   159  
   160  func TestIDSetLexiconClear(t *testing.T) {
   161  	lex := newIDSetLexicon()
   162  
   163  	if got, want := lex.add(1, 2), int32(^0); got != want {
   164  		t.Errorf("lex.add([]int32{1, 2}) = %v, want %v", got, want)
   165  	}
   166  	if got, want := lex.add(3, 4), int32(^1); got != want {
   167  		t.Errorf("lex.add(sequence{3, 4}) = %v, want %v", got, want)
   168  	}
   169  	lex.clear()
   170  	if got, want := lex.add(3, 4), int32(^0); got != want {
   171  		t.Errorf("lex.add([]int32{3, 4}) = %v, want %v", got, want)
   172  	}
   173  	if got, want := lex.add(1, 2), int32(^1); got != want {
   174  		t.Errorf("lex.add([]int32{1, 2}) = %v, want %v", got, want)
   175  	}
   176  }
   177  
   178  // TODO(roberts): Differences from C++
   179  // Benchmarking methods.
   180  

View as plain text