summaryrefslogtreecommitdiff
path: root/libio/tests/tiomisc.cc
blob: 207a3f16e2b4e1f6902584037cbc754ba74111f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* Random regression tests etc. */

#include <fstream.h>
#include <stdio.h>
#include <strstream.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <assert.h>

#define BUF_SIZE 4096

void
test1 ()
{
   fstream f;
   char    buf[BUF_SIZE];

   f.setbuf( buf, BUF_SIZE );
}

void
test2 ( )
{
   char string[BUF_SIZE];
   ostrstream s( string, BUF_SIZE );

   s << "Bla bla bla " << 55 << ' ' << 3.23 << '\0' << endl;
   cout << "Test2: " << string << endl;
}


/* Test case from Joe Buck <jbuck@Synopsys.COM>. */

class special_ofstream : public ofstream {
public:
	special_ofstream() : ofstream() {}
	special_ofstream(int fd) : ofstream(fd) {}
	special_ofstream(const char *name, int mode=ios::out, int prot=0664) {
		open(name,mode,prot);
	}
	void open(const char *name, int mode=ios::out, int prot=0664);
};

void special_ofstream::open(const char* name, int mode, int prot) {
	if (strcmp(name, "<cout>") == 0) {
		rdbuf()->attach(1);
	}
	else if (strcmp(name, "<cerr>") == 0) {
		rdbuf()->attach(2);
		setf(unitbuf);
	}
	else ofstream::open(name,mode,prot);
}

void
test3 ()
{
	{
		special_ofstream o("<cout>");
		o << "Hello\n";
		// o is destructed now.  This should not close cout
	}
	{
		special_ofstream o("<cout>");
		o << "Line 2\n";
	}
}

void
getline_test1 ()
{
  char buf[1000];
  char data[] = "#include <iostream.h>\n#include <fstream.h>\n";
  istrstream infile(data, strlen(data));
  infile.getline(buf,1000);
  infile.getline(buf,1000);

  cout << buf << '\n';
}

// test istream::getline on readng overlong lines.
void
getline_test2 ()
{
  char data[] = "Line one.\nline 2.\n";
  char line[100];
  istrstream strin(data, strlen(data));
  strin.getline(line, 10);
  cout << "line: " << line << ", count: " << strin.gcount () << "\n";
}

void
getline_test3 ()
{
  char data[] = "123456789\nabcdefghijkl.\n";
  char line[10];
  istrstream strin(data, strlen(data));
  strin.getline(line, 10);
  cout << "line: " << line << ", count: " << strin.gcount () << "\n";
  strin.getline(line, 10);
  cout << "line: " << line << ", count: " << strin.gcount () << "\n";
  assert (!strin.good());
  strin.clear ();
  strin.getline(line, 10);
  cout << "line: " << line << ", count: " << strin.gcount () << "\n";
}

class A : private ostream
{
public:
  A(streambuf* s);
  ostream::flush;
};
A::A(streambuf* s)
: ostream(s)
{
}

void
flush1_test()
{
  A os(cout.rdbuf());
  os.flush();
}

void
reread_test ()
{  // This is PR 5486.
   int tag_char;
   char *fname = "Makefile";
   int mode = O_RDONLY;
   filebuf file_p; 

   int fd = ::open(fname, mode, 0666);
   file_p.attach(fd); 

   istream d_istream(&file_p);

   // Read a character from the stream, save it and put it back.
   tag_char = d_istream.get();
   int save_char = tag_char;
   d_istream.putback((char) tag_char);

   // Uncomment then next statement and the next get will be EOF.
   streampos pos = d_istream.tellg();  

   // Re-read the first character
   tag_char = d_istream.get();

   cout << "reread_test: " << (char)save_char << " " << (char)tag_char << "\n";
   cout.flush();

}

void *danger_pointer;
void operator delete (void *p) throw()
{
  if (p)
    {
      if (p == danger_pointer)
	fprintf (stderr, "maybe deleted\n");

      free (p);
    }
}

struct my_ostream: virtual public ios, public ostream
{
  my_ostream (ostream &s): ios (s.rdbuf()) { }
};

void
test_destroy ()
{
  ofstream fstr ("foo.dat");
  my_ostream wa (fstr);

  /* Check that sure wa.rdbuf() is only freed once. */
  danger_pointer = wa.rdbuf ();

  wa << "Hi there" << endl;
#ifdef _IO_NEW_STREAMS
  fprintf (stderr, "maybe deleted\n");
#endif
}

/* Submitted by Luke Blanshard <luke@cs.wisc.edu>.

   In certain circumstances, the library will write past the end of the
   buffer it has allocated for a file:  You must read from the file,
   exactly enough bytes that the read pointer is at the end of the
   buffer.  Then you must write to the file, at the same place you just
   finished reading from.

   "Your patch looks great, and you're welcome to use the test code for any  
   purpose whatever.  I hereby renounce my implicit copyright on it."  */

void
test_read_write_flush ()
{
    fstream f;
    char buf[8192];

    for ( int index=0; index < sizeof buf; ++index )
        buf[index] = (index+1)&63? 'x' : '\n';

    f.open( "foo.dat", ios::in|ios::out|ios::trunc );
    f.write( buf, sizeof buf );

    f.seekg( 0, ios::beg );
    f.read( buf, sizeof buf );

//    f.seekp( sizeof buf, ios::beg );	// Present or absent, bug still happens.
    f.write( "a", 1 );

    if ( f.rdbuf()->_IO_write_ptr > f.rdbuf()->_IO_buf_end )
        cerr << "test_read_write_flush: it's broken.\n";
    else
        cout << "test_read_write_flush: the problem isn't showing itself.\n";
}

int main( )
{
  test1 ();
  test2 ();
  test3 ();
  getline_test1 ();
  getline_test2 ();
  getline_test3 ();
  flush1_test ();
  reread_test ();
  test_destroy ();
  test_read_write_flush ();
  return 0;
}