diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2018-10-05 11:06:01 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-05 11:06:01 +0800 |
commit | 9bb7b300f2061a59392934406413d8d06d9fd542 (patch) | |
tree | a457b3604191ea6c83d227c51d9d957af197342c /core/types/block_test.go | |
parent | efcb301ec31acf7b87312cbec962682148555999 (diff) | |
download | dexon-consensus-9bb7b300f2061a59392934406413d8d06d9fd542.tar dexon-consensus-9bb7b300f2061a59392934406413d8d06d9fd542.tar.gz dexon-consensus-9bb7b300f2061a59392934406413d8d06d9fd542.tar.bz2 dexon-consensus-9bb7b300f2061a59392934406413d8d06d9fd542.tar.lz dexon-consensus-9bb7b300f2061a59392934406413d8d06d9fd542.tar.xz dexon-consensus-9bb7b300f2061a59392934406413d8d06d9fd542.tar.zst dexon-consensus-9bb7b300f2061a59392934406413d8d06d9fd542.zip |
types: Test block clone (#175)
Diffstat (limited to 'core/types/block_test.go')
-rw-r--r-- | core/types/block_test.go | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/core/types/block_test.go b/core/types/block_test.go index 2e6807d..3e601b6 100644 --- a/core/types/block_test.go +++ b/core/types/block_test.go @@ -18,10 +18,14 @@ package types import ( + "math/rand" + "reflect" "sort" "testing" + "time" "github.com/dexon-foundation/dexon-consensus-core/common" + "github.com/dexon-foundation/dexon-consensus-core/core/crypto" "github.com/stretchr/testify/suite" ) @@ -29,6 +33,55 @@ type BlockTestSuite struct { suite.Suite } +func (s *BlockTestSuite) randomBytes() []byte { + h := common.NewRandomHash() + return h[:] +} + +func (s *BlockTestSuite) createRandomBlock() *Block { + b := &Block{ + ProposerID: NodeID{common.NewRandomHash()}, + ParentHash: common.NewRandomHash(), + Hash: common.NewRandomHash(), + Position: Position{ + ChainID: rand.Uint32(), + Height: rand.Uint64(), + }, + Acks: common.NewSortedHashes(common.Hashes{ + common.NewRandomHash(), + common.NewRandomHash(), + }), + Timestamp: time.Now().Add(time.Duration(rand.Int())), + Witness: Witness{ + Height: rand.Uint64(), + Timestamp: time.Now().Add(time.Duration(rand.Int())), + Data: s.randomBytes(), + }, + ConsensusHeight: rand.Uint64(), + ConsensusTimestamp: time.Now().Add(time.Duration(rand.Int())), + Payload: s.randomBytes(), + Signature: crypto.Signature{Signature: s.randomBytes()}, + CRSSignature: crypto.Signature{Signature: s.randomBytes()}, + } + return b +} + +func (s *BlockTestSuite) TestCreateRandomBlock() { + b1 := *s.createRandomBlock() + b2 := *s.createRandomBlock() + + v1 := reflect.ValueOf(b1) + v2 := reflect.ValueOf(b2) + for i := 0; i < v1.NumField(); i++ { + f1 := v1.Field(i) + f2 := v2.Field(i) + if reflect.DeepEqual(f1.Interface(), f2.Interface()) { + s.Failf("Non randomized field found", "Field %s is not randomized\n", + v1.Type().Field(i).Name) + } + } +} + func (s *BlockTestSuite) TestSortByHash() { hash := common.Hash{} copy(hash[:], "aaaaaa") @@ -100,6 +153,24 @@ func (s *BlockTestSuite) TestIsAcking() { s.False(b0.IsAcking(common.NewRandomHash())) } +func (s *BlockTestSuite) TestClone() { + b1 := *s.createRandomBlock() + b2 := *b1.Clone() + + // Use reflect here to better understand the error message. + v1 := reflect.ValueOf(b1) + v2 := reflect.ValueOf(b2) + for i := 0; i < v1.NumField(); i++ { + f1 := v1.Field(i) + f2 := v2.Field(i) + if !reflect.DeepEqual(f1.Interface(), f2.Interface()) { + s.Failf("Field Not Equal", "Field %s is not equal.\n-%v\n+%v\n", + v1.Type().Field(i).Name, + f1, f2) + } + } +} + func TestBlock(t *testing.T) { suite.Run(t, new(BlockTestSuite)) } |