1
2
3
4
5 package strconv_test
6
7 import (
8 . "internal/strconv"
9 "math"
10 "math/cmplx"
11 "testing"
12 )
13
14 var (
15 infp0 = complex(math.Inf(+1), 0)
16 infm0 = complex(math.Inf(-1), 0)
17 inf0p = complex(0, math.Inf(+1))
18 inf0m = complex(0, math.Inf(-1))
19
20 infpp = complex(math.Inf(+1), math.Inf(+1))
21 infpm = complex(math.Inf(+1), math.Inf(-1))
22 infmp = complex(math.Inf(-1), math.Inf(+1))
23 infmm = complex(math.Inf(-1), math.Inf(-1))
24 )
25
26 type atocTest struct {
27 in string
28 out complex128
29 err error
30 }
31
32 func TestParseComplex(t *testing.T) {
33 tests := []atocTest{
34
35 {"", 0, ErrSyntax},
36 {" ", 0, ErrSyntax},
37 {"(", 0, ErrSyntax},
38 {")", 0, ErrSyntax},
39 {"i", 0, ErrSyntax},
40 {"+i", 0, ErrSyntax},
41 {"-i", 0, ErrSyntax},
42 {"1I", 0, ErrSyntax},
43 {"10 + 5i", 0, ErrSyntax},
44 {"3+", 0, ErrSyntax},
45 {"3+5", 0, ErrSyntax},
46 {"3+5+5i", 0, ErrSyntax},
47
48
49 {"()", 0, ErrSyntax},
50 {"(i)", 0, ErrSyntax},
51 {"(0)", 0, nil},
52 {"(1i)", 1i, nil},
53 {"(3.0+5.5i)", 3.0 + 5.5i, nil},
54 {"(1)+1i", 0, ErrSyntax},
55 {"(3.0+5.5i", 0, ErrSyntax},
56 {"3.0+5.5i)", 0, ErrSyntax},
57
58
59 {"NaN", complex(math.NaN(), 0), nil},
60 {"NANi", complex(0, math.NaN()), nil},
61 {"nan+nAni", complex(math.NaN(), math.NaN()), nil},
62 {"+NaN", 0, ErrSyntax},
63 {"-NaN", 0, ErrSyntax},
64 {"NaN-NaNi", 0, ErrSyntax},
65
66
67 {"Inf", infp0, nil},
68 {"+inf", infp0, nil},
69 {"-inf", infm0, nil},
70 {"Infinity", infp0, nil},
71 {"+INFINITY", infp0, nil},
72 {"-infinity", infm0, nil},
73 {"+infi", inf0p, nil},
74 {"0-infinityi", inf0m, nil},
75 {"Inf+Infi", infpp, nil},
76 {"+Inf-Infi", infpm, nil},
77 {"-Infinity+Infi", infmp, nil},
78 {"inf-inf", 0, ErrSyntax},
79
80
81 {"0", 0, nil},
82 {"0i", 0, nil},
83 {"-0.0i", 0, nil},
84 {"0+0.0i", 0, nil},
85 {"0e+0i", 0, nil},
86 {"0e-0+0i", 0, nil},
87 {"-0.0-0.0i", 0, nil},
88 {"0e+012345", 0, nil},
89 {"0x0p+012345i", 0, nil},
90 {"0x0.00p-012345i", 0, nil},
91 {"+0e-0+0e-0i", 0, nil},
92 {"0e+0+0e+0i", 0, nil},
93 {"-0e+0-0e+0i", 0, nil},
94
95
96 {"0.1", 0.1, nil},
97 {"0.1i", 0 + 0.1i, nil},
98 {"0.123", 0.123, nil},
99 {"0.123i", 0 + 0.123i, nil},
100 {"0.123+0.123i", 0.123 + 0.123i, nil},
101 {"99", 99, nil},
102 {"+99", 99, nil},
103 {"-99", -99, nil},
104 {"+1i", 1i, nil},
105 {"-1i", -1i, nil},
106 {"+3+1i", 3 + 1i, nil},
107 {"30+3i", 30 + 3i, nil},
108 {"+3e+3-3e+3i", 3e+3 - 3e+3i, nil},
109 {"+3e+3+3e+3i", 3e+3 + 3e+3i, nil},
110 {"+3e+3+3e+3i+", 0, ErrSyntax},
111
112
113 {"0.1", 0.1, nil},
114 {"0.1i", 0 + 0.1i, nil},
115 {"0.1_2_3", 0.123, nil},
116 {"+0x_3p3i", 0x3p3i, nil},
117 {"0_0+0x_0p0i", 0, nil},
118 {"0x_10.3p-8+0x3p3i", 0x10.3p-8 + 0x3p3i, nil},
119 {"+0x_1_0.3p-8+0x_3_0p3i", 0x10.3p-8 + 0x30p3i, nil},
120 {"0x1_0.3p+8-0x_3p3i", 0x10.3p+8 - 0x3p3i, nil},
121
122
123 {"0x10.3p-8+0x3p3i", 0x10.3p-8 + 0x3p3i, nil},
124 {"+0x10.3p-8+0x3p3i", 0x10.3p-8 + 0x3p3i, nil},
125 {"0x10.3p+8-0x3p3i", 0x10.3p+8 - 0x3p3i, nil},
126 {"0x1p0", 1, nil},
127 {"0x1p1", 2, nil},
128 {"0x1p-1", 0.5, nil},
129 {"0x1ep-1", 15, nil},
130 {"-0x1ep-1", -15, nil},
131 {"-0x2p3", -16, nil},
132 {"0x1e2", 0, ErrSyntax},
133 {"1p2", 0, ErrSyntax},
134 {"0x1e2i", 0, ErrSyntax},
135
136
137
138 {"+0x1p1024", infp0, ErrRange},
139 {"-0x1p1024", infm0, ErrRange},
140 {"+0x1p1024i", inf0p, ErrRange},
141 {"-0x1p1024i", inf0m, ErrRange},
142 {"+0x1p1024+0x1p1024i", infpp, ErrRange},
143 {"+0x1p1024-0x1p1024i", infpm, ErrRange},
144 {"-0x1p1024+0x1p1024i", infmp, ErrRange},
145 {"-0x1p1024-0x1p1024i", infmm, ErrRange},
146
147
148 {"+0x1.fffffffffffff7fffp1023+0x1.fffffffffffff7fffp1023i", 1.7976931348623157e+308 + 1.7976931348623157e+308i, nil},
149 {"+0x1.fffffffffffff7fffp1023-0x1.fffffffffffff7fffp1023i", 1.7976931348623157e+308 - 1.7976931348623157e+308i, nil},
150 {"-0x1.fffffffffffff7fffp1023+0x1.fffffffffffff7fffp1023i", -1.7976931348623157e+308 + 1.7976931348623157e+308i, nil},
151 {"-0x1.fffffffffffff7fffp1023-0x1.fffffffffffff7fffp1023i", -1.7976931348623157e+308 - 1.7976931348623157e+308i, nil},
152
153 {"+0x1.fffffffffffff8p1023", infp0, ErrRange},
154 {"-0x1fffffffffffff.8p+971", infm0, ErrRange},
155 {"+0x1.fffffffffffff8p1023i", inf0p, ErrRange},
156 {"-0x1fffffffffffff.8p+971i", inf0m, ErrRange},
157 {"+0x1.fffffffffffff8p1023+0x1.fffffffffffff8p1023i", infpp, ErrRange},
158 {"+0x1.fffffffffffff8p1023-0x1.fffffffffffff8p1023i", infpm, ErrRange},
159 {"-0x1fffffffffffff.8p+971+0x1fffffffffffff.8p+971i", infmp, ErrRange},
160 {"-0x1fffffffffffff8p+967-0x1fffffffffffff8p+967i", infmm, ErrRange},
161
162 {"1e308+1e308i", 1e+308 + 1e+308i, nil},
163 {"2e308+2e308i", infpp, ErrRange},
164 {"1e309+1e309i", infpp, ErrRange},
165 {"0x1p1025+0x1p1025i", infpp, ErrRange},
166 {"2e308", infp0, ErrRange},
167 {"1e309", infp0, ErrRange},
168 {"0x1p1025", infp0, ErrRange},
169 {"2e308i", inf0p, ErrRange},
170 {"1e309i", inf0p, ErrRange},
171 {"0x1p1025i", inf0p, ErrRange},
172
173 {"+1e310+1e310i", infpp, ErrRange},
174 {"+1e310-1e310i", infpm, ErrRange},
175 {"-1e310+1e310i", infmp, ErrRange},
176 {"-1e310-1e310i", infmm, ErrRange},
177
178 {"1e-4294967296", 0, nil},
179 {"1e-4294967296i", 0, nil},
180 {"1e-4294967296+1i", 1i, nil},
181 {"1+1e-4294967296i", 1, nil},
182 {"1e-4294967296+1e-4294967296i", 0, nil},
183 {"1e+4294967296", infp0, ErrRange},
184 {"1e+4294967296i", inf0p, ErrRange},
185 {"1e+4294967296+1e+4294967296i", infpp, ErrRange},
186 {"1e+4294967296-1e+4294967296i", infpm, ErrRange},
187 }
188 for i := range tests {
189 test := &tests[i]
190 c, e := ParseComplex(test.in, 128)
191 if !sameComplex(c, test.out) || e != test.err {
192 t.Errorf("ParseComplex(%s, 128) = %v, %v, want %v, %v", test.in, c, e, test.out, test.err)
193 }
194 if complex128(complex64(test.out)) == test.out {
195 c, e := ParseComplex(test.in, 64)
196 c64 := complex64(c)
197 if !sameComplex(complex128(c64), test.out) || e != test.err {
198 t.Errorf("ParseComplex(%s, 64) = %v, %v, want %v, %v", test.in, c, e, test.out, test.err)
199 }
200 }
201 }
202 }
203
204 func sameComplex(c1, c2 complex128) bool {
205 return cmplx.IsNaN(c1) && cmplx.IsNaN(c2) || c1 == c2
206 }
207
208
209 func TestParseComplexIncorrectBitSize(t *testing.T) {
210 const s = "1.5e308+1.0e307i"
211 const want = 1.5e308 + 1.0e307i
212
213 for _, bitSize := range []int{0, 10, 100, 256} {
214 c, err := ParseComplex(s, bitSize)
215 if err != nil {
216 t.Fatalf("ParseComplex(%q, %d) gave error %s", s, bitSize, err)
217 }
218 if c != want {
219 t.Fatalf("ParseComplex(%q, %d) = %g (expected %g)", s, bitSize, c, want)
220 }
221 }
222 }
223
View as plain text