github.com/gopherd/gonum@v0.0.4/cmplxs/cscalar/parse_test.go (about) 1 // Copyright ©2020 The Gonum Authors. All rights reserved. 2 // Use of this code is governed by a BSD-style 3 // license that can be found in the LICENSE file 4 5 package cscalar 6 7 import ( 8 "math" 9 "math/cmplx" 10 "testing" 11 ) 12 13 var parseTests = []struct { 14 s string 15 want complex128 16 wantErr error 17 }{ 18 // Simple error states: 19 {s: "", wantErr: parseError{state: -1}}, 20 {s: "()", wantErr: parseError{string: "()", state: -1}}, 21 {s: "(1", wantErr: parseError{string: "(1", state: -1}}, 22 {s: "1)", wantErr: parseError{string: "1)", state: -1}}, 23 24 // Ambiguous parse error states: 25 {s: "1+2i+3i", wantErr: parseError{string: "1+2i+3i", state: -1}}, 26 {s: "1e-4i+", wantErr: parseError{string: "1e-4i+", state: -1}}, 27 {s: "1e-4i-", wantErr: parseError{string: "1e-4i-", state: -1}}, 28 29 // Valid input: 30 {s: "1+4i", want: 1 + 4i}, 31 {s: "4i+1", want: 1 + 4i}, 32 {s: "+1+4i", want: 1 + 4i}, 33 {s: "+4i+1", want: 1 + 4i}, 34 {s: ".1+.4i", want: 0.1 + 0.4i}, 35 {s: ".4i+.1", want: 0.1 + 0.4i}, 36 {s: "+.1+.4i", want: 0.1 + 0.4i}, 37 {s: "+.4i+.1", want: 0.1 + 0.4i}, 38 {s: "1.+4.i", want: 1 + 4i}, 39 {s: "4.i+1.", want: 1 + 4i}, 40 {s: "+1.+4.i", want: 1 + 4i}, 41 {s: "+4.i+1.", want: 1 + 4i}, 42 {s: "1.0+4.0i", want: 1 + 4i}, 43 {s: "4.0i+1.0", want: 1 + 4i}, 44 {s: "+1.0+4.0i", want: 1 + 4i}, 45 {s: "+4.0i+1.0", want: 1 + 4i}, 46 {s: "1.0e-4+1i", want: 1e-4 + 1i}, 47 {s: "1.0e-4+i", want: 1e-4 + 1i}, 48 {s: "1.0e-4-i", want: 1e-4 - 1i}, 49 {s: "1.0e-4i-1", want: -1 + 1e-4i}, 50 {s: "1.0e-4i+1", want: 1 + 1e-4i}, 51 {s: "1e-4+1i", want: 1e-4 + 1i}, 52 {s: "1e-4+i", want: 1e-4 + 1i}, 53 {s: "1e-4-i", want: 1e-4 - 1i}, 54 {s: "1e-4i-1", want: -1 + 1e-4i}, 55 {s: "1e-4i+1", want: 1 + 1e-4i}, 56 {s: "(1+4i)", want: 1 + 4i}, 57 {s: "(4i+1)", want: 1 + 4i}, 58 {s: "(+1+4i)", want: 1 + 4i}, 59 {s: "(+4i+1)", want: 1 + 4i}, 60 {s: "(1e-4+1i)", want: 1e-4 + 1i}, 61 {s: "(1e-4+i)", want: 1e-4 + 1i}, 62 {s: "(1e-4-i)", want: 1e-4 - 1i}, 63 {s: "(1e-4i-1)", want: -1 + 1e-4i}, 64 {s: "(1e-4i+1)", want: 1 + 1e-4i}, 65 {s: "NaN", want: cmplx.NaN()}, 66 {s: "nan", want: cmplx.NaN()}, 67 {s: "Inf", want: cmplx.Inf()}, 68 {s: "inf", want: cmplx.Inf()}, 69 {s: "(Inf+Infi)", want: complex(math.Inf(1), math.Inf(1))}, 70 {s: "(-Inf+Infi)", want: complex(math.Inf(-1), math.Inf(1))}, 71 {s: "(+Inf-Infi)", want: complex(math.Inf(1), math.Inf(-1))}, 72 {s: "(inf+infi)", want: complex(math.Inf(1), math.Inf(1))}, 73 {s: "(-inf+infi)", want: complex(math.Inf(-1), math.Inf(1))}, 74 {s: "(+inf-infi)", want: complex(math.Inf(1), math.Inf(-1))}, 75 {s: "(nan+nani)", want: complex(math.NaN(), math.NaN())}, 76 {s: "(nan-nani)", want: complex(math.NaN(), math.NaN())}, 77 } 78 79 func TestParse(t *testing.T) { 80 for _, test := range parseTests { 81 got, err := parse(test.s) 82 if err != test.wantErr { 83 t.Errorf("unexpected error for Parse(%q): got:%#v, want:%#v", test.s, err, test.wantErr) 84 } 85 if err != nil { 86 continue 87 } 88 if !Same(got, test.want) { 89 t.Errorf("unexpected result for Parse(%q): got:%v, want:%v", test.s, got, test.want) 90 } 91 } 92 }