Skip to content

Commit 4fc7ffd

Browse files
committed
rest of the assert inline optimized + benches
1 parent ca6dc12 commit 4fc7ffd

File tree

2 files changed

+139
-22
lines changed

2 files changed

+139
-22
lines changed

assert/assert.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -529,11 +529,15 @@ func Longer(s string, length int, a ...any) {
529529
l := len(s)
530530

531531
if l <= length {
532-
defMsg := fmt.Sprintf(assertionMsg+gotWantLongerFmt, l, length)
533-
current().reportAssertionFault(0, defMsg, a)
532+
doLonger(l, length, a)
534533
}
535534
}
536535

536+
func doLonger(l int, length int, a []any) {
537+
defMsg := fmt.Sprintf(assertionMsg+gotWantLongerFmt, l, length)
538+
current().reportAssertionFault(1, defMsg, a)
539+
}
540+
537541
// Shorter asserts that the length of the string is shorter to the given. If not
538542
// it panics/errors (according the current Asserter) with the auto-generated
539543
// message. You can append the generated got-want message by using optional
@@ -548,11 +552,15 @@ func Shorter(str string, length int, a ...any) {
548552
l := len(str)
549553

550554
if l >= length {
551-
defMsg := fmt.Sprintf(assertionMsg+gotWantShorterFmt, l, length)
552-
current().reportAssertionFault(0, defMsg, a)
555+
doShorter(l, length, a)
553556
}
554557
}
555558

559+
func doShorter(l int, length int, a []any) {
560+
defMsg := fmt.Sprintf(assertionMsg+gotWantShorterFmt, l, length)
561+
current().reportAssertionFault(1, defMsg, a)
562+
}
563+
556564
// SLen asserts that the length of the slice is equal to the given. If not it
557565
// panics/errors (according the current Asserter) with the auto-generated
558566
// message. You can append the generated got-want message by using optional
@@ -585,8 +593,7 @@ func SLonger[S ~[]T, T any](obj S, length int, a ...any) {
585593
l := len(obj)
586594

587595
if l <= length {
588-
defMsg := fmt.Sprintf(assertionMsg+gotWantLongerFmt, l, length)
589-
current().reportAssertionFault(0, defMsg, a)
596+
doLonger(l, length, a)
590597
}
591598
}
592599

@@ -604,8 +611,7 @@ func SShorter[S ~[]T, T any](obj S, length int, a ...any) {
604611
l := len(obj)
605612

606613
if l >= length {
607-
defMsg := fmt.Sprintf(assertionMsg+gotWantShorterFmt, l, length)
608-
current().reportAssertionFault(0, defMsg, a)
614+
doShorter(l, length, a)
609615
}
610616
}
611617

@@ -641,8 +647,7 @@ func MLonger[M ~map[T]U, T comparable, U any](obj M, length int, a ...any) {
641647
l := len(obj)
642648

643649
if l <= length {
644-
defMsg := fmt.Sprintf(assertionMsg+gotWantLongerFmt, l, length)
645-
current().reportAssertionFault(0, defMsg, a)
650+
doLonger(l, length, a)
646651
}
647652
}
648653

@@ -660,8 +665,7 @@ func MShorter[M ~map[T]U, T comparable, U any](obj M, length int, a ...any) {
660665
l := len(obj)
661666

662667
if l >= length {
663-
defMsg := fmt.Sprintf(assertionMsg+gotWantShorterFmt, l, length)
664-
current().reportAssertionFault(0, defMsg, a)
668+
doShorter(l, length, a)
665669
}
666670
}
667671

@@ -679,8 +683,7 @@ func CLen[C ~chan T, T any](obj C, length int, a ...any) {
679683
l := len(obj)
680684

681685
if l != length {
682-
defMsg := fmt.Sprintf(assertionMsg+gotWantFmt, l, length)
683-
current().reportAssertionFault(0, defMsg, a)
686+
doShouldBeEqual(l, length, a)
684687
}
685688
}
686689

@@ -698,8 +701,7 @@ func CLonger[C ~chan T, T any](obj C, length int, a ...any) {
698701
l := len(obj)
699702

700703
if l <= length {
701-
defMsg := fmt.Sprintf(assertionMsg+gotWantLongerFmt, l, length)
702-
current().reportAssertionFault(0, defMsg, a)
704+
doLonger(l, length, a)
703705
}
704706
}
705707

@@ -717,8 +719,7 @@ func CShorter[C ~chan T, T any](obj C, length int, a ...any) {
717719
l := len(obj)
718720

719721
if l >= length {
720-
defMsg := fmt.Sprintf(assertionMsg+gotWantShorterFmt, l, length)
721-
current().reportAssertionFault(0, defMsg, a)
722+
doShorter(l, length, a)
722723
}
723724
}
724725

@@ -737,7 +738,7 @@ func MKeyExists[M ~map[T]U, T comparable, U any](obj M, key T, a ...any) (val U)
737738
return val
738739
}
739740

740-
func doMKeyExists[T comparable](key T, a []any) {
741+
func doMKeyExists(key any, a []any) {
741742
defMsg := fmt.Sprintf(assertionMsg+": key '%v' doesn't exist", key)
742743
current().reportAssertionFault(1, defMsg, a)
743744
}

assert/assert_test.go

Lines changed: 119 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,20 @@ func BenchmarkEmpty(b *testing.B) {
269269
}
270270
}
271271

