1 /*************************************************************************
2 /* BufferedReaderTest.java -- Tests BufferedReader's
4 /* Copyright (c) 1998 Free Software Foundation, Inc.
5 /* Written by Aaron M. Renn (arenn@urbanophile.com)
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 /*************************************************************************/
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
38 BufferedReaderTest(char[] buf
)
44 marktest(Reader ins
) throws IOException
46 BufferedReader br
= new BufferedReader(ins
, 15);
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
));
67 chars_read
= br
.read(buf
);
68 total_read
+= chars_read
;
69 System
.out
.print(new String(buf
, 0, chars_read
));
73 chars_read
= br
.read(buf
);
74 total_read
+= chars_read
;
75 System
.out
.print(new String(buf
, 0, chars_read
));
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
));
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
;
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];
130 while((chars_read
= br
.read(buf
)) != -1)
131 System
.out
.print(new String(buf
, 0, chars_read
));
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");
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");
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");
199 } // class BufferedReaderTest