-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench2_test.go
115 lines (101 loc) · 3.59 KB
/
bench2_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
// Copyright 2015-2016, Cyrill @ Schumacher.fm and the CoreStore contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package errors
import (
"errors"
"testing"
)
var benchmarkAsserted bool
type aExistsEStruct struct{}
func (a aExistsEStruct) Error() string { return "Err" }
func (a aExistsEStruct) ErrorKind() Kind { return AlreadyExists }
func BenchmarkAssertKindEmptyStruct(b *testing.B) {
ae := aExistsEStruct{}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchmarkAsserted = AlreadyExists.Match(ae)
if !benchmarkAsserted {
b.Errorf("Hell should already exists: %#v", ae)
}
}
}
type aExistsStr string
func (c aExistsStr) Error() string { return string(c) }
func (c aExistsStr) ErrorKind() Kind { return AlreadyExists }
func BenchmarkAssertKindConstant(b *testing.B) {
const hell aExistsStr = "Hell"
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchmarkAsserted = AlreadyExists.Match(hell)
if !benchmarkAsserted {
b.Error("Hell should already exists.")
}
}
}
func BenchmarkAssertKindPointer(b *testing.B) {
var hell error = AlreadyExists.New(errors.New("hell"), "There is already a place for you")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchmarkAsserted = AlreadyExists.Match(hell)
if !benchmarkAsserted {
b.Error("Hell should already exists.")
}
}
}
func BenchmarkAssertKindIFace(b *testing.B) {
var hell error = errTerminated{Kind: Terminated}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchmarkAsserted = Terminated.MatchInterface(hell)
if !benchmarkAsserted {
b.Error("Hell should ber terminated.")
}
}
}
func BenchmarkAssertKindNoMatch(b *testing.B) {
b.Run("type", func(b *testing.B) {
hell := AlreadyClosed.New(errors.New("hell"), "There is already a place for you")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchmarkAsserted = AlreadyExists.Match(hell)
if benchmarkAsserted {
b.Error("Hell should already be closed.")
}
}
})
b.Run("iface", func(b *testing.B) {
hell := errors.New("not matched")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchmarkAsserted = AlreadyCaptured.MatchInterface(hell)
if benchmarkAsserted {
b.Error("Hell should not match")
}
}
})
}
var benchmarkKindUnwrap Kinds
func BenchmarkKindUnwrap(b *testing.B) {
b.ReportAllocs()
const ek = Kind("Aborted|AlreadyCaptured|AlreadyClosed|AlreadyExists|AlreadyInUse|AlreadyRefunded|Blocked|ReadFailed|WriteFailed|VerificationFailed|DecryptionFailed|EncryptionFailed|ConnectionFailed|BadEncoding|ConnectionLost|Declined|Denied|Duplicated|NotEmpty|Empty|Exceeded|Exists|NotExists|Expired|Fatal|InProgress|Insufficient|Interrupted|IsDirectory|IsFile|NotDirectory|NotFile|Locked|Mismatch|NotAcceptable|NotAllowed|NotFound|NotImplemented|NotRecoverable|NotSupported|NotValid|Overflowed|PermissionDenied|Unauthorized|UserNotFound|QuotaExceeded|Rejected|Required|Restricted|Revoked|Temporary|Terminated|Timeout|TooLarge|Unavailable|WrongVersion|CorruptData|OutOfRange|OutOfDate|TooShort")
for i := 0; i < b.N; i++ {
benchmarkKindUnwrap = ek.Unwrap()
}
}