aboutsummaryrefslogtreecommitdiff
path: root/internal/hash/hash_log2_test.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2019-01-16 01:07:38 +0100
committerDimitri Sokolyuk <demon@dim13.org>2019-01-16 01:07:38 +0100
commitba25b16a8182e291b6bcc6e4c230a5178ee21ae8 (patch)
tree40eab63a22778b98e9b7323208e641e72aaa47d0 /internal/hash/hash_log2_test.go
parent2603c5fa0799be938ce2979ac01ea72d7b9b6304 (diff)
add log2
Diffstat (limited to 'internal/hash/hash_log2_test.go')
-rw-r--r--internal/hash/hash_log2_test.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/internal/hash/hash_log2_test.go b/internal/hash/hash_log2_test.go
new file mode 100644
index 0000000..f4c8bf3
--- /dev/null
+++ b/internal/hash/hash_log2_test.go
@@ -0,0 +1,37 @@
+package hash
+
+import (
+ "fmt"
+ "testing"
+)
+
+func TestLog2(t *testing.T) {
+ testCases := []struct {
+ n, res 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.n, tc.res), func(t *testing.T) {
+ res := log2(tc.n)
+ if res != tc.res {
+ t.Errorf("got %v; want %v", res, tc.res)
+ }
+ })
+ }
+}
+
+func BenchmarkLog2(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ log2(1024)
+ }
+}