1 /*************************************************************************
2 /* BufferedInputStreamTest.java -- Tests BufferedInputStream'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 BufferedInputStreamTest
extends BufferedInputStream
28 BufferedInputStreamTest(InputStream in
, int size
)
34 marktest(InputStream ins
) throws IOException
36 BufferedInputStream bis
= new BufferedInputStream(ins
, 15);
40 byte[] buf
= new byte[12];
42 bytes_read
= bis
.read(buf
);
43 total_read
+= bytes_read
;
44 System
.out
.print(new String(buf
, 0, bytes_read
));
46 bytes_read
= bis
.read(buf
);
47 total_read
+= bytes_read
;
48 System
.out
.print(new String(buf
, 0, bytes_read
));
57 bytes_read
= bis
.read(buf
);
58 total_read
+= bytes_read
;
59 System
.out
.print(new String(buf
, 0, bytes_read
));
63 bytes_read
= bis
.read(buf
);
64 total_read
+= bytes_read
;
65 System
.out
.print(new String(buf
, 0, bytes_read
));
70 bytes_read
= bis
.read(buf
);
71 total_read
+= bytes_read
;
72 System
.out
.print(new String(buf
, 0, bytes_read
));
74 bytes_read
= bis
.read(buf
);
75 total_read
+= bytes_read
;
76 System
.out
.print(new String(buf
, 0, bytes_read
));
84 bytes_read
= bis
.read(buf
);
85 total_read
+= bytes_read
;
86 System
.out
.print(new String(buf
, 0, bytes_read
));
88 while ((bytes_read
= bis
.read(buf
)) != -1)
90 System
.out
.print(new String(buf
, 0, bytes_read
));
91 total_read
+= bytes_read
;
100 System
.out
.println("Started test of BufferedInputStream");
104 System
.out
.println("Test 1: Protected Variables Test");
106 String str
= "This is a test line of text for this pass";
108 StringBufferInputStream sbis
= new StringBufferInputStream(str
);
109 BufferedInputStreamTest bist
= new BufferedInputStreamTest(sbis
, 12);
114 boolean passed
= true;
116 if (bist
.buf
.length
!= 12)
119 System
.out
.println("buf.length is wrong. Expected 12, but got " +
122 if (bist
.count
!= 12)
125 System
.out
.println("count is wrong. Expected 12, but got " +
128 if (bist
.marklimit
!= 5)
131 System
.out
.println("marklimit is wrong. Expected 5, but got " +
134 if (bist
.markpos
!= 1)
137 System
.out
.println("markpos is wrong. Expected 5, but got " +
143 System
.out
.println("pos is wrong. Expected 1, but got " +
148 System
.out
.println("PASSED: Protected Variables Test");
150 System
.out
.println("FAILED: Protected Variables Test");
154 System
.out
.println("FAILED: Protected Variables Test: " + e
);
159 System
.out
.println("Test 2: Simple Read Test");
161 String str
= "One of my 8th grade teachers was Mr. Russell.\n" +
162 "He used to start each year off by telling the class that the\n" +
163 "earth was flat. He did it to teach people to question\n" +
164 "things they are told. But everybody knew that he did it\n" +
165 "so it lost its effect.\n";
167 StringBufferInputStream sbis
= new StringBufferInputStream(str
);
168 BufferedInputStream bis
= new BufferedInputStream(sbis
, 15);
170 byte[] buf
= new byte[12];
172 while((bytes_read
= bis
.read(buf
)) != -1)
173 System
.out
.print(new String(buf
, 0, bytes_read
));
176 System
.out
.println("PASSED: Simple Read Test");
178 catch (IOException e
)
180 System
.out
.println("FAILED: Simple Read Test: " + e
);
185 System
.out
.println("Test 3: First mark/reset Test");
187 String str
= "My 6th grade teacher was named Mrs. Hostetler.\n" +
188 "She had a whole list of rules that you were supposed to follow\n" +
189 "in class and if you broke a rule she would make you write the\n" +
190 "rules out several times. The number varied depending on what\n" +
191 "rule you broke. Since I knew I would get in trouble, I would\n" +
192 "just go ahead and write out a few sets on the long school bus\n" +
193 "ride home so that if had to stay in during recess and write\n" +
194 "rules, five minutes later I could just tell the teacher I was\n" +
195 "done so I could go outside and play kickball.\n";
197 StringBufferInputStream sbis
= new StringBufferInputStream(str
);
199 int total_read
= marktest(sbis
);
201 if (total_read
== str
.length())
202 System
.out
.println("PASSED: First mark/reset Test");
204 System
.out
.println("FAILED: First mark/reset Test");
206 catch (IOException e
)
208 System
.out
.println("FAILED: First mark/reset Test: " + e
);
213 System
.out
.println("Test 4: Second mark/reset Test");
215 String str
= "My first day of college was fun. A bunch of us\n" +
216 "got pretty drunk, then this guy named Rick Flake (I'm not\n" +
217 "making that name up) took a piss in the bed of a Physical\n" +
218 "Plant dept pickup truck. Later on we were walking across\n" +
219 "campus, saw a cop, and took off running for no reason.\n" +
220 "When we got back to the dorm we found an even drunker guy\n" +
221 "passed out in a shopping cart outside.\n";
223 ByteArrayInputStream sbis
= new ByteArrayInputStream(str
.getBytes());
225 int total_read
= marktest(sbis
);
227 if (total_read
== str
.length())
228 System
.out
.println("PASSED: Second mark/reset Test");
230 System
.out
.println("FAILED: Second mark/reset Test");
232 catch (IOException e
)
234 System
.out
.println("FAILED: Second mark/reset Test: " + e
);
237 System
.out
.println("Finished test of BufferedInputStream");
240 } // class BufferedInputStreamTest