From e7db2648034fb71b051902a02bc25d4468ed492e Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Thu, 9 Apr 2015 12:51:21 +0200 Subject: [PATCH] Extract counter value from vector --- vector.go | 10 ++++++++++ vector_test.go | 14 +++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/vector.go b/vector.go index 04859452..edd15614 100644 --- a/vector.go +++ b/vector.go @@ -103,3 +103,13 @@ func (a Vector) Concurrent(b Vector) bool { comp := a.Compare(b) return comp == ConcurrentGreater || comp == ConcurrentLesser } + +// Counter returns the current value of the given counter ID. +func (v Vector) Counter(id uint64) uint64 { + for _, c := range v { + if c.ID == id { + return c.Value + } + } + return 0 +} diff --git a/vector_test.go b/vector_test.go index 7815412c..c01255e7 100644 --- a/vector_test.go +++ b/vector_test.go @@ -118,5 +118,17 @@ func TestMerge(t *testing.T) { t.Errorf("%d: %+v.Merge(%+v) == %+v (expected %+v)", i, tc.a, tc.b, m, tc.m) } } - +} + +func TestCounterValue(t *testing.T) { + v0 := Vector{Counter{42, 1}, Counter{64, 5}} + if v0.Counter(42) != 1 { + t.Error("Counter error, %d != %d", v0.Counter(42), 1) + } + if v0.Counter(64) != 5 { + t.Error("Counter error, %d != %d", v0.Counter(64), 5) + } + if v0.Counter(72) != 0 { + t.Error("Counter error, %d != %d", v0.Counter(72), 0) + } }