272+
func BenchmarkLonger(b *testing.B) {
273+
bs := "tst"
274+
for n := 0; n < b.N; n++ {
275+
assert.Longer(bs, 2)
276+
}
277+
}
278+
279+
func BenchmarkShorter(b *testing.B) {
280+
bs := "1"
281+
for n := 0; n < b.N; n++ {
282+
assert.Shorter(bs, 2)
283+
}
284+
}
285+
272286
func BenchmarkSEmpty(b *testing.B) {
273287
bs := []int{}
274288
for n := 0; n < b.N; n++ {
@@ -290,13 +304,69 @@ func BenchmarkSNotNil(b *testing.B) {
290304
}
291305
}
292306

307+
func BenchmarkMNotNil(b *testing.B) {
308+
var bs = map[int]int{0: 0}
309+
for n := 0; n < b.N; n++ {
310+
assert.MNotNil(bs)
311+
}
312+
}
313+
314+
func BenchmarkCNotNil(b *testing.B) {
315+
var bs = make(chan int)
316+
for n := 0; n < b.N; n++ {
317+
assert.CNotNil(bs)
318+
}
319+
}
320+
321+
func BenchmarkINotNil(b *testing.B) {
322+
var bs any = err2.ErrNotAccess
323+
for n := 0; n < b.N; n++ {
324+
assert.INotNil(bs)
325+
}
326+
}
327+
328+
func BenchmarkINil(b *testing.B) {
329+
var bs any
330+
for n := 0; n < b.N; n++ {
331+
assert.INil(bs)
332+
}
333+
}
334+
335+
func BenchmarkNil(b *testing.B) {
336+
var bs *int
337+
for n := 0; n < b.N; n++ {
338+
assert.Nil(bs)
339+
}
340+
}
341+
293342
func BenchmarkNotNil(b *testing.B) {
294343
bs := new(int)
295344
for n := 0; n < b.N; n++ {
296345
assert.NotNil(bs)
297346
}
298347
}
299348

349+
func BenchmarkSNil(b *testing.B) {
350+
var bs []int
351+
for n := 0; n < b.N; n++ {
352+
assert.SNil(bs)
353+
}
354+
}
355+
356+
func BenchmarkMNil(b *testing.B) {
357+
var bs map[int]int
358+
for n := 0; n < b.N; n++ {
359+
assert.MNil(bs)
360+
}
361+
}
362+
363+
func BenchmarkCNil(b *testing.B) {
364+
var bs chan int
365+
for n := 0; n < b.N; n++ {
366+
assert.CNil(bs)
367+
}
368+
}
369+
300370
func BenchmarkThat(b *testing.B) {
301371
const four = 4
302372
for n := 0; n < b.N; n++ {
@@ -359,22 +429,68 @@ func BenchmarkMLen(b *testing.B) {
359429
}
360430
}
361431

432+
func BenchmarkMShorter(b *testing.B) {
433+
d := map[byte]byte{1: 1, 2: 2}
434+
for n := 0; n < b.N; n++ {
435+
assert.MShorter(d, 4)
436+
}
437+
}
438+
439+
func BenchmarkMLonger(b *testing.B) {
440+
d := map[byte]byte{1: 1, 2: 2}
441+
for n := 0; n < b.N; n++ {
442+
assert.MLonger(d, 1)
443+
}
444+
}
445+
362446
func BenchmarkSLen(b *testing.B) {
363447
d := []byte{1, 2}
364448
for n := 0; n < b.N; n++ {
365449
assert.SLen(d, 2)
366450
}
367451
}
368452

453+
func BenchmarkSShorter(b *testing.B) {
454+
d := []byte{1, 2}
455+
for n := 0; n < b.N; n++ {
456+
assert.SShorter(d, 3)
457+
}
458+
}
459+
460+
func BenchmarkSLonger(b *testing.B) {
461+
d := []byte{1, 2}
462+
for n := 0; n < b.N; n++ {
463+
assert.SLonger(d, 1)
464+
}
465+
}
466+
369467
func BenchmarkCLen(b *testing.B) {
370-
d := make(chan byte, 2)
371-
d <- byte(1)
372-
d <- byte(1)
468+
d := make(chan int, 2)
469+
d <- int(1)
470+
d <- int(1)
373471
for n := 0; n < b.N; n++ {
374472
assert.CLen(d, 2)
375473
}
376474
}
377475

476+
func BenchmarkCShorter(b *testing.B) {
477+
d := make(chan int, 2)
478+
d <- int(1)
479+
d <- int(1)
480+
for n := 0; n < b.N; n++ {
481+
assert.CShorter(d, 3)
482+
}
483+
}
484+
485+
func BenchmarkCLonger(b *testing.B) {
486+
d := make(chan int, 2)
487+
d <- int(1)
488+
d <- int(1)
489+
for n := 0; n < b.N; n++ {
490+
assert.CLonger(d, 1)
491+
}
492+
}
493+
378494
func BenchmarkLen(b *testing.B) {
379495
s := "len"
380496
for n := 0; n < b.N; n++ {

0 commit comments

Comments
 (0)