OSDN Git Service

2010-06-07 Paolo Carlini <paolo.carlini@oracle.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 22_locale / num_put / put / wchar_t / 1.cc
1 // { dg-require-namedlocale "" }
2
3 // 2001-11-19 Benjamin Kosnik  <bkoz@redhat.com>
4
5 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010
6 // Free Software Foundation
7 //
8 // This file is part of the GNU ISO C++ Library.  This library is free
9 // software; you can redistribute it and/or modify it under the
10 // terms of the GNU General Public License as published by the
11 // Free Software Foundation; either version 3, or (at your option)
12 // any later version.
13
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18
19 // You should have received a copy of the GNU General Public License along
20 // with this library; see the file COPYING3.  If not see
21 // <http://www.gnu.org/licenses/>.
22
23 // 22.2.2.2.1  num_put members
24
25 #include <locale>
26 #include <sstream>
27 #include <testsuite_hooks.h>
28
29 void test01()
30 {
31   using namespace std;
32   typedef ostreambuf_iterator<wchar_t> iterator_type;
33
34   bool test __attribute__((unused)) = true;
35
36   // basic construction
37   locale loc_c = locale::classic();
38   locale loc_de = locale("de_DE");
39   VERIFY( loc_c != loc_de );
40
41   // cache the numpunct facets
42   const numpunct<wchar_t>& numpunct_de = use_facet<numpunct<wchar_t> >(loc_de);
43  
44   // sanity check the data is correct.
45   const wstring empty;
46   wstring result1;
47   wstring result2;
48
49   bool b1 = true;
50   bool b0 = false;
51   unsigned long ul1 = 1294967294;
52   double d1 =  1.7976931348623157e+308;
53   double d2 = 2.2250738585072014e-308;
54   long double ld1 = 1.7976931348623157e+308;
55   long double ld2 = 2.2250738585072014e-308;
56   const void* cv = &ld1;
57
58   // cache the num_put facet
59   wostringstream oss;
60   oss.imbue(loc_de);
61   const num_put<wchar_t>& np = use_facet<num_put<wchar_t> >(oss.getloc()); 
62
63   // bool, simple
64   iterator_type os_it00 = oss.rdbuf();
65   np.put(os_it00, oss, L'+', b1);
66   result1 = oss.str();
67   VERIFY( result1 == L"1" );
68
69   oss.str(empty);
70   np.put(oss.rdbuf(), oss, L'+', b0);
71   result2 = oss.str();
72   VERIFY( result2 == L"0" );
73
74   // ... and one that does
75   oss.imbue(loc_de);
76   oss.str(empty);
77   oss.clear();
78   oss.width(20);
79   oss.setf(ios_base::left, ios_base::adjustfield);
80   np.put(oss.rdbuf(), oss, L'+', ul1);
81   result1 = oss.str();
82   VERIFY( result1 == L"1.294.967.294+++++++" );
83
84   // double
85   oss.str(empty);
86   oss.clear();
87   oss.width(20);
88   oss.setf(ios_base::left, ios_base::adjustfield);
89   np.put(oss.rdbuf(), oss, L'+', d1);
90   result1 = oss.str();
91   VERIFY( result1 == L"1,79769e+308++++++++" );
92
93   oss.str(empty);
94   oss.clear();
95   oss.width(20);
96   oss.setf(ios_base::right, ios_base::adjustfield);
97   np.put(oss.rdbuf(), oss, L'+', d2);
98   result1 = oss.str();
99   VERIFY( result1 == L"++++++++2,22507e-308" );
100
101   oss.str(empty);
102   oss.clear();
103   oss.width(20);
104   oss.setf(ios_base::right, ios_base::adjustfield);
105   oss.setf(ios_base::scientific, ios_base::floatfield);
106   np.put(oss.rdbuf(), oss, L'+', d2);
107   result2 = oss.str();
108   VERIFY( result2 == L"+++++++2,225074e-308" );
109
110   oss.str(empty);
111   oss.clear();
112   oss.width(20);
113   oss.precision(10);
114   oss.setf(ios_base::right, ios_base::adjustfield);
115   oss.setf(ios_base::scientific, ios_base::floatfield);
116   oss.setf(ios_base::uppercase);
117   np.put(oss.rdbuf(), oss, L'+', d2);
118   result1 = oss.str();
119   VERIFY( result1 == L"+++2,2250738585E-308" );
120
121   // long double
122   oss.str(empty);
123   oss.clear();
124   np.put(oss.rdbuf(), oss, L'+', ld1);
125   result1 = oss.str();
126   VERIFY( result1 == L"1,7976931349E+308" );
127
128   oss.str(empty);
129   oss.clear();
130   oss.precision(0);
131   oss.setf(ios_base::fixed, ios_base::floatfield);
132   np.put(oss.rdbuf(), oss, L'+', ld2);
133   result1 = oss.str();
134   VERIFY( result1 == L"0" );
135
136   // const void*
137   oss.str(empty);
138   oss.clear();
139   np.put(oss.rdbuf(), oss, L'+', cv);
140   result1 = oss.str();
141   // No grouping characters.
142   VERIFY( !char_traits<wchar_t>::find(result1.c_str(), 
143                                    result1.size(),
144                                    numpunct_de.decimal_point()) );
145   // Should contain an 'x'.
146   VERIFY( result1.find(L'x') == 1 );
147
148 #ifdef _GLIBCXX_USE_LONG_LONG
149   long long ll1 = 9223372036854775807LL;
150
151   oss.str(empty);
152   oss.clear();
153   np.put(oss.rdbuf(), oss, L'+', ll1);
154   result1 = oss.str();
155   VERIFY( result1 == L"9.223.372.036.854.775.807" );
156 #endif
157 }
158
159 int main()
160 {
161   test01();
162   return 0;
163 }