...

Source file src/github.com/dgraph-io/ristretto/sketch_test.go

Documentation: github.com/dgraph-io/ristretto

     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 BenchmarkSketchIncrement(b *testing.B) {
    62  	s := newCmSketch(16)
    63  	b.SetBytes(1)
    64  	for n := 0; n < b.N; n++ {
    65  		s.Increment(1)
    66  	}
    67  }
    68  
    69  func BenchmarkSketchEstimate(b *testing.B) {
    70  	s := newCmSketch(16)
    71  	s.Increment(1)
    72  	b.SetBytes(1)
    73  	for n := 0; n < b.N; n++ {
    74  		s.Estimate(1)
    75  	}
    76  }
    77  

View as plain text