Skip to content

Commit

Permalink
Added access tests and added full bounds checking to At and Set
Browse files Browse the repository at this point in the history
  • Loading branch information
btracey committed Mar 17, 2014
1 parent 4ecb412 commit d46424e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
14 changes: 10 additions & 4 deletions mat64/dense.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,21 @@ func (m *Dense) isZero() bool {
}

func (m *Dense) At(r, c int) float64 {
if c >= m.mat.Cols {
panic("dense access out of bounds")
if r >= m.mat.Rows || r < 0 {
panic("index error: row access out of bounds")
}
if c >= m.mat.Cols || c < 0 {
panic("index error: column access out of bounds")
}
return m.mat.Data[r*m.mat.Stride+c]
}

func (m *Dense) Set(r, c int, v float64) {
if c >= m.mat.Cols {
panic("dense access out of bounds")
if r >= m.mat.Rows || r < 0 {
panic("index error: row access out of bounds")
}
if c >= m.mat.Cols || c < 0 {
panic("index error: column access out of bounds")
}
m.mat.Data[r*m.mat.Stride+c] = v
}
Expand Down
35 changes: 35 additions & 0 deletions mat64/dense_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,41 @@ func (s *S) TestNewDense(c *check.C) {
}
}

func (s *S) TestAtSet(c *check.C) {
for test, af := range [][][]float64{
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, // even
{{1, 2}, {4, 5}, {7, 8}}, // wide
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, //skinny
} {
m := NewDense(flatten(af))
rows, cols := m.Dims()
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
c.Check(m.At(i, j), check.Equals, af[i][j], check.Commentf("At test %d", test))

v := float64(i * j)
m.Set(i, j, v)
c.Check(m.At(i, j), check.Equals, v, check.Commentf("Set test %d", test))
}
}
// Check access out of bounds fails
c.Check(func() { m.At(rows, 0) }, check.PanicMatches, "index error: row access out of bounds", check.Commentf("Test %d", test))
c.Check(func() { m.At(rows+1, 0) }, check.PanicMatches, "index error: row access out of bounds", check.Commentf("Test %d", test))
c.Check(func() { m.At(0, cols) }, check.PanicMatches, "index error: column access out of bounds", check.Commentf("Test %d", test))
c.Check(func() { m.At(0, cols+1) }, check.PanicMatches, "index error: column access out of bounds", check.Commentf("Test %d", test))
c.Check(func() { m.At(-1, 0) }, check.PanicMatches, "index error: row access out of bounds", check.Commentf("Test %d", test))
c.Check(func() { m.At(0, -1) }, check.PanicMatches, "index error: column access out of bounds", check.Commentf("Test %d", test))

// Check access out of bounds fails
c.Check(func() { m.Set(rows, 0, 1.2) }, check.PanicMatches, "index error: row access out of bounds", check.Commentf("Test %d", test))
c.Check(func() { m.Set(rows+1, 0, 1.2) }, check.PanicMatches, "index error: row access out of bounds", check.Commentf("Test %d", test))
c.Check(func() { m.Set(0, cols, 1.2) }, check.PanicMatches, "index error: column access out of bounds", check.Commentf("Test %d", test))
c.Check(func() { m.Set(0, cols+1, 1.2) }, check.PanicMatches, "index error: column access out of bounds", check.Commentf("Test %d", test))
c.Check(func() { m.Set(-1, 0, 1.2) }, check.PanicMatches, "index error: row access out of bounds", check.Commentf("Test %d", test))
c.Check(func() { m.Set(0, -1, 1.2) }, check.PanicMatches, "index error: column access out of bounds", check.Commentf("Test %d", test))
}
}

func (s *S) TestRowCol(c *check.C) {
for i, af := range [][][]float64{
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}},
Expand Down

0 comments on commit d46424e

Please sign in to comment.