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.
85 lines
1.8 KiB
Go
85 lines
1.8 KiB
Go
package clock
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func assertClocksEqual(t testing.TB, got, want []int) {
|
|
t.Helper()
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("Error: got %v want %v", got, want)
|
|
}
|
|
|
|
}
|
|
|
|
func TestVectorClock(t *testing.T) {
|
|
got := NewVectorClock(2)
|
|
want := VectorClock{clock: []int{0, 0}}
|
|
|
|
assertClocksEqual(t, got.GetClock(), want.GetClock())
|
|
|
|
}
|
|
|
|
func TestArrayToVectorClock(t *testing.T) {
|
|
|
|
t.Run("from empty slice", func(t *testing.T) {
|
|
got := ArrayToVectorClock([]int{})
|
|
want := VectorClock{
|
|
clock: []int{},
|
|
}
|
|
|
|
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([]int, 6))
|
|
want := VectorClock{
|
|
clock: []int{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: []int{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 := []int{1, 2, 3, 4}
|
|
got := ArrayToVectorClock(initial_a)
|
|
|
|
want := make([]int, 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())
|
|
}
|
|
|
|
})
|
|
|
|
}
|