github.com/turingchain2020/turingchain@v1.1.21/common/listmap/listmap_test.go (about)

     1  package listmap
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/turingchain2020/turingchain/types"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestListMap(t *testing.T) {
    11  	l := New()
    12  	l.Push("1", "1")
    13  	assert.Equal(t, 1, l.Size())
    14  	l.Push("1", "11")
    15  	assert.Equal(t, 1, l.Size())
    16  	value, err := l.GetItem("1")
    17  	assert.Equal(t, err, nil)
    18  	assert.Equal(t, "11", value.(string))
    19  	l.Remove("1")
    20  	assert.Equal(t, 0, l.Size())
    21  	assert.Equal(t, false, l.Exist("1"))
    22  
    23  	v := l.GetTop()
    24  	assert.Equal(t, nil, v)
    25  
    26  	_, err = l.GetItem("11")
    27  	assert.Equal(t, types.ErrNotFound, err)
    28  	l.Push("1", "11")
    29  	assert.Equal(t, true, l.Exist("1"))
    30  	l.Push("2", "2")
    31  	assert.Equal(t, "11", l.GetTop().(string))
    32  
    33  	var data [2]string
    34  	i := 0
    35  	l.Walk(func(value interface{}) bool {
    36  		data[i] = value.(string)
    37  		i++
    38  		return true
    39  	})
    40  	assert.Equal(t, data[0], "11")
    41  	assert.Equal(t, data[1], "2")
    42  
    43  	var data2 [2]string
    44  	i = 0
    45  	l.Walk(func(value interface{}) bool {
    46  		data2[i] = value.(string)
    47  		i++
    48  		return false
    49  	})
    50  	assert.Equal(t, data2[0], "11")
    51  	assert.Equal(t, data2[1], "")
    52  
    53  	l.Walk(nil)
    54  	l.Remove("xxxx")
    55  }