Skip to content

Commit 8df2934

Browse files
committed
- added some tests
1 parent a16a927 commit 8df2934

File tree

1 file changed

+111
-1
lines changed

1 file changed

+111
-1
lines changed

src/strconv/atoc_test.go

+111-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
package strconv_test
66

7-
import ()
7+
import (
8+
. "strconv"
9+
"testing"
10+
)
811

912
// Test cases required:
1013
// hex form
@@ -15,3 +18,110 @@ import ()
1518
// With and without parentheses
1619
// NaN
1720
// ±Inf
21+
22+
type tcase struct {
23+
str string
24+
expAnswer complex128
25+
expErr error
26+
}
27+
28+
func TestParseComplex(t *testing.T) {
29+
30+
tests := []tcase{
31+
{
32+
str: "99",
33+
expAnswer: complex(99, 0),
34+
},
35+
{
36+
str: "+99",
37+
expAnswer: complex(99, 0),
38+
},
39+
{
40+
str: "-99",
41+
expAnswer: complex(-99, 0),
42+
},
43+
{
44+
str: "+1i",
45+
expAnswer: complex(0, 1),
46+
},
47+
{
48+
str: "-1i",
49+
expAnswer: complex(0, -1),
50+
},
51+
{
52+
str: "+3-i",
53+
expAnswer: complex(3, -1),
54+
},
55+
{
56+
str: "+3+i",
57+
expAnswer: complex(3, 1),
58+
},
59+
{
60+
str: "3-i",
61+
expAnswer: complex(3, -1),
62+
},
63+
{
64+
str: "3+i",
65+
expAnswer: complex(3, 1),
66+
},
67+
{
68+
str: "+i",
69+
expAnswer: complex(0, 1),
70+
},
71+
{
72+
str: "-i",
73+
expAnswer: complex(0, -1),
74+
},
75+
{
76+
str: "3e3-i",
77+
expAnswer: complex(3e3, -1),
78+
},
79+
{
80+
str: "-3e3-i",
81+
expAnswer: complex(-3e3, -1),
82+
},
83+
{
84+
str: "+3e3-i",
85+
expAnswer: complex(3e3, -1),
86+
},
87+
{
88+
str: "3e+3-i",
89+
expAnswer: complex(3e+3, -1),
90+
},
91+
{
92+
str: "-3e+3-i",
93+
expAnswer: complex(-3e+3, -1),
94+
},
95+
{
96+
str: "-3e+3-i",
97+
expAnswer: complex(-3e+3, -1),
98+
},
99+
{
100+
str: "+3e+3-3e+3i",
101+
expAnswer: complex(3e+3, -3e+3),
102+
},
103+
{
104+
str: "+3e+3+3e+3i",
105+
expAnswer: complex(3e+3, 3e+3),
106+
},
107+
}
108+
109+
for i, tc := range tests {
110+
111+
got, gotErr := ParseComplex(tc.str, 128)
112+
if gotErr != nil {
113+
if tc.expErr == nil {
114+
t.Errorf("%d: |got: %v |expected: %v", i, gotErr, tc.expErr)
115+
}
116+
} else {
117+
if tc.expErr != nil {
118+
t.Errorf("%d: |got: %v |expected: %v", i, got, tc.expErr)
119+
} else {
120+
if got != tc.expAnswer {
121+
t.Errorf("%d: |got: %v |expected: %v", i, got, tc.expAnswer)
122+
}
123+
}
124+
}
125+
}
126+
127+
}

0 commit comments

Comments
 (0)