-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path02-12_comparison_test.go
157 lines (142 loc) · 3.13 KB
/
02-12_comparison_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package hd_test
import (
"fmt"
"testing"
hd "github.com/nikolaydubina/go-hackers-delight"
)
func ExampleEqual_equal() {
fmt.Println(hd.Equal(10, 10) >> 31)
// Output: -1
}
func ExampleEqual_not_equal() {
fmt.Println(hd.Equal(10, 11) >> 31)
// Output: 0
}
func FuzzCompareUint32(f *testing.F) {
for _, x := range fuzzUint32 {
for _, y := range fuzzUint32 {
f.Add(x, y)
}
}
f.Fuzz(func(t *testing.T, x, y uint32) {
v := []struct {
got uint32
exp bool
}{
{hd.Equal5(x, y), x == y},
{hd.NotEqual3(x, y), x != y},
{hd.EqualZero4(x), x == 0},
{hd.NotEqualZero3(x), x != 0},
{hd.Less4(x, y), x < y},
{hd.LessUnsigned(x, y), x < y},
{hd.LessUnsigned2(x, y), x < y},
{hd.LessOrEqualUnsigned(x, y), x <= y},
}
for i, q := range v {
if hd.IsMostSignificantSet(q.got) != q.exp {
t.Error(i, x)
}
}
})
}
func fuzzCompare[T hd.Signed](t *testing.T, x, y T) {
v := []struct {
got T
exp bool
}{
{hd.Equal(x, y), x == y},
{hd.Equal5(x, y), x == y},
{hd.EqualZero5(x), x == 0},
{hd.NotEqual(x, y), x != y},
{hd.NotEqual3(x, y), x != y},
{hd.NotEqualZero3(x), x != 0},
{hd.Less(x, y), x < y},
{hd.Less2(x, y), x < y},
{hd.Less3(x, y), x < y},
{hd.Less4(x, y), x < y},
{hd.LessOrEqual(x, y), x <= y},
{hd.LessOrEqual2(x, y), x <= y},
}
for i, q := range v {
if (q.got < 0) != q.exp {
t.Error(i, x)
}
}
}
func FuzzCompare_int32(f *testing.F) {
for _, x := range fuzzInt32 {
for _, y := range fuzzInt32 {
f.Add(x, y)
}
}
f.Fuzz(func(t *testing.T, x, y int32) {
fuzzCompare(t, x, y)
// int32 only code
got := []struct {
got int32
exp bool
}{
{hd.Equal2(x, y), x == y},
{hd.Equal3(x, y), x == y},
{hd.Equal4(x, y), x == y},
{hd.NotEqual2(x, y), x != y},
{hd.NotEqualZero4(x), x != 0},
}
for i, q := range got {
if (q.got < 0) != q.exp {
t.Error(i, x)
}
}
})
}
func FuzzCompare_int16(f *testing.F) { f.Fuzz(fuzzCompare[int64]) }
func FuzzCompare_int64(f *testing.F) { f.Fuzz(fuzzCompare[int64]) }
func fuzzCompareZero[T hd.Signed](t *testing.T, x T) {
got := []struct {
got T
exp bool
}{
{hd.EqualZero(x), x == 0},
{hd.EqualZero4(x), x == 0},
{hd.EqualZero5(x), x == 0},
{hd.NotEqualZero(x), x != 0},
{hd.NotEqualZero3(x), x != 0},
{hd.LessZero(x), x < 0},
{hd.LessOrEqualZero(x), x <= 0},
{hd.LessOrEqualZero2(x), x <= 0},
{hd.HigherZero(x), x > 0},
{hd.HigherZero2(x), x > 0},
{hd.HigherZero3(x), x > 0},
{hd.HigherEqualZero(x), x >= 0},
}
for i, q := range got {
if (q.got < 0) != q.exp {
t.Error(i, x)
}
}
}
func FuzzCompareZero_int32(f *testing.F) {
for _, v := range fuzzInt32 {
f.Add(v)
}
f.Fuzz(func(t *testing.T, x int32) {
fuzzCompareZero(t, x)
// int32 only code
got := []struct {
got int32
exp bool
}{
{hd.EqualZero2(x), x == 0},
{hd.EqualZero3(x), x == 0},
{hd.NotEqualZero2(x), x != 0},
{hd.NotEqualZero4(x), x != 0},
}
for i, q := range got {
if (q.got < 0) != q.exp {
t.Error(i, x)
}
}
})
}
func FuzzCompareZero_int16(f *testing.F) { f.Fuzz(fuzzCompareZero[int16]) }
func FuzzCompareZero_int64(f *testing.F) { f.Fuzz(fuzzCompareZero[int64]) }