Add unit test to ActiveValidatorIndices (#4263)

* Add regression test to ActiveValidatorIndices

* fix test, more comments

* imports
This commit is contained in:
Preston Van Loon 2019-12-11 12:27:25 -08:00 committed by GitHub
parent 0cb59bb018
commit e72ff1bb4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
package helpers
import (
"reflect"
"testing"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
@ -248,3 +249,159 @@ func TestDomain_OK(t *testing.T) {
}
}
}
// Test basic functionality of ActiveValidatorIndices without caching. This test will need to be
// rewritten when releasing some cache flag.
func TestActiveValidatorIndices(t *testing.T) {
farFutureEpoch := params.BeaconConfig().FarFutureEpoch
type args struct {
state *pb.BeaconState
epoch uint64
}
tests := []struct {
name string
args args
want []uint64
wantErr bool
}{
{
name: "all_active_epoch_10",
args: args{
state: &pb.BeaconState{
Validators: []*ethpb.Validator{
&ethpb.Validator{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
&ethpb.Validator{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
&ethpb.Validator{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
},
},
epoch: 10,
},
want: []uint64{0, 1, 2},
},
{
name: "some_active_epoch_10",
args: args{
state: &pb.BeaconState{
Validators: []*ethpb.Validator{
&ethpb.Validator{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
&ethpb.Validator{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
&ethpb.Validator{
ActivationEpoch: 0,
ExitEpoch: 1,
},
},
},
epoch: 10,
},
want: []uint64{0, 1},
},
{
name: "some_active_with_recent_new_epoch_10",
args: args{
state: &pb.BeaconState{
Validators: []*ethpb.Validator{
&ethpb.Validator{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
&ethpb.Validator{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
&ethpb.Validator{
ActivationEpoch: 0,
ExitEpoch: 1,
},
&ethpb.Validator{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
},
},
epoch: 10,
},
want: []uint64{0, 1, 3},
},
{
name: "some_active_with_recent_new_epoch_10",
args: args{
state: &pb.BeaconState{
Validators: []*ethpb.Validator{
&ethpb.Validator{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
&ethpb.Validator{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
&ethpb.Validator{
ActivationEpoch: 0,
ExitEpoch: 1,
},
&ethpb.Validator{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
},
},
epoch: 10,
},
want: []uint64{0, 1, 3},
},
{
name: "some_active_with_recent_new_epoch_10",
args: args{
state: &pb.BeaconState{
Validators: []*ethpb.Validator{
&ethpb.Validator{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
&ethpb.Validator{
ActivationEpoch: 0,
ExitEpoch: 1,
},
&ethpb.Validator{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
&ethpb.Validator{
ActivationEpoch: 0,
ExitEpoch: farFutureEpoch,
},
},
},
epoch: 10,
},
want: []uint64{0, 2, 3},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ActiveValidatorIndices(tt.args.state, tt.args.epoch)
if (err != nil) != tt.wantErr {
t.Errorf("ActiveValidatorIndices() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ActiveValidatorIndices() got = %v, want %v", got, tt.want)
}
})
}
}