OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [gcc-4.5.1/] [libstdc++-v3/] [testsuite/] [27_io/] [basic_filebuf/] [underflow/] [wchar_t/] [11603.cc] - Blame information for rev 565

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 424 jeremybenn
// Copyright (C) 2003, 2004, 2009 Free Software Foundation, Inc.
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 3, 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 COPYING3.  If not see
16
// <http://www.gnu.org/licenses/>.
17
 
18
// 27.8.1.4 Overridden virtual functions
19
 
20
// { dg-require-binary-io "" }
21
 
22 565 jeremybenn
// { dg-xfail-run-if "not supported on OR32 newlib" { or32-*-elf } }
23
 
24 424 jeremybenn
#include <fstream>
25
#include <locale>
26
#include <testsuite_hooks.h>
27
 
28
template <typename InternT, typename StateT = std::mbstate_t>
29
class checksumcvt : public std::codecvt<InternT, char, StateT>
30
{
31
  typedef std::codecvt<InternT, char, StateT> Base;
32
  static const std::size_t width = sizeof(InternT) + 1;
33
 
34
public:
35
  typedef InternT intern_type;
36
  typedef char extern_type;
37
 
38
  explicit checksumcvt(std::size_t refs = 0)
39
    : Base(refs)
40
  { }
41
 
42
protected:
43
  virtual std::codecvt_base::result
44
  do_out(StateT&, const intern_type* from,
45
         const intern_type* from_end, const intern_type*& from_next,
46
         extern_type* to, extern_type* to_end,
47
         extern_type*& to_next) const
48
  {
49
    size_t len = std::min(static_cast<size_t>(from_end - from),
50
                          static_cast<size_t>(to_end - to) / width);
51
 
52
    while (len--)
53
      {
54
        const char* p =
55
          reinterpret_cast<const char*>(from);
56
        unsigned char checksum = 0;
57
 
58
        for (std::size_t i = 0; i < sizeof(intern_type); ++i)
59
          {
60
            *to++ = p[i];
61
            checksum ^= static_cast<unsigned char>(p[i]);
62
          }
63
 
64
        *to++ = checksum;
65
        ++from;
66
      }
67
 
68
    from_next = from;
69
    to_next = to;
70
    return from_next == from_end ? std::codecvt_base::ok
71
                                 : std::codecvt_base::partial;
72
  }
73
 
74
  virtual std::codecvt_base::result
75
  do_unshift(StateT&, extern_type* to,
76
             extern_type*, extern_type*& to_next) const
77
  {
78
    to_next = to;
79
    return std::codecvt_base::ok;
80
  }
81
 
82
  virtual std::codecvt_base::result
83
  do_in(StateT&, const extern_type* from,
84
        const extern_type* from_end, const extern_type*& from_next,
85
        intern_type* to, intern_type* to_end,
86
        intern_type*& to_next) const
87
  {
88
    size_t len = std::min(static_cast<size_t>(to_end - to),
89
                          static_cast<size_t>(from_end - from) / width);
90
 
91
    while (len)
92
      {
93
        const char* f = from;
94
        intern_type tmp;
95
        char* p = reinterpret_cast<char*>(&tmp);
96
        unsigned char checksum = 0;
97
 
98
        for (std::size_t i = 0; i < sizeof(intern_type); ++i)
99
          {
100
            p[i] = *f;
101
            checksum ^= static_cast<unsigned char>(*f++);
102
          }
103
 
104
        if (*f++ != checksum)
105
          break;
106
 
107
        from = f;
108
        *to++ = tmp;
109
        len--;
110
      }
111
 
112
    from_next = from;
113
    to_next = to;
114
    return len ? std::codecvt_base::error :
115
      (from_next == from_end ? std::codecvt_base::ok
116
                             : std::codecvt_base::partial);
117
  }
118
 
119
  virtual int
120
  do_encoding() const throw()
121
  { return width; }
122
 
123
  virtual int
124
  do_length(const StateT&, const extern_type* from,
125
            const extern_type* end, size_t max) const
126
  {
127
    size_t len = std::min(max,
128
                          static_cast<size_t>(end - from) / width);
129
 
130
    int ret = 0;
131
    while (len--)
132
      {
133
        unsigned char checksum = 0;
134
 
135
        for (std::size_t i = 0; i < sizeof(intern_type); ++i)
136
          {
137
            checksum ^= static_cast<unsigned char>(*from++);
138
          }
139
 
140
        if (*from++ != checksum)
141
          break;
142
 
143
        ret++;
144
      }
145
 
146
    return ret;
147
  }
148
 
149
  virtual int
150
  do_max_length() const throw()
151
  { return width; }
152
 
153
  virtual bool
154
  do_always_noconv() const throw()
155
  { return false; }
156
};
157
 
158
class Buf : public std::wfilebuf
159
{
160
public:
161
  std::streamsize pub_showmanyc()
162
  { return showmanyc(); }
163
  std::wfilebuf::int_type pub_underflow()
164
  { return underflow(); }
165
};
166
 
167
// libstdc++/11603
168
void test01()
169
{
170
  using namespace std;
171
  bool test __attribute__((unused)) = true;
172
 
173
  filebuf fbout;
174
  fbout.open("tmp_11603", ios_base::out);
175
  fbout.sputn("aaaab", 5);
176
  fbout.close();
177
 
178
  locale loc(locale::classic(), new checksumcvt<wchar_t>);
179
 
180
  Buf fb;
181
  fb.pubimbue(loc);
182
  fb.open("tmp_11603", ios_base::in);
183
  VERIFY( fb.pub_showmanyc() == 1 );
184
 
185
  try
186
    {
187
      wfilebuf::int_type ret = fb.pub_underflow();
188
      VERIFY( ret != wfilebuf::traits_type::eof() );
189
      fb.sbumpc();
190
      ret = fb.pub_underflow();
191
      VERIFY( ret == wfilebuf::traits_type::eof() );
192
    }
193
  catch (...)
194
    { }
195
 
196
  fb.close();
197
}
198
 
199
int main()
200
{
201
  test01();
202
  return 0;
203
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.