]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/basic_istream/sentry/char/3983-fstream.cc
locale_facets.tcc: Tweak to avoid warnings.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / basic_istream / sentry / char / 3983-fstream.cc
1 // 2001-06-05 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 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 // 27.4.2.1.6 class ios_base::init
31
32 #include <fstream>
33 #include <typeinfo>
34 #include <testsuite_hooks.h>
35
36 // char_traits specialization
37 namespace std
38 {
39 template<>
40 struct char_traits<unsigned char>
41 {
42 typedef unsigned char char_type;
43 // Unsigned as wint_t in unsigned.
44 typedef unsigned long int_type;
45 typedef streampos pos_type;
46 typedef streamoff off_type;
47 typedef mbstate_t state_type;
48
49 static void
50 assign(char_type& __c1, const char_type& __c2)
51 { __c1 = __c2; }
52
53 static bool
54 eq(const char_type& __c1, const char_type& __c2)
55 { return __c1 == __c2; }
56
57 static bool
58 lt(const char_type& __c1, const char_type& __c2)
59 { return __c1 < __c2; }
60
61 static int
62 compare(const char_type* __s1, const char_type* __s2, size_t __n)
63 {
64 for (size_t __i = 0; __i < __n; ++__i)
65 if (!eq(__s1[__i], __s2[__i]))
66 return lt(__s1[__i], __s2[__i]) ? -1 : 1;
67 return 0;
68 }
69
70 static size_t
71 length(const char_type* __s)
72 {
73 const char_type* __p = __s;
74 while (__p && *__p)
75 ++__p;
76 return (__p - __s);
77 }
78
79 static const char_type*
80 find(const char_type* __s, size_t __n, const char_type& __a)
81 {
82 for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
83 if (*__p == __a) return __p;
84 return 0;
85 }
86
87 static char_type*
88 move(char_type* __s1, const char_type* __s2, size_t __n)
89 { return (char_type*) memmove(__s1, __s2, __n * sizeof(char_type)); }
90
91 static char_type*
92 copy(char_type* __s1, const char_type* __s2, size_t __n)
93 { return (char_type*) memcpy(__s1, __s2, __n * sizeof(char_type)); }
94
95 static char_type*
96 assign(char_type* __s, size_t __n, char_type __a)
97 {
98 for (char_type* __p = __s; __p < __s + __n; ++__p)
99 assign(*__p, __a);
100 return __s;
101 }
102
103 static char_type
104 to_char_type(const int_type& __c)
105 { return char_type(); }
106
107 static int_type
108 to_int_type(const char_type& __c) { return int_type(); }
109
110 static bool
111 eq_int_type(const int_type& __c1, const int_type& __c2)
112 { return __c1 == __c2; }
113
114 static int_type
115 eof() { return static_cast<int_type>(-1); }
116
117 static int_type
118 not_eof(const int_type& __c)
119 { return eq_int_type(__c, eof()) ? int_type(0) : __c; }
120 };
121 } // namespace std
122
123 // libstdc++/3983
124 // Sentry uses locale info, so have to try one formatted input/output.
125 void test03()
126 {
127 using namespace std;
128 bool test __attribute__((unused)) = true;
129
130 // input streams
131 basic_ifstream<unsigned char> ifs_uc;
132 unsigned char arr[6] = { 'a', 'b', 'c', 'd', 'e' };
133
134 try
135 {
136 int i;
137 ifs_uc >> i;
138 }
139 catch (bad_cast& obj)
140 { }
141 catch (exception& obj)
142 { test = false; }
143
144 try
145 {
146 ifs_uc >> arr;
147 }
148 catch (bad_cast& obj)
149 { }
150 catch (exception& obj)
151 { test = false; }
152
153 try
154 {
155 ifs_uc >> ws;
156 }
157 catch (bad_cast& obj)
158 { }
159 catch (exception& obj)
160 { test = false; }
161
162 try
163 {
164 basic_string<unsigned char> s_uc(arr);
165 ifs_uc >> s_uc;
166 }
167 catch (bad_cast& obj)
168 { }
169 catch (exception& obj)
170 { test = false; }
171
172 VERIFY( test );
173 }
174
175 #if !__GXX_WEAK__
176 // Explicitly instantiate for systems with no COMDAT or weak support.
177 template
178 std::basic_string<unsigned char>::size_type
179 std::basic_string<unsigned char>::_Rep::_S_max_size;
180
181 template
182 unsigned char
183 std::basic_string<unsigned char>::_Rep::_S_terminal;
184 #endif
185
186 int main()
187 {
188 test03();
189 return 0;
190 }