Merge from mainline.
[official-gcc.git] / libjava / classpath / test / java.io / BufferedReaderTest.java
blobd3c1d4fcf655d0fc99746a1611af88596a72844f
1 /*************************************************************************
2 /* BufferedReaderTest.java -- Tests BufferedReader's
3 /*
4 /* Copyright (c) 1998 Free Software Foundation, Inc.
5 /* Written by Aaron M. Renn (arenn@urbanophile.com)
6 /*
7 /* This program is free software; you can redistribute it and/or modify
8 /* it under the terms of the GNU General Public License as published
9 /* by the Free Software Foundation, either version 2 of the License, or
10 /* (at your option) any later version.
12 /* This program is distributed in the hope that it will be useful, but
13 /* WITHOUT ANY WARRANTY; without even the implied warranty of
14 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 /* GNU General Public License for more details.
17 /* You should have received a copy of the GNU General Public License
18 /* along with this program; if not, write to the Free Software Foundation
19 /* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 /*************************************************************************/
22 import java.io.*;
24 public class BufferedReaderTest extends CharArrayReader
27 // Hehe. We override CharArrayReader.markSupported() in order to return
28 // false so that we can test BufferedReader's handling of mark/reset in
29 // both the case where the underlying stream does and does not support
30 // mark/reset
31 public boolean
32 markSupported()
34 return(false);
37 public
38 BufferedReaderTest(char[] buf)
40 super(buf);
43 public static int
44 marktest(Reader ins) throws IOException
46 BufferedReader br = new BufferedReader(ins, 15);
48 int chars_read;
49 int total_read = 0;
50 char[] buf = new char[12];
52 chars_read = br.read(buf);
53 total_read += chars_read;
54 System.out.print(new String(buf, 0, chars_read));
56 chars_read = br.read(buf);
57 total_read += chars_read;
58 System.out.print(new String(buf, 0, chars_read));
60 br.mark(75);
61 br.read();
62 br.read(buf);
63 br.read(buf);
64 br.read(buf);
65 br.reset();
67 chars_read = br.read(buf);
68 total_read += chars_read;
69 System.out.print(new String(buf, 0, chars_read));
71 br.mark(555);
73 chars_read = br.read(buf);
74 total_read += chars_read;
75 System.out.print(new String(buf, 0, chars_read));
77 br.reset();
79 br.read(buf);
80 chars_read = br.read(buf);
81 total_read += chars_read;
82 System.out.print(new String(buf, 0, chars_read));
84 chars_read = br.read(buf);
85 total_read += chars_read;
86 System.out.print(new String(buf, 0, chars_read));
88 br.mark(14);
90 br.read(buf);
92 br.reset();
94 chars_read = br.read(buf);
95 total_read += chars_read;
96 System.out.print(new String(buf, 0, chars_read));
98 while ((chars_read = br.read(buf)) != -1)
100 System.out.print(new String(buf, 0, chars_read));
101 total_read += chars_read;
104 return(total_read);
107 public static void
108 main(String[] argv)
110 System.out.println("Started test of BufferedReader");
114 System.out.println("Test 1: Simple Read Test");
116 String str = "My 5th grade teacher was named Mr. Thompson. Terry\n" +
117 "George Thompson to be precise. He had these sideburns like\n" +
118 "Isaac Asimov's, only uglier. One time he had a contest and said\n" +
119 "that if any kid who could lift 50lbs worth of weights on a barbell\n" +
120 "all the way over their head, he would shave it off. Nobody could\n" +
121 "though. One time I guess I made a comment about how stupid his\n" +
122 "sideburns worked and he not only kicked me out of class, he called\n" +
123 "my mother. Jerk.\n";
125 StringReader sr = new StringReader(str);
126 BufferedReader br = new BufferedReader(sr, 15);
128 char[] buf = new char[12];
129 int chars_read;
130 while((chars_read = br.read(buf)) != -1)
131 System.out.print(new String(buf, 0, chars_read));
133 br.close();
134 System.out.println("PASSED: Simple Read Test");
136 catch (IOException e)
138 System.out.println("FAILED: Simple Read Test: " + e);
143 System.out.println("Test 2: First mark/reset Test");
145 String str = "Growing up in a rural area brings such delights. One\n" +
146 "time my uncle called me up and asked me to come over and help him\n" +
147 "out with something. Since he lived right across the field, I\n" +
148 "walked right over. Turned out he wanted me to come down to the\n" +
149 "barn and help him castrate a calf. Oh, that was fun. Not.\n";
151 StringReader sr = new StringReader(str);
152 // BufferedReader br = new BufferedReader(sr);
154 int total_read = marktest(sr);
156 if (total_read == str.length())
157 System.out.println("PASSED: First mark/reset Test");
158 else
159 System.out.println("FAILED: First mark/reset Test");
161 catch (IOException e)
163 System.out.println("FAILED: First mark/reset Test: " + e);
168 System.out.println("Test 3: Second mark/reset Test");
170 String str = "Growing up we heated our house with a wood stove. That\n" +
171 "thing could pump out some BTU's, let me tell you. No matter how\n" +
172 "cold it got outside, it was always warm inside. Of course the\n" +
173 "downside is that somebody had to chop the wood for the stove. That\n" +
174 "somebody was me. I was slave labor. My uncle would go back and\n" +
175 "chain saw up dead trees and I would load the wood in wagons and\n" +
176 "split it with a maul. Somehow my no account brother always seemed\n" +
177 "to get out of having to work.\n";
179 char[] buf = new char[str.length()];
180 str.getChars(0, str.length(), buf, 0);
181 BufferedReaderTest brt = new BufferedReaderTest(buf);
182 // BufferedReader br = new BufferedReader(car);
184 int total_read = marktest(brt);
186 if (total_read == str.length())
187 System.out.println("PASSED: Second mark/reset Test");
188 else
189 System.out.println("FAILED: Second mark/reset Test");
191 catch (IOException e)
193 System.out.println("FAILED: Second mark/reset Test: " + e);
196 System.out.println("Finished test of BufferedReader");
197 } // main
199 } // class BufferedReaderTest