2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / testsuite / 22_locale / numpunct / members / pod / 2.cc
blobbb970369ee34f1fabb6b6f75d8b6252e90aa3fb4
1 // 2003-07-09 Benjamin Kosnik <bkoz@redhat.com>
3 // Copyright (C) 2003 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
21 #include <locale>
22 #include <sstream>
23 #include <ostream>
24 #include <stdexcept>
25 #include <ext/pod_char_traits.h>
26 #include <testsuite_hooks.h>
28 // Check for numpunct and ctype dependencies. Make sure that numpunct
29 // can be created without ctype.
30 void test01()
32 using namespace std;
33 using __gnu_test::pod_type;
35 typedef numpunct<pod_type>::string_type string_type;
36 typedef basic_ostringstream<pod_type> ostream_type;
38 bool test = true;
40 // Test formatted output.
41 ostream_type os;
42 const locale loc = locale::classic();
43 os.imbue(loc);
44 os.setf(ios_base::boolalpha);
45 os.exceptions(ios_base::badbit);
47 // 1: fail, no num_put.
48 try
50 // Calls to num_put.put will fail, as there's no num_put facet.
51 os << true;
52 test = false;
54 catch(const bad_cast& obj)
55 { }
56 catch(...)
57 { test = false; }
58 VERIFY( test );
60 // 2: fail, no ctype
61 const locale loc2(loc, new num_put<pod_type>);
62 os.clear();
63 os.imbue(loc2);
64 try
66 // Calls to ctype.widen will fail, as there's no ctype facet.
67 os << true;
68 test = false;
70 catch(const bad_cast& obj)
71 { }
72 catch(...)
73 { test = false; }
74 VERIFY( test );
76 // 3: fail, no numpunct
77 const locale loc3(loc, new ctype<pod_type>);
78 os.clear();
79 os.imbue(loc3);
80 try
82 // Formatted output fails as no numpunct.
83 os << true;
84 test = false;
86 catch(const bad_cast& obj)
87 { }
88 catch(...)
89 { test = false; }
90 VERIFY( test );
92 // 4: works.
93 const locale loc4(loc3, new numpunct<pod_type>);
94 os.clear();
95 os.imbue(loc4);
96 try
98 os << long(500);
99 string_type s = os.str();
100 VERIFY( s.length() == 3 );
102 VERIFY( os.narrow(s[0], char()) == '5' );
103 VERIFY( os.narrow(s[1], char()) == '0' );
104 VERIFY( os.narrow(s[2], char()) == '0' );
106 catch(const bad_cast& obj)
107 { test = false; }
108 catch(...)
109 { test = false; }
110 VERIFY( test );
113 int main()
115 test01();
116 return 0;