Skip to content

Commit

Permalink
Merge pull request #11 from Henry-Sarabia/iss-1
Browse files Browse the repository at this point in the history
Iss 1
  • Loading branch information
Henry Sarabia authored Feb 20, 2019
2 parents 546afbe + 50bbd71 commit 95a1654
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 5 deletions.
31 changes: 31 additions & 0 deletions sliceconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
// representations. If any of the strings are not valid digits, an error is returned.
func Atoi(str []string) ([]int, error) {
var ints []int

for _, s := range str {
i, err := strconv.Atoi(s)
if err != nil {
Expand All @@ -25,8 +26,38 @@ func Atoi(str []string) ([]int, error) {
// in base 10.
func Itoa(ints []int) []string {
var str []string

for _, i := range ints {
str = append(str, strconv.Itoa(i))
}
return str
}

// Atof converts the provided strings into their respective float representations.
// If any of the strings are not valid floats, an error is returned.
func Atof(str []string) ([]float64, error) {
var flts []float64

for _, s := range str {
f, err := strconv.ParseFloat(s, 64)
if err != nil {
return nil, errors.Wrap(err, "one or more strings could not be converted to type float64")
}

flts = append(flts, f)
}

return flts, nil
}

// Ftoa converts the provided floats into their respective string representations
// with no exponent, the smallest precision needed, and a bitsize of 64.
func Ftoa(flts []float64) []string {
var str []string

for _, f := range flts {
str = append(str, strconv.FormatFloat(f, 'f', -1, 64))
}

return str
}
61 changes: 56 additions & 5 deletions sliceconv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ func TestAtoi(t *testing.T) {

func TestItoa(t *testing.T) {
tests := []struct {
name string
ints []int
wantStrings []string
name string
ints []int
wantStr []string
}{
{"Nil slice", nil, nil},
{"Empty int slice", []int{}, nil},
Expand All @@ -52,8 +52,59 @@ func TestItoa(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
s := Itoa(test.ints)

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

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)
}
})
}
}

func TestFtoa(t *testing.T) {
tests := []struct {
name string
flts []float64
wantStr []string
}{
{"Nil slice", nil, nil},
{"Empty float slice", []float64{}, nil},
{"Single float", []float64{1.5}, []string{"1.5"}},
{"Multiple floats", []float64{1.25, 2.125, 3.0625, 4.03125, 5.015625}, []string{"1.25", "2.125", "3.0625", "4.03125", "5.015625"}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
s := Ftoa(test.flts)

if !reflect.DeepEqual(s, test.wantStr) {
t.Errorf("got: <%v>, want: <%v>", s, test.wantStr)
}
})
}
Expand Down

0 comments on commit 95a1654

Please sign in to comment.