Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / libstdc++-v3 / testsuite / 27_io / basic_filebuf / seekoff / wchar_t / 11543.cc
1 // Copyright (C) 2003-2013 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 #include <fstream>
21 #include <locale>
22 #include <testsuite_hooks.h>
23
24 struct MyState
25 {
26 };
27
28 struct MyCharTraits : std::char_traits<wchar_t>
29 {
30   typedef std::fpos<MyState> pos_type;
31   typedef MyState state_type;
32 };
33
34 namespace std
35 {
36   template <>
37     class codecvt<wchar_t, char, MyState> :
38       public locale::facet, public codecvt_base
39     {
40     public:
41       typedef wchar_t intern_type;
42       typedef char extern_type;
43       typedef MyState state_type;
44     
45       explicit codecvt(size_t refs = 0)
46       : locale::facet(refs) { }
47     
48       result out(state_type& state, const intern_type* from,
49                  const intern_type* from_end,  const intern_type*& from_next,
50                  extern_type* to, extern_type* to_limit,
51                  extern_type*& to_next) const
52       { return do_out(state, from, from_end, from_next,
53                       to, to_limit, to_next); }
54
55       result unshift(state_type& state, extern_type* to, extern_type* to_limit,
56                      extern_type*& to_next) const
57       { return do_unshift(state, to, to_limit, to_next); }
58
59       result in(state_type& state, const extern_type* from,
60                 const extern_type* from_end, const extern_type*& from_next,
61                 intern_type* to, intern_type* to_limit,
62                 intern_type*& to_next) const
63       { return do_in(state, from, from_end, from_next,
64                      to, to_limit, to_next); }
65
66       int encoding() const throw()
67       { return do_encoding(); }
68       
69       bool always_noconv() const throw()
70       { return do_always_noconv(); }
71
72       int length(state_type& state, const extern_type* from,
73                  const extern_type* end, size_t max) const
74       { return do_length(state, from, end, max); }
75
76       int max_length() const throw()
77       { return do_max_length(); }
78     
79       static locale::id id;
80     
81     protected:
82       virtual ~codecvt();
83
84       virtual result do_out(state_type&, const intern_type*,
85                             const intern_type*, const intern_type*&,
86                             extern_type*, extern_type*, extern_type*&) const
87       { return error; }
88
89       virtual result do_in(state_type&, const extern_type*, const extern_type*,
90                            const extern_type*&, intern_type*, intern_type*,
91                            intern_type*&) const
92       { return error; }
93
94       virtual result do_unshift(state_type&, extern_type*, extern_type*,
95                                 extern_type*&) const
96       { return noconv; }
97
98       virtual int do_encoding() const throw()
99       { return 1; }
100
101       virtual bool do_always_noconv() const throw()
102       { return false; }
103
104       virtual int do_length(state_type&, const extern_type* from,
105                             const extern_type* end, size_t max) const
106       {
107         size_t len = end - from;
108         return std::min(max, len);
109       }
110
111       virtual int do_max_length() const throw()
112       { return 1; }
113     };
114   
115   locale::id codecvt<wchar_t, char, MyState>::id;
116
117   codecvt<wchar_t, char, MyState>::~codecvt()
118   { }
119 }
120
121 void test01()
122 {
123   bool test __attribute__((unused)) = true;
124
125   std::locale loc(std::locale::classic(),
126                   new std::codecvt<wchar_t, char, MyState>);
127   std::basic_filebuf<wchar_t, MyCharTraits> fb;
128   fb.pubimbue(loc);
129   fb.open("tmp_11543", std::ios_base::out);
130   VERIFY( fb.is_open() );
131   MyCharTraits::pos_type pos = fb.pubseekoff(0, std::ios_base::beg);
132   VERIFY( pos != MyCharTraits::pos_type(MyCharTraits::off_type(-1)) );
133   fb.close();
134 }
135
136 int main()
137 {
138   test01();
139   return 0;
140 }