1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17 package android.core; 18 19 import junit.framework.TestCase; 20 21 import java.io.PipedInputStream; 22 import java.io.PipedOutputStream; 23 import android.test.suitebuilder.annotation.MediumTest; 24 import android.test.suitebuilder.annotation.SmallTest; 25 26 class Fibonacci { 27 int n1 = -1; 28 int n2; 29 30 public int next() { 31 if (n1 < 0) { 32 n1 = 0; 33 return 0; 34 } else if (n1 == 0) { 35 n2 = 0; 36 n1 = 1; 37 return 1; 38 } else { 39 int ret = n1 + n2; 40 n2 = n1; 41 n1 = ret; 42 return ret; 43 } 44 } 45 } 46 47 48 public class PipedStreamTest extends TestCase { 49 50 private abstract static class TestThread extends Thread { 51 public abstract void runTest() throws Exception; 52 53 public final void run() { 54 try { 55 runTest(); 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)); 59 exception = e; 60 } 61 } 62 63 Throwable exception; 64 int countRead = 0; 65 } 66 67 @MediumTest 68 public void testA() throws Exception { 69 70 final PipedInputStream in = new PipedInputStream(); 71 final PipedOutputStream out = new PipedOutputStream(in); 72 73 assertEquals(0, in.available()); 74 75 TestThread reader, writer; 76 77 reader = new TestThread() { 78 Fibonacci fib = new Fibonacci(); 79 80 @Override 81 public void runTest() throws Exception { 82 int readInt; 83 byte readByte; 84 85 for (; ;) { 86 readInt = in.read(); 87 88 if (readInt == -1) { 89 return; 90 } 91 92 readByte = (byte) readInt; 93 assertEquals(readByte, (byte) fib.next()); 94 countRead++; 95 } 96 } 97 }; 98 99 reader.start(); 100 101 writer = new TestThread() { 102 Fibonacci fib = new Fibonacci(); 103 104 @Override 105 public void runTest() throws Exception { 106 for (int i = 0; i < 2000; i++) { 107 int toWrite = fib.next(); 108 out.write(toWrite); 109 } 110 out.close(); 111 } 112 }; 113 114 writer.start(); 115 116 117 for (; ;) { 118 try { 119 reader.join(60 * 1000); 120 writer.join(1000); 121 break; 122 } catch (InterruptedException ex) { 123 } 124 } 125 126 assertEquals(2000, reader.countRead); 127 128 if (writer.exception != null) { 129 throw new Exception(writer.exception); 130 } 131 if (reader.exception != null) { 132 throw new Exception(reader.exception); 133 } 134 } 135 136 @MediumTest 137 public void testB() throws Exception { 138 final PipedInputStream in = new PipedInputStream(); 139 final PipedOutputStream out = new PipedOutputStream(in); 140 141 assertEquals(0, in.available()); 142 143 TestThread reader, writer; 144 145 reader = new TestThread() { 146 Fibonacci fib = new Fibonacci(); 147 148 @Override 149 public void runTest() throws Exception { 150 byte readBytes[] = new byte[5]; 151 int ret; 152 153 for (; ;) { 154 int nread = 0; 155 while (nread < 5) { 156 ret = in.read(readBytes, nread, readBytes.length - nread); 157 158 if (ret == -1) { 159 return; 160 } 161 nread += ret; 162 } 163 164 assertEquals(5, nread); 165 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)); 170 171 172 assertEquals("Error at " + countRead, fib.next(), readInt); 173 assertEquals("Error at " + countRead, 0, readBytes[4]); 174 countRead++; 175 } 176 } 177 }; 178 179 reader.start(); 180 181 writer = new TestThread() { 182 Fibonacci fib = new Fibonacci(); 183 184 @Override 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); 193 writeBytes[4] = 0; 194 out.write(writeBytes, 0, writeBytes.length); 195 } 196 out.close(); 197 } 198 }; 199 200 writer.start(); 201 202 203 for (; ;) { 204 try { 205 reader.join(60 * 1000); 206 writer.join(1000); 207 break; 208 } catch (InterruptedException ex) { 209 } 210 } 211 212 if (reader.exception != null) { 213 throw new Exception(reader.exception); 214 } 215 if (writer.exception != null) { 216 throw new Exception(writer.exception); 217 } 218 219 assertEquals(2000, reader.countRead); 220 } 221 222 @SmallTest 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]; 227 228 assertEquals(0, in.available()); 229 230 TestThread reader, writer; 231 232 reader = new TestThread() { 233 @Override 234 public void runTest() throws Exception { 235 int ret; 236 237 for (; ;) { 238 int nread = 0; 239 while (nread < readBytes.length) { 240 ret = in.read(readBytes, nread, readBytes.length - nread); 241 242 if (ret == -1) { 243 return; 244 } 245 nread += ret; 246 } 247 } 248 } 249 }; 250 251 reader.start(); 252 253 writer = new TestThread() { 254 Fibonacci fib = new Fibonacci(); 255 256 @Override 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); 265 } 266 out.write(writeBytes, 0, writeBytes.length); 267 out.close(); 268 } 269 }; 270 271 writer.start(); 272 273 274 for (; ;) { 275 try { 276 reader.join(60 * 1000); 277 writer.join(1000); 278 break; 279 } catch (InterruptedException ex) { 280 } 281 } 282 283 if (reader.exception != null) { 284 throw new Exception(reader.exception); 285 } 286 if (writer.exception != null) { 287 throw new Exception(writer.exception); 288 } 289 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)); 296 297 assertEquals("Error at " + i, readInt, fib.next()); 298 } 299 } 300 } 301