github.com/sandwich-go/boost@v1.3.29/isnil/nil_test.go (about)

     1  package isnil
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  type foo struct{}
     9  
    10  func TestIsNil(t *testing.T) {
    11  	cases := []struct {
    12  		in  interface{}
    13  		exp bool
    14  	}{
    15  		{1, false},
    16  		{"nope", false},
    17  		{foo{}, false},
    18  		{&foo{}, false},
    19  		{nil, true},
    20  		{(*foo)(nil), true},
    21  		{(interface{})(nil), true},
    22  		{fmt.Stringer(nil), true},
    23  	}
    24  
    25  	for _, tcase := range cases {
    26  		if out := Check(tcase.in); out != tcase.exp {
    27  			if tcase.exp {
    28  				t.Errorf("Expected %++v to be nil", tcase.in)
    29  			} else {
    30  				t.Errorf("Expected %++v to not be nil", tcase.in)
    31  			}
    32  		}
    33  	}
    34  }
    35  
    36  func BenchmarkEqNilBasic(b *testing.B) {
    37  	var v *int
    38  	for i := 0; i < b.N; i++ {
    39  		_ = v != nil
    40  	}
    41  }
    42  
    43  func BenchmarkEqNilInterface(b *testing.B) {
    44  	var v interface{}
    45  	v = (*foo)(nil)
    46  	for i := 0; i < b.N; i++ {
    47  		_ = v != nil
    48  	}
    49  }
    50  
    51  func BenchmarkIsNilBasic(b *testing.B) {
    52  	var v *int
    53  	for i := 0; i < b.N; i++ {
    54  		Check(v)
    55  	}
    56  }
    57  
    58  func BenchmarkIsNilInterface(b *testing.B) {
    59  	var v interface{}
    60  	v = (*foo)(nil)
    61  	for i := 0; i < b.N; i++ {
    62  		Check(v)
    63  	}
    64  }
    65  
    66  func BenchmarkIsNilNil(b *testing.B) {
    67  	for i := 0; i < b.N; i++ {
    68  		Check(nil)
    69  	}
    70  }