...

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

Documentation: github.com/golang/geo/s2

     1  // Copyright 2018 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  	"testing"
    19  )
    20  
    21  func TestFindMSBSetNonZero64(t *testing.T) {
    22  	testOne := uint64(0x8000000000000000)
    23  	testAll := uint64(0xFFFFFFFFFFFFFFFF)
    24  	testSome := uint64(0xFEDCBA9876543210)
    25  	for i := 63; i >= 0; i-- {
    26  		if got := findMSBSetNonZero64(testOne); got != i {
    27  			t.Errorf("findMSBSetNonZero64(%x) = %d, want = %d", testOne, got, i)
    28  		}
    29  		if got := findMSBSetNonZero64(testAll); got != i {
    30  			t.Errorf("findMSBSetNonZero64(%x) = %d, want = %d", testAll, got, i)
    31  		}
    32  		if got := findMSBSetNonZero64(testSome); got != i {
    33  			t.Errorf("findMSBSetNonZero64(%x) = %d, want = %d", testSome, got, i)
    34  		}
    35  		testOne >>= 1
    36  		testAll >>= 1
    37  		testSome >>= 1
    38  	}
    39  
    40  	if got := findMSBSetNonZero64(1); got != 0 {
    41  		t.Errorf("findMSBSetNonZero64(1) = %v, want 0", got)
    42  	}
    43  
    44  	if got := findMSBSetNonZero64(0); got != 0 {
    45  		t.Errorf("findMSBSetNonZero64(0) = %v, want 0", got)
    46  	}
    47  }
    48  
    49  func TestFindLSBSetNonZero64(t *testing.T) {
    50  	testOne := uint64(0x0000000000000001)
    51  	testAll := uint64(0xFFFFFFFFFFFFFFFF)
    52  	testSome := uint64(0x0123456789ABCDEF)
    53  	for i := 0; i < 64; i++ {
    54  		if got := findLSBSetNonZero64(testOne); got != i {
    55  			t.Errorf("findLSBSetNonZero64(%x) = %d, want = %d", testOne, got, i)
    56  		}
    57  		if got := findLSBSetNonZero64(testAll); got != i {
    58  			t.Errorf("findLSBSetNonZero64(%x) = %d, want = %d", testAll, got, i)
    59  		}
    60  		if got := findLSBSetNonZero64(testSome); got != i {
    61  			t.Errorf("findLSBSetNonZero64(%x) = %d, want = %d", testSome, got, i)
    62  		}
    63  		testOne <<= 1
    64  		testAll <<= 1
    65  		testSome <<= 1
    66  	}
    67  
    68  	if got := findLSBSetNonZero64(0); got != 0 {
    69  		t.Errorf("findLSBSetNonZero64(0) = %v, want 0", got)
    70  	}
    71  }
    72  

View as plain text