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/] [11544-1.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 <locale>
24
#include <cstdio>
25
#include <testsuite_hooks.h>
26
 
27
template <typename InternT, typename StateT = mbstate_t>
28
class checksumcvt : public std::codecvt<InternT, char, StateT>
29
{
30
  typedef std::codecvt<InternT, char, StateT> Base;
31
  static const size_t width = sizeof(InternT) + 1;
32
 
33
public:
34
  typedef InternT intern_type;
35
  typedef char extern_type;
36
 
37
  explicit checksumcvt(size_t refs = 0)
38
  : Base(refs)
39
  { }
40
 
41
protected:
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
47
  {
48
    size_t len = std::min(static_cast<size_t>(from_end - from),
49
                          static_cast<size_t>(to_end - to) / width);
50
 
51
    while (len--)
52
      {
53
        const char* p = reinterpret_cast<const char*>(from);
54
        unsigned char checksum = 0;
55
 
56
        for (size_t i = 0; i < sizeof(intern_type); ++i)
57
          {
58
            *to++ = p[i];
59
            checksum ^= static_cast<unsigned char>(p[i]);
60
          }
61
 
62
        *to++ = checksum;
63
        ++from;
64
      }
65
 
66
    from_next = from;
67
    to_next = to;
68
    return from_next == from_end ? std::codecvt<InternT, char, StateT>::ok
69
           : std::codecvt<InternT, char, StateT>::partial;
70
  }
71
 
72
  virtual typename std::codecvt<InternT, char, StateT>::result
73
  do_unshift(StateT&, extern_type* to,
74
             extern_type*, extern_type*& to_next) const
75
  {
76
    to_next = to;
77
    return std::codecvt<InternT, char, StateT>::ok;
78
  }
79
 
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
85
  {
86
    size_t len = std::min(static_cast<size_t>(to_end - to),
87
                          static_cast<size_t>(from_end - from) / width);
88
 
89
    while (len)
90
      {
91
        const char* f = from;
92
        intern_type tmp;
93
        char* p = reinterpret_cast<char*>(&tmp);
94
        unsigned char checksum = 0;
95
 
96
        for (size_t i = 0; i < sizeof(intern_type); ++i)
97
          {
98
            p[i] = *f;
99
            checksum ^= static_cast<unsigned char>(*f++);
100
          }
101
 
102
        if (*f++ != checksum)
103
          break;
104
 
105
        from = f;
106
        *to++ = tmp;
107
        len--;
108
      }
109
 
110
    from_next = from;
111
    to_next = to;
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);
115
  }
116
 
117
  virtual int
118
  do_encoding() const throw()
119
  { return width; }
120
 
121
  virtual int
122
  do_length(StateT&, const extern_type* from,
123
            const extern_type* end, size_t max) const
124
  {
125
    size_t len = std::min(max, static_cast<size_t>(end - from) / width);
126
 
127
    int ret = 0;
128
    while (len--)
129
      {
130
        unsigned char checksum = 0;
131
 
132
        for (size_t i = 0; i < sizeof(intern_type); ++i)
133
          {
134
            checksum ^= static_cast<unsigned char>(*from++);
135
          }
136
 
137
        if (*from++ != checksum)
138
          break;
139
 
140
        ret++;
141
      }
142
 
143
    return ret;
144
  }
145
 
146
  virtual int
147
  do_max_length() const throw()
148
  { return width; }
149
 
150
  virtual bool
151
  do_always_noconv() const throw()
152
  { return false; }
153
};
154
 
155
// libstdc++/11544 (incomplete character in file)
156
void test01()
157
{
158
  using namespace std;
159
  bool test __attribute__((unused)) = true;
160
 
161
  locale loc(locale::classic(), new checksumcvt<wchar_t>);
162
 
163
  const char* name = "tmp_11544-1";
164
 
165
  FILE* f = fopen(name, "w");
166
  putc('a', f);
167
  fclose(f);
168
 
169
  wifstream in;
170
  in.imbue(loc);
171
  in.open(name);
172
 
173
  VERIFY( in.good() );
174
  in.get();
175
  VERIFY( !in.good() );
176
  VERIFY( in.bad() );
177
  VERIFY( !in.eof() );
178
 
179
  in.close();
180
}
181
 
182
int main()
183
{
184
  test01();
185
  return 0;
186
}

powered by: WebSVN 2.1.0

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