Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
language: go
- 1.9.2
install: true
go_import_path: github.com/yuichi10/matrix
before_install:
- go get -v github.com/golang/lint/golint
- go get -v github.com/yuichi10/matrix
script:
- go test
30 changes: 15 additions & 15 deletions arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (m *Matrix) Add(num interface{}) (matrix *Matrix) {
return
}
if err = matrix.addByMatrix(mat); err != nil {
matrix.err = &MatrixError{msg: err.Error(), funcName: "Add", opt1: m, opt2: &mat}
matrix.err = newError(err.Error(), "ADD", m, &mat)
}
return
}
Expand All @@ -160,7 +160,7 @@ func (m *Matrix) Add(num interface{}) (matrix *Matrix) {
return
}
if err = matrix.addByMatrix(*mat); err != nil {
matrix.err = &MatrixError{msg: err.Error(), funcName: "Add", opt1: m, opt2: mat}
matrix.err = newError(err.Error(), "ADD", m, mat)
}
return
}
Expand All @@ -172,7 +172,7 @@ func (m *Matrix) Add(num interface{}) (matrix *Matrix) {
matrix.addByFloat(float64(mat))
return
}
matrix.err = &MatrixError{msg: "The add type is not allowed", funcName: "Add", opt1: m, opt2: nil}
matrix.err = newError("The add type is not allowed", "Add", m, nil)
return
}

Expand All @@ -189,7 +189,7 @@ func (m *Matrix) Sub(num interface{}) (matrix *Matrix) {
return
}
if err = matrix.subByMatrix(mat); err != nil {
matrix.err = &MatrixError{msg: err.Error(), funcName: "Sub", opt1: m, opt2: &mat}
matrix.err = newError(err.Error(), "Sub", m, &mat)
}
return
} else if mat, ok := num.(*Matrix); ok {
Expand All @@ -198,7 +198,7 @@ func (m *Matrix) Sub(num interface{}) (matrix *Matrix) {
return
}
if err = matrix.subByMatrix(*mat); err != nil {
matrix.err = &MatrixError{msg: err.Error(), funcName: "Sub", opt1: m, opt2: mat}
matrix.err = newError(err.Error(), "Sub", m, mat)
}
return
} else if mat, ok := num.(float64); ok {
Expand All @@ -208,7 +208,7 @@ func (m *Matrix) Sub(num interface{}) (matrix *Matrix) {
matrix.subByFloat(float64(mat))
return
}
matrix.err = &MatrixError{msg: "The sub op2 type is not allowed", funcName: "Sub", opt1: m, opt2: nil}
matrix.err = newError("The sub op2 type is not allowed", "Sub", m, nil)
return
}

Expand All @@ -226,7 +226,7 @@ func (m *Matrix) Multi(num interface{}) (matrix *Matrix) {
}
// if err = matrix.multiByMatrixParallel(mat); err != nil {
if err = matrix.multiByMatrix(mat); err != nil {
matrix.err = &MatrixError{msg: err.Error(), funcName: "Multi", opt1: m, opt2: &mat}
matrix.err = newError(err.Error(), "Multi", m, &mat)
}
return
} else if mat, ok := num.(*Matrix); ok {
Expand All @@ -236,7 +236,7 @@ func (m *Matrix) Multi(num interface{}) (matrix *Matrix) {
}
// if err = matrix.multiByMatrixParallel(*mat); err != nil {
if err = matrix.multiByMatrix(*mat); err != nil {
matrix.err = &MatrixError{msg: err.Error(), funcName: "Multi", opt1: m, opt2: mat}
matrix.err = newError(err.Error(), "Multi", m, mat)
}
return
} else if mat, ok := num.(float64); ok {
Expand All @@ -246,7 +246,7 @@ func (m *Matrix) Multi(num interface{}) (matrix *Matrix) {
matrix.multiByFloat(float64(mat))
return
}
matrix.err = &MatrixError{msg: "The multi op2 type is not allowed", funcName: "Multi", opt1: m, opt2: nil}
matrix.err = newError("The multi op2 type is not allowed", "Multi", m, nil)
return
}

Expand All @@ -263,7 +263,7 @@ func (m *Matrix) MultiEach(num interface{}) (matrix *Matrix) {
return
}
if err = matrix.multiEachByMatrix(mat); err != nil {
matrix.err = &MatrixError{msg: err.Error(), funcName: "MultiEach", opt1: m, opt2: &mat}
matrix.err = newError(err.Error(), "MultiEach", m, &mat)
}
return
} else if mat, ok := num.(*Matrix); ok {
Expand All @@ -272,7 +272,7 @@ func (m *Matrix) MultiEach(num interface{}) (matrix *Matrix) {
return
}
if err = matrix.multiEachByMatrix(*mat); err != nil {
matrix.err = &MatrixError{msg: err.Error(), funcName: "MultiEach", opt1: m, opt2: mat}
matrix.err = newError(err.Error(), "MultiEach", m, mat)
}
return
} else if mat, ok := num.(float64); ok {
Expand All @@ -282,7 +282,7 @@ func (m *Matrix) MultiEach(num interface{}) (matrix *Matrix) {
matrix.multiByFloat(float64(mat))
return
}
matrix.err = &MatrixError{msg: "The multi op2 type is not allowed", funcName: "MultiEach", opt1: m, opt2: nil}
matrix.err = newError("The multi op2 type is not allowed", "MultiEach", m, nil)
return
}

