consensus/utils/number_test.go

22 lines
423 B
Go
Raw Normal View History

2022-10-08 18:55:01 +00:00
package utils
import (
"testing"
)
2022-10-08 18:55:01 +00:00
func TestPreviousPowerOfTwo(t *testing.T) {
loopPath := func(x uint64) int {
//find closest low power of two
var cnt uint64
for cnt = 1; cnt <= x; cnt <<= 1 {
}
cnt >>= 1
return int(cnt)
}
for i := uint64(1); i < 65536; i++ {
if PreviousPowerOfTwo(i) != loopPath(i) {
t.Fatalf("expected %d, got %d for iteration %d", loopPath(i), PreviousPowerOfTwo(i), i)
}
}
}