From dc3663850fa17c58f7699e9b14a41c5517b2f3e2 Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Mon, 17 Apr 2017 17:05:55 -0700 Subject: [PATCH] test coverage --- matrix/matrix.go | 4 ++-- matrix/matrix_test.go | 12 ++++++++++++ matrix/util.go | 5 +++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/matrix/matrix.go b/matrix/matrix.go index fc02e56..c307cd5 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -3,7 +3,6 @@ package matrix import ( "bytes" "errors" - "fmt" "math" ) @@ -239,6 +238,7 @@ func (m *Matrix) L() *Matrix { } // U returns the matrix with zeros above the diagonal. +// Does not include the diagonal. func (m *Matrix) U() *Matrix { m2 := New(m.rows, m.cols) for row := 0; row < m.rows; row++ { @@ -254,7 +254,7 @@ func (m *Matrix) String() string { buffer := bytes.NewBuffer(nil) for row := 0; row < m.rows; row++ { for col := 0; col < m.cols; col++ { - buffer.WriteString(fmt.Sprintf("%f", m.Get(row, col))) + buffer.WriteString(f64s(m.Get(row, col))) buffer.WriteRune(' ') } buffer.WriteRune('\n') diff --git a/matrix/matrix_test.go b/matrix/matrix_test.go index ad00a7c..d9a8dc3 100644 --- a/matrix/matrix_test.go +++ b/matrix/matrix_test.go @@ -316,3 +316,15 @@ func TestMatrixU(t *testing.T) { u := m.U() assert.True(u.Equals(New(3, 3, 0, 0, 0, 4, 0, 0, 7, 8, 0))) } + +func TestMatrixString(t *testing.T) { + assert := assert.New(t) + + m := NewFromArrays([][]float64{ + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9}, + }) + + assert.Equal("1 2 3 \n4 5 6 \n7 8 9 \n", m.String()) +} diff --git a/matrix/util.go b/matrix/util.go index be851f2..094d93a 100644 --- a/matrix/util.go +++ b/matrix/util.go @@ -2,6 +2,7 @@ package matrix import ( "math" + "strconv" ) func minInt(values ...int) int { @@ -25,3 +26,7 @@ func maxInt(values ...int) int { } return max } + +func f64s(v float64) string { + return strconv.FormatFloat(v, 'f', -1, 64) +}