github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/bit/scan_test.go (about)

     1  package bit
     2  
     3  import (
     4  	"math/rand"
     5  	"testing"
     6  )
     7  
     8  func TestScanPowers(t *testing.T) {
     9  	for shift := uint(0); shift < 64; shift += 1 {
    10  		sl := ScanLeft(1 << shift)
    11  		sr := ScanRight(1 << shift)
    12  		if sl != shift || sr != shift {
    13  			t.Errorf("fail %v: got %v, %v", shift, sl, sr)
    14  		}
    15  	}
    16  }
    17  
    18  func TestScanLeftSmall(t *testing.T) {
    19  	for i := 0; i < 1521; i += 1 {
    20  		x := uint64(i * 31)
    21  		got := ScanLeft(x)
    22  		exp := slowScanLeft(x)
    23  		if got != exp {
    24  			t.Errorf("failed %b: got %v exp %v", x, got, exp)
    25  			break
    26  		}
    27  	}
    28  }
    29  
    30  func TestScanLeftRandom(t *testing.T) {
    31  	for i := 0; i < 1521; i += 1 {
    32  		x := uint64(rand.Int63())
    33  		got := ScanLeft(x)
    34  		exp := slowScanLeft(x)
    35  		if got != exp {
    36  			t.Errorf("failed %b: got %v exp %v", x, got, exp)
    37  			break
    38  		}
    39  	}
    40  }
    41  
    42  func TestScanRightSmall(t *testing.T) {
    43  	for i := 0; i < 1521; i += 1 {
    44  		x := uint64(i * 31)
    45  		got := ScanRight(x)
    46  		exp := slowScanRight(x)
    47  		if got != exp {
    48  			t.Errorf("failed %b: got %v exp %v", x, got, exp)
    49  			break
    50  		}
    51  	}
    52  }
    53  
    54  func TestScanRightRandom(t *testing.T) {
    55  	for i := 0; i < 1521; i += 1 {
    56  		x := uint64(rand.Int63())
    57  		got := ScanRight(x)
    58  		exp := slowScanRight(x)
    59  		if got != exp {
    60  			t.Errorf("failed %b: got %v exp %v", x, got, exp)
    61  			break
    62  		}
    63  	}
    64  }