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

     1  package bit
     2  
     3  import (
     4  	"math/rand"
     5  	"testing"
     6  )
     7  
     8  func TestReverseRandom(t *testing.T) {
     9  	for i := 0; i < 1521; i += 1 {
    10  		width := uint(rand.Intn(61) + 1)
    11  		x := uint64(rand.Intn(1<<width - 1))
    12  		rx := Reverse(x, width)
    13  		rrx := Reverse(rx, width)
    14  		if rrx != x {
    15  			t.Errorf("failed <%v,%v>:  %v -> %v -> %v", x, width, x, rx, rrx)
    16  		}
    17  	}
    18  }
    19  
    20  func TestReversePowers(t *testing.T) {
    21  	for width := uint(2); width < 8; width += 1 {
    22  		exp := uint64(1 << (width - 1))
    23  		got := Reverse(1, width)
    24  		if exp != got {
    25  			t.Errorf("fail <1,%v>: got %v exp %v", width, got, exp)
    26  		}
    27  	}
    28  }
    29  
    30  func TestReverseOracle(t *testing.T) {
    31  	for i := 0; i < 1521; i += 1 {
    32  		width := uint(rand.Intn(61) + 1)
    33  		x := uint64(rand.Intn(1<<width - 1))
    34  		rx := Reverse(x, width)
    35  		rx2 := slowReverse(x, width)
    36  		if rx != rx2 {
    37  			t.Errorf("failed <%v,%v>: got %v exp %v", x, width, rx, rx2)
    38  		}
    39  	}
    40  }