]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/basic_ostream/inserters_arithmetic/wchar_t/1.cc
re PR c++/59378 (Internal compiler error when using __builtin_shuffle in a template...
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / basic_ostream / inserters_arithmetic / wchar_t / 1.cc
1 // 1999-11-15 Kevin Ediger <kediger@licor.com>
2 // test the floating point inserters (facet num_put)
3
4 // Copyright (C) 1999-2013 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
20
21 #include <iostream>
22 #include <iomanip>
23 #include <sstream>
24 #include <limits>
25 #include <testsuite_hooks.h>
26
27 using namespace std;
28
29 #ifndef _GLIBCXX_ASSERT
30 # define TEST_NUMPUT_VERBOSE 1
31 #endif
32
33 struct _TestCase
34 {
35 double val;
36
37 int precision;
38 int width;
39 char decimal;
40 char fill;
41
42 bool fixed;
43 bool scientific;
44 bool showpos;
45 bool showpoint;
46 bool uppercase;
47 bool internal;
48 bool left;
49 bool right;
50
51 const char* result;
52 const wchar_t* wresult;
53 };
54
55 static bool T=true;
56 static bool F=false;
57
58 static _TestCase testcases[] =
59 {
60 // standard output (no formatting applied) 1-4
61 { 1.2, 6,0,'.',' ', F,F,F,F,F,F,F,F, "1.2",L"1.2" },
62 { 54, 6,0,'.',' ', F,F,F,F,F,F,F,F, "54",L"54" },
63 { -.012, 6,0,'.',' ', F,F,F,F,F,F,F,F, "-0.012",L"-0.012" },
64 { -.00000012, 6,0,'.',' ', F,F,F,F,F,F,F,F, "-1.2e-07",L"-1.2e-07" },
65
66 // fixed formatting 5-11
67 { 10.2345, 0,0,'.',' ', T,F,F,F,F,F,F,F, "10",L"10" },
68 { 10.2345, 0,0,'.',' ', T,F,F,T,F,F,F,F, "10.",L"10." },
69 { 10.2345, 1,0,'.',' ', T,F,F,F,F,F,F,F, "10.2",L"10.2" },
70 { 10.2345, 4,0,'.',' ', T,F,F,F,F,F,F,F, "10.2345",L"10.2345" },
71 { 10.2345, 6,0,'.',' ', T,F,T,F,F,F,F,F, "+10.234500",L"+10.234500" },
72 { -10.2345, 6,0,'.',' ', T,F,F,F,F,F,F,F, "-10.234500",L"-10.234500" },
73 { -10.2345, 6,0,',',' ', T,F,F,F,F,F,F,F, "-10,234500",L"-10,234500" },
74
75 // fixed formatting with width 12-22
76 { 10.2345, 4,5,'.',' ', T,F,F,F,F,F,F,F, "10.2345",L"10.2345" },
77 { 10.2345, 4,6,'.',' ', T,F,F,F,F,F,F,F, "10.2345",L"10.2345" },
78 { 10.2345, 4,7,'.',' ', T,F,F,F,F,F,F,F, "10.2345",L"10.2345" },
79 { 10.2345, 4,8,'.',' ', T,F,F,F,F,F,F,F, " 10.2345",L" 10.2345" },
80 { 10.2345, 4,10,'.',' ', T,F,F,F,F,F,F,F, " 10.2345",L" 10.2345" },
81 { 10.2345, 4,10,'.',' ', T,F,F,F,F,F,T,F, "10.2345 ",L"10.2345 " },
82 { 10.2345, 4,10,'.',' ', T,F,F,F,F,F,F,T, " 10.2345",L" 10.2345" },
83 { 10.2345, 4,10,'.',' ', T,F,F,F,F,T,F,F, " 10.2345",L" 10.2345" },
84 { -10.2345, 4,10,'.',' ', T,F,F,F,F,T,F,F, "- 10.2345",L"- 10.2345" },
85 { -10.2345, 4,10,'.','A', T,F,F,F,F,T,F,F, "-AA10.2345",L"-AA10.2345" },
86 { 10.2345, 4,10,'.','#', T,F,T,F,F,T,F,F, "+##10.2345",L"+##10.2345" },
87
88 // scientific formatting 23-29
89 { 1.23e+12, 1,0,'.',' ', F,T,F,F,F,F,F,F, "1.2e+12",L"1.2e+12" },
90 { 1.23e+12, 1,0,'.',' ', F,T,F,F,T,F,F,F, "1.2E+12",L"1.2E+12" },
91 { 1.23e+12, 2,0,'.',' ', F,T,F,F,F,F,F,F, "1.23e+12",L"1.23e+12" },
92 { 1.23e+12, 3,0,'.',' ', F,T,F,F,F,F,F,F, "1.230e+12",L"1.230e+12" },
93 { 1.23e+12, 3,0,'.',' ', F,T,T,F,F,F,F,F, "+1.230e+12",L"+1.230e+12" },
94 { -1.23e-12, 3,0,'.',' ', F,T,F,F,F,F,F,F, "-1.230e-12",L"-1.230e-12" },
95 { 1.23e+12, 3,0,',',' ', F,T,F,F,F,F,F,F, "1,230e+12",L"1,230e+12" },
96 };
97
98 template<typename _CharT>
99 class testpunct : public numpunct<_CharT>
100 {
101 public:
102 typedef _CharT char_type;
103 const char_type dchar;
104
105 explicit
106 testpunct(char_type decimal_char) : numpunct<_CharT>(), dchar(decimal_char)
107 { }
108
109 protected:
110 char_type
111 do_decimal_point() const
112 { return dchar; }
113
114 char_type
115 do_thousands_sep() const
116 { return ','; }
117
118 string
119 do_grouping() const
120 { return string(); }
121 };
122
123 template<typename _CharT>
124 void apply_formatting(const _TestCase & tc, basic_ostream<_CharT> & os)
125 {
126 os.precision(tc.precision);
127 os.width(tc.width);
128 os.fill(static_cast<_CharT>(tc.fill));
129 if (tc.fixed)
130 os.setf(ios::fixed);
131 if (tc.scientific)
132 os.setf(ios::scientific);
133 if (tc.showpos)
134 os.setf(ios::showpos);
135 if (tc.showpoint)
136 os.setf(ios::showpoint);
137 if (tc.uppercase)
138 os.setf(ios::uppercase);
139 if (tc.internal)
140 os.setf(ios::internal);
141 if (tc.left)
142 os.setf(ios::left);
143 if (tc.right)
144 os.setf(ios::right);
145 }
146
147 void
148 test01()
149 {
150 bool test __attribute__((unused)) = true;
151 for (std::size_t j = 0; j<sizeof(testcases)/sizeof(testcases[0]); j++)
152 {
153 _TestCase & tc = testcases[j];
154 #ifdef TEST_NUMPUT_VERBOSE
155 cout << "expect: " << tc.result << endl;
156 #endif
157 // test double with wchar_t type
158 {
159 testpunct<wchar_t>* __tp = new testpunct<wchar_t>(tc.decimal);
160 wostringstream os;
161 locale __loc(os.getloc(), __tp);
162 os.imbue(__loc);
163 apply_formatting(tc, os);
164 os << tc.val;
165 VERIFY( os && os.str() == tc.wresult );
166 }
167 // test long double with wchar_t type
168 {
169 testpunct<wchar_t>* __tp = new testpunct<wchar_t>(tc.decimal);
170 wostringstream os;
171 locale __loc(os.getloc(), __tp);
172 os.imbue(__loc);
173 apply_formatting(tc, os);
174 os << (long double)tc.val;
175 VERIFY( os && os.str() == tc.wresult );
176 }
177 }
178 }
179
180 int
181 main()
182 {
183 test01();
184 return 0;
185 }