OSDN Git Service

2005-08-17 Kelley Cook <kcook@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 22_locale / num_put / put / char / 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 Free Software Foundation
6 //
7 // This file is part of the GNU ISO C++ Library.  This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option)
11 // any later version.
12
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING.  If not, write to the Free
20 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // USA.
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<char> 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<char>& numpunct_de = use_facet<numpunct<char> >(loc_de); 
43
44   // sanity check the data is correct.
45   const string empty;
46   string result1;
47   string 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   ostringstream oss;
60   oss.imbue(loc_de);
61   const num_put<char>& np = use_facet<num_put<char> >(oss.getloc()); 
62
63   // bool, simple
64   iterator_type os_it00 = oss.rdbuf();
65   iterator_type os_it01 = np.put(os_it00, oss, '+', b1);
66   result1 = oss.str();
67   VERIFY( result1 == "1" );
68   //  VERIFY( os_it00 != os_it01 );
69
70   oss.str(empty);
71   np.put(oss.rdbuf(), oss, '+', b0);
72   result2 = oss.str();
73   VERIFY( result2 == "0" );
74
75   // ... and one that does
76   oss.imbue(loc_de);
77   oss.str(empty);
78   oss.clear();
79   oss.width(20);
80   oss.setf(ios_base::left, ios_base::adjustfield);
81   np.put(oss.rdbuf(), oss, '+', ul1);
82   result1 = oss.str();
83   VERIFY( result1 == "1.294.967.294+++++++" );
84
85   // double
86   oss.str(empty);
87   oss.clear();
88   oss.width(20);
89   oss.setf(ios_base::left, ios_base::adjustfield);
90   np.put(oss.rdbuf(), oss, '+', d1);
91   result1 = oss.str();
92   VERIFY( result1 == "1,79769e+308++++++++" );
93
94   oss.str(empty);
95   oss.clear();
96   oss.width(20);
97   oss.setf(ios_base::right, ios_base::adjustfield);
98   np.put(oss.rdbuf(), oss, '+', d2);
99   result1 = oss.str();
100   VERIFY( result1 == "++++++++2,22507e-308" );
101
102   oss.str(empty);
103   oss.clear();
104   oss.width(20);
105   oss.setf(ios_base::right, ios_base::adjustfield);
106   oss.setf(ios_base::scientific, ios_base::floatfield);
107   np.put(oss.rdbuf(), oss, '+', d2);
108   result2 = oss.str();
109   VERIFY( result2 == "+++++++2,225074e-308" );
110
111   oss.str(empty);
112   oss.clear();
113   oss.width(20);
114   oss.precision(10);
115   oss.setf(ios_base::right, ios_base::adjustfield);
116   oss.setf(ios_base::scientific, ios_base::floatfield);
117   oss.setf(ios_base::uppercase);
118   np.put(oss.rdbuf(), oss, '+', d2);
119   result1 = oss.str();
120   VERIFY( result1 == "+++2,2250738585E-308" );
121
122   // long double
123   oss.str(empty);
124   oss.clear();
125   np.put(oss.rdbuf(), oss, '+', ld1);
126   result1 = oss.str();
127   VERIFY( result1 == "1,7976931349E+308" );
128
129   oss.str(empty);
130   oss.clear();
131   oss.precision(0);
132   oss.setf(ios_base::fixed, ios_base::floatfield);
133   np.put(oss.rdbuf(), oss, '+', ld2);
134   result1 = oss.str();
135   VERIFY( result1 == "0" );
136
137   // const void
138   oss.str(empty);
139   oss.clear();
140   np.put(oss.rdbuf(), oss, '+', cv);
141   result1 = oss.str();
142   // No grouping characters.
143   VERIFY( !char_traits<char>::find(result1.c_str(), 
144                                    result1.size(),
145                                    numpunct_de.decimal_point()) );
146   // Should contain an 'x'.
147   VERIFY( result1.find('x') == 1 );
148
149 #ifdef _GLIBCXX_USE_LONG_LONG
150   long long ll1 = 9223372036854775807LL;
151
152   oss.str(empty);
153   oss.clear();
154   np.put(oss.rdbuf(), oss, '+', ll1);
155   result1 = oss.str();
156   VERIFY( result1 == "9.223.372.036.854.775.807" );
157 #endif
158 }
159
160 int main()
161 {
162   test01();
163   return 0;
164 }
165
166