Merge from mainline.
[official-gcc.git] / libjava / classpath / test / java.io / BufferedInputStreamTest.java
blobc7d73e352a02b592912b58a45f73eee9f12bb50d
1 /*************************************************************************
2 /* BufferedInputStreamTest.java -- Tests BufferedInputStream'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 BufferedInputStreamTest extends BufferedInputStream
27 public
28 BufferedInputStreamTest(InputStream in, int size)
30 super(in, size);
33 public static int
34 marktest(InputStream ins) throws IOException
36 BufferedInputStream bis = new BufferedInputStream(ins, 15);
38 int bytes_read;
39 int total_read = 0;
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));
50 bis.mark(75);
51 bis.read();
52 bis.read(buf);
53 bis.read(buf);
54 bis.read(buf);
55 bis.reset();
57 bytes_read = bis.read(buf);
58 total_read += bytes_read;
59 System.out.print(new String(buf, 0, bytes_read));
61 bis.mark(555);
63 bytes_read = bis.read(buf);
64 total_read += bytes_read;
65 System.out.print(new String(buf, 0, bytes_read));
67 bis.reset();
69 bis.read(buf);
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));
78 bis.mark(14);
80 bis.read(buf);
82 bis.reset();
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;
94 return(total_read);
97 public static void
98 main(String[] argv)
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);
111 bist.read();
112 bist.mark(5);
114 boolean passed = true;
116 if (bist.buf.length != 12)
118 passed = false;
119 System.out.println("buf.length is wrong. Expected 12, but got " +
120 bist.buf.length);
122 if (bist.count != 12)
124 passed = false;
125 System.out.println("count is wrong. Expected 12, but got " +
126 bist.count);
128 if (bist.marklimit != 5)
130 passed = false;
131 System.out.println("marklimit is wrong. Expected 5, but got " +
132 bist.marklimit);
134 if (bist.markpos != 1)
136 passed = false;
137 System.out.println("markpos is wrong. Expected 5, but got " +
138 bist.markpos);
140 if (bist.pos != 1)
142 passed = false;
143 System.out.println("pos is wrong. Expected 1, but got " +
144 bist.pos);
147 if (passed)
148 System.out.println("PASSED: Protected Variables Test");
149 else
150 System.out.println("FAILED: Protected Variables Test");
152 catch(IOException e)
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];
171 int bytes_read;
172 while((bytes_read = bis.read(buf)) != -1)
173 System.out.print(new String(buf, 0, bytes_read));
175 bis.close();
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");
203 else
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");
229 else
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");
238 } // main
240 } // class BufferedInputStreamTest