| 1 |
// Copyright 2020 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package strconv_test |
| 6 |
|
| 7 |
import ( |
| 8 |
. "internal/strconv" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
func TestFormatComplex(t *testing.T) { |
| 13 |
tests := []struct { |
| 14 |
c complex128 |
| 15 |
fmt byte |
| 16 |
prec int |
| 17 |
bitSize int |
| 18 |
out string |
| 19 |
}{ |
| 20 |
// a variety of signs |
| 21 |
{1 + 2i, 'g', -1, 128, "(1+2i)"}, |
| 22 |
{3 - 4i, 'g', -1, 128, "(3-4i)"}, |
| 23 |
{-5 + 6i, 'g', -1, 128, "(-5+6i)"}, |
| 24 |
{-7 - 8i, 'g', -1, 128, "(-7-8i)"}, |
| 25 |
|
| 26 |
// test that fmt and prec are working |
| 27 |
{3.14159 + 0.00123i, 'e', 3, 128, "(3.142e+00+1.230e-03i)"}, |
| 28 |
{3.14159 + 0.00123i, 'f', 3, 128, "(3.142+0.001i)"}, |
| 29 |
{3.14159 + 0.00123i, 'g', 3, 128, "(3.14+0.00123i)"}, |
| 30 |
|
| 31 |
// ensure bitSize rounding is working |
| 32 |
{1.2345678901234567 + 9.876543210987654i, 'f', -1, 128, "(1.2345678901234567+9.876543210987654i)"}, |
| 33 |
{1.2345678901234567 + 9.876543210987654i, 'f', -1, 64, "(1.2345679+9.876543i)"}, |
| 34 |
|
| 35 |
// other cases are handled by FormatFloat tests |
| 36 |
} |
| 37 |
for _, test := range tests { |
| 38 |
out := FormatComplex(test.c, test.fmt, test.prec, test.bitSize) |
| 39 |
if out != test.out { |
| 40 |
t.Fatalf("FormatComplex(%v, %q, %d, %d) = %q; want %q", |
| 41 |
test.c, test.fmt, test.prec, test.bitSize, out, test.out) |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
func TestFormatComplexInvalidBitSize(t *testing.T) { |
| 47 |
defer func() { |
| 48 |
if r := recover(); r == nil { |
| 49 |
t.Fatalf("expected panic due to invalid bitSize") |
| 50 |
} |
| 51 |
}() |
| 52 |
_ = FormatComplex(1+2i, 'g', -1, 100) |
| 53 |
} |