OSDN Git Service

02ef036a282c82cc0e71224dcb489a7f2aac185c
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 22_locale / locale / cons / unicode / 1.cc
1 // Copyright (C) 2006 Free Software Foundation
2 //
3 // This file is part of the GNU ISO C++ Library.  This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 2, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING.  If not, write to the Free
16 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 // USA.
18
19 // 22.1.1.2 locale constructors and destructors [lib.locale.cons]
20
21 #include <cwchar> // for mbstate_t
22 #include <locale>
23 #include <stdexcept>
24 #include <testsuite_hooks.h>
25
26 #if _GLIBCXX_USE___ENC_TRAITS
27 typedef std::codecvt<char, char, std::mbstate_t>              c_codecvt;
28
29 #ifdef _GLIBCXX_USE_WCHAR_T
30 typedef std::codecvt<wchar_t, char, std::mbstate_t>           w_codecvt;
31 #endif
32
33 class gnu_facet: public std::locale::facet
34 {
35 public:
36   static std::locale::id id;
37 };
38
39 std::locale::id gnu_facet::id;
40
41 // Need some char_traits specializations for this to work.
42 typedef unsigned short                  unicode_t;
43
44 namespace std
45 {
46   template<>
47     struct char_traits<unicode_t>
48     {
49       typedef unicode_t         char_type;
50       // Unsigned as wint_t is unsigned.
51       typedef unsigned long     int_type;
52       typedef streampos         pos_type;
53       typedef streamoff         off_type;
54       typedef mbstate_t         state_type;
55       
56       static void 
57       assign(char_type& __c1, const char_type& __c2);
58
59       static bool 
60       eq(const char_type& __c1, const char_type& __c2);
61
62       static bool 
63       lt(const char_type& __c1, const char_type& __c2);
64
65       static int 
66       compare(const char_type* __s1, const char_type* __s2, size_t __n)
67       { return memcmp(__s1, __s2, __n); }
68
69       static size_t
70       length(const char_type* __s);
71
72       static const char_type* 
73       find(const char_type* __s, size_t __n, const char_type& __a);
74
75       static char_type* 
76       move(char_type* __s1, const char_type* __s2, size_t __n);
77
78       static char_type* 
79       copy(char_type* __s1, const char_type* __s2, size_t __n)
80       { return static_cast<char_type*>(memcpy(__s1, __s2, __n)); }
81
82       static char_type* 
83       assign(char_type* __s, size_t __n, char_type __a);
84
85       static char_type 
86       to_char_type(const int_type& __c);
87
88       static int_type 
89       to_int_type(const char_type& __c);
90
91       static bool 
92       eq_int_type(const int_type& __c1, const int_type& __c2);
93
94       static int_type 
95       eof(); 
96
97       static int_type 
98       not_eof(const int_type& __c);
99     };
100 }
101
102 void test01()
103 {
104   using namespace std;
105   typedef unicode_t                             int_type;
106   typedef char                                  ext_type;
107   typedef __enc_traits                          enc_type;
108   typedef codecvt<int_type, ext_type, enc_type> unicode_codecvt;
109
110   bool test __attribute__((unused)) = true;
111
112   // unicode_codecvt
113   locale loc13(locale::classic(), new unicode_codecvt);  
114   VERIFY( loc01 != loc13 );
115   VERIFY( loc13.name() == "*" );
116   try 
117     {
118       VERIFY( has_facet<c_codecvt>(loc13) );
119 #ifdef _GLIBCXX_USE_WCHAR_T
120       VERIFY( has_facet<w_codecvt>(loc13) );
121 #endif
122       VERIFY( has_facet<unicode_codecvt>(loc13) );
123     }
124   catch(...)
125     { VERIFY( false ); }
126
127   try 
128     { use_facet<gnu_facet>(loc13); }
129   catch(bad_cast& obj)
130     { VERIFY( true ); }
131   catch(...)
132     { VERIFY( false ); }
133 }
134 #endif // _GLIBCXX_USE___ENC_TRAITS
135
136 int main()
137 {
138 #if _GLIBCXX_USE___ENC_TRAITS
139   test01();
140 #endif 
141   return 0;
142 }