]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/basic_ios/cons/char/3.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / basic_ios / cons / char / 3.cc
1 // 2001-06-05 Benjamin Kosnik <bkoz@redhat.com>
2
3 // Copyright (C) 2001-2021 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 3, 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 COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
19
20
21 // 27.4.2.1.6 class ios_base::init
22
23 #include <sstream>
24 #include <typeinfo>
25 #include <cstring>
26 #include <testsuite_hooks.h>
27
28 // char_traits specialization
29 namespace std
30 {
31 template<>
32 struct char_traits<unsigned short>
33 {
34 typedef unsigned short char_type;
35 // Unsigned as wint_t in unsigned.
36 typedef unsigned long int_type;
37 typedef streampos pos_type;
38 typedef streamoff off_type;
39 typedef mbstate_t state_type;
40
41 static void
42 assign(char_type& __c1, const char_type& __c2)
43 { __c1 = __c2; }
44
45 static bool
46 eq(const char_type& __c1, const char_type& __c2)
47 { return __c1 == __c2; }
48
49 static bool
50 lt(const char_type& __c1, const char_type& __c2)
51 { return __c1 < __c2; }
52
53 static int
54 compare(const char_type* __s1, const char_type* __s2, size_t __n)
55 {
56 for (size_t __i = 0; __i < __n; ++__i)
57 if (!eq(__s1[__i], __s2[__i]))
58 return lt(__s1[__i], __s2[__i]) ? -1 : 1;
59 return 0;
60 }
61
62 static size_t
63 length(const char_type* __s)
64 {
65 const char_type* __p = __s;
66 while (__p)
67 ++__p;
68 return (__p - __s);
69 }
70
71 static const char_type*
72 find(const char_type* __s, size_t __n, const char_type& __a)
73 {
74 for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
75 if (*__p == __a) return __p;
76 return 0;
77 }
78
79 static char_type*
80 move(char_type* __s1, const char_type* __s2, size_t __n)
81 { return (char_type*) memmove(__s1, __s2, __n * sizeof(char_type)); }
82
83 static char_type*
84 copy(char_type* __s1, const char_type* __s2, size_t __n)
85 { return (char_type*) memcpy(__s1, __s2, __n * sizeof(char_type)); }
86
87 static char_type*
88 assign(char_type* __s, size_t __n, char_type __a)
89 {
90 for (char_type* __p = __s; __p < __s + __n; ++__p)
91 assign(*__p, __a);
92 return __s;
93 }
94
95 static char_type
96 to_char_type(const int_type&)
97 { return char_type(); }
98
99 static int_type
100 to_int_type(const char_type&) { return int_type(); }
101
102 static bool
103 eq_int_type(const int_type& __c1, const int_type& __c2)
104 { return __c1 == __c2; }
105
106 static int_type
107 eof() { return static_cast<int_type>(-1); }
108
109 static int_type
110 not_eof(const int_type& __c)
111 { return eq_int_type(__c, eof()) ? int_type(0) : __c; }
112 };
113 } // namespace std
114
115 // Non-required instantiations don't have the required facets inbued,
116 // by default, into the locale object.
117 // See 27.4.4.1
118
119 void test02()
120 {
121 bool test = true;
122
123 // 02: Calls basic_ios::init, which may call ctype<char_type>...
124 try
125 {
126 std::basic_string<unsigned short> str;
127 std::basic_ostringstream<unsigned short> oss(str);
128
129 // Try each member functions for unformatted io.
130 // put
131 oss.put(324);
132
133 // write
134 const unsigned short us[4] = {1246, 433, 520, 0};
135 oss.write(us, 4);
136
137 // flush
138 oss.flush();
139 }
140 catch(const std::bad_cast& obj)
141 {
142 // Should be able to do the above without calling fill() and
143 // forcing a call to widen...
144 test = false;
145 }
146 catch(...)
147 {
148 test = false;
149 }
150 VERIFY( test );
151 }
152
153 #if !__GXX_WEAK__
154 // Explicitly instantiate for systems with no COMDAT or weak support.
155 template
156 const std::basic_string<unsigned short>::size_type
157 std::basic_string<unsigned short>::_Rep::_S_max_size;
158
159 template
160 const unsigned short
161 std::basic_string<unsigned short>::_Rep::_S_terminal;
162 #endif
163
164 int main()
165 {
166 test02();
167 return 0;
168 }