-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
02-04_abs_test.go
150 lines (131 loc) · 2.56 KB
/
02-04_abs_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
package hd_test
import (
"fmt"
"math/rand/v2"
"testing"
hd "github.com/nikolaydubina/go-hackers-delight"
)
func ExampleAbs() {
fmt.Print(hd.Abs(-42))
// Output: 42
}
func ExampleNAbs() {
fmt.Print(hd.NAbs(-42))
// Output: -42
}
func ExampleAbsDiff() {
fmt.Print(hd.AbsDiff(1, 100))
// Output: 99
}
func abs[T hd.Integer](x T) T {
if x < 0 {
return -x
}
return x
}
func fuzzAbs[T hd.Signed](t *testing.T, x T) {
t.Run("abs", func(t *testing.T) {
got := []T{
hd.Abs(x),
hd.Abs2(x),
hd.Abs3(x),
hd.Abs4(x),
}
for i, v := range got {
if abs := abs(x); v != abs {
t.Error(i, "x", x, "exp", abs, "got", v)
}
}
})
t.Run("nabs", func(t *testing.T) {
got := []T{
hd.NAbs(x),
hd.NAbs2(x),
hd.NAbs3(x),
}
for i, v := range got {
if abs := abs(x); v != -abs {
t.Error(i, "x", x, "exp", -abs, "got", v)
}
}
})
}
func FuzzAbsNormal_int32(f *testing.F) {
for _, x := range fuzzInt32 {
f.Add(x)
}
f.Fuzz(fuzzAbs[int32])
}
func FuzzAbsNormal_int16(f *testing.F) { f.Fuzz(fuzzAbs[int16]) }
func FuzzAbsNormal_int64(f *testing.F) { f.Fuzz(fuzzAbs[int32]) }
func FuzzAbsDiffInt(f *testing.F) {
for _, x := range fuzzInt32 {
for _, y := range fuzzInt32 {
f.Add(x, y)
}
}
f.Fuzz(func(t *testing.T, x, y int32) {
vs := []int32{
hd.AbsDiff2(x, y),
}
for i, v := range vs {
if v != hd.AbsDiff(x, y) {
t.Error(i, x, y, v)
}
}
})
}
func FuzzAbsDiffUint(f *testing.F) {
for _, x := range fuzzUint32 {
for _, y := range fuzzUint32 {
f.Add(x, y)
}
}
f.Fuzz(func(t *testing.T, x, y uint32) {
ux, uy := uint64(x), uint64(y)
exp := uint32(max(ux, uy) - min(ux, uy))
if hd.AbsDiffUnsigned(x, y) != exp {
t.Error(x, y, hd.AbsDiffUnsigned(x, y), exp)
}
})
}
func BenchmarkAbs(b *testing.B) {
var out int32
var vals []int32
for i := 0; i < 1000; i++ {
a := rand.Int32()
b := rand.Int32()
vals = append(vals, a-b)
}
// info on random numbers
countPositive := 0
for _, v := range vals {
if v > 0 {
countPositive++
}
}
b.Logf("positive %d/%d = %.2f", countPositive, len(vals), float64(countPositive)/float64(len(vals)))
vs := []struct {
name string
f func(int32) int32
}{
{"basic", abs[int32]},
{"Abs", hd.Abs[int32]},
{"Abs2", hd.Abs2[int32]},
{"Abs3", hd.Abs3[int32]},
{"Abs4", hd.Abs4[int32]},
{"AbsFastMul", hd.AbsFastMul},
}
for _, v := range vs {
b.Run(v.name, func(b *testing.B) {
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
out = v.f(vals[j])
}
}
})
}
if (out*2 - out - out) != 0 {
b.Fatal("never")
}
}