[test] add unit tests for resharding func

Signed-off-by: Leo Chen <leo@harmony.one>
pull/1258/head
Leo Chen 5 years ago
parent d56c00ac8a
commit 685e200ee3
  1. 67
      core/core_test.go
  2. 72
      internal/configs/sharding/shardingconfig_test.go

@ -0,0 +1,67 @@
package core
import (
"math/big"
"testing"
"github.com/harmony-one/harmony/core/types"
shardingconfig "github.com/harmony-one/harmony/internal/configs/sharding"
)
func TestIsEpochBlock(t *testing.T) {
block1 := types.NewBlock(&types.Header{Number: big.NewInt(10)}, nil, nil)
block2 := types.NewBlock(&types.Header{Number: big.NewInt(0)}, nil, nil)
block3 := types.NewBlock(&types.Header{Number: big.NewInt(327680)}, nil, nil)
block4 := types.NewBlock(&types.Header{Number: big.NewInt(77)}, nil, nil)
block5 := types.NewBlock(&types.Header{Number: big.NewInt(78)}, nil, nil)
block6 := types.NewBlock(&types.Header{Number: big.NewInt(188)}, nil, nil)
block7 := types.NewBlock(&types.Header{Number: big.NewInt(189)}, nil, nil)
tests := []struct {
schedule shardingconfig.Schedule
block *types.Block
expected bool
}{
{
shardingconfig.MainnetSchedule,
block1,
false,
},
{
shardingconfig.MainnetSchedule,
block2,
true,
},
{
shardingconfig.MainnetSchedule,
block3,
true,
},
{
shardingconfig.TestnetSchedule,
block4,
false,
},
{
shardingconfig.TestnetSchedule,
block5,
true,
},
{
shardingconfig.TestnetSchedule,
block6,
false,
},
{
shardingconfig.TestnetSchedule,
block7,
true,
},
}
for _, test := range tests {
ShardingSchedule = test.schedule
r := IsEpochBlock(test.block)
if r != test.expected {
t.Errorf("expected: %v, got: %v\n", test.expected, r)
}
}
}

@ -0,0 +1,72 @@
package shardingconfig
import (
"math/big"
"testing"
)
func TestMainnetInstanceForEpoch(t *testing.T) {
tests := []struct {
epoch *big.Int
instance Instance
}{
{
big.NewInt(0),
mainnetV0,
},
{
big.NewInt(1),
mainnetV1,
},
{
big.NewInt(2),
mainnetV1,
},
}
for _, test := range tests {
in := MainnetSchedule.InstanceForEpoch(test.epoch)
if in.NumShards() != test.instance.NumShards() || in.NumNodesPerShard() != test.instance.NumNodesPerShard() {
t.Errorf("can't get the right instane for epoch: %v\n", test.epoch)
}
}
}
func TestCalcEpochNumber(t *testing.T) {
tests := []struct {
block uint64
epoch *big.Int
}{
{
0,
big.NewInt(0),
},
{
1,
big.NewInt(0),
},
{
327679,
big.NewInt(0),
},
{
327680,
big.NewInt(1),
},
{
344064,
big.NewInt(2),
},
{
344063,
big.NewInt(1),
},
}
for _, test := range tests {
ep := MainnetSchedule.CalcEpochNumber(test.block)
if ep.Cmp(test.epoch) != 0 {
t.Errorf("CalcEpochNumber error: got %v, expect %v\n", ep, test.epoch)
}
}
}
Loading…
Cancel
Save