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