github.com/jackc/puddle/v2@v2.2.2-0.20240301145809-72b022bcfc59/log_test.go (about) 1 package puddle 2 3 import ( 4 "math" 5 "math/rand" 6 "testing" 7 "time" 8 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestLog2Uint(t *testing.T) { 13 r := require.New(t) 14 15 r.Equal(uint8(0), log2Int(1)) 16 r.Equal(uint8(0), log2Int[uint64](1)) 17 r.Equal(uint8(1), log2Int[uint32](2)) 18 r.Equal(uint8(7), log2Int[uint8](math.MaxUint8)) 19 r.Equal(uint8(15), log2Int[uint16](math.MaxUint16)) 20 r.Equal(uint8(31), log2Int[uint32](math.MaxUint32)) 21 r.Equal(uint8(63), log2Int[uint64](math.MaxUint64)) 22 23 r.Panics(func() { log2Int[uint64](0) }) 24 r.Panics(func() { log2Int[int64](-1) }) 25 } 26 27 func FuzzLog2Uint(f *testing.F) { 28 const cnt = 1000 29 30 rand := rand.New(rand.NewSource(time.Now().UnixNano())) 31 for i := 0; i < cnt; i++ { 32 val := uint64(rand.Int63()) 33 // val + 1 not to test val == 0. 34 f.Add(val + 1) 35 } 36 37 f.Fuzz(func(t *testing.T, val uint64) { 38 var mx uint8 39 for i := 63; i >= 0; i-- { 40 mask := uint64(1) << i 41 if mask&val != 0 { 42 mx = uint8(i) 43 break 44 } 45 } 46 47 require.Equal(t, mx, log2Int(val)) 48 }) 49 }