MemoryFileTestpublic class MemoryFileTest extends android.test.AndroidTestCase
Fields Summary |
---|
private static final byte[] | testString |
Methods Summary |
---|
private void | compareBuffers(byte[] buffer1, byte[] buffer2, int length)
for (int i = 0; i < length; i++) {
if (buffer1[i] != buffer2[i]) {
throw new Exception("readBytes did not read back what writeBytes wrote");
}
}
| private void | readIndexOutOfBoundsException(int offset, int count, java.lang.String msg)
MemoryFile file = new MemoryFile("MemoryFileTest", testString.length);
try {
file.writeBytes(testString, 0, 0, testString.length);
InputStream is = file.getInputStream();
byte[] buffer = new byte[testString.length + 10];
try {
is.read(buffer, offset, count);
fail(msg);
} catch (IndexOutOfBoundsException ex) {
// this is what should happen
} finally {
is.close();
}
} finally {
file.close();
}
| public void | testCloseAllowPurging()
MemoryFile file = new MemoryFile("MemoryFileTest", 1000000);
byte[] data = new byte[512];
file.writeBytes(data, 0, 0, 128);
file.close();
try {
file.allowPurging(true);
fail("allowPurging() after close() did not throw IOException.");
} catch (IOException e) {
// this is what should happen
}
| public void | testCloseClose()
MemoryFile file = new MemoryFile("MemoryFileTest", 1000000);
byte[] data = new byte[512];
file.writeBytes(data, 0, 0, 128);
file.close();
file.close();
| public void | testCloseLeak()
// open enough memory files that we should run out of
// file descriptors or address space if we leak either.
for (int i = 0; i < 1025; i++) {
MemoryFile file = new MemoryFile("MemoryFileTest", 5000000);
file.writeBytes(testString, 0, 0, testString.length);
file.close();
}
| public void | testCloseRead()
MemoryFile file = new MemoryFile("MemoryFileTest", 1000000);
file.close();
try {
byte[] data = new byte[512];
assertEquals(128, file.readBytes(data, 0, 0, 128));
fail("readBytes() after close() did not throw IOException.");
} catch (IOException e) {
// this is what should happen
}
| public void | testCloseWrite()
MemoryFile file = new MemoryFile("MemoryFileTest", 1000000);
file.close();
try {
byte[] data = new byte[512];
file.writeBytes(data, 0, 0, 128);
fail("writeBytes() after close() did not throw IOException.");
} catch (IOException e) {
// this is what should happen
}
| public void | testOutputStreamAdvances()
MemoryFile file = new MemoryFile("MemoryFileTest", 10);
OutputStream os = file.getOutputStream();
os.write(new byte[] { 1, 2, 3, 4, 5 });
os.write(new byte[] { -1, -1, 6, 7, 8, -1 }, 2, 3);
os.write(9);
try {
os.write(new byte[] { -1, -1 });
fail();
} catch (IndexOutOfBoundsException expected) {
}
byte[] copy = new byte[file.length()];
file.readBytes(copy, 0, 0, file.length());
assertEquals("[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]", Arrays.toString(copy));
| public void | testPurge()Keep allocating new files till the system purges them.
List<MemoryFile> files = new ArrayList<MemoryFile>();
try {
while (true) {
// This will fail if the process runs out of file descriptors before
// the kernel starts purging ashmem areas.
MemoryFile newFile = new MemoryFile("MemoryFileTest", 10000000);
newFile.allowPurging(true);
newFile.writeBytes(testString, 0, 0, testString.length);
files.add(newFile);
for (MemoryFile file : files) {
try {
file.readBytes(testString, 0, 0, testString.length);
} catch (IOException e) {
// Expected
return;
}
}
}
} finally {
for (MemoryFile fileToClose : files) {
fileToClose.close();
}
}
| public void | testReadEOF()
MemoryFile file = new MemoryFile("MemoryFileTest", testString.length);
try {
file.writeBytes(testString, 0, 0, testString.length);
InputStream is = file.getInputStream();
try {
byte[] buffer = new byte[testString.length + 10];
// read() with count larger than data should succeed, and return # of bytes read
assertEquals(testString.length, is.read(buffer));
compareBuffers(testString, buffer, testString.length);
// Read at EOF should return -1
assertEquals(-1, is.read());
} finally {
is.close();
}
} finally {
file.close();
}
| public void | testReadNegativeCount()
readIndexOutOfBoundsException(5, -1,
"read() with negative length should throw IndexOutOfBoundsException");
| public void | testReadNegativeOffset()
readIndexOutOfBoundsException(-1, 5,
"read() with negative offset should throw IndexOutOfBoundsException");
| public void | testReadOffsetCountOverflow()
readIndexOutOfBoundsException(testString.length, 11,
"read() with offset + count outside buffer should throw IndexOutOfBoundsException");
| public void | testReadOffsetOverflow()
readIndexOutOfBoundsException(testString.length + 10, 5,
"read() with offset outside buffer should throw IndexOutOfBoundsException");
| public void | testRun()
MemoryFile file = new MemoryFile("MemoryFileTest", 1000000);
byte[] buffer = new byte[testString.length];
// check low level accessors
file.writeBytes(testString, 0, 2000, testString.length);
file.readBytes(buffer, 2000, 0, testString.length);
compareBuffers(testString, buffer, testString.length);
// check streams
buffer = new byte[testString.length];
OutputStream os = file.getOutputStream();
os.write(testString);
InputStream is = file.getInputStream();
is.mark(testString.length);
is.read(buffer);
compareBuffers(testString, buffer, testString.length);
// test mark/reset
buffer = new byte[testString.length];
is.reset();
is.read(buffer);
compareBuffers(testString, buffer, testString.length);
file.close();
|
|