Skip to content

Commit

Permalink
appsrv: rework TestRing
Browse files Browse the repository at this point in the history
  • Loading branch information
yousong committed Jun 17, 2020
1 parent e37f15c commit 358f5d4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
6 changes: 4 additions & 2 deletions pkg/appsrv/ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ type Ring struct {
}

func NewRing(size int) *Ring {
r := Ring{buffer: make([]interface{}, size+1),
r := Ring{
buffer: make([]interface{}, size+1),
header: 0,
tail: 0,
lock: &sync.Mutex{}}
lock: &sync.Mutex{},
}
return &r
}

Expand Down
49 changes: 27 additions & 22 deletions pkg/appsrv/ring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,33 @@ import (
)

func TestRing(t *testing.T) {
r := NewRing(10)
var v int32 = 10
r.Push(v)
v = 20
r.Push(v)
v = 30
r.Push(v)
v1 := r.Pop().(int32)
if v1 != 10 {
t.Error("Fail")
}
v2 := r.Pop().(int32)
if v2 != 20 {
t.Error("Fail")
}
v3 := r.Pop().(int32)
if v3 != 30 {
t.Error("Fail")
}
v4 := r.Pop()
if v4 != nil {
t.Error("Fail")
var (
r = NewRing(10)
push = func(v int32) {
r.Push(v)
}
pop = func(want int32) {
got := r.Pop().(int32)
if got != want {
t.Fatalf("got %d, want %d", got, want)
}
for i := r.header; i != r.tail; i = nextPointer(i, len(r.buffer)) {
if r.buffer[i] != nil {
t.Fatalf("head %d, tail %d, index %d not nil",
r.header, r.tail, i)
}
}
}
)
push(10)
push(20)
push(30)

pop(10)
pop(20)
pop(30)
if v := r.Pop(); v != nil {
t.Fatalf("want nil, got %#v", v)
}
}

Expand Down

0 comments on commit 358f5d4

Please sign in to comment.