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/] [9178.cc] - Blame information for rev 565

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 424 jeremybenn
// Copyright (C) 2003, 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 565 jeremybenn
// { dg-xfail-run-if "not supported on OR32 newlib" { or32-*-elf } }
21
 
22 424 jeremybenn
#include <fstream>
23
#include <string>
24
#include <iterator>
25
#include <algorithm>
26
#include <locale>
27
#include <testsuite_hooks.h>
28
 
29
template <typename InternT, typename StateT = mbstate_t>
30
class checksumcvt : public std::codecvt<InternT, char, StateT>
31
{
32
  typedef std::codecvt<InternT, char, StateT> Base;
33
  static const size_t width = sizeof(InternT) + 1;
34
 
35
public:
36
  typedef InternT intern_type;
37
  typedef char extern_type;
38
 
39
  explicit checksumcvt(size_t refs = 0)
40
  : Base(refs)
41
  { }
42
 
43
protected:
44
  virtual typename std::codecvt<InternT, char, StateT>::result
45
  do_out(StateT&, const intern_type* from,
46
         const intern_type* from_end, const intern_type*& from_next,
47
         extern_type* to, extern_type* to_end,
48
         extern_type*& to_next) const
49
  {
50
    size_t len = std::min(static_cast<size_t>(from_end - from),
51
                          static_cast<size_t>(to_end - to) / width);
52
 
53
    while (len--)
54
      {
55
        const char* p = reinterpret_cast<const char*>(from);
56
        unsigned char checksum = 0;
57
 
58
        for (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<InternT, char, StateT>::ok
71
           : std::codecvt<InternT, char, StateT>::partial;
72
  }
73
 
74
  virtual typename std::codecvt<InternT, char, StateT>::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<InternT, char, StateT>::ok;
80
  }
81
 
82
  virtual typename std::codecvt<InternT, char, StateT>::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 (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<InternT, char, StateT>::error :
115
      (from_next == from_end ? std::codecvt<InternT, char, StateT>::ok
116
       : std::codecvt<InternT, char, StateT>::partial);
117
  }
118
 
119
  virtual int
120
  do_encoding() const throw()
121
  {
122
    return width;
123
  }
124
 
125
  virtual int
126
  do_length(StateT&, const extern_type* from,
127
            const extern_type* end, size_t max) const
128
  {
129
    size_t len = std::min(max, static_cast<size_t>(end - from) / width);
130
 
131
    int ret = 0;
132
    while (len--)
133
      {
134
        unsigned char checksum = 0;
135
 
136
        for (size_t i = 0; i < sizeof(intern_type); ++i)
137
          {
138
            checksum ^= static_cast<unsigned char>(*from++);
139
          }
140
 
141
        if (*from++ != checksum)
142
          break;
143
 
144
        ret++;
145
      }
146
 
147
    return ret;
148
  }
149
 
150
  virtual int
151
  do_max_length() const throw()
152
  {
153
    return width;
154
  }
155
 
156
  virtual bool
157
  do_always_noconv() const throw()
158
  {
159
    return false;
160
  }
161
};
162
 
163
void test01()
164
{
165
  using namespace std;
166
  bool test __attribute__((unused)) = true;
167
 
168
  locale loc;
169
  loc = locale(loc, new checksumcvt<wchar_t>);
170
 
171
  wfilebuf fbuf1;
172
  fbuf1.pubimbue(loc);
173
  fbuf1.open("tmp_9178", ios_base::out | ios_base::trunc);
174
 
175
  string tmpstr = "abcdefghijklmnopqrstuvwxyz0123456789 \t\n";
176
 
177
  wifstream stream;
178
  wstring str1;
179
 
180
  while (str1.length() < 20000)
181
    {
182
      transform(tmpstr.begin(), tmpstr.end(),
183
                back_inserter(str1),
184
                bind1st(std::mem_fun(&std::wios::widen), &stream));
185
    }
186
 
187
  fbuf1.sputn(str1.data(), str1.size());
188
  fbuf1.close();
189
 
190
  wfilebuf fbuf2;
191
  fbuf2.pubimbue(loc);
192
  fbuf2.open("tmp_9178", std::ios_base::in);
193
 
194
  wstring str2;
195
  copy(istreambuf_iterator<wchar_t>(&fbuf2),
196
       istreambuf_iterator<wchar_t>(),
197
       back_inserter(str2));
198
 
199
  VERIFY( str1 == str2 );
200
}
201
 
202
int main()
203
{
204
  test01();
205
  return 0;
206
}

powered by: WebSVN 2.1.0

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