Skip to content

Commit

Permalink
Merge #3
Browse files Browse the repository at this point in the history
3: Return correct value from maskRing.Shift r=antifuchs a=antifuchs

As @jsnell correctly notes in #1, we're not returning the right index value there when shifting from a ring that has already gone around once.

This PR adds a test and fixes #1.

Co-authored-by: Andreas Fuchs <[email protected]>
  • Loading branch information
bors[bot] and antifuchs committed Mar 16, 2019
2 parents 1689068 + 3fbd3f7 commit f1151a5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mask_ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (r *maskRing) Shift() (uint, error) {
}
i := r.read
r.read++
return i, nil
return r.Mask(i), nil
}

func (r *maskRing) Size() uint {
Expand Down
28 changes: 28 additions & 0 deletions ring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,38 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestForcePush(t *testing.T) {
r := NewRing(1)
r.Push()
assert.Equal(t, ForcePush(r), uint(0))
}

func TestPushAndShift(t *testing.T) {
tests := []struct {
name string
cap uint
turns uint
}{
{"mask/3", 16, 3},
{"basic/3", 19, 3},
}
for _, elt := range tests {
test := elt
t.Run(test.name, func(t *testing.T) {
t.Parallel()
ring := NewRing(test.cap)
for i := uint(0); i < test.cap*test.turns; i++ {
pushed, err := ring.Push()
require.NoError(t, err)
require.True(t, pushed < test.cap)

shifted, err := ring.Shift()
require.NoError(t, err)
require.Equal(t, pushed, shifted, "on attempt %d", i)
}
})
}
}

0 comments on commit f1151a5

Please sign in to comment.