aboutsummaryrefslogtreecommitdiff
path: root/internal/hash/log2_test.go
blob: c58e2132c0e06273e6be158d027e03c1d82590e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package hash

import (
	"fmt"
	"testing"
)

func TestLog2(t *testing.T) {
	testCases := []struct {
		num, want uint32
	}{
		{0, 0},
		{1, 0},
		{2, 1},
		{3, 2},
		{4, 2},
		{8, 3},
		{9, 4},
		{128, 7},
		{129, 8},
		{1024, 10},
	}
	for _, tc := range testCases {
		t.Run(fmt.Sprintf("log2(%v)=%v", tc.num, tc.want), func(t *testing.T) {
			got := log2(tc.num)
			if got != tc.want {
				t.Errorf("got %v, want %v", got, tc.want)
			}
		})
	}
}

func BenchmarkLog2(b *testing.B) {
	benchCases := []uint32{1, 1024}
	for _, bc := range benchCases {
		b.Run(fmt.Sprintf("log(%v)", bc), func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				log2(1024)
			}
		})
	}
}