Skip to content

Commit

Permalink
- added some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pjebs committed Jan 29, 2020
1 parent a16a927 commit 8df2934
Showing 1 changed file with 111 additions and 1 deletion.
112 changes: 111 additions & 1 deletion src/strconv/atoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

package strconv_test

import ()
import (
. "strconv"
"testing"
)

// Test cases required:
// hex form
Expand All @@ -15,3 +18,110 @@ import ()
// With and without parentheses
// NaN
// ±Inf

type tcase struct {
str string
expAnswer complex128
expErr error
}

func TestParseComplex(t *testing.T) {

tests := []tcase{
{
str: "99",
expAnswer: complex(99, 0),
},
{
str: "+99",
expAnswer: complex(99, 0),
},
{
str: "-99",
expAnswer: complex(-99, 0),
},
{
str: "+1i",
expAnswer: complex(0, 1),
},
{
str: "-1i",
expAnswer: complex(0, -1),
},
{
str: "+3-i",
expAnswer: complex(3, -1),
},
{
str: "+3+i",
expAnswer: complex(3, 1),
},
{
str: "3-i",
expAnswer: complex(3, -1),
},
{
str: "3+i",
expAnswer: complex(3, 1),
},
{
str: "+i",
expAnswer: complex(0, 1),
},
{
str: "-i",
expAnswer: complex(0, -1),
},
{
str: "3e3-i",
expAnswer: complex(3e3, -1),
},
{
str: "-3e3-i",
expAnswer: complex(-3e3, -1),
},
{
str: "+3e3-i",
expAnswer: complex(3e3, -1),
},
{
str: "3e+3-i",
expAnswer: complex(3e+3, -1),
},
{
str: "-3e+3-i",
expAnswer: complex(-3e+3, -1),
},
{
str: "-3e+3-i",
expAnswer: complex(-3e+3, -1),
},
{
str: "+3e+3-3e+3i",
expAnswer: complex(3e+3, -3e+3),
},
{
str: "+3e+3+3e+3i",
expAnswer: complex(3e+3, 3e+3),
},
}

for i, tc := range tests {

got, gotErr := ParseComplex(tc.str, 128)
if gotErr != nil {
if tc.expErr == nil {
t.Errorf("%d: |got: %v |expected: %v", i, gotErr, tc.expErr)
}
} else {
if tc.expErr != nil {
t.Errorf("%d: |got: %v |expected: %v", i, got, tc.expErr)
} else {
if got != tc.expAnswer {
t.Errorf("%d: |got: %v |expected: %v", i, got, tc.expAnswer)
}
}
}
}

}

0 comments on commit 8df2934

Please # to comment.