Skip to content

Commit

Permalink
rest of the assert inline optimized + benches
Browse files Browse the repository at this point in the history
  • Loading branch information
lainio committed Aug 22, 2024
1 parent ca6dc12 commit 4fc7ffd
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 22 deletions.
39 changes: 20 additions & 19 deletions assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,11 +529,15 @@ func Longer(s string, length int, a ...any) {
l := len(s)

if l <= length {
defMsg := fmt.Sprintf(assertionMsg+gotWantLongerFmt, l, length)
current().reportAssertionFault(0, defMsg, a)
doLonger(l, length, a)
}
}

func doLonger(l int, length int, a []any) {
defMsg := fmt.Sprintf(assertionMsg+gotWantLongerFmt, l, length)
current().reportAssertionFault(1, defMsg, a)
}

// Shorter asserts that the length of the string is shorter to the given. If not
// it panics/errors (according the current Asserter) with the auto-generated
// message. You can append the generated got-want message by using optional
Expand All @@ -548,11 +552,15 @@ func Shorter(str string, length int, a ...any) {
l := len(str)

if l >= length {
defMsg := fmt.Sprintf(assertionMsg+gotWantShorterFmt, l, length)
current().reportAssertionFault(0, defMsg, a)
doShorter(l, length, a)
}
}

func doShorter(l int, length int, a []any) {
defMsg := fmt.Sprintf(assertionMsg+gotWantShorterFmt, l, length)
current().reportAssertionFault(1, defMsg, a)
}

// SLen asserts that the length of the slice is equal to the given. If not it
// panics/errors (according the current Asserter) with the auto-generated
// message. You can append the generated got-want message by using optional
Expand Down Expand Up @@ -585,8 +593,7 @@ func SLonger[S ~[]T, T any](obj S, length int, a ...any) {
l := len(obj)

if l <= length {
defMsg := fmt.Sprintf(assertionMsg+gotWantLongerFmt, l, length)
current().reportAssertionFault(0, defMsg, a)
doLonger(l, length, a)
}
}

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

if l >= length {
defMsg := fmt.Sprintf(assertionMsg+gotWantShorterFmt, l, length)
current().reportAssertionFault(0, defMsg, a)
doShorter(l, length, a)
}
}

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

if l <= length {
defMsg := fmt.Sprintf(assertionMsg+gotWantLongerFmt, l, length)
current().reportAssertionFault(0, defMsg, a)
doLonger(l, length, a)
}
}

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

if l >= length {
defMsg := fmt.Sprintf(assertionMsg+gotWantShorterFmt, l, length)
current().reportAssertionFault(0, defMsg, a)
doShorter(l, length, a)
}
}

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

if l != length {
defMsg := fmt.Sprintf(assertionMsg+gotWantFmt, l, length)
current().reportAssertionFault(0, defMsg, a)
doShouldBeEqual(l, length, a)
}
}

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

if l <= length {
defMsg := fmt.Sprintf(assertionMsg+gotWantLongerFmt, l, length)
current().reportAssertionFault(0, defMsg, a)
doLonger(l, length, a)
}
}

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

if l >= length {
defMsg := fmt.Sprintf(assertionMsg+gotWantShorterFmt, l, length)
current().reportAssertionFault(0, defMsg, a)
doShorter(l, length, a)
}
}

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

func doMKeyExists[T comparable](key T, a []any) {
func doMKeyExists(key any, a []any) {
defMsg := fmt.Sprintf(assertionMsg+": key '%v' doesn't exist", key)
current().reportAssertionFault(1, defMsg, a)
}
Expand Down
122 changes: 119 additions & 3 deletions assert/assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,20 @@ func BenchmarkEmpty(b *testing.B) {
}
}

func BenchmarkLonger(b *testing.B) {
bs := "tst"
for n := 0; n < b.N; n++ {
assert.Longer(bs, 2)
}
}

func BenchmarkShorter(b *testing.B) {
bs := "1"
for n := 0; n < b.N; n++ {
assert.Shorter(bs, 2)
}
}

func BenchmarkSEmpty(b *testing.B) {
bs := []int{}
for n := 0; n < b.N; n++ {
Expand All @@ -290,13 +304,69 @@ func BenchmarkSNotNil(b *testing.B) {
}
}

func BenchmarkMNotNil(b *testing.B) {
var bs = map[int]int{0: 0}
for n := 0; n < b.N; n++ {
assert.MNotNil(bs)
}
}

func BenchmarkCNotNil(b *testing.B) {
var bs = make(chan int)
for n := 0; n < b.N; n++ {
assert.CNotNil(bs)
}
}

func BenchmarkINotNil(b *testing.B) {
var bs any = err2.ErrNotAccess
for n := 0; n < b.N; n++ {
assert.INotNil(bs)
}
}

func BenchmarkINil(b *testing.B) {
var bs any
for n := 0; n < b.N; n++ {
assert.INil(bs)
}
}

func BenchmarkNil(b *testing.B) {
var bs *int
for n := 0; n < b.N; n++ {
assert.Nil(bs)
}
}

func BenchmarkNotNil(b *testing.B) {
bs := new(int)
for n := 0; n < b.N; n++ {
assert.NotNil(bs)
}
}

func BenchmarkSNil(b *testing.B) {
var bs []int
for n := 0; n < b.N; n++ {
assert.SNil(bs)
}
}

func BenchmarkMNil(b *testing.B) {
var bs map[int]int
for n := 0; n < b.N; n++ {
assert.MNil(bs)
}
}

func BenchmarkCNil(b *testing.B) {
var bs chan int
for n := 0; n < b.N; n++ {
assert.CNil(bs)
}
}

func BenchmarkThat(b *testing.B) {
const four = 4
for n := 0; n < b.N; n++ {
Expand Down Expand Up @@ -359,22 +429,68 @@ func BenchmarkMLen(b *testing.B) {
}
}

func BenchmarkMShorter(b *testing.B) {
d := map[byte]byte{1: 1, 2: 2}
for n := 0; n < b.N; n++ {
assert.MShorter(d, 4)
}
}

func BenchmarkMLonger(b *testing.B) {
d := map[byte]byte{1: 1, 2: 2}
for n := 0; n < b.N; n++ {
assert.MLonger(d, 1)
}
}

func BenchmarkSLen(b *testing.B) {
d := []byte{1, 2}
for n := 0; n < b.N; n++ {
assert.SLen(d, 2)
}
}

func BenchmarkSShorter(b *testing.B) {
d := []byte{1, 2}
for n := 0; n < b.N; n++ {
assert.SShorter(d, 3)
}
}

func BenchmarkSLonger(b *testing.B) {
d := []byte{1, 2}
for n := 0; n < b.N; n++ {
assert.SLonger(d, 1)
}
}

func BenchmarkCLen(b *testing.B) {
d := make(chan byte, 2)
d <- byte(1)
d <- byte(1)
d := make(chan int, 2)
d <- int(1)
d <- int(1)
for n := 0; n < b.N; n++ {
assert.CLen(d, 2)
}
}

func BenchmarkCShorter(b *testing.B) {
d := make(chan int, 2)
d <- int(1)
d <- int(1)
for n := 0; n < b.N; n++ {
assert.CShorter(d, 3)
}
}

func BenchmarkCLonger(b *testing.B) {
d := make(chan int, 2)
d <- int(1)
d <- int(1)
for n := 0; n < b.N; n++ {
assert.CLonger(d, 1)
}
}

func BenchmarkLen(b *testing.B) {
s := "len"
for n := 0; n < b.N; n++ {
Expand Down

0 comments on commit 4fc7ffd

Please sign in to comment.