summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/20_util/from_chars/5.cc
blob: 6aaf8e27eb3ac1c3bb7ec568fa6acecd96582c95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (C) 2020-2022 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

// <charconv> is supported in C++14 as a GNU extension
// { dg-do run { target c++14 } }

#include <charconv>
#include <string>
#include <cmath>
#include <testsuite_hooks.h>

// Test std::from_chars error handling.

#if __cpp_lib_to_chars >= 201611L
void
test01()
{
  std::from_chars_result r;
  double d = 3.2;
  std::string s;

  for (auto p : { "", "*", ".", "-", "-*", "-.", "+", "+.", "+-", "-+", "+1",
		  ".p1", "-.p1",
		  "in", "inch", "+inf", "na", "nam", "+nan" })
  {
    s = p;
    for (auto fmt : { std::chars_format::fixed, std::chars_format::scientific,
		      std::chars_format::general, std::chars_format::hex })
    {
      r = std::from_chars(s.data(), s.data() + s.length(), d, fmt);
      VERIFY( r.ec == std::errc::invalid_argument );
      VERIFY( r.ptr == s.data() );
      VERIFY( d == 3.2 );
    }
  }

  for (auto p : { ".e1", "-.e1" }) // These are valid patterns for hex format
  {
    s = p;
    for (auto fmt : { std::chars_format::fixed, std::chars_format::scientific,
		      std::chars_format::general })
    {
      r = std::from_chars(s.data(), s.data() + s.length(), d, fmt);
      VERIFY( r.ec == std::errc::invalid_argument );
      VERIFY( r.ptr == s.data() );
      VERIFY( d == 3.2 );
    }
  }

  // scientific format requires an exponent
  for (auto p : { "1.2", "-1.2", "1.2e", "-1.2e", "1.2e-", "-1.2e+" })
  {
    s = p;
    r = std::from_chars(s.data(), s.data() + s.length(), d,
			std::chars_format::scientific);
    VERIFY( r.ec == std::errc::invalid_argument );
    VERIFY( r.ptr == s.data() );
    VERIFY( d == 3.2 );
  }

  // patterns that are invalid without the final character
  for (auto p : { "1", ".1", "-1", "-.1",
		  "inf", "-inf", "nan", "-nan" })
  {
    s = p;
    for (auto fmt : { std::chars_format::fixed, std::chars_format::scientific,
		      std::chars_format::general, std::chars_format::hex })
    {
      r = std::from_chars(s.data(), s.data() + s.length() - 1, d, fmt);
      VERIFY( r.ec == std::errc::invalid_argument );
      VERIFY( r.ptr == s.data() );
      VERIFY( d == 3.2 );
    }
  }
}

void
test02()
{
  std::from_chars_result r;
  std::string s;

  float f = 0.5;
  // Overflow
  s = "99999999999999999e999999999999999999";
  r = std::from_chars(s.data(), s.data() + s.length(), f);
  VERIFY( r.ec == std::errc::result_out_of_range );
  VERIFY( r.ptr == s.data() + s.length() );
  VERIFY( f == 0.5 );

  s += '*';
  r = std::from_chars(s.data(), s.data() + s.length(), f);
  VERIFY( r.ec == std::errc::result_out_of_range );
  VERIFY( r.ptr == s.data() + s.length() - 1 );
  VERIFY( f == 0.5 );

  s.insert(s.begin(), '-');
  r = std::from_chars(s.data(), s.data() + s.length(), f);
  VERIFY( r.ec == std::errc::result_out_of_range );
  VERIFY( r.ptr == s.data() + s.length() - 1 );
  VERIFY( f == 0.5 );
}

void
test03()
{
  double d = 0.5;
  // Underflow
  std::string s("-1.2345e-9999zzz");
  std::from_chars_result res;
  res = std::from_chars(s.data(), s.data() + s.length(), d);
  VERIFY( res.ptr == s.data() + s.length() - 3 );
  VERIFY( res.ec == std::errc::result_out_of_range );
  VERIFY( d == 0.5 );

  res = std::from_chars(s.data() + 1, s.data() + s.length(), d);
  VERIFY( res.ptr == s.data() + s.length() - 3 );
  VERIFY( res.ec == std::errc::result_out_of_range );
  VERIFY( d == 0.5 );
}

void
test04()
{
  std::from_chars_result res;
  std::string z(2000, '0');
  // Invalid inputs for scientific format
  for (const char* s : { "", "1", ".", ".0", ".5", "1e+", "1e+-1" })
  {
    for (auto len : { 0, 10, 100, 1000, 2000 })
    {
      auto str = z.substr(len) + s;
      double d = 99.0;
      res = std::from_chars(str.data(), str.data() + str.length(), d,
			    std::chars_format::scientific);
      VERIFY( res.ec == std::errc::invalid_argument );
      VERIFY( res.ptr == str.data() );
      VERIFY( d == 99.0 );
    }
  }
}
#endif

int
main()
{
#if __cpp_lib_to_chars >= 201611L
  test01();
  test02();
  test03();
  test04();
#endif
}