2 * Copyright (C) 2007 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 import junit.framework.TestCase;
21 import java.io.PipedInputStream;
22 import java.io.PipedOutputStream;
23 import android.test.suitebuilder.annotation.MediumTest;
24 import android.test.suitebuilder.annotation.SmallTest;
48 public class PipedStreamTest extends TestCase {
50 private abstract static class TestThread extends Thread {
51 public abstract void runTest() throws Exception;
53 public final void run() {
56 } catch (Throwable e) {
57 android.util.Log.e("PST", "Got exception " + e, e);
58 android.util.Log.e("PST", android.util.Log.getStackTraceString(e));
68 public void testA() throws Exception {
70 final PipedInputStream in = new PipedInputStream();
71 final PipedOutputStream out = new PipedOutputStream(in);
73 assertEquals(0, in.available());
75 TestThread reader, writer;
77 reader = new TestThread() {
78 Fibonacci fib = new Fibonacci();
81 public void runTest() throws Exception {
92 readByte = (byte) readInt;
93 assertEquals(readByte, (byte) fib.next());
101 writer = new TestThread() {
102 Fibonacci fib = new Fibonacci();
105 public void runTest() throws Exception {
106 for (int i = 0; i < 2000; i++) {
107 int toWrite = fib.next();
119 reader.join(60 * 1000);
122 } catch (InterruptedException ex) {
126 assertEquals(2000, reader.countRead);
128 if (writer.exception != null) {
129 throw new Exception(writer.exception);
131 if (reader.exception != null) {
132 throw new Exception(reader.exception);
137 public void testB() throws Exception {
138 final PipedInputStream in = new PipedInputStream();
139 final PipedOutputStream out = new PipedOutputStream(in);
141 assertEquals(0, in.available());
143 TestThread reader, writer;
145 reader = new TestThread() {
146 Fibonacci fib = new Fibonacci();
149 public void runTest() throws Exception {
150 byte readBytes[] = new byte[5];
156 ret = in.read(readBytes, nread, readBytes.length - nread);
164 assertEquals(5, nread);
166 int readInt = (((int) readBytes[0] & 0xff) << 24)
167 | (((int) readBytes[1] & 0xff) << 16)
168 | (((int) readBytes[2] & 0xff) << 8)
169 | (((int) readBytes[3] & 0xff));
172 assertEquals("Error at " + countRead, fib.next(), readInt);
173 assertEquals("Error at " + countRead, 0, readBytes[4]);
181 writer = new TestThread() {
182 Fibonacci fib = new Fibonacci();
185 public void runTest() throws Exception {
186 byte writeBytes[] = new byte[5];
187 for (int i = 0; i < 2000; i++) {
188 int toWrite = fib.next();
189 writeBytes[0] = (byte) (toWrite >> 24);
190 writeBytes[1] = (byte) (toWrite >> 16);
191 writeBytes[2] = (byte) (toWrite >> 8);
192 writeBytes[3] = (byte) (toWrite);
194 out.write(writeBytes, 0, writeBytes.length);
205 reader.join(60 * 1000);
208 } catch (InterruptedException ex) {
212 if (reader.exception != null) {
213 throw new Exception(reader.exception);
215 if (writer.exception != null) {
216 throw new Exception(writer.exception);
219 assertEquals(2000, reader.countRead);
223 public void testC() throws Exception {
224 final PipedInputStream in = new PipedInputStream();
225 final PipedOutputStream out = new PipedOutputStream(in);
226 final byte readBytes[] = new byte[1024 * 2];
228 assertEquals(0, in.available());
230 TestThread reader, writer;
232 reader = new TestThread() {
234 public void runTest() throws Exception {
239 while (nread < readBytes.length) {
240 ret = in.read(readBytes, nread, readBytes.length - nread);
253 writer = new TestThread() {
254 Fibonacci fib = new Fibonacci();
257 public void runTest() throws Exception {
258 byte writeBytes[] = new byte[1024 * 2];
259 for (int i = 0; i < (writeBytes.length - 4); i += 4) {
260 int toWrite = fib.next();
261 writeBytes[i ] = (byte) (toWrite >> 24);
262 writeBytes[i + 1] = (byte) (toWrite >> 16);
263 writeBytes[i + 2] = (byte) (toWrite >> 8);
264 writeBytes[i + 3] = (byte) (toWrite);
266 out.write(writeBytes, 0, writeBytes.length);
276 reader.join(60 * 1000);
279 } catch (InterruptedException ex) {
283 if (reader.exception != null) {
284 throw new Exception(reader.exception);
286 if (writer.exception != null) {
287 throw new Exception(writer.exception);
290 Fibonacci fib = new Fibonacci();
291 for (int i = 0; i < (readBytes.length - 4); i += 4) {
292 int readInt = (((int) readBytes[i] & 0xff) << 24)
293 | (((int) readBytes[i + 1] & 0xff) << 16)
294 | (((int) readBytes[i + 2] & 0xff) << 8)
295 | (((int) readBytes[i + 3] & 0xff));
297 assertEquals("Error at " + i, readInt, fib.next());