github.com/benz9527/toy-box/algo@v0.0.0-20240221120937-66c0c6bd5abd/bit/bit_test.go (about)

     1  package bit
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"testing"
     6  )
     7  
     8  func TestRoundupPowOf2(t *testing.T) {
     9  	n := RoundupPowOf2(7)
    10  	assert.Equal(t, RoundupPowOf2ByCeil(7), n)
    11  	assert.Equal(t, RoundupPowOf2ByLoop(7), n)
    12  
    13  	n = RoundupPowOf2(10)
    14  	assert.Equal(t, RoundupPowOf2ByCeil(10), n)
    15  	assert.Equal(t, RoundupPowOf2ByLoop(10), n)
    16  
    17  	n = RoundupPowOf2(17)
    18  	assert.Equal(t, RoundupPowOf2ByCeil(17), n)
    19  	assert.Equal(t, RoundupPowOf2ByLoop(17), n)
    20  
    21  	n = RoundupPowOf2(127)
    22  	assert.Equal(t, RoundupPowOf2ByCeil(127), n)
    23  	assert.Equal(t, RoundupPowOf2ByLoop(127), n)
    24  }
    25  
    26  func TestCeilPowOf2(t *testing.T) {
    27  	n := CeilPowOf2(7)
    28  	assert.Equal(t, uint8(3), n)
    29  
    30  	n = CeilPowOf2(10)
    31  	assert.Equal(t, uint8(4), n)
    32  
    33  	n = CeilPowOf2(17)
    34  	assert.Equal(t, uint8(5), n)
    35  }
    36  
    37  func TestOneBitsConvert(t *testing.T) {
    38  	n := int8(-1)
    39  	assert.Equal(t, uint64(255), convert[int8](n))
    40  
    41  	n2 := int16(-1)
    42  	assert.Equal(t, uint64(65535), convert[int16](n2))
    43  
    44  }
    45  
    46  func TestHammingWeight(t *testing.T) {
    47  	n := 7
    48  	assert.Equal(t, uint8(3), HammingWeightBySWAR[int](n))
    49  	assert.Equal(t, uint8(3), HammingWeightBySWAR2[int](n))
    50  	assert.Equal(t, uint8(3), HammingWeightBySWAR3[int](n))
    51  	assert.Equal(t, uint8(3), HammingWeightByGroupCount[int](n))
    52  
    53  	n2 := int64(0)
    54  	assert.Equal(t, uint8(0), HammingWeightBySWAR[int64](n2))
    55  	assert.Equal(t, uint8(0), HammingWeightBySWAR2[int64](n2))
    56  	assert.Equal(t, uint8(0), HammingWeightBySWAR3[int64](n2))
    57  	assert.Equal(t, uint8(0), HammingWeightByGroupCount[int64](n2))
    58  
    59  	n3 := int8(-1)
    60  	assert.Equal(t, uint8(8), HammingWeightBySWAR[int8](n3))
    61  	assert.Equal(t, uint8(8), HammingWeightBySWAR2[int8](n3))
    62  	assert.Equal(t, uint8(8), HammingWeightBySWAR3[int8](n3))
    63  	assert.Equal(t, uint8(8), HammingWeightByGroupCount[int8](n3))
    64  
    65  	n4 := int16(-1)
    66  	assert.Equal(t, uint8(16), HammingWeightBySWAR[int16](n4))
    67  	assert.Equal(t, uint8(16), HammingWeightBySWAR2[int16](n4))
    68  	assert.Equal(t, uint8(16), HammingWeightBySWAR3[int16](n4))
    69  	assert.Equal(t, uint8(16), HammingWeightByGroupCount[int16](n4))
    70  
    71  	n5 := int32(-1)
    72  	assert.Equal(t, uint8(32), HammingWeightBySWAR[int32](n5))
    73  	assert.Equal(t, uint8(32), HammingWeightBySWAR2[int32](n5))
    74  	assert.Equal(t, uint8(32), HammingWeightBySWAR3[int32](n5))
    75  	assert.Equal(t, uint8(32), HammingWeightByGroupCount[int32](n5))
    76  
    77  	n6 := int64(-1)
    78  	assert.Equal(t, uint8(64), HammingWeightBySWAR[int64](n6))
    79  	assert.Equal(t, uint8(64), HammingWeightBySWAR2[int64](n6))
    80  	assert.Equal(t, uint8(64), HammingWeightBySWAR3[int64](n6))
    81  	assert.Equal(t, uint8(64), HammingWeightByGroupCount[int64](n6))
    82  
    83  }