1// 2003-07-09 Benjamin Kosnik <bkoz@redhat.com> 2 3// Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009 4// 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 <locale> 22#include <sstream> 23#include <ostream> 24#include <stdexcept> 25#include <typeinfo> 26#include <testsuite_hooks.h> 27#include <testsuite_character.h> 28 29// Check for numpunct and ctype dependencies. Make sure that numpunct 30// can be created without ctype. 31void test01() 32{ 33 using namespace std; 34 using __gnu_test::pod_ushort; 35 36 typedef numpunct<pod_ushort>::string_type string_type; 37 typedef basic_ostringstream<pod_ushort> ostream_type; 38 39 bool test = true; 40 41 // Test formatted output. 42 ostream_type os; 43 const locale loc = locale::classic(); 44 os.imbue(loc); 45 os.setf(ios_base::boolalpha); 46 os.exceptions(ios_base::badbit); 47 48 // 1: fail, no num_put. 49 try 50 { 51 // Calls to num_put.put will fail, as there's no num_put facet. 52 os << true; 53 test = false; 54 } 55 catch(const bad_cast& obj) 56 { } 57 catch(...) 58 { test = false; } 59 VERIFY( test ); 60 61 // 2: fail, no ctype 62 const locale loc2(loc, new num_put<pod_ushort>); 63 os.clear(); 64 os.imbue(loc2); 65 try 66 { 67 // Calls to ctype.widen will fail, as there's no ctype facet. 68 os << true; 69 test = false; 70 } 71 catch(const bad_cast& obj) 72 { } 73 catch(...) 74 { test = false; } 75 VERIFY( test ); 76 77 // 3: fail, no numpunct 78 const locale loc3(loc2, new ctype<pod_ushort>); 79 os.clear(); 80 os.imbue(loc3); 81 try 82 { 83 // Formatted output fails as no numpunct. 84 os << true; 85 test = false; 86 } 87 catch(const bad_cast& obj) 88 { } 89 catch(...) 90 { test = false; } 91 VERIFY( test ); 92 93 // 4: works. 94 const locale loc4(loc3, new numpunct<pod_ushort>); 95 os.clear(); 96 os.imbue(loc4); 97 try 98 { 99 os << long(500); 100 string_type s = os.str(); 101 VERIFY( s.length() == 3 ); 102 103 VERIFY( os.narrow(s[0], char()) == '5' ); 104 VERIFY( os.narrow(s[1], char()) == '0' ); 105 VERIFY( os.narrow(s[2], char()) == '0' ); 106 } 107 catch(const bad_cast& obj) 108 { test = false; } 109 catch(...) 110 { test = false; } 111 VERIFY( test ); 112} 113 114int main() 115{ 116 test01(); 117 return 0; 118} 119