You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
learn_mqtt_go/clock/clock_test.go

146 lines
3.1 KiB
Go

package clock
import (
"reflect"
"testing"
)
func assertClocksEqual(t testing.TB, got, want []uint) {
t.Helper()
if !reflect.DeepEqual(got, want) {
t.Errorf("Error: got %v want %v", got, want)
}
}
func TestVectorClock(t *testing.T) {
t.Run("start value sync", func(t *testing.T) {
testCases := []struct {
a []uint
b []uint
expected []uint
}{
{[]uint{0, 0}, []uint{0, 0}, []uint{0, 0}},
{[]uint{2, 0}, []uint{0, 2}, []uint{2, 2}},
{[]uint{4, 11}, []uint{3, 10}, []uint{4, 11}},
{[]uint{5, 9}, []uint{8, 12}, []uint{8, 12}},
{[]uint{1, 1}, []uint{1, 1}, []uint{1, 1}},
// {[]uint{math.MaxUint32, 1}, []uint{(math.MaxUint32 + 1), 1}, []uint{1, 1}},
}
for _, tc := range testCases {
vc := VectorClock{clock: tc.a}
clock, err := vc.Sync(VectorClock{clock: tc.b})
if err != nil {
t.Errorf("Sync should not have errored")
}
assertClocksEqual(t, clock, tc.expected)
}
})
t.Run("basic sync 2", func(t *testing.T) {
vc := VectorClock{clock: []uint{4, 11}}
clock, err := vc.Sync(VectorClock{clock: []uint{4, 10}})
if err != nil {
t.Errorf("Sync should not have errored")
}
assertClocksEqual(t, clock, []uint{4, 11})
})
t.Run("basic sync", func(t *testing.T) {
})
t.Run("basic sync", func(t *testing.T) {
})
vc := VectorClock{clock: []uint{2, 0}}
clock, err := vc.Sync(VectorClock{clock: []uint{0, 2}})
if err != nil {
t.Errorf("Sync should not have errored")
}
assertClocksEqual(t, clock, []uint{2, 2})
}
func TestNewVectorClock(t *testing.T) {
got := NewVectorClock(2)
want := VectorClock{clock: []uint{0, 0}}
assertClocksEqual(t, got.GetClock(), want.GetClock())
}
func TestArrayToVectorClock(t *testing.T) {
t.Run("from empty slice", func(t *testing.T) {
got := ArrayToVectorClock([]uint{})
want := VectorClock{
clock: []uint{},
}
if len(got.GetClock()) != 0 {
t.Errorf("Expected VectorClock.clock to be of length zero.")
}
if !reflect.DeepEqual(got.GetClock(), want.GetClock()) {
t.Errorf("Error: got %v want %v", got.GetClock(), want.GetClock())
}
})
t.Run("from zero slice", func(t *testing.T) {
got := ArrayToVectorClock(make([]uint, 6))
want := VectorClock{
clock: []uint{0, 0, 0, 0, 0, 0},
}
assertClocksEqual(t, got.GetClock(), want.GetClock())
})
t.Run("from start clock", func(t *testing.T) {
want := NewVectorClock(3)
got := ArrayToVectorClock(want.GetClock())
assertClocksEqual(t, got.GetClock(), want.GetClock())
})
t.Run("from a populated clock", func(t *testing.T) {
want := VectorClock{
clock: []uint{2, 3},
}
got := ArrayToVectorClock(want.GetClock())
assertClocksEqual(t, got.GetClock(), want.GetClock())
})
t.Run("from a slice that is then modified", func(t *testing.T) {
initial_a := []uint{1, 2, 3, 4}
got := ArrayToVectorClock(initial_a)
want := make([]uint, len(initial_a))
copy(want, initial_a)
initial_a[0] = 12
initial_a[2] = 4
assertClocksEqual(t, got.GetClock(), want)
if reflect.DeepEqual(initial_a, got.GetClock()) {
t.Errorf("Initial array %v should not be equal to clock %v", initial_a, got.GetClock())
}
})
}