Skip to content

Commit

Permalink
add variance option
Browse files Browse the repository at this point in the history
  • Loading branch information
ch1aki committed May 22, 2020
1 parent 02774a3 commit aedab9f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
7 changes: 6 additions & 1 deletion lib/st.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type St struct {
Sum bool
Mean bool
Stddev bool
Variance bool

count int64
min float64
Expand Down Expand Up @@ -58,7 +59,7 @@ func (s *St) Output(writer io.Writer) {
header := []string{}
data := []string{}

defaults := !(s.Count || s.Min || s.Max || s.Sum || s.Mean || s.Stddev)
defaults := !(s.Count || s.Min || s.Max || s.Sum || s.Mean || s.Stddev || s.Variance)

if defaults || s.Count {
header = append(header, "N")
Expand All @@ -84,6 +85,10 @@ func (s *St) Output(writer io.Writer) {
header = append(header, "STDDEV")
data = append(data, strconv.FormatFloat(s.stddev, 'f', -1, 64))
}
if s.Variance {
header = append(header, "VARIANCE")
data = append(data, strconv.FormatFloat(s.variance, 'f', -1, 64))
}

s.Formatter(writer, header, [][]string{data})
}
Expand Down
18 changes: 17 additions & 1 deletion lib/st_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestSetOption(t *testing.T) {
t.Errorf("expected != actual\n%s\n", diff)
}
}
func TestNoOption(t *testing.T) {
func TestDefaultOption(t *testing.T) {
w := new(bytes.Buffer)
st := St{
Formatter: func(writer io.Writer, header []string, data [][]string) {
Expand All @@ -39,6 +39,22 @@ func TestNoOption(t *testing.T) {
}
}

func TestNotDefaultOption(t *testing.T) {
w := new(bytes.Buffer)
st := St{
Formatter: func(writer io.Writer, header []string, data [][]string) {
s := []byte(strings.Join(header, ","))
writer.Write(s)
},
Variance: true,
}
st.Output(w)

if diff := cmp.Diff(w.String(), "VARIANCE"); diff != "" {
t.Errorf("expected != actual\n%s\n", diff)
}
}

func TestPlainTextFromatter(t *testing.T) {
stdout := new(bytes.Buffer)
st := St{
Expand Down
30 changes: 16 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import (
)

type options struct {
Version func() `short:"v" long:"version" description:"show version"`
Format string `short:"f" long:"format" default:"%g" description:"outpuut format"`
Count bool `short:"N" long:"count" description:"sample size"`
Min bool `long:"min" description:"minimum"`
Max bool `long:"max" description:"maximum"`
Sum bool `long:"sum" description:"sum of elements of the sample"`
Mean bool `short:"m" long:"mean" description:"mean"`
Stddev bool `long:"sd" description:"standard deviation"`
Version func() `short:"v" long:"version" description:"show version"`
Format string `short:"f" long:"format" default:"%g" description:"outpuut format"`
Count bool `short:"N" long:"count" description:"sample size"`
Min bool `long:"min" description:"minimum"`
Max bool `long:"max" description:"maximum"`
Sum bool `long:"sum" description:"sum of elements of the sample"`
Mean bool `short:"m" long:"mean" description:"mean"`
Stddev bool `long:"sd" description:"standard deviation"`
Vatiance bool `long:"variance" description:"variance"`
}

func main() {
Expand All @@ -37,12 +38,13 @@ func main() {
s := st.St{
Formatter: st.PlainTextFormatter,

Count: opts.Count,
Min: opts.Min,
Max: opts.Max,
Sum: opts.Sum,
Mean: opts.Mean,
Stddev: opts.Stddev,
Count: opts.Count,
Min: opts.Min,
Max: opts.Max,
Sum: opts.Sum,
Mean: opts.Mean,
Stddev: opts.Stddev,
Variance: opts.Vatiance,
}

scanner := bufio.NewScanner(os.Stdin)
Expand Down

0 comments on commit aedab9f

Please sign in to comment.