Skip to content

Commit

Permalink
Add Atof tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry-Sarabia committed Feb 20, 2019
1 parent fcd58a3 commit 1805bc2
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions sliceconv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,32 @@ func TestItoa(t *testing.T) {
})
}
}

func TestAtof(t *testing.T) {
tests := []struct {
name string
strings []string
wantFlts []float64
wantErr error
}{
{"Nil slice", nil, nil, nil},
{"Empty string slice", []string{}, nil, nil},
{"Single valid string", []string{"0.000000000000001"}, []float64{0.000000000000001}, nil},
{"Multiple valid strings", []string{"1.25", "2.125", "3.0625", "4.03125", "5.015625"}, []float64{1.25, 2.125, 3.0625, 4.03125, 5.015625}, nil},
{"Single invalid string", []string{"abc"}, nil, &strconv.NumError{Func: "ParseFloat", Num: "abc", Err: strconv.ErrSyntax}},
{"Multiple invalid strings", []string{"abc", "def", "ghi"}, nil, &strconv.NumError{Func: "ParseFloat", Num: "abc", Err: strconv.ErrSyntax}},
{"Mixed valid and invalid strings", []string{"1.25", "abc"}, nil, &strconv.NumError{Func: "ParseFloat", Num: "abc", Err: strconv.ErrSyntax}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
flts, err := Atof(test.strings)
if !reflect.DeepEqual(errors.Cause(err), test.wantErr) {
t.Errorf("got: <%v>, want: <%v>", errors.Cause(err), test.wantErr)
}

if !reflect.DeepEqual(flts, test.wantFlts) {
t.Errorf("got: <%v>, want: <%v>", flts, test.wantFlts)
}
})
}
}

0 comments on commit 1805bc2

Please sign in to comment.