Skip to content

Commit

Permalink
NewNormal and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cshenton committed Feb 24, 2018
1 parent fdc2aa8 commit af1b6ae
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
15 changes: 15 additions & 0 deletions dist/uv/normal.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uv

import (
"errors"
"math"

"gonum.org/v1/gonum/mathext"
Expand All @@ -12,6 +13,20 @@ type Normal struct {
Scale float64
}

// NewNormal checks the input parameters and returns a Normal constructed
// using them, if they are valid.
func NewNormal(location, scale float64) (n *Normal, err error) {
if scale <= 0 {
err := errors.New("scale must be strictly greater than zero")
return nil, err
}
n = &Normal{
Location: location,
Scale: scale,
}
return n, nil
}

// Mean returns the first moment of the distribution.
func (n *Normal) Mean() float64 {
return n.Location
Expand Down
55 changes: 55 additions & 0 deletions dist/uv/normal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package uv_test

import (
"testing"

"github.com/chulabs/seer/dist/uv"
)

func TestNewNormal(t *testing.T) {
tt := []struct {
name string
loc float64
scale float64
}{
{"standard", 0, 1},
{"negative loc", -10, 2},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
n, err := uv.NewNormal(tc.loc, tc.scale)
if err != nil {
t.Error("unexpected error in NewNormal,", err)
}
if n.Location != tc.loc {
t.Errorf("expected location %v, but got %v", n.Location, tc.loc)
}
if n.Scale != tc.scale {
t.Errorf("expected Scale %v, but got %v", n.Scale, tc.scale)
}

})
}
}

func TestNewNormalErrs(t *testing.T) {
tt := []struct {
name string
loc float64
scale float64
}{
{"zero scale", 10, 0},
{"negative scale", 10, -2},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
n, err := uv.NewNormal(tc.loc, tc.scale)
if err == nil {
t.Error("expected error, but it was nil")
}
if n != nil {
t.Error("expected nil dist but it was", n)
}
})
}
}
Empty file added stream/transform_test.go
Empty file.

0 comments on commit af1b6ae

Please sign in to comment.