github.com/xgzlucario/GigaCache@v0.0.0-20240508025442-54204e9c8a6b/cache_fuzz_test.go (about)

     1  package cache
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func FuzzCacheHashConflict(f *testing.F) {
    10  	m1 := make(map[string]string, 100*10000)
    11  
    12  	options := DefaultOptions
    13  	options.DisableEvict = true
    14  	options.HashFn = func(s string) uint64 {
    15  		return uint64(uint16(MemHash(s)))
    16  	}
    17  	m2 := New(options)
    18  
    19  	f.Fuzz(func(t *testing.T, key string, val []byte) {
    20  		assert := assert.New(t)
    21  
    22  		// set
    23  		m1[key] = string(val)
    24  		m2.Set(key, val)
    25  
    26  		// check
    27  		var count int
    28  		for k, v := range m1 {
    29  			res, ts, ok := m2.Get(k)
    30  			assert.Equal(v, string(res))
    31  			assert.Equal(ts, int64(0))
    32  			assert.True(ok)
    33  
    34  			count++
    35  			if count > 10 {
    36  				break
    37  			}
    38  		}
    39  
    40  		assert.Equal(len(m1), m2.Stat().Len)
    41  	})
    42  }