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