Debian Codegraph @

libstdc++-v3/testsuite/26_numerics/complex/inserters_extractors/wchar_t/1.cc

raw
1 // Copyright (C) 2007-2025 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18
19 #include <iostream>
20 #include <string>
21 #include <sstream>
22 #include <complex>
23 #include <testsuite_hooks.h>
24 #include <cmath>
25
26 template<typename R>
27 inline bool flteq(R x, R y)
28 {
29 if (x == R(0)) return y == R(0);
30 else return std::fabs(x-y) < 1e-6*std::fabs(x);
31 }
32
33 template<typename R>
34 int
35 test_good(std::wstring str, R x, R y)
36 {
37 std::complex<R> z;
38 wchar_t ch;
39 std::wistringstream iss(str);
40 iss >> z >> ch;
41 VERIFY( iss.good() );
42 VERIFY( flteq(z.real(), x) );
43 VERIFY( flteq(z.imag(), y) );
44 VERIFY( ch == L'#' );
45 return 0;
46 }
47
48 template<typename R>
49 int
50 test_fail(std::wstring str)
51 {
52 std::complex<R> z;
53 std::wistringstream iss(str);
54 iss >> z;
55 VERIFY( iss.fail() && !iss.bad() );
56 return 0;
57 }
58
59 template<typename R>
60 int
61 testall()
62 {
63 test_good<R>(L"(-1.1,3.7)#", -1.1, 3.7);
64 test_good<R>(L"( .7e6 , \n-3.1)#", .7e6, -3.1);
65 test_good<R>(L"(\t0,-1)#", 0.0, -1.0);
66 test_good<R>(L"(-3.14)#", -3.14, 0.0);
67 test_good<R>(L"-.1#", -.1, 0.0);
68 test_good<R>(L" ( -2.7e3 )#", -2.7e3, 0.0);
69 test_good<R>(L" -.1#", -.1, 0.0);
70 test_fail<R>(L"(a,1)");
71 test_fail<R>(L"(,1)");
72 test_fail<R>(L"(1,a)");
73 test_fail<R>(L"(1, )");
74 test_fail<R>(L"|1,1)");
75 test_fail<R>(L"(1|1)");
76 test_fail<R>(L"(1,1|");
77 return 0;
78 }
79
80 // libstdc++/2970
81 void test01()
82 {
83 using namespace std;
84
85 complex<float> cf01(-1.1, -333.2);
86 wstringstream ss;
87 ss << cf01;
88 wstring str = ss.str();
89 VERIFY( str == L"(-1.1,-333.2)" );
90 }
91
92 // libstdc++/2985
93 struct gnu_char_traits : public std::char_traits<wchar_t>
94 { };
95
96 typedef std::basic_ostringstream<wchar_t, gnu_char_traits> gnu_sstream;
97 template class std::basic_string<wchar_t, gnu_char_traits,
98 std::allocator<wchar_t> >;
99
100 void test02()
101 {
102 // Construct locale with specialized facets.
103 typedef gnu_sstream::__num_put_type numput_type;
104 typedef gnu_sstream::__num_get_type numget_type;
105 std::locale loc_c = std::locale::classic();
106 std::locale loc_1(loc_c, new numput_type);
107 std::locale loc_2(loc_1, new numget_type);
108 VERIFY( std::has_facet<numput_type>(loc_2) );
109 VERIFY( std::has_facet<numget_type>(loc_2) );
110
111 gnu_sstream sstr;
112 sstr.imbue(loc_2);
113
114
115 std::complex<double> x(3, 4);
116 sstr << x;
117 VERIFY( sstr.str() == L"(3,4)" );
118 }
119
120 int
121 main()
122 {
123 testall<float>();
124 testall<double>();
125 testall<long double>();
126
127 test01();
128 test02();
129
130 return 0;
131 }