OSDN Git Service

2003-07-09 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 22_locale / numpunct / members / pod / 1.cc
1 // 2003-07-09  Benjamin Kosnik  <bkoz@redhat.com>
2
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.
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 #include <locale>
22 #include <sstream>
23 #include <ostream>
24 #include <stdexcept>
25 #include <ext/pod_char_traits.h>
26 #include <testsuite_hooks.h>
27
28 typedef unsigned short                                  value_type;
29 typedef unsigned int                                    int_type;
30 typedef __gnu_cxx::character<value_type, int_type>      podchar_type;
31
32 // Member specializations for the existing facet classes.
33 // NB: This isn't especially portable. Perhaps a better way would be
34 // to just specialize all of numpunct and ctype.
35 namespace std
36 {
37   template<>
38     void
39     numpunct<podchar_type>::_M_initialize_numpunct(__c_locale __cloc)
40     {
41       if (!_M_data)
42         _M_data = new __numpunct_cache<podchar_type>;
43
44       _M_data->_M_grouping = "";
45       _M_data->_M_use_grouping = false;
46
47       _M_data->_M_decimal_point.value =  value_type('.');
48       _M_data->_M_thousands_sep.value = value_type(',');
49       
50       for (size_t i = 0; i < __num_base::_S_oend; ++i)
51         {
52           value_type v = __num_base::_S_atoms_out[i];
53           _M_data->_M_atoms_out[i].value = v;
54         }
55       _M_data->_M_atoms_out[__num_base::_S_oend] = podchar_type();
56       
57       for (size_t i = 0; i < __num_base::_S_iend; ++i)
58         _M_data->_M_atoms_in[i].value = value_type(__num_base::_S_atoms_in[i]);
59       _M_data->_M_atoms_in[__num_base::_S_iend] = podchar_type();
60
61       // "true"
62       podchar_type* __truename = new podchar_type[4 + 1];
63       __truename[0].value = value_type('t');
64       __truename[1].value = value_type('r');
65       __truename[2].value = value_type('u');
66       __truename[3].value = value_type('e');
67       __truename[4] = podchar_type();
68       _M_data->_M_truename = __truename;
69
70       // "false"
71       podchar_type* __falsename = new podchar_type[5 + 1];
72       __falsename[0].value = value_type('f');
73       __falsename[1].value = value_type('a');
74       __falsename[2].value = value_type('l');
75       __falsename[3].value = value_type('s');
76       __falsename[4].value = value_type('e');
77       __falsename[5] = podchar_type();
78       _M_data->_M_falsename = __falsename;
79     }
80
81   template<>
82     numpunct<podchar_type>::~numpunct()
83     { delete _M_data; }
84 }
85
86 // Check for numpunct and ctype dependencies. Make sure that numpunct
87 // can be created without ctype.
88 void test01()
89 {
90   using namespace std;
91   typedef numpunct<podchar_type>::string_type   string_type;
92   typedef basic_stringbuf<podchar_type>         stringbuf_type;
93   typedef basic_ostream<podchar_type>           ostream_type;
94   
95   bool          test = true;
96
97   // Pre-cache sanity check.
98   const locale  loc(locale::classic(), new numpunct<podchar_type>);
99   const numpunct<podchar_type>& np = use_facet<numpunct<podchar_type> >(loc);
100
101   podchar_type dp = np.decimal_point();
102   podchar_type ts = np.thousands_sep();
103   string g = np.grouping();
104   string_type strue = np.truename();
105   string_type sfalse = np.falsename();
106
107   podchar_type basedp = { value_type('.') };
108   podchar_type basets = { value_type(',') };
109
110   string_type basetrue(4, podchar_type());
111   basetrue[0].value = value_type('t');
112   basetrue[1].value = value_type('r');
113   basetrue[2].value = value_type('u');
114   basetrue[3].value = value_type('e');
115
116   string_type basefalse(5, podchar_type());
117   basefalse[0].value = value_type('f');
118   basefalse[1].value = value_type('a');
119   basefalse[2].value = value_type('l');
120   basefalse[3].value = value_type('s');
121   basefalse[4].value = value_type('e');
122
123   VERIFY( char_traits<podchar_type>::eq(dp, basedp) );
124   VERIFY( char_traits<podchar_type>::eq(ts, basets) );
125   VERIFY( g == "" );
126   VERIFY( strue == basetrue );
127   VERIFY( sfalse == basefalse );
128 }
129
130 int main()
131 {
132   test01();
133   return 0;
134 }