1 /* Random regression tests etc. */
19 f.setbuf( buf, BUF_SIZE );
25 char string[BUF_SIZE];
26 ostrstream s( string, BUF_SIZE );
28 s << "Bla bla bla " << 55 << ' ' << 3.23 << '\0' << endl;
29 cout << "Test2: " << string << endl;
33 /* Test case from Joe Buck <jbuck@Synopsys.COM>. */
35 class special_ofstream : public ofstream {
37 special_ofstream() : ofstream() {}
38 special_ofstream(int fd) : ofstream(fd) {}
39 special_ofstream(const char *name, int mode=ios::out, int prot=0664) {
42 void open(const char *name, int mode=ios::out, int prot=0664);
45 void special_ofstream::open(const char* name, int mode, int prot) {
46 if (strcmp(name, "<cout>") == 0) {
49 else if (strcmp(name, "<cerr>") == 0) {
53 else ofstream::open(name,mode,prot);
60 special_ofstream o("<cout>");
62 // o is destructed now. This should not close cout
65 special_ofstream o("<cout>");
74 char data[] = "#include <iostream.h>\n#include <fstream.h>\n";
75 istrstream infile(data, strlen(data));
76 infile.getline(buf,1000);
77 infile.getline(buf,1000);
82 // test istream::getline on readng overlong lines.
86 char data[] = "Line one.\nline 2.\n";
88 istrstream strin(data, strlen(data));
89 strin.getline(line, 10);
90 cout << "line: " << line << ", count: " << strin.gcount () << "\n";
96 char data[] = "123456789\nabcdefghijkl.\n";
98 istrstream strin(data, strlen(data));
99 strin.getline(line, 10);
100 cout << "line: " << line << ", count: " << strin.gcount () << "\n";
101 strin.getline(line, 10);
102 cout << "line: " << line << ", count: " << strin.gcount () << "\n";
103 assert (!strin.good());
105 strin.getline(line, 10);
106 cout << "line: " << line << ", count: " << strin.gcount () << "\n";
109 class A : private ostream
129 { // This is PR 5486.
131 char *fname = "Makefile";
135 int fd = ::open(fname, mode, 0666);
138 istream d_istream(&file_p);
140 // Read a character from the stream, save it and put it back.
141 tag_char = d_istream.get();
142 int save_char = tag_char;
143 d_istream.putback((char) tag_char);
145 // Uncomment then next statement and the next get will be EOF.
146 streampos pos = d_istream.tellg();
148 // Re-read the first character
149 tag_char = d_istream.get();
151 cout << "reread_test: " << (char)save_char << " " << (char)tag_char << "\n";
156 void *danger_pointer;
157 void operator delete (void *p) throw()
161 if (p == danger_pointer)
162 fprintf (stderr, "maybe deleted\n");
168 struct my_ostream: virtual public ios, public ostream
170 my_ostream (ostream &s): ios (s.rdbuf()) { }
176 ofstream fstr ("foo.dat");
177 my_ostream wa (fstr);
179 /* Check that sure wa.rdbuf() is only freed once. */
180 danger_pointer = wa.rdbuf ();
182 wa << "Hi there" << endl;
183 #ifdef _IO_NEW_STREAMS
184 fprintf (stderr, "maybe deleted\n");
188 /* Submitted by Luke Blanshard <luke@cs.wisc.edu>.
190 In certain circumstances, the library will write past the end of the
191 buffer it has allocated for a file: You must read from the file,
192 exactly enough bytes that the read pointer is at the end of the
193 buffer. Then you must write to the file, at the same place you just
194 finished reading from.
196 "Your patch looks great, and you're welcome to use the test code for any
197 purpose whatever. I hereby renounce my implicit copyright on it." */
200 test_read_write_flush ()
205 for ( int index=0; index < sizeof buf; ++index )
206 buf[index] = (index+1)&63? 'x' : '\n';
208 f.open( "foo.dat", ios::in|ios::out|ios::trunc );
209 f.write( buf, sizeof buf );
211 f.seekg( 0, ios::beg );
212 f.read( buf, sizeof buf );
214 // f.seekp( sizeof buf, ios::beg ); // Present or absent, bug still happens.
217 if ( f.rdbuf()->_IO_write_ptr > f.rdbuf()->_IO_buf_end )
218 cerr << "test_read_write_flush: it's broken.\n";
220 cout << "test_read_write_flush: the problem isn't showing itself.\n";
234 test_read_write_flush ();