Skip to content

Commit 1605cb4

Browse files
committed
test: Add more benchmarking and fuzzing tests for the core library.
1 parent efbe1e3 commit 1605cb4

File tree

6 files changed

+450
-85
lines changed

6 files changed

+450
-85
lines changed

corefunc/cidr_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,25 @@ func TestCIDRContains(t *testing.T) { // lint:allow_complexity
5050
})
5151
}
5252
}
53+
54+
func BenchmarkCIDRContains(b *testing.B) {
55+
for name, tc := range testfixtures.NetCidrContainsTestTable {
56+
b.Run(name, func(b *testing.B) {
57+
for range b.N {
58+
_, _ = CIDRContains(tc.ContainerCidr, tc.ContainedIPOrCidr) // lint:allow_unhandled
59+
}
60+
})
61+
}
62+
}
63+
64+
func BenchmarkCIDRContainsParallel(b *testing.B) {
65+
for name, tc := range testfixtures.NetCidrContainsTestTable {
66+
b.Run(name, func(b *testing.B) {
67+
b.RunParallel(func(pb *testing.PB) {
68+
for pb.Next() {
69+
_, _ = CIDRContains(tc.ContainerCidr, tc.ContainedIPOrCidr) // lint:allow_unhandled
70+
}
71+
})
72+
})
73+
}
74+
}

corefunc/str_byte_length_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,42 @@ func TestStrByteLength(t *testing.T) { // lint:allow_complexity
5353
})
5454
}
5555
}
56+
57+
func BenchmarkStrByteLength(b *testing.B) {
58+
b.ReportAllocs()
59+
60+
for name, tc := range testfixtures.StrByteLengthTestTable {
61+
b.Run(name, func(b *testing.B) {
62+
b.ResetTimer()
63+
64+
for range b.N {
65+
_ = StrByteLength(tc.Input) // lint:allow_unhandled
66+
}
67+
})
68+
}
69+
}
70+
71+
func BenchmarkStrByteLengthParallel(b *testing.B) {
72+
b.ReportAllocs()
73+
74+
for name, tc := range testfixtures.StrByteLengthTestTable {
75+
b.Run(name, func(b *testing.B) {
76+
b.ResetTimer()
77+
b.RunParallel(func(pb *testing.PB) {
78+
for pb.Next() {
79+
_ = StrByteLength(tc.Input) // lint:allow_unhandled
80+
}
81+
})
82+
})
83+
}
84+
}
85+
86+
func FuzzStrByteLength(f *testing.F) {
87+
for _, tc := range testfixtures.StrByteLengthTestTable {
88+
f.Add(tc.Input)
89+
}
90+
91+
f.Fuzz(func(_ *testing.T, input string) {
92+
_ = StrByteLength(input) // lint:allow_unhandled
93+
})
94+
}

