cleaned up ID tests

This commit is contained in:
StephenButtolph 2020-04-04 17:47:44 -04:00
parent daa7abdba5
commit 61ed4bb660
7 changed files with 118 additions and 143 deletions

View File

@ -86,6 +86,19 @@ func (b *Bag) List() []ID {
return idList
}
// Equals returns true if the bags contain the same elements
func (b *Bag) Equals(oIDs Bag) bool {
if b.Len() != oIDs.Len() {
return false
}
for key, value := range b.counts {
if value != oIDs.counts[key] {
return false
}
}
return true
}
// Mode returns the id that has been seen the most and the number of times it
// has been seen. Ties are broken by the first id to be seen the reported number
// of times.

33
ids/slice.go Normal file
View File

@ -0,0 +1,33 @@
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package ids
// Equals returns true if the arrays are equal
func Equals(a, b []ID) bool {
if len(a) != len(b) {
return false
}
for i, aID := range a {
if !aID.Equals(b[i]) {
return false
}
}
return true
}
// UnsortedEquals returns true if the have the same number of each ID
func UnsortedEquals(a, b []ID) bool {
if len(a) != len(b) {
return false
}
aBag := Bag{}
aBag.Add(a...)
bBag := Bag{}
bBag.Add(b...)
return aBag.Equals(bBag)
}

View File