Expand All @@ -299,7 +299,7 @@ func (m *Matrix) Div(num interface{}) (matrix *Matrix) {
return
}
if err = matrix.divByMatrix(mat); err != nil {
matrix.err = &MatrixError{msg: err.Error(), funcName: "Div", opt1: m, opt2: &mat}
matrix.err = newError(err.Error(), "Div", m, &mat)
}
return
} else if mat, ok := num.(*Matrix); ok {
Expand All @@ -308,7 +308,7 @@ func (m *Matrix) Div(num interface{}) (matrix *Matrix) {
return
}
if err = matrix.divByMatrix(*mat); err != nil {
matrix.err = &MatrixError{msg: err.Error(), funcName: "Div", opt1: m, opt2: mat}
matrix.err = newError(err.Error(), "Div", m, mat)
}
return
} else if mat, ok := num.(float64); ok {
Expand All @@ -318,6 +318,6 @@ func (m *Matrix) Div(num interface{}) (matrix *Matrix) {
matrix.divByFloat(float64(mat))
return
}
matrix.err = &MatrixError{msg: "The op2 type is not allowed", funcName: "Div", opt1: m, opt2: nil}
matrix.err = newError("The op2 type is not allowed", "Div", m, nil)
return
}
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package config

// Config has matrix configuration
type Config struct {
Panic bool
}
12 changes: 12 additions & 0 deletions matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"strings"

"github.com/yuichi10/matrix/config"
)

// Matrix has information of matrix
Expand All @@ -14,6 +16,16 @@ type Matrix struct {
err error
}

var (
// Config has configure of this matrix library
Config *config.Config
)

func init() {
Config = new(config.Config)
Config.Panic = false
}

// Row return this matrix's row
func (m *Matrix) Row() int {
return m.row
Expand Down
21 changes: 17 additions & 4 deletions matrixError.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,29 @@ package matrix

import "fmt"

// MatrixError is struct of error
// matrixError is struct of error
// to make easy to show error message
type MatrixError struct {
type matrixError struct {
msg string
funcName string
opt1 *Matrix
opt2 *Matrix
}

func (err *MatrixError) sizeDetail() string {
func newError(msg, funcName string, opt1, opt2 *Matrix) *matrixError {
err := &matrixError{
msg: msg,
funcName: funcName,
opt1: opt1,
opt2: opt2,
}
if Config.Panic {
panic(err.Error())
}
return err
}

func (err *matrixError) sizeDetail() string {
opt1 := "opt1: No matrix"
opt2 := "opt2: No matrix"
if err.opt1 != nil {
Expand All @@ -26,7 +39,7 @@ func (err *MatrixError) sizeDetail() string {
}

// MyError構造体にerrorインタフェースのError関数を実装
func (err *MatrixError) Error() string {
func (err *matrixError) Error() string {
size := err.sizeDetail()
return fmt.Sprintf("%v\n[function: %s] %s", size, err.funcName, err.msg)
}
33 changes: 30 additions & 3 deletions matrixError_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,37 @@ package matrix
import "testing"

func TestError(t *testing.T) {
opt1 := New(3, 4, nil)
err := MatrixError{msg: "sample", funcName: "TestError", opt1: opt1, opt2: nil}
ans := "Size: opt1: (3, 4), opt2: No matrix\n[function: TestError] sample"
var opt1 *Matrix
var opt2 *Matrix
var ans string
var err *matrixError
opt1 = New(3, 4, nil)
err = newError("sample", "TestError", opt1, nil)
ans = "Size: opt1: (3, 4), opt2: No matrix\n[function: TestError] sample"
if err.Error() != ans {
t.Errorf("want \n %v but got \n %v", ans, err.Error())
}

opt1 = New(3, 4, nil)
opt2 = New(4, 5, nil)
err = newError("sample", "TestError", opt1, opt2)
ans = "Size: opt1: (3, 4), opt2: (4, 5)\n[function: TestError] sample"
if err.Error() != ans {
t.Errorf("want \n %v \nbut got \n %v", ans, err.Error())
}

Config.Panic = true
opt1 = New(3, 4, nil)
opt2 = New(4, 5, nil)
f := func() { newError("sample", "TestError", opt1, opt2) }
assertPanic(t, f)
}

func assertPanic(t *testing.T, f func()) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
f()
}