corefunc/strings_test.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,126 @@ func TestStrContains(t *testing.T) { // lint:allow_complexity
9393
})
9494
}
9595
}
96+
97+
func BenchmarkStrStartsWith(b *testing.B) {
98+
b.ReportAllocs()
99+
100+
for name, tc := range testfixtures.StrStartsWithTestTable {
101+
b.Run(name, func(b *testing.B) {
102+
b.ResetTimer()
103+
104+
for range b.N {
105+
_ = StrStartsWith(tc.Input, tc.Prefix) // lint:allow_unhandled
106+
}
107+
})
108+
}
109+
}
110+
111+
func BenchmarkStrEndsWith(b *testing.B) {
112+
b.ReportAllocs()
113+
114+
for name, tc := range testfixtures.StrEndsWithTestTable {
115+
b.Run(name, func(b *testing.B) {
116+
b.ResetTimer()
117+
118+
for range b.N {
119+
_ = StrEndsWith(tc.Input, tc.Suffix) // lint:allow_unhandled
120+
}
121+
})
122+
}
123+
}
124+
125+
func BenchmarkStrContains(b *testing.B) {
126+
b.ReportAllocs()
127+
128+
for name, tc := range testfixtures.StrContainsTestTable {
129+
b.Run(name, func(b *testing.B) {
130+
b.ResetTimer()
131+
132+
for range b.N {
133+
_ = StrContains(tc.Input, tc.Substr) // lint:allow_unhandled
134+
}
135+
})
136+
}
137+
}
138+
139+
func BenchmarkStrStartsWithParallel(b *testing.B) {
140+
b.ReportAllocs()
141+
142+
for name, tc := range testfixtures.StrStartsWithTestTable {
143+
b.Run(name, func(b *testing.B) {
144+
b.ResetTimer()
145+
b.RunParallel(func(pb *testing.PB) {
146+
for pb.Next() {
147+
_ = StrStartsWith(tc.Input, tc.Prefix) // lint:allow_unhandled
148+
}
149+
})
150+
})
151+
}
152+
}
153+
154+
func BenchmarkStrEndsWithParallel(b *testing.B) {
155+
b.ReportAllocs()
156+
157+
for name, tc := range testfixtures.StrEndsWithTestTable {
158+
b.Run(name, func(b *testing.B) {
159+
b.ResetTimer()
160+
b.RunParallel(func(pb *testing.PB) {
161+
for pb.Next() {
162+
_ = StrEndsWith(tc.Input, tc.Suffix) // lint:allow_unhandled
163+
}
164+
})
165+
})
166+
}
167+
}
168+
169+
func BenchmarkStrContainsParallel(b *testing.B) {
170+
b.ReportAllocs()
171+
172+
for name, tc := range testfixtures.StrContainsTestTable {
173+
b.Run(name, func(b *testing.B) {
174+
b.ResetTimer()
175+
b.RunParallel(func(pb *testing.PB) {
176+
for pb.Next() {
177+
_ = StrContains(tc.Input, tc.Substr) // lint:allow_unhandled
178+
}
179+
})
180+
})
181+
}
182+
}
183+
184+
func FuzzStrStartsWith(f *testing.F) {
185+
for _, tc := range testfixtures.StrStartsWithTestTable {
186+
f.Add(tc.Input, tc.Prefix)
187+
}
188+
189+
f.Fuzz(
190+
func(_ *testing.T, in, prefix string) {
191+
_ = StrStartsWith(in, prefix) // lint:allow_unhandled
192+
},
193+
)
194+
}
195+
196+
func FuzzStrEndsWith(f *testing.F) {
197+
for _, tc := range testfixtures.StrEndsWithTestTable {
198+
f.Add(tc.Input, tc.Suffix)
199+
}
200+
201+
f.Fuzz(
202+
func(_ *testing.T, in, suffix string) {
203+
_ = StrEndsWith(in, suffix) // lint:allow_unhandled
204+
},
205+
)
206+
}
207+
208+
func FuzzStrContains(f *testing.F) {
209+
for _, tc := range testfixtures.StrContainsTestTable {
210+
f.Add(tc.Input, tc.Substr)
211+
}
212+
213+
f.Fuzz(
214+
func(_ *testing.T, in, substr string) {
215+
_ = StrContains(in, substr) // lint:allow_unhandled
216+
},
217+
)
218+
}