@ -16,6 +16,11 @@ import (
"github.com/ava-labs/gecko/snow/consensus/snowstorm"
)
func GenerateID() ids.ID {
offset++
return ids.Empty.Prefix(offset)
}
var (
Genesis = GenerateID()
offset = uint64(0)
@ -100,7 +105,7 @@ func AddTest(t *testing.T, factory Factory) {
if !avl.Finalized() {
t.Fatalf("An empty avalanche instance is not finalized")
} else if !Matches([]ids.ID{vts[0].ID(), vts[1].ID()}, avl.Preferences().List()) {
} else if !ids.UnsortedEquals([]ids.ID{vts[0].ID(), vts[1].ID()}, avl.Preferences().List()) {
t.Fatalf("Initial frontier failed to be set")
}
@ -119,7 +124,7 @@ func AddTest(t *testing.T, factory Factory) {
if avl.Finalized() {
t.Fatalf("A non-empty avalanche instance is finalized")
} else if !Matches([]ids.ID{vtx0.id}, avl.Preferences().List()) {
} else if !ids.UnsortedEquals([]ids.ID{vtx0.id}, avl.Preferences().List()) {
t.Fatalf("Initial frontier failed to be set")
}
@ -138,7 +143,7 @@ func AddTest(t *testing.T, factory Factory) {
if avl.Finalized() {
t.Fatalf("A non-empty avalanche instance is finalized")
} else if !Matches([]ids.ID{vtx0.id}, avl.Preferences().List()) {
} else if !ids.UnsortedEquals([]ids.ID{vtx0.id}, avl.Preferences().List()) {
t.Fatalf("Initial frontier failed to be set")
}
@ -146,7 +151,7 @@ func AddTest(t *testing.T, factory Factory) {
if avl.Finalized() {
t.Fatalf("A non-empty avalanche instance is finalized")
} else if !Matches([]ids.ID{vtx0.id}, avl.Preferences().List()) {
} else if !ids.UnsortedEquals([]ids.ID{vtx0.id}, avl.Preferences().List()) {
t.Fatalf("Initial frontier failed to be set")
}
@ -154,7 +159,7 @@ func AddTest(t *testing.T, factory Factory) {
if avl.Finalized() {
t.Fatalf("A non-empty avalanche instance is finalized")
} else if !Matches([]ids.ID{vtx0.id}, avl.Preferences().List()) {
} else if !ids.UnsortedEquals([]ids.ID{vtx0.id}, avl.Preferences().List()) {
t.Fatalf("Initial frontier failed to be set")
}
}

View File

@ -1,40 +0,0 @@
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package avalanche
import (
"github.com/ava-labs/gecko/ids"
)
func GenerateID() ids.ID {
offset++
return ids.Empty.Prefix(offset)
}
func Matches(a, b []ids.ID) bool {
if len(a) != len(b) {
return false
}
set := ids.Set{}
set.Add(a...)
for _, id := range b {
if !set.Contains(id) {
return false
}
}
return true
}
func MatchesShort(a, b []ids.ShortID) bool {
if len(a) != len(b) {
return false
}
set := ids.ShortSet{}
set.Add(a...)
for _, id := range b {
if !set.Contains(id) {
return false
}
}
return true
}

View File

@ -27,12 +27,12 @@ func TestTopologicalTxIssued(t *testing.T) { TxIssuedTest(t, TopologicalFactory{
func TestAvalancheVoting(t *testing.T) {
params := Parameters{
Parameters: snowball.Parameters{
Metrics: prometheus.NewRegistry(),
K: 2,
Alpha: 2,
BetaVirtuous: 1,
BetaRogue: 2,
ConcurrentRepolls: 1,
Metrics: prometheus.NewRegistry(),
K: 2,
Alpha: 2,
BetaVirtuous: 1,
BetaRogue: 2,
ConcurrentRepolls: 1,
},
Parents: 2,
BatchSize: 1,
@ -87,7 +87,7 @@ func TestAvalancheVoting(t *testing.T) {
if ta.Finalized() {
t.Fatalf("An avalanche instance finalized too early")
} else if !Matches([]ids.ID{vtx1.id}, ta.Preferences().List()) {
} else if !ids.UnsortedEquals([]ids.ID{vtx1.id}, ta.Preferences().List()) {
t.Fatalf("Initial frontier failed to be set")
}
@ -95,7 +95,7 @@ func TestAvalancheVoting(t *testing.T) {
if !ta.Finalized() {
t.Fatalf("An avalanche instance finalized too late")
} else if !Matches([]ids.ID{vtx1.id}, ta.Preferences().List()) {
} else if !ids.UnsortedEquals([]ids.ID{vtx1.id}, ta.Preferences().List()) {
t.Fatalf("Initial frontier failed to be set")
} else if tx0.Status() != choices.Rejected {
t.Fatalf("Tx should have been rejected")
@ -107,12 +107,12 @@ func TestAvalancheVoting(t *testing.T) {
func TestAvalancheTransitiveVoting(t *testing.T) {
params := Parameters{
Parameters: snowball.Parameters{
Metrics: prometheus.NewRegistry(),
K: 2,
Alpha: 2,
BetaVirtuous: 1,
BetaRogue: 2,
ConcurrentRepolls: 1,
Metrics: prometheus.NewRegistry(),
K: 2,
Alpha: 2,
BetaVirtuous: 1,
BetaRogue: 2,
ConcurrentRepolls: 1,
},
Parents: 2,
BatchSize: 1,
@ -176,7 +176,7 @@ func TestAvalancheTransitiveVoting(t *testing.T) {
if ta.Finalized() {
t.Fatalf("An avalanche instance finalized too early")
} else if !Matches([]ids.ID{vtx2.id}, ta.Preferences().List()) {
} else if !ids.UnsortedEquals([]ids.ID{vtx2.id}, ta.Preferences().List()) {
t.Fatalf("Initial frontier failed to be set")
} else if tx0.Status() != choices.Accepted {
t.Fatalf("Tx should have been accepted")
@ -189,7 +189,7 @@ func TestAvalancheTransitiveVoting(t *testing.T) {
if !ta.Finalized() {
t.Fatalf("An avalanche instance finalized too late")
} else if !Matches([]ids.ID{vtx2.id}, ta.Preferences().List()) {
} else if !ids.UnsortedEquals([]ids.ID{vtx2.id}, ta.Preferences().List()) {
t.Fatalf("Initial frontier failed to be set")
} else if tx0.Status() != choices.Accepted {
t.Fatalf("Tx should have been accepted")
@ -201,12 +201,12 @@ func TestAvalancheTransitiveVoting(t *testing.T) {
func TestAvalancheSplitVoting(t *testing.T) {
params := Parameters{
Parameters: snowball.Parameters{
Metrics: prometheus.NewRegistry(),
K: 2,
Alpha: 2,
BetaVirtuous: 1,
BetaRogue: 2,
ConcurrentRepolls: 1,
Metrics: prometheus.NewRegistry(),
K: 2,
Alpha: 2,
BetaVirtuous: 1,
BetaRogue: 2,
ConcurrentRepolls: 1,
},
Parents: 2,
BatchSize: 1,
@ -255,7 +255,7 @@ func TestAvalancheSplitVoting(t *testing.T) {
if !ta.Finalized() {
t.Fatalf("An avalanche instance finalized too late")
} else if !Matches([]ids.ID{vtx0.id, vtx1.id}, ta.Preferences().List()) {
} else if !ids.UnsortedEquals([]ids.ID{vtx0.id, vtx1.id}, ta.Preferences().List()) {
t.Fatalf("Initial frontier failed to be set")
} else if tx0.Status() != choices.Accepted {
t.Fatalf("Tx should have been accepted")
@ -265,12 +265,12 @@ func TestAvalancheSplitVoting(t *testing.T) {
func TestAvalancheTransitiveRejection(t *testing.T) {
params := Parameters{
Parameters: snowball.Parameters{
Metrics: prometheus.NewRegistry(),
K: 2,
Alpha: 2,
BetaVirtuous: 1,
BetaRogue: 2,
ConcurrentRepolls: 1,
Metrics: prometheus.NewRegistry(),
K: 2,
Alpha: 2,
BetaVirtuous: 1,
BetaRogue: 2,
ConcurrentRepolls: 1,
},
Parents: 2,
BatchSize: 1,
@ -340,7 +340,7 @@ func TestAvalancheTransitiveRejection(t *testing.T) {
if ta.Finalized() {
t.Fatalf("An avalanche instance finalized too early")
} else if !Matches([]ids.ID{vtx1.id}, ta.Preferences().List()) {
} else if !ids.UnsortedEquals([]ids.ID{vtx1.id}, ta.Preferences().List()) {
t.Fatalf("Initial frontier failed to be set")
}
@ -348,7 +348,7 @@ func TestAvalancheTransitiveRejection(t *testing.T) {
if ta.Finalized() {
t.Fatalf("An avalanche instance finalized too early")
} else if !Matches([]ids.ID{vtx1.id}, ta.Preferences().List()) {
} else if !ids.UnsortedEquals([]ids.ID{vtx1.id}, ta.Preferences().List()) {
t.Fatalf("Initial frontier failed to be set")
} else if tx0.Status() != choices.Rejected {
t.Fatalf("Tx should have been rejected")
@ -367,12 +367,12 @@ func TestAvalancheTransitiveRejection(t *testing.T) {
func TestAvalancheVirtuous(t *testing.T) {
params := Parameters{
Parameters: snowball.Parameters{
Metrics: prometheus.NewRegistry(),
K: 2,
Alpha: 2,
BetaVirtuous: 1,
BetaRogue: 2,
ConcurrentRepolls: 1,
Metrics: prometheus.NewRegistry(),
K: 2,
Alpha: 2,
BetaVirtuous: 1,
BetaRogue: 2,
ConcurrentRepolls: 1,
},
Parents: 2,
BatchSize: 1,
@ -489,12 +489,12 @@ func TestAvalancheVirtuous(t *testing.T) {
func TestAvalancheIsVirtuous(t *testing.T) {
params := Parameters{
Parameters: snowball.Parameters{
Metrics: prometheus.NewRegistry(),
K: 2,
Alpha: 2,
BetaVirtuous: 1,
BetaRogue: 2,
ConcurrentRepolls: 1,
Metrics: prometheus.NewRegistry(),
K: 2,
Alpha: 2,
BetaVirtuous: 1,
BetaRogue: 2,
ConcurrentRepolls: 1,
},
Parents: 2,
BatchSize: 1,
@ -573,12 +573,12 @@ func TestAvalancheIsVirtuous(t *testing.T) {
func TestAvalancheQuiesce(t *testing.T) {
params := Parameters{
Parameters: snowball.Parameters{
Metrics: prometheus.NewRegistry(),
K: 1,
Alpha: 1,
BetaVirtuous: 1,
BetaRogue: 1,
ConcurrentRepolls: 1,
Metrics: prometheus.NewRegistry(),
K: 1,
Alpha: 1,
BetaVirtuous: 1,
BetaRogue: 1,
ConcurrentRepolls: 1,
},
Parents: 2,
BatchSize: 1,
@ -667,12 +667,12 @@ func TestAvalancheQuiesce(t *testing.T) {
func TestAvalancheOrphans(t *testing.T) {
params := Parameters{
Parameters: snowball.Parameters{
Metrics: prometheus.NewRegistry(),
K: 1,
Alpha: 1,
BetaVirtuous: math.MaxInt32,
BetaRogue: math.MaxInt32,
ConcurrentRepolls: 1,
Metrics: prometheus.NewRegistry(),
K: 1,
Alpha: 1,
BetaVirtuous: math.MaxInt32,
BetaRogue: math.MaxInt32,
ConcurrentRepolls: 1,
},
Parents: 2,
BatchSize: 1,

View File

@ -16,6 +16,13 @@ import (
"github.com/ava-labs/gecko/snow/consensus/snowball"
)
var (
Genesis = &Blk{
id: ids.Empty.Prefix(0),
status: choices.Accepted,
}
)
func ParamsTest(t *testing.T, factory Factory) {
sm := factory.New()

View File

@ -1,43 +0,0 @@
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package snowman
import (
"github.com/ava-labs/gecko/ids"
"github.com/ava-labs/gecko/snow/choices"
)
var (
Genesis = &Blk{
id: ids.Empty.Prefix(0),
status: choices.Accepted,
}
)
func Matches(a, b []ids.ID) bool {
if len(a) != len(b) {
return false
}
set := ids.Set{}
set.Add(a...)
for _, id := range b {
if !set.Contains(id) {
return false
}
}
return true
}
func MatchesShort(a, b []ids.ShortID) bool {
if len(a) != len(b) {
return false
}
set := ids.ShortSet{}
set.Add(a...)
for _, id := range b {
if !set.Contains(id) {
return false
}
}
return true
}