-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbenchmark_alternation_test.go
More file actions
88 lines (76 loc) · 2.29 KB
/
benchmark_alternation_test.go
File metadata and controls
88 lines (76 loc) · 2.29 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
package coregex
import (
"regexp"
"testing"
)
// Benchmarks for anchored alternation patterns (ID validation, UUID, hex).
// Common enterprise patterns: ^(\d+|UUID|hex32)$
var anchoredAltPattern = `^(\d+|[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}|[0-9a-fA-F]{32})$`
var anchoredAltInputs = []string{
"12345", // matches \d+
"550e8400-e29b-41d4-a716-446655440000", // matches UUID
"550e8400e29b41d4a716446655440000", // matches hex32
"not-a-match", // no match
"12345-extra", // no match
"abc", // no match
}
func BenchmarkAnchoredAlt_UUID_GoStdlib(b *testing.B) {
re := regexp.MustCompile(anchoredAltPattern)
input := []byte("550e8400-e29b-41d4-a716-446655440000")
b.ResetTimer()
for i := 0; i < b.N; i++ {
re.Match(input)
}
}
func BenchmarkAnchoredAlt_UUID_Coregex(b *testing.B) {
re := MustCompile(anchoredAltPattern)
input := []byte("550e8400-e29b-41d4-a716-446655440000")
b.ResetTimer()
for i := 0; i < b.N; i++ {
re.Match(input)
}
}
func BenchmarkAnchoredAlt_NoMatch_GoStdlib(b *testing.B) {
re := regexp.MustCompile(anchoredAltPattern)
input := []byte("not-a-match-at-all")
b.ResetTimer()
for i := 0; i < b.N; i++ {
re.Match(input)
}
}
func BenchmarkAnchoredAlt_NoMatch_Coregex(b *testing.B) {
re := MustCompile(anchoredAltPattern)
input := []byte("not-a-match-at-all")
b.ResetTimer()
for i := 0; i < b.N; i++ {
re.Match(input)
}
}
func BenchmarkAnchoredAlt_Digits_GoStdlib(b *testing.B) {
re := regexp.MustCompile(anchoredAltPattern)
input := []byte("12345")
b.ResetTimer()
for i := 0; i < b.N; i++ {
re.Match(input)
}
}
func BenchmarkAnchoredAlt_Digits_Coregex(b *testing.B) {
re := MustCompile(anchoredAltPattern)
input := []byte("12345")
b.ResetTimer()
for i := 0; i < b.N; i++ {
re.Match(input)
}
}
// TestAnchoredAltCorrectness verifies coregex matches the same as stdlib
func TestAnchoredAltCorrectness(t *testing.T) {
re := regexp.MustCompile(anchoredAltPattern)
cre := MustCompile(anchoredAltPattern)
for _, input := range anchoredAltInputs {
stdMatch := re.MatchString(input)
coreMatch := cre.MatchString(input)
if stdMatch != coreMatch {
t.Errorf("input %q: stdlib=%v, coregex=%v", input, stdMatch, coreMatch)
}
}
}