github.com/turingchain2020/turingchain@v1.1.21/common/utils/util_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func Test_spaceLimitCache(t *testing.T) {
    10  
    11  	c := NewSpaceLimitCache(3, 10)
    12  	assert.Equal(t, 3, c.capacity)
    13  	assert.True(t, c.Add(1, 1, 1))
    14  	assert.True(t, c.Add(1, 1, 1))
    15  	assert.False(t, c.Add(2, 2, 20))
    16  	assert.Nil(t, c.Get(2))
    17  	assert.True(t, c.Add(2, 1, 10))
    18  	c.Add(3, 2, 2)
    19  	c.Add(4, 2, 2)
    20  	c.Add(5, 2, 2)
    21  	c.Add(6, 2, 1)
    22  	assert.False(t, c.Contains(3))
    23  	assert.Equal(t, 3, c.data.Len())
    24  	assert.Equal(t, 3, len(c.sizeMap))
    25  	assert.Equal(t, 5, c.currSize)
    26  	assert.True(t, c.Contains(4))
    27  	assert.True(t, c.Contains(5))
    28  	assert.True(t, c.Add(7, 7, 8))
    29  	assert.True(t, c.Contains(7))
    30  	assert.Equal(t, 2, c.data.Len())
    31  	assert.Equal(t, 2, len(c.sizeMap))
    32  	assert.Equal(t, 9, c.currSize)
    33  	_, exist := c.Remove(6)
    34  	assert.True(t, exist)
    35  	_, exist = c.Remove(5)
    36  	assert.False(t, exist)
    37  	assert.Equal(t, 8, c.currSize)
    38  }