-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompose_test.go
More file actions
100 lines (81 loc) · 2.16 KB
/
Copy pathcompose_test.go
File metadata and controls
100 lines (81 loc) · 2.16 KB
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
package compose
import (
"github.com/stretchr/testify/assert"
"strconv"
"testing"
)
func TestCompose(t *testing.T) {
fn := Must(intToStr, strToInt).(func(int) int)
assert.Equal(t, 10, fn(10))
}
func TestVariadic(t *testing.T) {
minus := Must(argVardMinusVard, argVard).(func(string) (string, int))
s, i := minus("foo")
assert.Equal(t, "foo minus argVard", s)
assert.Equal(t, 0, i)
lit := Must(argVardLit, argVard).(func(string, int) (string, int))
s, i = lit("foo", 7)
assert.Equal(t, "foo lit argVard", s)
assert.Equal(t, 7, i)
plus := Must(argVardPlus, argVard).(func(string, int, int) (string, int))
s, i = plus("foo", 8, 9)
assert.Equal(t, "foo plus argVard", s)
assert.Equal(t, 17, i)
slice := Must(argVardSlice, argVard).(func(string, []int) (string, int))
s, i = slice("foo", []int{10, 11, 12})
assert.Equal(t, "foo slice argVard", s)
assert.Equal(t, 33, i)
emptyToJustVard := Must(empty, justVard).(func() []int)
assert.Equal(t, []int{}, emptyToJustVard())
}
func intToStr(x int) string {
return strconv.Itoa(x)
}
func strToInt(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return i
}
func argVard(a string, b ...int) (string, int) {
var s int
for _, i := range b {
s += i
}
return a + " argVard", s
}
func argVardMinusVard(a string) string {
return a + " minus"
}
func argVardLit(a string, b int) (string, int) {
return a + " lit", b
}
func argVardPlus(a string, b, c int) (string, int, int) {
return a + " plus", b, c
}
func argVardSlice(a string, b []int) (string, []int) {
return a + " slice", b
}
func justVard(a ...int) []int {
return a
}
func empty() {}
// I initially had very simple math operations (either returning the value given
// or doubling it), but I think those were getting in-lined, which was a real
// worse case scenario. Casting to a string and back to an int is a pretty
// small overhead, but enough to keep it from inlining.
func BenchmarkCompose(b *testing.B) {
fn := Must(intToStr, strToInt).(func(int) int)
for n := 0; n < b.N; n++ {
fn(n)
}
}
func BenchmarkCompile(b *testing.B) {
fn := func(x int) int {
return strToInt(intToStr(x))
}
for n := 0; n < b.N; n++ {
fn(n)
}
}