-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path13-00_gray_code_test.go
52 lines (46 loc) · 1.08 KB
/
13-00_gray_code_test.go
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
51
52
package hd_test
import (
"fmt"
"testing"
hd "github.com/nikolaydubina/go-hackers-delight"
)
func ExampleGrayCodeFromUint() {
for i := range 10 {
fmt.Printf("%08b -> %08b\n", i, hd.GrayCodeFromUint(uint64(i)))
}
// Output:
// 00000000 -> 00000000
// 00000001 -> 00000001
// 00000010 -> 00000011
// 00000011 -> 00000010
// 00000100 -> 00000110
// 00000101 -> 00000111
// 00000110 -> 00000101
// 00000111 -> 00000100
// 00001000 -> 00001100
// 00001001 -> 00001101
}
func FuzzGrayCode(f *testing.F) {
for _, u := range fuzzUint64 {
f.Add(u)
}
f.Fuzz(func(t *testing.T, x uint64) {
t.Run("uint16", func(t *testing.T) {
x := uint16(x)
if xx := hd.GrayCodeToUint16(hd.GrayCodeFromUint(x)); x != xx {
t.Errorf("x=%d, xx=%d)", x, xx)
}
})
t.Run("uint32", func(t *testing.T) {
x := uint32(x)
if xx := hd.GrayCodeToUint32(hd.GrayCodeFromUint(x)); x != xx {
t.Errorf("x=%d, xx=%d)", x, xx)
}
})
t.Run("uint64", func(t *testing.T) {
if xx := hd.GrayCodeToUint64(hd.GrayCodeFromUint(x)); x != xx {
t.Errorf("x=%d, xx=%d)", x, xx)
}
})
})
}