github.com/fiatjaf/generic-ristretto@v0.0.1/sketch_test.go (about) 1 package ristretto 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 ) 8 9 func TestSketch(t *testing.T) { 10 defer func() { 11 require.NotNil(t, recover()) 12 }() 13 14 s := newCmSketch(5) 15 require.Equal(t, uint64(7), s.mask) 16 newCmSketch(0) 17 } 18 19 func TestSketchIncrement(t *testing.T) { 20 s := newCmSketch(16) 21 s.Increment(1) 22 s.Increment(5) 23 s.Increment(9) 24 for i := 0; i < cmDepth; i++ { 25 if s.rows[i].string() != s.rows[0].string() { 26 break 27 } 28 require.False(t, i == cmDepth-1, "identical rows, bad seeding") 29 } 30 } 31 32 func TestSketchEstimate(t *testing.T) { 33 s := newCmSketch(16) 34 s.Increment(1) 35 s.Increment(1) 36 require.Equal(t, int64(2), s.Estimate(1)) 37 require.Equal(t, int64(0), s.Estimate(0)) 38 } 39 40 func TestSketchReset(t *testing.T) { 41 s := newCmSketch(16) 42 s.Increment(1) 43 s.Increment(1) 44 s.Increment(1) 45 s.Increment(1) 46 s.Reset() 47 require.Equal(t, int64(2), s.Estimate(1)) 48 } 49 50 func TestSketchClear(t *testing.T) { 51 s := newCmSketch(16) 52 for i := 0; i < 16; i++ { 53 s.Increment(uint64(i)) 54 } 55 s.Clear() 56 for i := 0; i < 16; i++ { 57 require.Equal(t, int64(0), s.Estimate(uint64(i))) 58 } 59 } 60 61 func TestNext2Power(t *testing.T) { 62 sz := 12 << 30 63 szf := float64(sz) * 0.01 64 val := int64(szf) 65 t.Logf("szf = %.2f val = %d\n", szf, val) 66 pow := next2Power(val) 67 t.Logf("pow = %d. mult 4 = %d\n", pow, pow*4) 68 } 69 70 func BenchmarkSketchIncrement(b *testing.B) { 71 s := newCmSketch(16) 72 b.SetBytes(1) 73 for n := 0; n < b.N; n++ { 74 s.Increment(1) 75 } 76 } 77 78 func BenchmarkSketchEstimate(b *testing.B) { 79 s := newCmSketch(16) 80 s.Increment(1) 81 b.SetBytes(1) 82 for n := 0; n < b.N; n++ { 83 s.Estimate(1) 84 } 85 }