corefunc/time_test.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,126 @@ func TestTimeMixedCompare(t *testing.T) {
119119
})
120120
}
121121
}
122+
123+
func BenchmarkTimeParse(b *testing.B) {
124+
b.ReportAllocs()
125+
126+
for name, tc := range testfixtures.TimeParseTestTable {
127+
b.Run(name, func(b *testing.B) {
128+
b.ResetTimer()
129+
130+
for range b.N {
131+
_, _ = TimeParse(tc.Input) // lint:allow_unhandled
132+
}
133+
})
134+
}
135+
}
136+
137+
func BenchmarkTimeParseParallel(b *testing.B) {
138+
b.ReportAllocs()
139+
140+
for name, tc := range testfixtures.TimeParseTestTable {
141+
b.Run(name, func(b *testing.B) {
142+
b.ResetTimer()
143+
b.RunParallel(func(pb *testing.PB) {
144+
for pb.Next() {
145+
_, _ = TimeParse(tc.Input) // lint:allow_unhandled
146+
}
147+
})
148+
})
149+
}
150+
}
151+
152+
func BenchmarkTimeCompare(b *testing.B) {
153+
b.ReportAllocs()
154+
155+
for name, tc := range testfixtures.TimeCompareStringTestTable {
156+
b.Run(name, func(b *testing.B) {
157+
b.ResetTimer()
158+
159+
for range b.N {
160+
_, _ = TimeCompare(tc.InputA, tc.InputB) // lint:allow_unhandled
161+
}
162+
})
163+
}
164+
}
165+
166+
func BenchmarkTimeCompareParallel(b *testing.B) {
167+
b.ReportAllocs()
168+
169+
for name, tc := range testfixtures.TimeCompareStringTestTable {
170+
b.Run(name, func(b *testing.B) {
171+
b.ResetTimer()
172+
b.RunParallel(func(pb *testing.PB) {
173+
for pb.Next() {
174+
_, _ = TimeCompare(tc.InputA, tc.InputB) // lint:allow_unhandled
175+
}
176+
})
177+
})
178+
}
179+
}
180+
181+
func BenchmarkTimeCompareMixed(b *testing.B) {
182+
b.ReportAllocs()
183+
184+
for name, tc := range testfixtures.TimeCompareMixedTestTable {
185+
b.Run(name, func(b *testing.B) {
186+
b.ResetTimer()
187+
188+
for range b.N {
189+
_, _ = TimeCompare(tc.InputA, tc.InputB) // lint:allow_unhandled
190+
}
191+
})
192+
}
193+
}
194+
195+
func BenchmarkTimeCompareMixedParallel(b *testing.B) {
196+
b.ReportAllocs()
197+
198+
for name, tc := range testfixtures.TimeCompareMixedTestTable {
199+
b.Run(name, func(b *testing.B) {
200+
b.ResetTimer()
201+
b.RunParallel(func(pb *testing.PB) {
202+
for pb.Next() {
203+
_, _ = TimeCompare(tc.InputA, tc.InputB) // lint:allow_unhandled
204+
}
205+
})
206+
})
207+
}
208+
}
209+
210+
func FuzzTimeParse(f *testing.F) {
211+
for _, tc := range testfixtures.TimeParseTestTable {
212+
f.Add(tc.Input)
213+
}
214+
215+
f.Fuzz(
216+
func(_ *testing.T, in string) {
217+
_, _ = TimeParse(in) // lint:allow_unhandled
218+
},
219+
)
220+
}
221+
222+
func FuzzTimeCompare(f *testing.F) {
223+
for _, tc := range testfixtures.TimeCompareStringTestTable {
224+
f.Add(tc.InputA, tc.InputB)
225+
}
226+
227+
f.Fuzz(
228+
func(_ *testing.T, inA, inB string) {
229+
_, _ = TimeCompare(inA, inB) // lint:allow_unhandled
230+
},
231+
)
232+
}
233+
234+
func FuzzTimeCompareMixed(f *testing.F) {
235+
for _, tc := range testfixtures.TimeCompareMixedTestTable {
236+
f.Add(tc.InputA, tc.InputB)
237+
}
238+
239+
f.Fuzz(
240+
func(_ *testing.T, inA string, inB int64) {
241+
_, _ = TimeCompare(inA, inB) // lint:allow_unhandled
242+
},
243+
)
244+
}

corefunc/toml_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,61 @@ func TestJSONtoTOML(t *testing.T) { // lint:allow_complexity
186186
})
187187
}
188188
}
189+
190+
func BenchmarkTOMLtoJSON(b *testing.B) {
191+
b.ReportAllocs()
192+
193+
for name, tc := range testfixtures.TOMLtoJSONTestTable {
194+
b.Run(name, func(b *testing.B) {
195+
b.ResetTimer()
196+
197+
for range b.N {
198+
_, _ = TOMLtoJSON(tc.Input) // lint:allow_unhandled
199+
}
200+
})
201+
}
202+
}
203+
204+
func BenchmarkJSONtoTOML(b *testing.B) {
205+
b.ReportAllocs()
206+
207+
for name, tc := range testfixtures.JSONtoTOMLTestTable {
208+
b.Run(name, func(b *testing.B) {
209+
b.ResetTimer()
210+
211+
for range b.N {
212+
_, _ = JSONtoTOML(tc.Input) // lint:allow_unhandled
213+
}
214+
})
215+
}
216+
}
217+
218+
func BenchmarkTOMLtoJSONParallel(b *testing.B) {
219+
b.ReportAllocs()
220+
221+
for name, tc := range testfixtures.TOMLtoJSONTestTable {
222+
b.Run(name, func(b *testing.B) {
223+
b.ResetTimer()
224+
b.RunParallel(func(pb *testing.PB) {
225+
for pb.Next() {
226+
_, _ = TOMLtoJSON(tc.Input) // lint:allow_unhandled
227+
}
228+
})
229+
})
230+
}
231+
}
232+
233+
func BenchmarkJSONtoTOMLParallel(b *testing.B) {
234+
b.ReportAllocs()
235+
236+
for name, tc := range testfixtures.JSONtoTOMLTestTable {
237+
b.Run(name, func(b *testing.B) {
238+
b.ResetTimer()
239+
b.RunParallel(func(pb *testing.PB) {
240+
for pb.Next() {
241+
_, _ = JSONtoTOML(tc.Input) // lint:allow_unhandled
242+
}
243+
})
244+
})
245+
}
246+
}

0 commit comments

Comments
 (0)