]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/basic_filebuf/underflow/wchar_t/9178.cc
1 // Copyright (C) 2003-2022 Free Software Foundation, Inc.
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 3, or (at your option)
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.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // 27.8.1.4 Overridden virtual functions
25 #include <testsuite_hooks.h>
27 template <typename InternT
, typename StateT
= mbstate_t>
28 class checksumcvt
: public std::codecvt
<InternT
, char, StateT
>
30 typedef std::codecvt
<InternT
, char, StateT
> Base
;
31 static const size_t width
= sizeof(InternT
) + 1;
34 typedef InternT intern_type
;
35 typedef char extern_type
;
37 explicit checksumcvt(size_t refs
= 0)
42 virtual typename
std::codecvt
<InternT
, char, StateT
>::result
43 do_out(StateT
&, const intern_type
* from
,
44 const intern_type
* from_end
, const intern_type
*& from_next
,
45 extern_type
* to
, extern_type
* to_end
,
46 extern_type
*& to_next
) const
48 size_t len
= std::min(static_cast<size_t>(from_end
- from
),
49 static_cast<size_t>(to_end
- to
) / width
);
53 const char* p
= reinterpret_cast<const char*>(from
);
54 unsigned char checksum
= 0;
56 for (size_t i
= 0; i
< sizeof(intern_type
); ++i
)
59 checksum
^= static_cast<unsigned char>(p
[i
]);
68 return from_next
== from_end
? std::codecvt
<InternT
, char, StateT
>::ok
69 : std::codecvt
<InternT
, char, StateT
>::partial
;
72 virtual typename
std::codecvt
<InternT
, char, StateT
>::result
73 do_unshift(StateT
&, extern_type
* to
,
74 extern_type
*, extern_type
*& to_next
) const
77 return std::codecvt
<InternT
, char, StateT
>::ok
;
80 virtual typename
std::codecvt
<InternT
, char, StateT
>::result
81 do_in(StateT
&, const extern_type
* from
,
82 const extern_type
* from_end
, const extern_type
*& from_next
,
83 intern_type
* to
, intern_type
* to_end
,
84 intern_type
*& to_next
) const
86 size_t len
= std::min(static_cast<size_t>(to_end
- to
),
87 static_cast<size_t>(from_end
- from
) / width
);
93 char* p
= reinterpret_cast<char*>(&tmp
);
94 unsigned char checksum
= 0;
96 for (size_t i
= 0; i
< sizeof(intern_type
); ++i
)
99 checksum
^= static_cast<unsigned char>(*f
++);
102 if (*f
++ != checksum
)
112 return len
? std::codecvt
<InternT
, char, StateT
>::error
:
113 (from_next
== from_end
? std::codecvt
<InternT
, char, StateT
>::ok
114 : std::codecvt
<InternT
, char, StateT
>::partial
);
118 do_encoding() const throw()
124 do_length(StateT
&, const extern_type
* from
,
125 const extern_type
* end
, size_t max
) const
127 size_t len
= std::min(max
, static_cast<size_t>(end
- from
) / width
);
132 unsigned char checksum
= 0;
134 for (size_t i
= 0; i
< sizeof(intern_type
); ++i
)
136 checksum
^= static_cast<unsigned char>(*from
++);
139 if (*from
++ != checksum
)
149 do_max_length() const throw()
155 do_always_noconv() const throw()
166 loc
= locale(loc
, new checksumcvt
<wchar_t>);
170 fbuf1
.open("tmp_9178", ios_base::out
| ios_base::trunc
);
172 string tmpstr
= "abcdefghijklmnopqrstuvwxyz0123456789 \t\n";
177 while (str1
.length() < 20000)
179 transform(tmpstr
.begin(), tmpstr
.end(),
181 bind1st(std::mem_fun(&std::wios::widen
), &stream
));
184 fbuf1
.sputn(str1
.data(), str1
.size());
189 fbuf2
.open("tmp_9178", std::ios_base::in
);
192 copy(istreambuf_iterator
<wchar_t>(&fbuf2
),
193 istreambuf_iterator
<wchar_t>(),
194 back_inserter(str2
));
196 VERIFY( str1
== str2
);