-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassert_test.go
78 lines (70 loc) · 1.17 KB
/
assert_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
package main
import (
"testing"
)
func assert(truth bool, msg string) {
if !truth {
panic(msg)
}
}
func BenchmarkAssertion(b *testing.B) {
b.Run(
"no assert",
benchmarkNoAssert,
)
b.Run(
"assert",
benchmarkAssert,
)
b.Run(
"assert(5)",
benchmarkAssert5,
)
b.Run(
"defer assert",
benchmarkDeferAssert,
)
}
var (
Truth = true
Truth2 = true
Truth3 = true
Truth4 = true
Truth5 = true
Out struct{}
)
func benchmarkNoAssert(b *testing.B) {
for n := 0; n < b.N; n++ {
func() {
Out = struct{}{}
}()
}
}
func benchmarkAssert(b *testing.B) {
for n := 0; n < b.N; n++ {
func() {
assert(Truth, "n must be larger than 0")
Out = struct{}{}
}()
}
}
func benchmarkAssert5(b *testing.B) {
for n := 0; n < b.N; n++ {
func() {
assert(Truth, "n must be larger than 0")
assert(Truth2, "n must be larger than 0")
assert(Truth3, "n must be larger than 0")
assert(Truth4, "n must be larger than 0")
assert(Truth5, "n must be larger than 0")
Out = struct{}{}
}()
}
}
func benchmarkDeferAssert(b *testing.B) {
for n := 0; n < b.N; n++ {
func() {
defer assert(Truth, "n must be larger than 0")
Out = struct{}{}
}()
}
}