aboutsummaryrefslogtreecommitdiffstats
path: root/dex/cache_test.go
diff options
context:
space:
mode:
authorWei-Ning Huang <w@dexon.org>2018-11-26 17:58:02 +0800
committerWei-Ning Huang <w@dexon.org>2019-04-09 21:32:54 +0800
commit91e02be28118b217d19fabe65cc01992e8837973 (patch)
tree90e37f0174e2281e688e293f3a123b83e0caa49a /dex/cache_test.go
parent2ea3e5bc1881de55b6c4a9fe77d735257536d068 (diff)
downloaddexon-91e02be28118b217d19fabe65cc01992e8837973.tar
dexon-91e02be28118b217d19fabe65cc01992e8837973.tar.gz
dexon-91e02be28118b217d19fabe65cc01992e8837973.tar.bz2
dexon-91e02be28118b217d19fabe65cc01992e8837973.tar.lz
dexon-91e02be28118b217d19fabe65cc01992e8837973.tar.xz
dexon-91e02be28118b217d19fabe65cc01992e8837973.tar.zst
dexon-91e02be28118b217d19fabe65cc01992e8837973.zip
dex: fix tests
Diffstat (limited to 'dex/cache_test.go')
-rw-r--r--dex/cache_test.go69
1 files changed, 53 insertions, 16 deletions
diff --git a/dex/cache_test.go b/dex/cache_test.go
index 0ab426f3a..3c04db673 100644
--- a/dex/cache_test.go
+++ b/dex/cache_test.go
@@ -18,12 +18,28 @@
package dex
import (
+ "sort"
+ "strings"
"testing"
coreCommon "github.com/dexon-foundation/dexon-consensus/common"
coreTypes "github.com/dexon-foundation/dexon-consensus/core/types"
)
+type byHash []*coreTypes.Vote
+
+func (v byHash) Len() int {
+ return len(v)
+}
+
+func (v byHash) Less(i int, j int) bool {
+ return strings.Compare(v[i].BlockHash.String(), v[j].BlockHash.String()) < 0
+}
+
+func (v byHash) Swap(i int, j int) {
+ v[i], v[j] = v[j], v[i]
+}
+
func TestCacheVote(t *testing.T) {
cache := newCache(3)
pos0 := coreTypes.Position{
@@ -33,36 +49,50 @@ func TestCacheVote(t *testing.T) {
Height: uint64(1),
}
vote1 := &coreTypes.Vote{
- BlockHash: coreCommon.NewRandomHash(),
- Position: pos0,
+ VoteHeader: coreTypes.VoteHeader{
+ BlockHash: coreCommon.NewRandomHash(),
+ Position: pos0,
+ },
}
vote2 := &coreTypes.Vote{
- BlockHash: coreCommon.NewRandomHash(),
- Position: pos0,
+ VoteHeader: coreTypes.VoteHeader{
+ BlockHash: coreCommon.NewRandomHash(),
+ Position: pos0,
+ },
}
vote3 := &coreTypes.Vote{
- BlockHash: coreCommon.NewRandomHash(),
- Position: pos1,
+ VoteHeader: coreTypes.VoteHeader{
+ BlockHash: coreCommon.NewRandomHash(),
+ Position: pos1,
+ },
}
vote4 := &coreTypes.Vote{
- BlockHash: coreCommon.NewRandomHash(),
- Position: pos1,
+ VoteHeader: coreTypes.VoteHeader{
+ BlockHash: coreCommon.NewRandomHash(),
+ Position: pos1,
+ },
}
cache.addVote(vote1)
cache.addVote(vote2)
cache.addVote(vote3)
votes := cache.votes(pos0)
+ sort.Sort(byHash(votes))
+
+ resultVotes := []*coreTypes.Vote{vote1, vote2}
+ sort.Sort(byHash(resultVotes))
+
if len(votes) != 2 {
t.Errorf("fail to get votes: have %d, want 2", len(votes))
}
- if !votes[0].BlockHash.Equal(vote1.BlockHash) {
- t.Errorf("get wrong vote: have %s, want %s", votes[0], vote1)
+ if !votes[0].BlockHash.Equal(resultVotes[0].BlockHash) {
+ t.Errorf("get wrong vote: have %s, want %s", votes[0], resultVotes[0])
}
- if !votes[1].BlockHash.Equal(vote2.BlockHash) {
- t.Errorf("get wrong vote: have %s, want %s", votes[1], vote2)
+ if !votes[1].BlockHash.Equal(resultVotes[1].BlockHash) {
+ t.Errorf("get wrong vote: have %s, want %s", votes[1], resultVotes[1])
}
votes = cache.votes(pos1)
+ sort.Sort(byHash(votes))
if len(votes) != 1 {
t.Errorf("fail to get votes: have %d, want 1", len(votes))
}
@@ -73,18 +103,25 @@ func TestCacheVote(t *testing.T) {
cache.addVote(vote4)
votes = cache.votes(pos0)
+ sort.Sort(byHash(votes))
+
if len(votes) != 0 {
t.Errorf("fail to get votes: have %d, want 0", len(votes))
}
votes = cache.votes(pos1)
+ sort.Sort(byHash(votes))
+
+ resultVotes = []*coreTypes.Vote{vote3, vote4}
+ sort.Sort(byHash(resultVotes))
+
if len(votes) != 2 {
t.Errorf("fail to get votes: have %d, want 1", len(votes))
}
- if !votes[0].BlockHash.Equal(vote3.BlockHash) {
- t.Errorf("get wrong vote: have %s, want %s", votes[0], vote3)
+ if !votes[0].BlockHash.Equal(resultVotes[0].BlockHash) {
+ t.Errorf("get wrong vote: have %s, want %s", votes[0], resultVotes[0])
}
- if !votes[1].BlockHash.Equal(vote4.BlockHash) {
- t.Errorf("get wrong vote: have %s, want %s", votes[1], vote4)
+ if !votes[1].BlockHash.Equal(resultVotes[1].BlockHash) {
+ t.Errorf("get wrong vote: have %s, want %s", votes[1], resultVotes[1])
}
}