File: bits_test.go

package info (click to toggle)
golang-github-dgryski-go-bits 0.0~git20180112.bd8a69a-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 104 kB
  • sloc: asm: 24; makefile: 2
file content (50 lines) | stat: -rw-r--r-- 848 bytes parent folder | download | duplicates (3)
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
43
44
45
46
47
48
49
50
package bits

import (
	"testing"
	"testing/quick"
)

func testQuick(t *testing.T, which string, ffast, fslow func(x uint64) uint64) {
	f := func(x uint64) bool {
		return ffast(x) == fslow(x)
	}
	if err := quick.Check(f, nil); err != nil {
		t.Errorf("fast%v != slow%v: %v: ", which, which, err)
	}
}

func ctzSlow(x uint64) uint64 {
	var n uint64
	for x&1 == 0 {
		n++
		x >>= 1
	}
	return n
}

func TestQuickCtz(t *testing.T) { testQuick(t, "ctz", Ctz, ctzSlow) }

func clzSlow(x uint64) uint64 {
	var n uint64
	for x&0x8000000000000000 == 0 {
		n++
		x <<= 1
	}
	return n
}

func TestQuickClz(t *testing.T) { testQuick(t, "clz", Clz, clzSlow) }

func popcntSlow(x uint64) uint64 {
	var n uint64
	for x != 0 {
		if x&1 == 1 {
			n++
		}
		x >>= 1
	}
	return n
}

func TestQuickPopcnt(t *testing.T) { testQuick(t, "popcnt", Popcnt, popcntSlow) }