github.com/sandwich-go/boost@v1.3.29/z/hash_test.go (about)

     1  package z
     2  
     3  import (
     4  	. "github.com/smartystreets/goconvey/convey"
     5  	"testing"
     6  )
     7  
     8  type hasher struct{}
     9  
    10  func (h hasher) HashUint64() uint64 { return 1 }
    11  
    12  type stringer struct{}
    13  
    14  func (h stringer) String() string { return "1" }
    15  
    16  func TestHash(t *testing.T) {
    17  	Convey("hash", t, func() {
    18  		s := `a {{ .val1 }} {{ .val2 }}`
    19  		s1 := MemHash([]byte(s))
    20  		s2 := MemHashString(s)
    21  		So(s1, ShouldEqual, s2)
    22  
    23  		for _, test := range []struct {
    24  			v     interface{}
    25  			hv    uint64
    26  			panic bool
    27  		}{
    28  			{v: nil, hv: 0}, {v: int8(4), hv: 4}, {v: int16(5), hv: 5}, {v: int32(6), hv: 6}, {v: int64(8), hv: 8}, {v: 3, hv: 3},
    29  			{v: byte(2), hv: 2}, {v: uint16(9), hv: 9}, {v: uint64(1), hv: 1}, {v: uint32(7), hv: 7}, {v: uint(10), hv: 10}, {v: uint8(11), hv: 11},
    30  			{v: hasher{}, hv: 1}, {v: stringer{}, hv: MemHashString("1")}, {v: []byte(s), hv: s1}, {v: s, hv: s1},
    31  			{v: float32(1), panic: true},
    32  		} {
    33  			if test.panic {
    34  				So(func() {
    35  					KeyToHash(test.v)
    36  				}, ShouldPanic)
    37  			} else {
    38  				So(KeyToHash(test.v), ShouldEqual, test.hv)
    39  			}
    40  		}
    41  	})
    42  }