package election
import (
"reflect"
"testing"
)
type testOperation struct {
id string
val bool
}
func TestMasterTracker(t *testing.T) {
tests := []struct {
ids []string
ops []testOperation
count int
held []string
str string
}{
{
ids: []string{"1", "2", "3"},
ops: []testOperation{{id: "1", val: true}},
count: 1,
held: []string{"1"},
str: "1 . .",
},
{
ids: []string{"1", "20000", "30000"},
ops: []testOperation{{id: "30000", val: true}},
count: 1,
held: []string{"30000"},
str: ". ..... 30000",
},
{
ids: []string{"1", "2", "3"},
ops: []testOperation{
{id: "1", val: true},
{id: "2", val: true},
{id: "3", val: true},
{id: "1", val: false},
},
count: 2,
held: []string{"2", "3"},
str: ". 2 3",
},
{
ids: []string{},
ops: []testOperation{
{id: "1", val: true},
{id: "2", val: true},
{id: "3", val: true},
{id: "1", val: false},
},
count: 2,
held: []string{"2", "3"},
str: ". 2 3",
},
{
ids: []string{"1", "2", "3"},
ops: []testOperation{
{id: "1", val: true},
{id: "1", val: true},
},
count: 1,
held: []string{"1"},
str: "1 . .",
},
{
ids: []string{"1", "2", "3"},
ops: []testOperation{
{id: "1", val: false},
},
count: 0,
held: []string{},
str: ". . .",
},
}
for _, test := range tests {
mt := NewMasterTracker(test.ids, nil)
for _, op := range test.ops {
mt.Set(op.id, op.val)
}
if got := mt.Count(); got != test.count {
t.Errorf("MasterTracker.Count(%+v)=%d; want %d", test.ops, got, test.count)
}
if got := mt.Held(); !reflect.DeepEqual(got, test.held) {
t.Errorf("MasterTracker.Held(%+v)=%v; want %v", test.ops, got, test.held)
}
if got := mt.String(); got != test.str {
t.Errorf("MasterTracker.String(%+v)=%q; want %q", test.ops, got, test.str)
}
}
}