Skip to content

Commit

Permalink
Merge pull request #1 from while-loop/fix/zero-weights-panic
Browse files Browse the repository at this point in the history
Fix/zero weights panic
  • Loading branch information
while-loop committed Mar 12, 2017
2 parents fe91eb6 + 27c531d commit a7e43d4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
## [1.0.1] 12-03-2017
### Added
- Bug fix to return error when no weights have been set

## [1.0.0] 12-03-2017
### Added
- Initial Release


[Unreleased]: https://github.com/while-loop/go-walk/compare/v0.0.1...HEAD
[0.0.2]: https://github.com/while-loop/go-walk/compare/v0.0.1...v0.0.2
[1.0.0]: https://github.com/while-loop/go-walk/compare/0.0.1...HEAD
[1.0.1]: https://github.com/while-loop/go-walk/compare/1.0.0...1.0.1

[comment]: # (Added, Changed, Removed)
9 changes: 8 additions & 1 deletion walk/randomwalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ func (w *RandomWalk) Walk(iterations uint32) error {
}

ws, sum := w.preprocess()
for i := uint32(0); i < iterations; i++ {
if sum <= 0 {
return errors.New("Sum of weights is <= 0")
}

for i := uint32(0); i < iterations; i++ {
switch getRandy(ws, sum) {
case lEFT:
w.Walker.Left()
Expand All @@ -91,6 +94,10 @@ func (w *RandomWalk) Walk(iterations uint32) error {
// Get a random direction given a set of normalized weights
// Using the Sum of Weights method
func getRandy(ws weights, sum uint64) int {
if sum <= 0 {
return -1
}

randy := r.Int63n(int64(sum))
ttl := int64(0)

Expand Down
49 changes: 43 additions & 6 deletions walk/randomwalk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func TestInterfaceCallsMethods(t *testing.T) {
assert.Equal(t, 0, mw.r, "right sums not 0")
assert.Equal(t, maxIts, mw.u, "up sums not maxIts")
assert.Equal(t, 0, mw.d, "down sums not 0")

}

func TestInterfaceCallsMethodsEqually(t *testing.T) {
Expand Down Expand Up @@ -94,7 +93,7 @@ func TestPreprocessAssignsCorrectDirEnum(t *testing.T) {
assert.Equal(t, uint64(l)+uint64(r)+uint64(u)+uint64(d), sum, "", l, r, u, d)
}

func TestZeroWeightsDontExecute(t *testing.T) {
func TestPrependedZeroWeightsDontExecute(t *testing.T) {
maxIts := 100000
sums := map[int]int{
lEFT: 0,
Expand All @@ -110,10 +109,10 @@ func TestZeroWeightsDontExecute(t *testing.T) {

assert.Equal(t, 4, len(sums), "Unknown direction given", sums)

assert.Equal(t, 0, sums[lEFT], "left sums not 0")
assert.Equal(t, 0, sums[rIGHT], "right sums not 0")
assert.Equal(t, 0, sums[uP], "up sums not 0")
assert.Equal(t, maxIts, sums[dOWN], "not all dirs were down")
assert.Equal(t, 0, sums[lEFT], "left sums not 0", sums[lEFT])
assert.Equal(t, 0, sums[rIGHT], "right sums not 0", sums[rIGHT])
assert.Equal(t, 0, sums[uP], "up sums not 0", sums[uP])
assert.Equal(t, maxIts, sums[dOWN], "not all dirs were down", sums[dOWN])
}

func TestHalfWeightsDontExecute(t *testing.T) {
Expand All @@ -138,6 +137,44 @@ func TestHalfWeightsDontExecute(t *testing.T) {
assert.True(t, sums[dOWN] >= maxIts/3.0, "down sums not distributed", sums[dOWN], maxIts/3.0)
}

func TestZeroWeightsReturnsNegative1(t *testing.T) {
maxIts := 100000
sums := map[int]int{
lEFT: 0,
rIGHT: 0,
uP: 0,
dOWN: 0,
}

weights, sum := NewRandomWalk(0, 0, 0, 0, nil).preprocess()
for i := 0; i < maxIts; i++ {
sums[getRandy(weights, sum)]++
}

assert.Equal(t, 5, len(sums), "Unknown direction given", sums)

assert.Equal(t, maxIts, sums[-1], "-1 sums not max Its", sums[-1])
assert.Equal(t, 0, sums[lEFT], "left sums not 0", sums[lEFT])
assert.Equal(t, 0, sums[rIGHT], "right sums not 0", sums[rIGHT])
assert.Equal(t, 0, sums[uP], "up sums not 0", sums[uP])
assert.Equal(t, 0, sums[dOWN], "down sums not 0", sums[dOWN])
}

func TestZeroWeightsReturnErrorWalking(t *testing.T) {
maxIts := uint32(100000)

mw := &TestWalker{}
rw := NewRandomWalk(0, 0, 0, 0, mw)
err := rw.Walk(maxIts)
assert.Error(t, err)
assert.Equal(t, "Sum of weights is <= 0", err.Error())

assert.Equal(t, 0, mw.l, "left sums not 0")
assert.Equal(t, 0, mw.r, "right sums not 0")
assert.Equal(t, 0, mw.u, "up sums not 0")
assert.Equal(t, 0, mw.d, "down sums not 0")
}

type TestWalker struct {
l, r, u, d int
}
Expand Down

0 comments on commit a7e43d4

Please sign in to comment.