ctoa_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. package strconv_test
  5. import (
  6. . "strconv"
  7. "testing"
  8. )
  9. func TestFormatComplex(t *testing.T) {
  10. tests := []struct {
  11. c complex128
  12. fmt byte
  13. prec int
  14. bitSize int
  15. out string
  16. }{
  17. // a variety of signs
  18. {1 + 2i, 'g', -1, 128, "(1+2i)"},
  19. {3 - 4i, 'g', -1, 128, "(3-4i)"},
  20. {-5 + 6i, 'g', -1, 128, "(-5+6i)"},
  21. {-7 - 8i, 'g', -1, 128, "(-7-8i)"},
  22. // test that fmt and prec are working
  23. {3.14159 + 0.00123i, 'e', 3, 128, "(3.142e+00+1.230e-03i)"},
  24. {3.14159 + 0.00123i, 'f', 3, 128, "(3.142+0.001i)"},
  25. {3.14159 + 0.00123i, 'g', 3, 128, "(3.14+0.00123i)"},
  26. // ensure bitSize rounding is working
  27. {1.2345678901234567 + 9.876543210987654i, 'f', -1, 128, "(1.2345678901234567+9.876543210987654i)"},
  28. {1.2345678901234567 + 9.876543210987654i, 'f', -1, 64, "(1.2345679+9.876543i)"},
  29. // other cases are handled by FormatFloat tests
  30. }
  31. for _, test := range tests {
  32. out := FormatComplex(test.c, test.fmt, test.prec, test.bitSize)
  33. if out != test.out {
  34. t.Fatalf("FormatComplex(%v, %q, %d, %d) = %q; want %q",
  35. test.c, test.fmt, test.prec, test.bitSize, out, test.out)
  36. }
  37. }
  38. }
  39. func TestFormatComplexInvalidBitSize(t *testing.T) {
  40. defer func() {
  41. if r := recover(); r == nil {
  42. t.Fatalf("expected panic due to invalid bitSize")
  43. }
  44. }()
  45. _ = FormatComplex(1+2i, 'g', -1, 100)
  46. }