GNU header update.
[cacao.git] / tests / kaffe / BufferedReaderTest.java
1 // BufferedReaderTest.java
2 // submitted by Dalibor Topic <robilad@yahoo.com>
3 import java.io.*;
4
5 public class BufferedReaderTest {
6
7         static class PseudoReader extends Reader {
8             public void close() {
9             }
10
11             public int read(char buf[], int offset, int count) {
12                 System.out.println("count = " + count);
13                 return (count);
14             }
15         }
16
17         static class PseudoReader2 extends Reader {
18             public int closeCounter = 0;
19   
20             public void close() {
21                 closeCounter++;
22             }
23  
24             public int read(char buf[], int offset, int count) {
25                 return (-1);
26             }
27         }
28
29         static class PseudoReader3 extends Reader {
30             boolean called;
31             boolean calledAgain;
32
33             public void close () {
34             }
35
36             /* This version of read returns a string of characters
37                without EOL on the first call,
38                then 0 characters on the second.
39
40                If its caller is blocking, the caller will try again,
41                otherwise it will have enough.
42
43                If it tries again, and calls read for the 
44                third time, an IOException is thrown.
45             */
46
47             public int read(char[] buf2, int offset, int count) 
48                 throws IOException {
49
50                 if (calledAgain) {
51                     throw (new IOException("you keep trying ..."));
52                 }
53                 if (called) {
54                     calledAgain = true;
55                     return (0);
56                 }
57                 else {
58                     called = true;
59   
60                     String s = new String("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
61
62                     s.getChars(0, count, buf2, offset);
63
64                     return (count);
65                 }
66             }
67         }
68
69         public static void main(String [] args) throws Exception {
70
71                 // This test shows that the default buffersize for
72                 // a BufferedReader is 8192 characters.
73
74                 final int defaultsize = 8 * 1024;
75
76                 BufferedReader br = new BufferedReader(new PseudoReader());
77                 try {
78
79                     // Mark br, to make sure the data is copied into
80                     // the buffer.
81                     
82                     br.mark(0);
83                     
84                     // This should call the read method of PseudoReader
85                     // only once if the default buffersize of
86                     // a buffered reader is 8192.
87                     
88                     br.read(new char[defaultsize], 0, defaultsize);
89
90                     // And now call it to get one character, and
91                     // it should refill its buffer
92
93                     br.read(new char[1], 0, 1);
94
95                 }
96                 catch (IOException e) {
97                         e.printStackTrace();
98                 }
99
100
101                 // Let's try to set a mark with
102                 // a negative read ahead,
103                 // this should throw an IllegalArgumentException
104
105                 StringReader sr = new StringReader("sr");
106                 br = new BufferedReader(sr);
107
108                 try {
109                         br.mark(-1);
110                 }
111                 catch (Exception e) {
112                         System.out.println(e.toString());
113                 }
114
115
116                 // This is a simple test of BufferedReader
117                 // functionality.
118
119                 sr = new StringReader("1234567");
120
121                 br = new BufferedReader(sr);
122
123                 try {
124                         char [] buf = new char[3];
125
126                         if (br.read(buf) == 3) {
127                                 System.out.println(buf);
128                         }
129                         else {
130                                 System.out.println("Couldn't read 3 characters");
131                         }
132
133                         br.mark(4);
134
135                         buf = new char[4];
136
137                         if (br.read(buf) == 4) {
138                                 System.out.println(buf);
139                         }
140                         else {
141                                 System.out.println("Couldn't read 4 characters");
142                         }
143
144                         br.reset();
145
146                         if (br.read(buf) == 4) {
147                                 System.out.println(buf);
148                         }
149                         else {
150                                 System.out.println("Couldn't re-read 4 characters");
151                         }               
152                 }
153                 catch (IOException e) {
154                         e.printStackTrace();
155                 }
156
157
158                 /* Trying to create a BufferedReader with 
159                    input Reader = null should not result in 
160                    an IllegalArgumentException being thrown,
161                    instead a NullPointerException should be thrown
162                    when the Reader is accessed.
163                  */
164
165                 sr = new StringReader("1234567");
166                 br = new BufferedReader(sr);
167
168                 try {
169                         br = new BufferedReader(null);
170                 }
171                 catch (NullPointerException e) {
172                         System.out.println(e.toString());
173                 }
174
175                 try {
176                         br.read();
177                 }
178                 catch (Exception e) {
179                         System.out.println(e.toString());
180                 }
181
182                 sr = new StringReader("1234567");
183
184                 /* Trying to create a BufferedReader with 
185                    buffer size <= 0 should result in 
186                    an IllegalArgumentException being thrown.
187                  */
188
189                 try {
190                         br = new BufferedReader(sr, 0);
191                 }
192                 catch (IllegalArgumentException e) {
193                         System.out.println(e.toString());
194                 }
195
196                 /* A call to close a BufferedReader that has
197                    already been closed should do nothing.
198                    So the expected result should be "1"
199                  */
200
201                 PseudoReader2 pr2 = new PseudoReader2();
202                 br = new BufferedReader(pr2);
203
204                 try {
205                         br.close();
206                         br.close();
207                 }
208                 catch (IOException e) {
209                         System.out.println(e.toString());
210                 }
211
212                 System.out.println(pr2.closeCounter);
213
214
215                 /* Trying to read from a BufferedReader that has
216                    already been closed should generate an IOException.
217                  */
218
219                 try {
220                         br.close();
221                 }
222                 catch (IOException e) {
223                         System.out.println(e.toString());
224                 }
225                 try {
226                         br.mark(1);
227                 }
228                 catch (IOException e) {
229                         System.out.println(e.toString());
230                 }
231                 try {
232                         br.read();
233                 }
234                 catch (IOException e) {
235                         System.out.println(e.toString());
236                 }
237                 try {
238                         br.read(new char[1]);
239                 }
240                 catch (IOException e) {
241                         System.out.println(e.toString());
242                 }
243                 try {
244                         br.read(new char[1], 0, 1);
245                 }
246                 catch (IOException e) {
247                         System.out.println(e.toString());
248                 }
249                 try {
250                         br.readLine();
251                 }
252                 catch (IOException e) {
253                         System.out.println(e.toString());
254                 }
255                 try {
256                         br.ready();
257                 }
258                 catch (IOException e) {
259                         System.out.println(e.toString());
260                 }
261                 try {
262                         br.reset();
263                 }
264                 catch (IOException e) {
265                         System.out.println(e.toString());
266                 }
267                 try {
268                         br.skip(1);
269                 }
270                 catch (IOException e) {
271                         System.out.println(e.toString());
272                 }
273
274                 int bufferSize = 1000000;
275
276                 sr = new StringReader("a");
277
278                 // Allocate a really huge buffer
279                 br = new BufferedReader(sr, bufferSize);
280
281                 /* A closed BufferReader should free its buffer */
282
283                 // close buffered reader
284                 try {
285                         long before = Runtime.getRuntime().freeMemory();
286                         br.close();
287
288                         // Call garbage collector and hope he does his work
289                         System.gc();
290
291                         long after = Runtime.getRuntime().freeMemory();
292
293 //                      System.out.println(before + " " + after);
294
295                         // If garbage collector was successful, and close() did
296                         // let go the buffer, we expect to see some dramatic
297                         // change in the amount of free memory.
298
299                         if (after - before >= bufferSize) {
300 //                              System.out.println("SUCCESS");
301                         }
302                         else {
303 //                              System.out.println("FAILURE");
304                         }
305                 }
306                 catch (IOException e) {
307                         e.printStackTrace();
308                 }
309
310
311                 /* Test to see what happens when we try to
312                    skip a negative number of characters
313                 */
314
315                 sr = new StringReader("0123456789");
316                 br = new BufferedReader(sr,1);
317
318                 try {
319                         System.out.println(br.skip(5));
320                         System.out.println(br.skip(-5));
321                         System.out.println((char) br.read());
322                 }
323                 catch (Exception e) {
324                         System.out.println(e.toString());
325                 }
326
327
328                 /* Testing the reset method */
329
330                 sr = new StringReader("ABCDEFGH");
331                 br = new BufferedReader(sr,1);
332
333                 /* Trying to reset an unmarked reader should result in
334                    an Exception being thrown
335                 */
336
337                 System.out.println("Reset unmarked");
338
339                 try {
340                         br.reset();
341                 }
342                 catch (Exception e) {
343                         System.out.println(e.toString());
344                 }
345
346
347                 /* Now we try something slightly different.
348                    We try to invalidate the mark by reading further
349                    than the read ahead value passed as the parameter
350                    when mark was called.
351                    If we try to reset the buffer afterwards,
352                    we should receive a nice Exception.
353                 */
354
355                 System.out.println("Reset invalidated");
356
357                 try {
358                         br.mark(1);
359                         System.out.println(br.read());
360                         System.out.println(br.read());
361                         br.reset();
362                 }
363                 catch (Exception e) {
364                         System.out.println(e.toString());
365                 }
366
367                 // let's see what happens when I skip over
368                 // marked read ahead buffer.
369
370                 System.out.println("Skipping over marked buffer");
371
372                 try {
373                         br.mark(1);
374                         br.skip(2);
375                         System.out.println(br.read());
376
377                         // According to JDK 1.1.8 behaviour,
378                         // this should invalidate the
379                         // mark, for this buffer size at least.
380
381                         br.reset();
382                         System.out.println(br.read());
383                 }
384                 catch (Exception e) {
385                         System.out.println(e.toString());
386                 }
387
388                 /*
389                    Actually, that seams to depend on the
390                    buffer size. I've run tests with different
391                    buffer sizes, and when the buffer is large
392                    enough to serve the read ahead buffer and the
393                    subsequent reads, then reset still works.
394                 */
395
396                 sr = new StringReader("ABCDEFGH");
397                 br = new BufferedReader(sr);
398
399                 System.out.println("Reset invalidated");
400
401                 try {
402                         br.mark(1);
403                         System.out.println(br.read());
404                         System.out.println(br.read());
405                         br.reset();
406                 }
407                 catch (Exception e) {
408                         System.out.println(e.toString());
409                 }
410
411                 // let's see what happens when I skip over
412                 // marked read ahead buffer, when
413                 // buffer size is large enough to accomodate 
414                 // the requests without the need for refilling.
415
416                 System.out.println("Skipping over marked buffer");
417
418                 try {
419                         br.mark(1);
420                         br.skip(2);
421                         System.out.println(br.read());
422
423                         // According to JDK 1.1.8 behaviour,
424                         // this should not invalidate the
425                         // mark. That is a strong hint that
426                         // mark validation/invalidation should
427                         // happen in the fillOutBuffer method.
428
429                         br.reset();
430                         System.out.println(br.read());
431                 }
432                 catch (Exception e) {
433                         System.out.println(e.toString());
434                 }
435
436
437                 /* And now to something completely different:
438                    BufferedReader.read (char [], int, int).
439
440                    Here is a small test to check whether all
441                    int arguments are checked against their
442                    constraints (Java Class Lib. Vol. 1, 2nd Ed.)
443
444                    This test should generate a lot of 
445                    Exceptions.
446                 */
447
448                 sr = new StringReader("sr");
449                 br = new BufferedReader(sr);
450                 char [] buf = new char[1];
451
452                 System.out.println("Null pointer");
453
454                 try {
455                         br.read(null, 0, 1);
456                 }
457                 catch (Exception e) {
458                         System.out.println(e.toString());
459                 }
460
461                 System.out.println("offset >= buf.length");
462
463                 try {
464                         br.read(buf, 1, 1);
465                 }
466                 catch (Exception e) {
467                         System.out.println(e.toString());
468                 }
469
470                 System.out.println("offset >= buf.length");
471
472                 try {
473
474                     /* The funny thing is that this one *doesn't*
475                        generate an exception in JDK1.1.8, although 
476                        the offset parameter is out of bounds,
477                        since there is no buf[1].
478                     */
479                         br.read(buf, 1, 0);
480                 }
481                 catch (Exception e) {
482                         System.out.println(e.toString());
483                 }
484
485                 System.out.println("offset >= buf.length");
486
487                 try {
488                         br.read(buf, 5, 1);
489                 }
490                 catch (Exception e) {
491                         System.out.println(e.toString());
492                 }
493
494                 System.out.println("count > buf.length - offset");
495
496                 try {
497                         br.read(buf, 0, 5);
498                 }
499                 catch (Exception e) {
500                         System.out.println(e.toString());
501                 }
502
503                 System.out.println("offset < 0");
504
505                 try {
506                         br.read(buf, -1, 1);
507                 }
508                 catch (Exception e) {
509                         System.out.println(e.toString());
510                 }
511
512                 System.out.println("count < 0");
513
514                 try {
515                         br.read(buf, 0, -1);
516                 }
517                 catch (Exception e) {
518                         System.out.println(e.toString());
519                 }
520
521                 /* Test whether readLine is blocking or not */
522
523                 PseudoReader3 pr3 = new PseudoReader3();
524                 br = new BufferedReader(pr3, 10);
525
526                 try {
527
528                     /* Java Class Libraries Vol 1 2nd Edition
529                        says that readLine is blocking.
530                        JDK 1.1.8 behaves that way.
531                     */
532                         System.out.println(br.readLine());
533                 }
534                 catch (Exception e) {
535                         System.out.println(e.toString());
536                 }
537         }
538 }
539
540 /* Expected Output:
541 count = 8192
542 count = 8192
543 java.lang.IllegalArgumentException: Read-ahead limit < 0
544 123
545 4567
546 4567
547 java.lang.NullPointerException
548 java.lang.IllegalArgumentException: Buffer size <= 0
549 1
550 java.io.IOException: Stream closed
551 java.io.IOException: Stream closed
552 java.io.IOException: Stream closed
553 java.io.IOException: Stream closed
554 java.io.IOException: Stream closed
555 java.io.IOException: Stream closed
556 java.io.IOException: Stream closed
557 java.io.IOException: Stream closed
558 5
559 java.lang.IllegalArgumentException: skip value is negative
560 Reset unmarked
561 java.io.IOException: Stream not marked
562 Reset invalidated
563 65
564 66
565 java.io.IOException: Mark invalid
566 Skipping over marked buffer
567 69
568 java.io.IOException: Mark invalid
569 Reset invalidated
570 65
571 66
572 Skipping over marked buffer
573 67
574 65
575 Null pointer
576 java.lang.NullPointerException
577 offset >= buf.length
578 java.lang.IndexOutOfBoundsException
579 offset >= buf.length
580 offset >= buf.length
581 java.lang.IndexOutOfBoundsException
582 count > buf.length - offset
583 java.lang.IndexOutOfBoundsException
584 offset < 0
585 java.lang.IndexOutOfBoundsException
586 count < 0
587 java.lang.IndexOutOfBoundsException
588 java.io.IOException: you keep trying ...
589 */