1*c2c66affSColin Finck #include <string>
2*c2c66affSColin Finck #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)
3*c2c66affSColin Finck # include <fstream>
4*c2c66affSColin Finck # include <iostream>
5*c2c66affSColin Finck # include <iomanip>
6*c2c66affSColin Finck # include <sstream>
7*c2c66affSColin Finck # include <vector>
8*c2c66affSColin Finck # include <stdexcept>
9*c2c66affSColin Finck
10*c2c66affSColin Finck #include <stdio.h>
11*c2c66affSColin Finck
12*c2c66affSColin Finck # include "full_streambuf.h"
13*c2c66affSColin Finck # include "cppunit/cppunit_proxy.h"
14*c2c66affSColin Finck
15*c2c66affSColin Finck # if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
16*c2c66affSColin Finck using namespace std;
17*c2c66affSColin Finck # endif
18*c2c66affSColin Finck
19*c2c66affSColin Finck //The macro value gives approximately the generated file
20*c2c66affSColin Finck //size in Go
21*c2c66affSColin Finck //#define CHECK_BIG_FILE 4
22*c2c66affSColin Finck
23*c2c66affSColin Finck # if (!defined(STLPORT) && (defined (__GNUC__) && (__GNUC__ > 3))) || \
24*c2c66affSColin Finck (defined (STLPORT) && !defined (_STLP_NO_CUSTOM_IO) && !defined (_STLP_NO_MEMBER_TEMPLATES) && \
25*c2c66affSColin Finck !((defined (_STLP_MSVC) && (_STLP_MSVC < 1300)) || \
26*c2c66affSColin Finck (defined (__GNUC__) && (__GNUC__ < 3)) || \
27*c2c66affSColin Finck (defined (__SUNPRO_CC)) || \
28*c2c66affSColin Finck (defined (__DMC__) && defined (_DLL))))
29*c2c66affSColin Finck # define DO_CUSTOM_FACET_TEST
30*c2c66affSColin Finck # endif
31*c2c66affSColin Finck
32*c2c66affSColin Finck //
33*c2c66affSColin Finck // TestCase class
34*c2c66affSColin Finck //
35*c2c66affSColin Finck class FstreamTest : public CPPUNIT_NS::TestCase
36*c2c66affSColin Finck {
37*c2c66affSColin Finck CPPUNIT_TEST_SUITE(FstreamTest);
38*c2c66affSColin Finck CPPUNIT_TEST(output);
39*c2c66affSColin Finck CPPUNIT_TEST(input);
40*c2c66affSColin Finck CPPUNIT_TEST(input_char);
41*c2c66affSColin Finck CPPUNIT_TEST(io);
42*c2c66affSColin Finck CPPUNIT_TEST(err);
43*c2c66affSColin Finck CPPUNIT_TEST(tellg);
44*c2c66affSColin Finck CPPUNIT_TEST(tellp);
45*c2c66affSColin Finck CPPUNIT_TEST(seek);
46*c2c66affSColin Finck CPPUNIT_TEST(buf);
47*c2c66affSColin Finck CPPUNIT_TEST(rdbuf);
48*c2c66affSColin Finck CPPUNIT_TEST(streambuf_output);
49*c2c66affSColin Finck CPPUNIT_TEST(win32_file_format);
50*c2c66affSColin Finck CPPUNIT_TEST(null_stream);
51*c2c66affSColin Finck # if defined (STLPORT) && (defined (_STLP_NO_WCHAR_T) || !defined (_STLP_USE_EXCEPTIONS))
52*c2c66affSColin Finck CPPUNIT_IGNORE;
53*c2c66affSColin Finck # endif
54*c2c66affSColin Finck CPPUNIT_TEST(null_buf);
55*c2c66affSColin Finck # if !defined (STLPORT) || !defined (_STLP_WIN32)
56*c2c66affSColin Finck CPPUNIT_TEST(offset);
57*c2c66affSColin Finck # endif
58*c2c66affSColin Finck # if defined (CHECK_BIG_FILE)
59*c2c66affSColin Finck CPPUNIT_TEST(big_file);
60*c2c66affSColin Finck # endif
61*c2c66affSColin Finck # if !defined (DO_CUSTOM_FACET_TEST)
62*c2c66affSColin Finck CPPUNIT_IGNORE;
63*c2c66affSColin Finck # endif
64*c2c66affSColin Finck CPPUNIT_TEST(custom_facet);
65*c2c66affSColin Finck CPPUNIT_TEST_SUITE_END();
66*c2c66affSColin Finck
67*c2c66affSColin Finck protected:
68*c2c66affSColin Finck void output();
69*c2c66affSColin Finck void input();
70*c2c66affSColin Finck void input_char();
71*c2c66affSColin Finck void io();
72*c2c66affSColin Finck void err();
73*c2c66affSColin Finck void tellg();
74*c2c66affSColin Finck void tellp();
75*c2c66affSColin Finck void seek();
76*c2c66affSColin Finck void buf();
77*c2c66affSColin Finck void rdbuf();
78*c2c66affSColin Finck void streambuf_output();
79*c2c66affSColin Finck void win32_file_format();
80*c2c66affSColin Finck void null_stream();
81*c2c66affSColin Finck void null_buf();
82*c2c66affSColin Finck # if !defined (STLPORT) || !defined (_STLP_WIN32)
83*c2c66affSColin Finck void offset();
84*c2c66affSColin Finck # endif
85*c2c66affSColin Finck void custom_facet();
86*c2c66affSColin Finck # if defined (CHECK_BIG_FILE)
87*c2c66affSColin Finck void big_file();
88*c2c66affSColin Finck # endif
89*c2c66affSColin Finck };
90*c2c66affSColin Finck
91*c2c66affSColin Finck CPPUNIT_TEST_SUITE_REGISTRATION(FstreamTest);
92*c2c66affSColin Finck
93*c2c66affSColin Finck //
94*c2c66affSColin Finck // tests implementation
95*c2c66affSColin Finck //
output()96*c2c66affSColin Finck void FstreamTest::output()
97*c2c66affSColin Finck {
98*c2c66affSColin Finck ofstream f( "test_file.txt" );
99*c2c66affSColin Finck
100*c2c66affSColin Finck f << 1 << '\n' << 2.0 << '\n' << "abcd\n" << "ghk lm\n" << "abcd ef";
101*c2c66affSColin Finck CPPUNIT_ASSERT (f.good());
102*c2c66affSColin Finck // CPPUNIT_ASSERT( s.str() == "1\n2\nabcd\nghk lm\nabcd ef" );
103*c2c66affSColin Finck }
104*c2c66affSColin Finck
input()105*c2c66affSColin Finck void FstreamTest::input()
106*c2c66affSColin Finck {
107*c2c66affSColin Finck {
108*c2c66affSColin Finck ifstream f( "test_file.txt" );
109*c2c66affSColin Finck int i = 0;
110*c2c66affSColin Finck f >> i;
111*c2c66affSColin Finck CPPUNIT_ASSERT( f.good() );
112*c2c66affSColin Finck CPPUNIT_ASSERT( i == 1 );
113*c2c66affSColin Finck double d = 0.0;
114*c2c66affSColin Finck f >> d;
115*c2c66affSColin Finck CPPUNIT_ASSERT( f.good() );
116*c2c66affSColin Finck CPPUNIT_ASSERT( d == 2.0 );
117*c2c66affSColin Finck string str;
118*c2c66affSColin Finck f >> str;
119*c2c66affSColin Finck CPPUNIT_ASSERT( f.good() );
120*c2c66affSColin Finck CPPUNIT_ASSERT( str == "abcd" );
121*c2c66affSColin Finck char c;
122*c2c66affSColin Finck f.get(c); // extract newline, that not extracted by operator >>
123*c2c66affSColin Finck CPPUNIT_ASSERT( f.good() );
124*c2c66affSColin Finck CPPUNIT_ASSERT( c == '\n' );
125*c2c66affSColin Finck getline( f, str );
126*c2c66affSColin Finck CPPUNIT_ASSERT( f.good() );
127*c2c66affSColin Finck CPPUNIT_ASSERT( str == "ghk lm" );
128*c2c66affSColin Finck getline( f, str );
129*c2c66affSColin Finck CPPUNIT_ASSERT( f.eof() );
130*c2c66affSColin Finck CPPUNIT_ASSERT( str == "abcd ef" );
131*c2c66affSColin Finck }
132*c2c66affSColin Finck #if defined (STLPORT) && !defined (_STLP_USE_WIN32_IO)
133*c2c66affSColin Finck {
134*c2c66affSColin Finck ifstream in("/tmp");
135*c2c66affSColin Finck if (in.good()) {
136*c2c66affSColin Finck string s;
137*c2c66affSColin Finck getline(in, s);
138*c2c66affSColin Finck CPPUNIT_ASSERT( in.fail() );
139*c2c66affSColin Finck }
140*c2c66affSColin Finck }
141*c2c66affSColin Finck #endif
142*c2c66affSColin Finck }
143*c2c66affSColin Finck
input_char()144*c2c66affSColin Finck void FstreamTest::input_char()
145*c2c66affSColin Finck {
146*c2c66affSColin Finck char buf[16] = { 0, '1', '2', '3' };
147*c2c66affSColin Finck ifstream s( "test_file.txt" );
148*c2c66affSColin Finck s >> buf;
149*c2c66affSColin Finck
150*c2c66affSColin Finck CPPUNIT_ASSERT( buf[0] == '1' );
151*c2c66affSColin Finck CPPUNIT_ASSERT( buf[1] == 0 );
152*c2c66affSColin Finck CPPUNIT_ASSERT( buf[2] == '2' );
153*c2c66affSColin Finck }
154*c2c66affSColin Finck
io()155*c2c66affSColin Finck void FstreamTest::io()
156*c2c66affSColin Finck {
157*c2c66affSColin Finck basic_fstream<char,char_traits<char> > f( "test_file.txt", ios_base::in | ios_base::out | ios_base::trunc );
158*c2c66affSColin Finck
159*c2c66affSColin Finck CPPUNIT_ASSERT( f.is_open() );
160*c2c66affSColin Finck
161*c2c66affSColin Finck f << 1 << '\n' << 2.0 << '\n' << "abcd\n" << "ghk lm\n" << "abcd ef";
162*c2c66affSColin Finck
163*c2c66affSColin Finck // f.flush();
164*c2c66affSColin Finck f.seekg( 0, ios_base::beg );
165*c2c66affSColin Finck
166*c2c66affSColin Finck int i = 0;
167*c2c66affSColin Finck f >> i;
168*c2c66affSColin Finck CPPUNIT_ASSERT( f.good() );
169*c2c66affSColin Finck CPPUNIT_ASSERT( i == 1 );
170*c2c66affSColin Finck double d = 0.0;
171*c2c66affSColin Finck f >> d;
172*c2c66affSColin Finck CPPUNIT_ASSERT( d == 2.0 );
173*c2c66affSColin Finck string s;
174*c2c66affSColin Finck f >> s;
175*c2c66affSColin Finck CPPUNIT_ASSERT( f.good() );
176*c2c66affSColin Finck CPPUNIT_ASSERT( s == "abcd" );
177*c2c66affSColin Finck char c;
178*c2c66affSColin Finck f.get(c); // extract newline, that not extracted by operator >>
179*c2c66affSColin Finck CPPUNIT_ASSERT( f.good() );
180*c2c66affSColin Finck CPPUNIT_ASSERT( c == '\n' );
181*c2c66affSColin Finck getline( f, s );
182*c2c66affSColin Finck CPPUNIT_ASSERT( f.good() );
183*c2c66affSColin Finck CPPUNIT_ASSERT( s == "ghk lm" );
184*c2c66affSColin Finck getline( f, s );
185*c2c66affSColin Finck CPPUNIT_ASSERT( !f.fail() );
186*c2c66affSColin Finck CPPUNIT_ASSERT( s == "abcd ef" );
187*c2c66affSColin Finck CPPUNIT_ASSERT( f.eof() );
188*c2c66affSColin Finck }
189*c2c66affSColin Finck
err()190*c2c66affSColin Finck void FstreamTest::err()
191*c2c66affSColin Finck {
192*c2c66affSColin Finck basic_fstream<char,char_traits<char> > f( "test_file.txt", ios_base::in | ios_base::out | ios_base::trunc );
193*c2c66affSColin Finck
194*c2c66affSColin Finck CPPUNIT_ASSERT( f.is_open() );
195*c2c66affSColin Finck
196*c2c66affSColin Finck int i = 9;
197*c2c66affSColin Finck f << i;
198*c2c66affSColin Finck CPPUNIT_ASSERT( f.good() );
199*c2c66affSColin Finck i = 0;
200*c2c66affSColin Finck f.seekg( 0, ios_base::beg );
201*c2c66affSColin Finck f >> i;
202*c2c66affSColin Finck CPPUNIT_ASSERT( !f.fail() );
203*c2c66affSColin Finck CPPUNIT_ASSERT( i == 9 );
204*c2c66affSColin Finck f >> i;
205*c2c66affSColin Finck CPPUNIT_ASSERT( f.fail() );
206*c2c66affSColin Finck CPPUNIT_ASSERT( f.eof() );
207*c2c66affSColin Finck CPPUNIT_ASSERT( i == 9 );
208*c2c66affSColin Finck }
209*c2c66affSColin Finck
tellg()210*c2c66affSColin Finck void FstreamTest::tellg()
211*c2c66affSColin Finck {
212*c2c66affSColin Finck {
213*c2c66affSColin Finck // bogus ios_base::binary is for Wins
214*c2c66affSColin Finck ofstream of("test_file.txt", ios_base::out | ios_base::binary | ios_base::trunc);
215*c2c66affSColin Finck CPPUNIT_ASSERT( of.is_open() );
216*c2c66affSColin Finck
217*c2c66affSColin Finck for (int i = 0; i < 50; ++i) {
218*c2c66affSColin Finck of << "line " << setiosflags(ios_base::right) << setfill('0') << setw(2) << i << "\n";
219*c2c66affSColin Finck CPPUNIT_ASSERT( !of.fail() );
220*c2c66affSColin Finck }
221*c2c66affSColin Finck of.close();
222*c2c66affSColin Finck }
223*c2c66affSColin Finck
224*c2c66affSColin Finck {
225*c2c66affSColin Finck // bogus ios_base::binary is for Wins
226*c2c66affSColin Finck ifstream is("test_file.txt", ios_base::in | ios_base::binary);
227*c2c66affSColin Finck CPPUNIT_ASSERT( is.is_open() );
228*c2c66affSColin Finck char buf[64];
229*c2c66affSColin Finck
230*c2c66affSColin Finck // CPPUNIT_ASSERT( is.tellg() == 0 );
231*c2c66affSColin Finck streampos p = 0;
232*c2c66affSColin Finck for (int i = 0; i < 50; ++i) {
233*c2c66affSColin Finck is.read(buf, 0);
234*c2c66affSColin Finck CPPUNIT_ASSERT( is.gcount() == 0 );
235*c2c66affSColin Finck CPPUNIT_ASSERT( is.tellg() == p );
236*c2c66affSColin Finck is.read( buf, 8 );
237*c2c66affSColin Finck CPPUNIT_ASSERT( !is.fail() );
238*c2c66affSColin Finck CPPUNIT_ASSERT( is.gcount() == 8 );
239*c2c66affSColin Finck p += 8;
240*c2c66affSColin Finck }
241*c2c66affSColin Finck }
242*c2c66affSColin Finck
243*c2c66affSColin Finck {
244*c2c66affSColin Finck // bogus ios_base::binary is for Wins
245*c2c66affSColin Finck ifstream is("test_file.txt", ios_base::in | ios_base::binary);
246*c2c66affSColin Finck CPPUNIT_ASSERT( is.is_open() );
247*c2c66affSColin Finck
248*c2c66affSColin Finck streampos p = 0;
249*c2c66affSColin Finck for (int i = 0; i < 50; ++i) {
250*c2c66affSColin Finck CPPUNIT_ASSERT( !is.fail() );
251*c2c66affSColin Finck is.tellg();
252*c2c66affSColin Finck CPPUNIT_ASSERT( is.tellg() == p );
253*c2c66affSColin Finck p += 8;
254*c2c66affSColin Finck is.seekg( p, ios_base::beg );
255*c2c66affSColin Finck CPPUNIT_ASSERT( !is.fail() );
256*c2c66affSColin Finck }
257*c2c66affSColin Finck }
258*c2c66affSColin Finck
259*c2c66affSColin Finck {
260*c2c66affSColin Finck // bogus ios_base::binary is for Wins
261*c2c66affSColin Finck ifstream is("test_file.txt", ios_base::in | ios_base::binary);
262*c2c66affSColin Finck CPPUNIT_ASSERT( is.is_open() );
263*c2c66affSColin Finck
264*c2c66affSColin Finck streampos p = 0;
265*c2c66affSColin Finck for (int i = 0; i < 50; ++i) {
266*c2c66affSColin Finck CPPUNIT_ASSERT( is.tellg() == p );
267*c2c66affSColin Finck p += 8;
268*c2c66affSColin Finck is.seekg( 8, ios_base::cur );
269*c2c66affSColin Finck CPPUNIT_ASSERT( !is.fail() );
270*c2c66affSColin Finck }
271*c2c66affSColin Finck }
272*c2c66affSColin Finck }
273*c2c66affSColin Finck
tellp()274*c2c66affSColin Finck void FstreamTest::tellp()
275*c2c66affSColin Finck {
276*c2c66affSColin Finck {
277*c2c66affSColin Finck ofstream o( "test_file.txt" );
278*c2c66affSColin Finck
279*c2c66affSColin Finck o << "123456";
280*c2c66affSColin Finck
281*c2c66affSColin Finck CPPUNIT_CHECK( o.rdbuf()->pubseekoff( 0, ios_base::cur, ios_base::out ) == ofstream::pos_type(6) );
282*c2c66affSColin Finck CPPUNIT_CHECK( o.tellp() == ofstream::pos_type(6) );
283*c2c66affSColin Finck }
284*c2c66affSColin Finck {
285*c2c66affSColin Finck ofstream o( "test_file.txt" );
286*c2c66affSColin Finck
287*c2c66affSColin Finck o << "123456789";
288*c2c66affSColin Finck
289*c2c66affSColin Finck CPPUNIT_CHECK( o.rdbuf()->pubseekoff( 0, ios_base::cur, ios_base::out ) == ofstream::pos_type(9) );
290*c2c66affSColin Finck CPPUNIT_CHECK( o.tellp() == ofstream::pos_type(9) );
291*c2c66affSColin Finck }
292*c2c66affSColin Finck /* According to the standard
293*c2c66affSColin Finck ofstream o( "test_file.txt", ios_base::app | ios_base::out )
294*c2c66affSColin Finck should give the same effect as fopen( "test_file.txt", "a" ).
295*c2c66affSColin Finck Problem is fopen( "test_file.txt", "a" ) has a bit different behaviour
296*c2c66affSColin Finck on different platforms, and this difference is not covered by specification.
297*c2c66affSColin Finck After fopen( "test_file.txt", "a" ) in this context ftell( f ) == 9 for
298*c2c66affSColin Finck Linux and Mac OS X (I expect the same for others POSIX-like platforms too);
299*c2c66affSColin Finck on Windows (independently from version?) ftell( f ) == 0, i.e. write pointer not
300*c2c66affSColin Finck shifted to EOF (but shifted to EOF just before write, as described in the specs).
301*c2c66affSColin Finck
302*c2c66affSColin Finck It isn't specifications violation, neither for Linux and Mac OS X nor for Windows.
303*c2c66affSColin Finck
304*c2c66affSColin Finck The code below is intended to demonstrate ambiguity (dependance from fopen implementation).
305*c2c66affSColin Finck */
306*c2c66affSColin Finck {
307*c2c66affSColin Finck #ifdef WIN32
308*c2c66affSColin Finck //In Windows, stlport and fopen use kernel32.CreateFile for open.
309*c2c66affSColin Finck //File position is at BOF after open, unless we open with ios_base::ate
310*c2c66affSColin Finck long expected_pos = 0;
311*c2c66affSColin Finck #else
312*c2c66affSColin Finck //On UNIX flavours, stlport and fopen use unix's open
313*c2c66affSColin Finck //File position is at EOF after open
314*c2c66affSColin Finck //
315*c2c66affSColin Finck //3rd possible scenario, "other platforms" - _STLP_USE_STDIO_IO
316*c2c66affSColin Finck //stlport uses fopen here. This case may fail this test, since the file position after
317*c2c66affSColin Finck //fopen is implementation-dependent
318*c2c66affSColin Finck long expected_pos = 9;
319*c2c66affSColin Finck #endif
320*c2c66affSColin Finck ofstream o( "test_file.txt", ios_base::app | ios_base::out );
321*c2c66affSColin Finck CPPUNIT_CHECK( o.rdbuf()->pubseekoff( 0, ios_base::cur, ios_base::out ) == ofstream::pos_type(expected_pos) );
322*c2c66affSColin Finck CPPUNIT_CHECK( o.tellp() == ofstream::pos_type(expected_pos) );
323*c2c66affSColin Finck }
324*c2c66affSColin Finck { // for reference, to test fopen/ftell behaviour in append mode:
325*c2c66affSColin Finck #ifdef WIN32
326*c2c66affSColin Finck long expected_pos = 0;
327*c2c66affSColin Finck #else
328*c2c66affSColin Finck long expected_pos = 9;
329*c2c66affSColin Finck #endif
330*c2c66affSColin Finck FILE* f = fopen( "test_file.txt", "a" );
331*c2c66affSColin Finck CPPUNIT_CHECK( ftell( f ) == expected_pos );
332*c2c66affSColin Finck fclose( f );
333*c2c66affSColin Finck }
334*c2c66affSColin Finck {
335*c2c66affSColin Finck //In append mode, file is positioned at EOF just before a write.
336*c2c66affSColin Finck // After a write, file is at EOF. This is implementation-independent.
337*c2c66affSColin Finck ofstream o( "test_file.txt", ios_base::app | ios_base::out );
338*c2c66affSColin Finck o << "X";
339*c2c66affSColin Finck CPPUNIT_CHECK( o.rdbuf()->pubseekoff( 0, ios_base::cur, ios_base::out ) == ofstream::pos_type(10) );
340*c2c66affSColin Finck CPPUNIT_CHECK( o.tellp() == ofstream::pos_type(10) );
341*c2c66affSColin Finck }
342*c2c66affSColin Finck }
343*c2c66affSColin Finck
buf()344*c2c66affSColin Finck void FstreamTest::buf()
345*c2c66affSColin Finck {
346*c2c66affSColin Finck fstream ss( "test_file.txt", ios_base::in | ios_base::out | ios_base::binary | ios_base::trunc );
347*c2c66affSColin Finck
348*c2c66affSColin Finck ss << "1234567\n89\n";
349*c2c66affSColin Finck ss.seekg( 0, ios_base::beg );
350*c2c66affSColin Finck char buf[10];
351*c2c66affSColin Finck buf[7] = 'x';
352*c2c66affSColin Finck ss.get( buf, 10 );
353*c2c66affSColin Finck CPPUNIT_ASSERT( !ss.fail() );
354*c2c66affSColin Finck CPPUNIT_ASSERT( buf[0] == '1' );
355*c2c66affSColin Finck CPPUNIT_ASSERT( buf[1] == '2' );
356*c2c66affSColin Finck CPPUNIT_ASSERT( buf[2] == '3' );
357*c2c66affSColin Finck CPPUNIT_ASSERT( buf[3] == '4' );
358*c2c66affSColin Finck CPPUNIT_ASSERT( buf[4] == '5' );
359*c2c66affSColin Finck CPPUNIT_ASSERT( buf[5] == '6' );
360*c2c66affSColin Finck CPPUNIT_ASSERT( buf[6] == '7' ); // 27.6.1.3 paragraph 10, paragraph 7
361*c2c66affSColin Finck CPPUNIT_ASSERT( buf[7] == 0 ); // 27.6.1.3 paragraph 8
362*c2c66affSColin Finck char c;
363*c2c66affSColin Finck ss.get(c);
364*c2c66affSColin Finck CPPUNIT_ASSERT( !ss.fail() );
365*c2c66affSColin Finck CPPUNIT_ASSERT( c == '\n' ); // 27.6.1.3 paragraph 10, paragraph 7
366*c2c66affSColin Finck ss.get(c);
367*c2c66affSColin Finck CPPUNIT_ASSERT( !ss.fail() );
368*c2c66affSColin Finck CPPUNIT_ASSERT( c == '8' );
369*c2c66affSColin Finck }
370*c2c66affSColin Finck
seek()371*c2c66affSColin Finck void FstreamTest::seek()
372*c2c66affSColin Finck {
373*c2c66affSColin Finck {
374*c2c66affSColin Finck // Test in binary mode:
375*c2c66affSColin Finck {
376*c2c66affSColin Finck fstream s( "test_file.txt", ios_base::in | ios_base::out | ios_base::binary | ios_base::trunc );
377*c2c66affSColin Finck CPPUNIT_ASSERT( s );
378*c2c66affSColin Finck
379*c2c66affSColin Finck s << "1234567890\n";
380*c2c66affSColin Finck CPPUNIT_ASSERT( s );
381*c2c66affSColin Finck }
382*c2c66affSColin Finck
383*c2c66affSColin Finck char b1[] = { 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x' };
384*c2c66affSColin Finck fstream s( "test_file.txt", ios_base::in | ios_base::out | ios_base::binary );
385*c2c66affSColin Finck CPPUNIT_ASSERT( s );
386*c2c66affSColin Finck
387*c2c66affSColin Finck int chars_read = (int)s.rdbuf()->sgetn( b1, sizeof(b1) );
388*c2c66affSColin Finck CPPUNIT_CHECK( chars_read == 11 );
389*c2c66affSColin Finck CPPUNIT_CHECK( b1[9] == '0' );
390*c2c66affSColin Finck CPPUNIT_ASSERT( s.rdbuf()->pubseekoff( 0, ios_base::cur ) == fstream::pos_type(chars_read) );
391*c2c66affSColin Finck CPPUNIT_ASSERT( s.rdbuf()->pubseekoff( -chars_read, ios_base::cur ) == fstream::pos_type(0) );
392*c2c66affSColin Finck
393*c2c66affSColin Finck char b2[10] = { 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y' };
394*c2c66affSColin Finck
395*c2c66affSColin Finck CPPUNIT_ASSERT( s.rdbuf()->sgetn( b2, 10 ) == 10 );
396*c2c66affSColin Finck CPPUNIT_CHECK( b2[9] == '0' );
397*c2c66affSColin Finck }
398*c2c66affSColin Finck
399*c2c66affSColin Finck {
400*c2c66affSColin Finck // Test in text mode:
401*c2c66affSColin Finck {
402*c2c66affSColin Finck fstream s( "test_file.txt", ios_base::in | ios_base::out | ios_base::trunc );
403*c2c66affSColin Finck CPPUNIT_ASSERT( s );
404*c2c66affSColin Finck
405*c2c66affSColin Finck s << "1234567890\n";
406*c2c66affSColin Finck CPPUNIT_ASSERT( s );
407*c2c66affSColin Finck }
408*c2c66affSColin Finck
409*c2c66affSColin Finck char b1[] = { 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x' };
410*c2c66affSColin Finck fstream s( "test_file.txt", ios_base::in | ios_base::out );
411*c2c66affSColin Finck CPPUNIT_ASSERT( s );
412*c2c66affSColin Finck
413*c2c66affSColin Finck int chars_read = (int)s.rdbuf()->sgetn( b1, sizeof(b1) );
414*c2c66affSColin Finck CPPUNIT_CHECK( chars_read == 11 );
415*c2c66affSColin Finck CPPUNIT_CHECK( b1[9] == '0' );
416*c2c66affSColin Finck
417*c2c66affSColin Finck fstream::pos_type pos = s.rdbuf()->pubseekoff(0, ios_base::cur);
418*c2c66affSColin Finck // Depending on how '\n' is written in file, file position can be greater or equal to the number of chars_read read.
419*c2c66affSColin Finck streamoff offset = pos;
420*c2c66affSColin Finck CPPUNIT_ASSERT( offset >= chars_read );
421*c2c66affSColin Finck offset = s.rdbuf()->pubseekoff( -offset, ios_base::cur );
422*c2c66affSColin Finck CPPUNIT_ASSERT( offset == 0 );
423*c2c66affSColin Finck
424*c2c66affSColin Finck char b2[10] = { 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y' };
425*c2c66affSColin Finck
426*c2c66affSColin Finck CPPUNIT_ASSERT( s.rdbuf()->sgetn( b2, 5 ) == 5 );
427*c2c66affSColin Finck CPPUNIT_CHECK( b2[4] == '5' );
428*c2c66affSColin Finck
429*c2c66affSColin Finck pos = s.rdbuf()->pubseekoff(0, ios_base::cur);
430*c2c66affSColin Finck CPPUNIT_ASSERT( pos == fstream::pos_type(5) );
431*c2c66affSColin Finck CPPUNIT_ASSERT( s.rdbuf()->pubseekoff(-5, ios_base::cur) == fstream::pos_type(0) );
432*c2c66affSColin Finck }
433*c2c66affSColin Finck
434*c2c66affSColin Finck #if !defined (STLPORT) || \
435*c2c66affSColin Finck (!defined (_STLP_NO_WCHAR_T) && defined (_STLP_USE_EXCEPTIONS))
436*c2c66affSColin Finck {
437*c2c66affSColin Finck // Test with a wariable encoding:
438*c2c66affSColin Finck locale loc;
439*c2c66affSColin Finck try
440*c2c66affSColin Finck {
441*c2c66affSColin Finck locale tmp(locale::classic(), new codecvt_byname<wchar_t, char, mbstate_t>(".UTF8"));
442*c2c66affSColin Finck loc = tmp;
443*c2c66affSColin Finck }
444*c2c66affSColin Finck catch (const runtime_error&)
445*c2c66affSColin Finck {
446*c2c66affSColin Finck // Localization no supported so no test:
447*c2c66affSColin Finck return;
448*c2c66affSColin Finck }
449*c2c66affSColin Finck
450*c2c66affSColin Finck {
451*c2c66affSColin Finck wfstream s( "test_file.txt", ios_base::in | ios_base::out | ios_base::trunc );
452*c2c66affSColin Finck CPPUNIT_ASSERT( s );
453*c2c66affSColin Finck s.imbue(loc);
454*c2c66affSColin Finck CPPUNIT_ASSERT( s );
455*c2c66affSColin Finck
456*c2c66affSColin Finck s << L"1234567890\n";
457*c2c66affSColin Finck CPPUNIT_ASSERT( s );
458*c2c66affSColin Finck }
459*c2c66affSColin Finck
460*c2c66affSColin Finck wchar_t b1[] = { L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x' };
461*c2c66affSColin Finck wfstream s( "test_file.txt", ios_base::in | ios_base::out );
462*c2c66affSColin Finck CPPUNIT_ASSERT( s );
463*c2c66affSColin Finck s.imbue(loc);
464*c2c66affSColin Finck CPPUNIT_ASSERT( s );
465*c2c66affSColin Finck
466*c2c66affSColin Finck int chars_read = (int)s.rdbuf()->sgetn( b1, sizeof(b1) / sizeof(wchar_t) );
467*c2c66affSColin Finck CPPUNIT_CHECK( chars_read == 11 );
468*c2c66affSColin Finck CPPUNIT_CHECK( b1[9] == L'0' );
469*c2c66affSColin Finck
470*c2c66affSColin Finck fstream::pos_type pos = s.rdbuf()->pubseekoff(0, ios_base::cur);
471*c2c66affSColin Finck // Depending on how '\n' is written in file, file position can be greater or equal to the number of chars_read read.
472*c2c66affSColin Finck streamoff off = pos;
473*c2c66affSColin Finck CPPUNIT_ASSERT( off >= chars_read );
474*c2c66affSColin Finck off = s.rdbuf()->pubseekoff(-off, ios_base::cur);
475*c2c66affSColin Finck CPPUNIT_ASSERT( off == -1 );
476*c2c66affSColin Finck off = s.rdbuf()->pubseekoff(0, ios_base::beg);
477*c2c66affSColin Finck CPPUNIT_ASSERT( off == 0 );
478*c2c66affSColin Finck
479*c2c66affSColin Finck wchar_t b2[10] = { L'y', L'y', L'y', L'y', L'y', L'y', L'y', L'y', L'y', L'y' };
480*c2c66affSColin Finck
481*c2c66affSColin Finck CPPUNIT_ASSERT( s.rdbuf()->sgetn( b2, 5 ) == 5 );
482*c2c66affSColin Finck CPPUNIT_CHECK( b2[4] == L'5' );
483*c2c66affSColin Finck
484*c2c66affSColin Finck pos = s.rdbuf()->pubseekoff(0, ios_base::cur);
485*c2c66affSColin Finck CPPUNIT_ASSERT( pos == fstream::pos_type(5) );
486*c2c66affSColin Finck //CPPUNIT_ASSERT( s.rdbuf()->pubseekoff(-5, ios_base::cur) == fstream::pos_type(0) );
487*c2c66affSColin Finck }
488*c2c66affSColin Finck #endif
489*c2c66affSColin Finck }
490*c2c66affSColin Finck
rdbuf()491*c2c66affSColin Finck void FstreamTest::rdbuf()
492*c2c66affSColin Finck {
493*c2c66affSColin Finck fstream ss( "test_file.txt", ios_base::in | ios_base::out | ios_base::binary | ios_base::trunc );
494*c2c66affSColin Finck
495*c2c66affSColin Finck ss << "1234567\n89\n";
496*c2c66affSColin Finck ss.seekg( 0, ios_base::beg );
497*c2c66affSColin Finck
498*c2c66affSColin Finck ostringstream os;
499*c2c66affSColin Finck ss.get( *os.rdbuf(), '\n' );
500*c2c66affSColin Finck CPPUNIT_ASSERT( !ss.fail() );
501*c2c66affSColin Finck char c;
502*c2c66affSColin Finck ss.get(c);
503*c2c66affSColin Finck CPPUNIT_ASSERT( !ss.fail() );
504*c2c66affSColin Finck CPPUNIT_ASSERT( c == '\n' ); // 27.6.1.3 paragraph 12
505*c2c66affSColin Finck CPPUNIT_ASSERT( os.str() == "1234567" );
506*c2c66affSColin Finck }
507*c2c66affSColin Finck
streambuf_output()508*c2c66affSColin Finck void FstreamTest::streambuf_output()
509*c2c66affSColin Finck {
510*c2c66affSColin Finck {
511*c2c66affSColin Finck ofstream ofstr("test_file.txt", ios_base::binary);
512*c2c66affSColin Finck if (!ofstr)
513*c2c66affSColin Finck //No test if we cannot create the file
514*c2c66affSColin Finck return;
515*c2c66affSColin Finck ofstr << "01234567890123456789";
516*c2c66affSColin Finck CPPUNIT_ASSERT( ofstr );
517*c2c66affSColin Finck }
518*c2c66affSColin Finck
519*c2c66affSColin Finck {
520*c2c66affSColin Finck ifstream in("test_file.txt", ios_base::binary);
521*c2c66affSColin Finck CPPUNIT_ASSERT( in );
522*c2c66affSColin Finck
523*c2c66affSColin Finck full_streambuf full_buf(10);
524*c2c66affSColin Finck ostream out(&full_buf);
525*c2c66affSColin Finck CPPUNIT_ASSERT( out );
526*c2c66affSColin Finck
527*c2c66affSColin Finck out << in.rdbuf();
528*c2c66affSColin Finck CPPUNIT_ASSERT( out );
529*c2c66affSColin Finck CPPUNIT_ASSERT( in );
530*c2c66affSColin Finck CPPUNIT_ASSERT( full_buf.str() == "0123456789" );
531*c2c66affSColin Finck
532*c2c66affSColin Finck out << in.rdbuf();
533*c2c66affSColin Finck CPPUNIT_ASSERT( out.fail() );
534*c2c66affSColin Finck CPPUNIT_ASSERT( in );
535*c2c66affSColin Finck
536*c2c66affSColin Finck ostringstream ostr;
537*c2c66affSColin Finck ostr << in.rdbuf();
538*c2c66affSColin Finck CPPUNIT_ASSERT( ostr );
539*c2c66affSColin Finck CPPUNIT_ASSERT( in );
540*c2c66affSColin Finck CPPUNIT_ASSERT( ostr.str() == "0123456789" );
541*c2c66affSColin Finck }
542*c2c66affSColin Finck
543*c2c66affSColin Finck # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
544*c2c66affSColin Finck {
545*c2c66affSColin Finck //If the output stream buffer throws:
546*c2c66affSColin Finck ifstream in("test_file.txt", ios_base::binary);
547*c2c66affSColin Finck CPPUNIT_ASSERT( in );
548*c2c66affSColin Finck
549*c2c66affSColin Finck full_streambuf full_buf(10, true);
550*c2c66affSColin Finck ostream out(&full_buf);
551*c2c66affSColin Finck CPPUNIT_ASSERT( out );
552*c2c66affSColin Finck
553*c2c66affSColin Finck out << in.rdbuf();
554*c2c66affSColin Finck CPPUNIT_ASSERT( out.bad() );
555*c2c66affSColin Finck CPPUNIT_ASSERT( in );
556*c2c66affSColin Finck //out is bad we have no guaranty on what has been extracted:
557*c2c66affSColin Finck //CPPUNIT_ASSERT( full_buf.str() == "0123456789" );
558*c2c66affSColin Finck
559*c2c66affSColin Finck out.clear();
560*c2c66affSColin Finck out << in.rdbuf();
561*c2c66affSColin Finck CPPUNIT_ASSERT( out.fail() && out.bad() );
562*c2c66affSColin Finck CPPUNIT_ASSERT( in );
563*c2c66affSColin Finck
564*c2c66affSColin Finck ostringstream ostr;
565*c2c66affSColin Finck ostr << in.rdbuf();
566*c2c66affSColin Finck CPPUNIT_ASSERT( ostr );
567*c2c66affSColin Finck CPPUNIT_ASSERT( in );
568*c2c66affSColin Finck CPPUNIT_ASSERT( ostr.str() == "0123456789" );
569*c2c66affSColin Finck }
570*c2c66affSColin Finck # endif
571*c2c66affSColin Finck }
572*c2c66affSColin Finck
win32_file_format()573*c2c66affSColin Finck void FstreamTest::win32_file_format()
574*c2c66affSColin Finck {
575*c2c66affSColin Finck const char* file_name = "win32_file_format.tmp";
576*c2c66affSColin Finck const size_t nb_lines = 2049;
577*c2c66affSColin Finck {
578*c2c66affSColin Finck ofstream out(file_name);
579*c2c66affSColin Finck CPPUNIT_ASSERT( out.good() );
580*c2c66affSColin Finck out << 'a';
581*c2c66affSColin Finck for (size_t i = 0; i < nb_lines - 1; ++i) {
582*c2c66affSColin Finck out << '\n';
583*c2c66affSColin Finck }
584*c2c66affSColin Finck out << '\r';
585*c2c66affSColin Finck CPPUNIT_ASSERT( out.good() );
586*c2c66affSColin Finck }
587*c2c66affSColin Finck {
588*c2c66affSColin Finck ifstream in(file_name);
589*c2c66affSColin Finck CPPUNIT_ASSERT( in.good() );
590*c2c66affSColin Finck string line, last_line;
591*c2c66affSColin Finck size_t nb_read_lines = 0;
592*c2c66affSColin Finck while (getline(in, line)) {
593*c2c66affSColin Finck ++nb_read_lines;
594*c2c66affSColin Finck last_line = line;
595*c2c66affSColin Finck }
596*c2c66affSColin Finck CPPUNIT_ASSERT( in.eof() );
597*c2c66affSColin Finck CPPUNIT_ASSERT( nb_read_lines == nb_lines );
598*c2c66affSColin Finck CPPUNIT_ASSERT( !last_line.empty() && (last_line[0] == '\r') );
599*c2c66affSColin Finck }
600*c2c66affSColin Finck }
601*c2c66affSColin Finck
602*c2c66affSColin Finck #if defined (DO_CUSTOM_FACET_TEST)
603*c2c66affSColin Finck struct my_state {
604*c2c66affSColin Finck char dummy;
605*c2c66affSColin Finck };
606*c2c66affSColin Finck
607*c2c66affSColin Finck struct my_traits : public char_traits<char> {
608*c2c66affSColin Finck typedef my_state state_type;
609*c2c66affSColin Finck typedef fpos<state_type> pos_type;
610*c2c66affSColin Finck };
611*c2c66affSColin Finck
612*c2c66affSColin Finck #if !defined (STLPORT)
613*c2c66affSColin Finck //STLport grant a default implementation, other Standard libs implementation
614*c2c66affSColin Finck //do not necessarily do the same:
615*c2c66affSColin Finck namespace std {
616*c2c66affSColin Finck template <>
617*c2c66affSColin Finck class codecvt<char, char, my_state>
618*c2c66affSColin Finck : public locale::facet, public codecvt_base {
619*c2c66affSColin Finck public:
620*c2c66affSColin Finck typedef char intern_type;
621*c2c66affSColin Finck typedef char extern_type;
622*c2c66affSColin Finck typedef my_state state_type;
623*c2c66affSColin Finck
codecvt(size_t __refs=0)624*c2c66affSColin Finck explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
out(state_type &,const intern_type * __from,const intern_type *,const intern_type * & __from_next,extern_type * __to,extern_type *,extern_type * & __to_next) const625*c2c66affSColin Finck result out(state_type&,
626*c2c66affSColin Finck const intern_type* __from,
627*c2c66affSColin Finck const intern_type*,
628*c2c66affSColin Finck const intern_type*& __from_next,
629*c2c66affSColin Finck extern_type* __to,
630*c2c66affSColin Finck extern_type*,
631*c2c66affSColin Finck extern_type*& __to_next) const
632*c2c66affSColin Finck { __from_next = __from; __to_next = __to; return noconv; }
633*c2c66affSColin Finck
in(state_type &,const extern_type * __from,const extern_type *,const extern_type * & __from_next,intern_type * __to,intern_type *,intern_type * & __to_next) const634*c2c66affSColin Finck result in (state_type&,
635*c2c66affSColin Finck const extern_type* __from,
636*c2c66affSColin Finck const extern_type*,
637*c2c66affSColin Finck const extern_type*& __from_next,
638*c2c66affSColin Finck intern_type* __to,
639*c2c66affSColin Finck intern_type*,
640*c2c66affSColin Finck intern_type*& __to_next) const
641*c2c66affSColin Finck { __from_next = __from; __to_next = __to; return noconv; }
642*c2c66affSColin Finck
unshift(state_type &,extern_type * __to,extern_type *,extern_type * & __to_next) const643*c2c66affSColin Finck result unshift(state_type&,
644*c2c66affSColin Finck extern_type* __to,
645*c2c66affSColin Finck extern_type*,
646*c2c66affSColin Finck extern_type*& __to_next) const
647*c2c66affSColin Finck { __to_next = __to; return noconv; }
648*c2c66affSColin Finck
encoding() const649*c2c66affSColin Finck int encoding() const throw()
650*c2c66affSColin Finck { return 1; }
651*c2c66affSColin Finck
always_noconv() const652*c2c66affSColin Finck bool always_noconv() const throw()
653*c2c66affSColin Finck { return true; }
654*c2c66affSColin Finck
length(const state_type &,const extern_type * __from,const extern_type * __end,size_t __max) const655*c2c66affSColin Finck int length(const state_type&,
656*c2c66affSColin Finck const extern_type* __from,
657*c2c66affSColin Finck const extern_type* __end,
658*c2c66affSColin Finck size_t __max) const
659*c2c66affSColin Finck { return (int)min(static_cast<size_t>(__end - __from), __max); }
660*c2c66affSColin Finck
max_length() const661*c2c66affSColin Finck int max_length() const throw()
662*c2c66affSColin Finck { return 1; }
663*c2c66affSColin Finck
664*c2c66affSColin Finck static locale::id id;
665*c2c66affSColin Finck };
666*c2c66affSColin Finck
667*c2c66affSColin Finck locale::id codecvt<char, char, my_state>::id;
668*c2c66affSColin Finck }
669*c2c66affSColin Finck # else
670*c2c66affSColin Finck # if defined (__BORLANDC__) && (__BORLANDC__ < 0x590)
671*c2c66affSColin Finck template <>
672*c2c66affSColin Finck locale::id codecvt<char, char, my_state>::id;
673*c2c66affSColin Finck # endif
674*c2c66affSColin Finck # endif
675*c2c66affSColin Finck #endif
676*c2c66affSColin Finck
custom_facet()677*c2c66affSColin Finck void FstreamTest::custom_facet()
678*c2c66affSColin Finck {
679*c2c66affSColin Finck #if defined (DO_CUSTOM_FACET_TEST)
680*c2c66affSColin Finck const char* fileName = "test_file.txt";
681*c2c66affSColin Finck //File preparation:
682*c2c66affSColin Finck {
683*c2c66affSColin Finck ofstream ofstr(fileName, ios_base::binary);
684*c2c66affSColin Finck ofstr << "0123456789";
685*c2c66affSColin Finck CPPUNIT_ASSERT( ofstr );
686*c2c66affSColin Finck }
687*c2c66affSColin Finck
688*c2c66affSColin Finck {
689*c2c66affSColin Finck typedef basic_ifstream<char, my_traits> my_ifstream;
690*c2c66affSColin Finck typedef basic_string<char, my_traits> my_string;
691*c2c66affSColin Finck
692*c2c66affSColin Finck my_ifstream ifstr(fileName);
693*c2c66affSColin Finck CPPUNIT_ASSERT( ifstr );
694*c2c66affSColin Finck
695*c2c66affSColin Finck # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
696*c2c66affSColin Finck ifstr.imbue(locale::classic());
697*c2c66affSColin Finck CPPUNIT_ASSERT( ifstr.fail() && !ifstr.bad() );
698*c2c66affSColin Finck ifstr.clear();
699*c2c66affSColin Finck # endif
700*c2c66affSColin Finck typedef codecvt<char, char, my_state> my_codecvt;
701*c2c66affSColin Finck locale my_loc(locale::classic(), new my_codecvt());
702*c2c66affSColin Finck // Check that my_codecvt has not replace default codecvt:
703*c2c66affSColin Finck CPPUNIT_ASSERT( (has_facet<my_codecvt>(my_loc)) );
704*c2c66affSColin Finck CPPUNIT_ASSERT( (has_facet<codecvt<char, char, mbstate_t> >(my_loc)) );
705*c2c66affSColin Finck # if !defined (STLPORT) || !defined (_STLP_NO_WCHAR_T)
706*c2c66affSColin Finck CPPUNIT_ASSERT( (has_facet<codecvt<wchar_t, char, mbstate_t> >(my_loc)) );
707*c2c66affSColin Finck # endif
708*c2c66affSColin Finck ifstr.imbue(my_loc);
709*c2c66affSColin Finck CPPUNIT_ASSERT( ifstr.good() );
710*c2c66affSColin Finck /*
711*c2c66affSColin Finck my_string res;
712*c2c66affSColin Finck ifstr >> res;
713*c2c66affSColin Finck CPPUNIT_ASSERT( !ifstr.fail() );
714*c2c66affSColin Finck CPPUNIT_ASSERT( !ifstr.bad() );
715*c2c66affSColin Finck CPPUNIT_ASSERT( ifstr.eof() );
716*c2c66affSColin Finck CPPUNIT_ASSERT( res == "0123456789" );
717*c2c66affSColin Finck */
718*c2c66affSColin Finck }
719*c2c66affSColin Finck #endif
720*c2c66affSColin Finck }
721*c2c66affSColin Finck
722*c2c66affSColin Finck # if defined (CHECK_BIG_FILE)
big_file()723*c2c66affSColin Finck void FstreamTest::big_file()
724*c2c66affSColin Finck {
725*c2c66affSColin Finck vector<pair<streamsize, streamoff> > file_pos;
726*c2c66affSColin Finck
727*c2c66affSColin Finck //Big file creation:
728*c2c66affSColin Finck {
729*c2c66affSColin Finck ofstream out("big_file.txt");
730*c2c66affSColin Finck CPPUNIT_ASSERT( out );
731*c2c66affSColin Finck
732*c2c66affSColin Finck //We are going to generate a file with the following schema for the content:
733*c2c66affSColin Finck //0(1019 times)0000 //1023 characters + 1 charater for \n (for some platforms it will be a 1 ko line)
734*c2c66affSColin Finck //0(1019 times)0001
735*c2c66affSColin Finck //...
736*c2c66affSColin Finck //0(1019 times)1234
737*c2c66affSColin Finck //...
738*c2c66affSColin Finck
739*c2c66affSColin Finck //Generation of the number of loop:
740*c2c66affSColin Finck streamoff nb = 1;
741*c2c66affSColin Finck for (int i = 0; i < 20; ++i) {
742*c2c66affSColin Finck //This assertion check that the streamoff can at least represent the necessary integers values
743*c2c66affSColin Finck //for this test:
744*c2c66affSColin Finck CPPUNIT_ASSERT( (nb << 1) > nb );
745*c2c66affSColin Finck nb <<= 1;
746*c2c66affSColin Finck }
747*c2c66affSColin Finck CPPUNIT_ASSERT( nb * CHECK_BIG_FILE >= nb );
748*c2c66affSColin Finck nb *= CHECK_BIG_FILE;
749*c2c66affSColin Finck
750*c2c66affSColin Finck //Preparation of the ouput stream state:
751*c2c66affSColin Finck out << setiosflags(ios_base::right) << setfill('*');
752*c2c66affSColin Finck for (streamoff index = 0; index < nb; ++index) {
753*c2c66affSColin Finck if (index % 1024 == 0) {
754*c2c66affSColin Finck file_pos.push_back(make_pair(out.tellp(), index));
755*c2c66affSColin Finck CPPUNIT_ASSERT( file_pos.back().first != streamsize(-1) );
756*c2c66affSColin Finck if (file_pos.size() > 1) {
757*c2c66affSColin Finck CPPUNIT_ASSERT( file_pos[file_pos.size() - 1].first > file_pos[file_pos.size() - 2].first );
758*c2c66affSColin Finck }
759*c2c66affSColin Finck }
760*c2c66affSColin Finck out << setw(1023) << index << '\n';
761*c2c66affSColin Finck }
762*c2c66affSColin Finck }
763*c2c66affSColin Finck
764*c2c66affSColin Finck {
765*c2c66affSColin Finck ifstream in("big_file.txt");
766*c2c66affSColin Finck CPPUNIT_ASSERT( in );
767*c2c66affSColin Finck
768*c2c66affSColin Finck string line;
769*c2c66affSColin Finck vector<pair<streamsize, streamsize> >::const_iterator pit(file_pos.begin()),
770*c2c66affSColin Finck pitEnd(file_pos.end());
771*c2c66affSColin Finck for (; pit != pitEnd; ++pit) {
772*c2c66affSColin Finck in.seekg((*pit).first);
773*c2c66affSColin Finck CPPUNIT_ASSERT( in );
774*c2c66affSColin Finck in >> line;
775*c2c66affSColin Finck size_t lastStarPos = line.rfind('*');
776*c2c66affSColin Finck CPPUNIT_ASSERT( atoi(line.substr(lastStarPos + 1).c_str()) == (*pit).second );
777*c2c66affSColin Finck }
778*c2c66affSColin Finck }
779*c2c66affSColin Finck
780*c2c66affSColin Finck /*
781*c2c66affSColin Finck The following test has been used to check that STLport do not generate
782*c2c66affSColin Finck an infinite loop when the file size is larger than the streamsize and
783*c2c66affSColin Finck streamoff representation (32 bits or 64 bits).
784*c2c66affSColin Finck {
785*c2c66affSColin Finck ifstream in("big_file.txt");
786*c2c66affSColin Finck CPPUNIT_ASSERT( in );
787*c2c66affSColin Finck char tmp[4096];
788*c2c66affSColin Finck streamsize nb_reads = 0;
789*c2c66affSColin Finck while ((!in.eof()) && in.good()){
790*c2c66affSColin Finck in.read(tmp, 4096);
791*c2c66affSColin Finck nb_reads += in.gcount();
792*c2c66affSColin Finck }
793*c2c66affSColin Finck }
794*c2c66affSColin Finck */
795*c2c66affSColin Finck }
796*c2c66affSColin Finck # endif
797*c2c66affSColin Finck
null_stream()798*c2c66affSColin Finck void FstreamTest::null_stream()
799*c2c66affSColin Finck {
800*c2c66affSColin Finck # if (defined (STLPORT) && defined (_STLP_USE_WIN32_IO)) || \
801*c2c66affSColin Finck (!defined (STLPORT) && (defined (WIN32) || defined (_WIN32)))
802*c2c66affSColin Finck const char* nullStreamName = "NUL";
803*c2c66affSColin Finck # else
804*c2c66affSColin Finck const char* nullStreamName = "/dev/null";
805*c2c66affSColin Finck # endif
806*c2c66affSColin Finck {
807*c2c66affSColin Finck ofstream nullStream(nullStreamName);
808*c2c66affSColin Finck CPPUNIT_CHECK( nullStream );
809*c2c66affSColin Finck }
810*c2c66affSColin Finck
811*c2c66affSColin Finck {
812*c2c66affSColin Finck ofstream nullStream(nullStreamName, ios_base::ate);
813*c2c66affSColin Finck CPPUNIT_CHECK( nullStream );
814*c2c66affSColin Finck }
815*c2c66affSColin Finck
816*c2c66affSColin Finck {
817*c2c66affSColin Finck ofstream nullStream(nullStreamName, ios_base::trunc);
818*c2c66affSColin Finck CPPUNIT_CHECK( nullStream );
819*c2c66affSColin Finck }
820*c2c66affSColin Finck
821*c2c66affSColin Finck {
822*c2c66affSColin Finck ofstream nullStream(nullStreamName, ios_base::app);
823*c2c66affSColin Finck CPPUNIT_CHECK( nullStream );
824*c2c66affSColin Finck }
825*c2c66affSColin Finck
826*c2c66affSColin Finck {
827*c2c66affSColin Finck ifstream nullStream(nullStreamName);
828*c2c66affSColin Finck CPPUNIT_CHECK( nullStream );
829*c2c66affSColin Finck }
830*c2c66affSColin Finck
831*c2c66affSColin Finck {
832*c2c66affSColin Finck ifstream nullStream(nullStreamName, ios_base::ate);
833*c2c66affSColin Finck CPPUNIT_CHECK( nullStream );
834*c2c66affSColin Finck }
835*c2c66affSColin Finck
836*c2c66affSColin Finck {
837*c2c66affSColin Finck fstream nullStream(nullStreamName);
838*c2c66affSColin Finck CPPUNIT_CHECK( nullStream );
839*c2c66affSColin Finck }
840*c2c66affSColin Finck
841*c2c66affSColin Finck {
842*c2c66affSColin Finck fstream nullStream(nullStreamName, ios_base::in | ios_base::out | ios_base::ate);
843*c2c66affSColin Finck CPPUNIT_CHECK( nullStream );
844*c2c66affSColin Finck }
845*c2c66affSColin Finck
846*c2c66affSColin Finck {
847*c2c66affSColin Finck fstream nullStream(nullStreamName, ios_base::in | ios_base::out | ios_base::trunc);
848*c2c66affSColin Finck CPPUNIT_CHECK( nullStream );
849*c2c66affSColin Finck }
850*c2c66affSColin Finck }
851*c2c66affSColin Finck
null_buf()852*c2c66affSColin Finck void FstreamTest::null_buf()
853*c2c66affSColin Finck {
854*c2c66affSColin Finck /* **********************************************************************************
855*c2c66affSColin Finck
856*c2c66affSColin Finck testcase for bug #1830513:
857*c2c66affSColin Finck in _istream.c
858*c2c66affSColin Finck
859*c2c66affSColin Finck template < class _CharT, class _Traits, class _Is_Delim>
860*c2c66affSColin Finck streamsize _STLP_CALL __read_unbuffered(basic_istream<_CharT, _Traits>* __that,
861*c2c66affSColin Finck basic_streambuf<_CharT, _Traits>* __buf,
862*c2c66affSColin Finck streamsize _Num, _CharT* __s,
863*c2c66affSColin Finck _Is_Delim __is_delim,
864*c2c66affSColin Finck bool __extract_delim, bool __append_null,
865*c2c66affSColin Finck bool __is_getline)
866*c2c66affSColin Finck
867*c2c66affSColin Finck can't accept _Num == 0; this is legal case, and may happen from
868*c2c66affSColin Finck
869*c2c66affSColin Finck template <class _CharT, class _Traits>
870*c2c66affSColin Finck basic_istream<_CharT, _Traits>&
871*c2c66affSColin Finck basic_istream<_CharT, _Traits>::getline(_CharT* __s, streamsize __n, _CharT __delim)
872*c2c66affSColin Finck
873*c2c66affSColin Finck *********************************************************************************** */
874*c2c66affSColin Finck
875*c2c66affSColin Finck fstream f( "test.txt", ios_base::in | ios_base::out | ios_base::trunc );
876*c2c66affSColin Finck // string line;
877*c2c66affSColin Finck
878*c2c66affSColin Finck for ( int i = 0; i < 0x200; ++i ) {
879*c2c66affSColin Finck f.put( ' ' );
880*c2c66affSColin Finck }
881*c2c66affSColin Finck
882*c2c66affSColin Finck // const streambuf *b = f.rdbuf();
883*c2c66affSColin Finck
884*c2c66affSColin Finck // string s;
885*c2c66affSColin Finck char buf[1024];
886*c2c66affSColin Finck buf[0] = 'a';
887*c2c66affSColin Finck buf[1] = 'b';
888*c2c66affSColin Finck buf[2] = 'c';
889*c2c66affSColin Finck
890*c2c66affSColin Finck // getline( f, s );
891*c2c66affSColin Finck // cerr << f.good() << endl;
892*c2c66affSColin Finck f.seekg( 0, ios_base::beg );
893*c2c66affSColin Finck // f.seekg( 0, ios_base::end );
894*c2c66affSColin Finck // buf[0] = f.get();
895*c2c66affSColin Finck
896*c2c66affSColin Finck // cerr << (void *)(b->_M_gptr()) << " " << (void *)(b->_M_egptr()) << endl;
897*c2c66affSColin Finck // cerr << f.good() << endl;
898*c2c66affSColin Finck // getline( f, s );
899*c2c66affSColin Finck f.getline( buf, 1 ); // <-- key line
900*c2c66affSColin Finck CPPUNIT_CHECK( buf[0] == 0 );
901*c2c66affSColin Finck CPPUNIT_CHECK( f.fail() ); // due to delimiter not found while buffer was exhausted
902*c2c66affSColin Finck }
903*c2c66affSColin Finck
904*c2c66affSColin Finck # if !defined (STLPORT) || !defined (_STLP_WIN32)
offset()905*c2c66affSColin Finck void FstreamTest::offset()
906*c2c66affSColin Finck {
907*c2c66affSColin Finck # if (defined(_LARGEFILE_SOURCE) || defined(_LARGEFILE64_SOURCE)) && !defined(_STLP_USE_DEFAULT_FILE_OFFSET)
908*c2c66affSColin Finck CPPUNIT_CHECK( sizeof(streamoff) == 8 );
909*c2c66affSColin Finck # else
910*c2c66affSColin Finck CPPUNIT_CHECK( sizeof(streamoff) == sizeof(off_t) );
911*c2c66affSColin Finck # endif
912*c2c66affSColin Finck }
913*c2c66affSColin Finck # endif
914*c2c66affSColin Finck
915*c2c66affSColin Finck #endif
916