github.com/turingchain2020/turingchain@v1.1.21/common/skiplist/skiplist_test.go (about)

     1  package skiplist
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  var (
    11  	s1  = &SkipValue{1, "111"}
    12  	s2  = &SkipValue{2, "222"}
    13  	s3  = &SkipValue{3, "333"}
    14  	s4  = &SkipValue{4, "444"}
    15  	s5  = &SkipValue{5, "555"}
    16  	s6  = &SkipValue{6, "666"}
    17  	s7  = &SkipValue{7, "777"}
    18  	s8  = &SkipValue{8, "888"}
    19  	s9  = &SkipValue{9, "999"}
    20  	s10 = &SkipValue{10, "101010"}
    21  	s11 = &SkipValue{11, "111111"}
    22  	s12 = &SkipValue{12, "121212"}
    23  )
    24  
    25  func TestInsert(t *testing.T) {
    26  	l := NewSkipList(nil)
    27  	l.Insert(s1)
    28  	assert.Equal(t, 1, l.Len())
    29  	l.Insert(s2)
    30  	assert.Equal(t, 2, l.Len())
    31  	iter := l.GetIterator()
    32  	assert.Equal(t, int64(2), iter.First().Score)
    33  	assert.Equal(t, "222", iter.First().Value.(string))
    34  	assert.Equal(t, int64(1), iter.Last().Score)
    35  	assert.Equal(t, "111", iter.Last().Value.(string))
    36  }
    37  
    38  func TestFind(t *testing.T) {
    39  	l := NewSkipList(nil)
    40  	l.Insert(s1)
    41  	assert.Equal(t, s1, l.Find(s1))
    42  	l.Insert(s2)
    43  	assert.Equal(t, s2, l.Find(s2))
    44  	l.Insert(s3)
    45  	assert.Equal(t, s3, l.Find(s3))
    46  }
    47  
    48  func TestDelete(t *testing.T) {
    49  	l := NewSkipList(nil)
    50  	l.Insert(s1)
    51  	l.Insert(s2)
    52  	l.Insert(s3)
    53  	l.Delete(s3)
    54  	assert.Equal(t, 2, l.Len())
    55  	assert.Equal(t, (*SkipValue)(nil), l.Find(s3))
    56  	assert.Equal(t, s2, l.Find(s2))
    57  }
    58  
    59  func TestWalk(t *testing.T) {
    60  	l := NewSkipList(nil)
    61  	l.Insert(s1)
    62  	l.Insert(s2)
    63  	l.Insert(s3)
    64  	l.Insert(s4)
    65  	l.Insert(s5)
    66  	l.Insert(s6)
    67  	l.Insert(s7)
    68  	l.Insert(s8)
    69  	l.Insert(s9)
    70  	l.Insert(s10)
    71  	l.Insert(s11)
    72  	l.Insert(s12)
    73  	var data [100]string
    74  	i := 0
    75  	l.Walk(func(value interface{}) bool {
    76  		data[i] = value.(string)
    77  		i++
    78  		return true
    79  	})
    80  	assert.Equal(t, data[0], "121212")
    81  	assert.Equal(t, data[1], "111111")
    82  
    83  	var data2 [100]string
    84  	i = 0
    85  	l.Walk(func(value interface{}) bool {
    86  		data2[i] = value.(string)
    87  		i++
    88  		return false
    89  	})
    90  	assert.Equal(t, data2[0], "121212")
    91  	assert.Equal(t, data2[1], "")
    92  
    93  	l.Walk(nil)
    94  	iter := l.GetIterator()
    95  	assert.Equal(t, int64(12), iter.First().Score)
    96  	assert.Equal(t, "121212", iter.First().Value.(string))
    97  	assert.Equal(t, int64(1), iter.Last().Score)
    98  	assert.Equal(t, "111", iter.Last().Value.(string))
    99  	l.Print()
   100  }
   101  
   102  func TestWalkS(t *testing.T) {
   103  	l := NewSkipList(nil)
   104  	l.Insert(s1)
   105  	l.Insert(s2)
   106  	l.Insert(s3)
   107  	l.Insert(s4)
   108  	l.Insert(s5)
   109  	l.Insert(s6)
   110  	l.Insert(s7)
   111  	l.Insert(s8)
   112  	l.Insert(s9)
   113  	l.Insert(s10)
   114  	l.Insert(s11)
   115  	l.Insert(s12)
   116  	var score [100]int64
   117  	var data [100]string
   118  	i := 0
   119  	l.WalkS(func(value interface{}) bool {
   120  		score[i] = value.(*SkipValue).Score
   121  		data[i] = value.(*SkipValue).Value.(string)
   122  		i++
   123  		return true
   124  	})
   125  	assert.Equal(t, data[0], "121212")
   126  	assert.Equal(t, data[1], "111111")
   127  	assert.Equal(t, int64(12), score[0])
   128  	assert.Equal(t, int64(11), score[1])
   129  
   130  	var score2 [2]int64
   131  	var data2 [2]string
   132  	i = 0
   133  	l.WalkS(func(value interface{}) bool {
   134  		score2[i] = value.(*SkipValue).Score
   135  		data2[i] = value.(*SkipValue).Value.(string)
   136  		i++
   137  		return false
   138  	})
   139  	assert.Equal(t, "121212", data2[0])
   140  	assert.Equal(t, "", data2[1])
   141  	assert.Equal(t, int64(12), score2[0])
   142  	assert.Equal(t, int64(0), score2[1])
   143  	l.WalkS(func(value interface{}) bool {
   144  		e := value.(*SkipValue)
   145  		if e != nil {
   146  			fmt.Print(e.Score)
   147  			fmt.Print("    ")
   148  			fmt.Print(e.Value)
   149  			fmt.Println("")
   150  			return true
   151  		}
   152  		return false
   153  	})
   154  }