github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/internal/math_test.go (about)

     1  package internal
     2  
     3  import "testing"
     4  
     5  func TestNextPowerOfTwo(t *testing.T) {
     6  	data := []struct {
     7  		X    uint
     8  		Want uint
     9  	}{
    10  		{0, 1},
    11  		{1, 1},
    12  		{2, 2},
    13  		{3, 4},
    14  		{4, 4},
    15  		{5, 8},
    16  		{7, 8},
    17  		{8, 8},
    18  		{9, 16},
    19  		{1<<16 - 1, 1 << 16},
    20  		{1 << 16, 1 << 16},
    21  		{1<<16 + 1, 1 << 17},
    22  		{1<<32 - 1, 1 << 32},
    23  		{1 << 32, 1 << 32},
    24  		{1<<63 - 1, 1 << 63},
    25  		{1 << 63, 1 << 63},
    26  
    27  		// overflows
    28  		//{1<<63 + 1, 1 << 64},
    29  		//{1<<64 - 1, 1 << 64},
    30  		//{1 << 64, 1 << 64},
    31  	}
    32  	for _, c := range data {
    33  		got := NextPowerOfTwo(c.X)
    34  		if got != c.Want {
    35  			t.Fatalf("x= %v, want %v, got %v", c.X, c.Want, got)
    36  		}
    37  	}
    38  }