github.com/turingchain2020/turingchain@v1.1.21/common/skiplist/queue_test.go (about) 1 package skiplist 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/turingchain2020/turingchain/types" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 type scorer struct { 12 score int64 13 hash string 14 } 15 16 func (item *scorer) GetScore() int64 { 17 return item.score 18 } 19 20 func (item *scorer) Hash() []byte { 21 return []byte(item.hash) 22 } 23 24 func (item *scorer) Compare(cmp Scorer) int { 25 switch item.GetScore() - cmp.GetScore() { 26 case Equal: 27 return Equal 28 case Big: 29 return Big 30 case Small: 31 return Small 32 default: 33 return 0 34 } 35 } 36 37 func (item *scorer) ByteSize() int64 { 38 return item.score 39 } 40 41 var ( 42 sc1 = &scorer{1, "111"} 43 sc2 = &scorer{2, "222"} 44 sc3 = &scorer{3, "333"} 45 sc4 = &scorer{4, "444"} 46 ) 47 48 func TestQueuePush(t *testing.T) { 49 q := NewQueue(10) 50 q.Push(sc1) 51 assert.Equal(t, 1, q.Size()) 52 q.Push(sc2) 53 assert.Equal(t, 2, q.Size()) 54 assert.Equal(t, sc2, q.First()) 55 assert.Equal(t, sc1, q.Last()) 56 assert.Equal(t, sc1.score+sc2.score, q.GetCacheBytes()) 57 } 58 59 func TestQueueFind(t *testing.T) { 60 q := NewQueue(10) 61 q.Push(sc1) 62 f1, _ := q.GetItem(string(sc1.Hash())) 63 assert.Equal(t, sc1, f1) 64 q.Push(sc2) 65 f2, _ := q.GetItem(string(sc2.Hash())) 66 assert.Equal(t, sc2, f2) 67 q.Push(sc3) 68 f3, _ := q.GetItem(string(sc3.Hash())) 69 assert.Equal(t, sc3, f3) 70 f4, err := q.GetItem(string(sc4.Hash())) 71 assert.Equal(t, nil, f4) 72 assert.Equal(t, types.ErrNotFound, err) 73 } 74 75 func TestQueueDelete(t *testing.T) { 76 q := NewQueue(10) 77 q.Push(sc1) 78 q.Push(sc2) 79 q.Push(sc3) 80 assert.Equal(t, sc1.score+sc2.score+sc3.score, q.GetCacheBytes()) 81 q.Remove(string(sc3.Hash())) 82 assert.Equal(t, 2, q.Size()) 83 f3, err := q.GetItem(string(sc3.Hash())) 84 assert.Equal(t, nil, f3) 85 assert.Equal(t, types.ErrNotFound, err) 86 assert.Equal(t, sc1.score+sc2.score, q.GetCacheBytes()) 87 } 88 89 func TestQueueWalk(t *testing.T) { 90 q := NewQueue(10) 91 q.Push(sc1) 92 q.Push(sc2) 93 var data [2]string 94 i := 0 95 q.Walk(0, func(value Scorer) bool { 96 data[i] = string(value.Hash()) 97 i++ 98 return true 99 }) 100 assert.Equal(t, data[0], "222") 101 assert.Equal(t, data[1], "111") 102 103 var data2 [2]string 104 i = 0 105 q.Walk(0, func(value Scorer) bool { 106 data2[i] = string(value.Hash()) 107 i++ 108 return false 109 }) 110 assert.Equal(t, data2[0], "222") 111 assert.Equal(t, data2[1], "") 112 113 i = 0 114 q.Walk(0, func(value Scorer) bool { 115 data2[i] = string(value.Hash()) 116 i++ 117 return !(i == 2) 118 }) 119 assert.Equal(t, data2[0], "222") 120 assert.Equal(t, data2[1], "111") 121 } 122 123 type scoreint struct { 124 data int64 125 } 126 127 func (s *scoreint) GetScore() int64 { 128 return s.data 129 } 130 131 func (s *scoreint) Hash() []byte { 132 return []byte(fmt.Sprint(s.data)) 133 } 134 135 func (s *scoreint) Compare(b Scorer) int { 136 return Big 137 } 138 139 func (s *scoreint) ByteSize() int64 { 140 return s.data 141 } 142 143 func TestQueue(t *testing.T) { 144 queue := NewQueue(10) 145 totalByte := 0 146 for i := 0; i < 11; i++ { 147 err := queue.Push(&scoreint{data: int64(i)}) 148 assert.Nil(t, err) 149 totalByte += i 150 } 151 assert.Equal(t, int64(totalByte), queue.GetCacheBytes()) 152 //缓存已满,触发优先级高的替换操作, 11替换1 153 assert.Nil(t, queue.Push(&scoreint{data: 11})) 154 assert.Equal(t, int64(totalByte)+10, queue.GetCacheBytes()) 155 assert.Equal(t, int64(10), queue.MaxSize()) 156 err := queue.Push(&scoreint{data: int64(0)}) 157 assert.Equal(t, err, types.ErrMemFull) 158 err = queue.Push(&scoreint{data: int64(2)}) 159 assert.Equal(t, err, types.ErrTxExist) 160 assert.Equal(t, int64(10), queue.MaxSize()) 161 162 item := &scoreint{data: int64(2)} 163 item2, err := queue.GetItem(string(item.Hash())) 164 assert.Nil(t, err) 165 assert.Equal(t, item.Hash(), item2.Hash()) 166 }