FileDocCategorySizeDatePackage
MemoryFileTest.javaAPI DocAndroid 1.5 API34348Wed May 06 22:42:02 BST 2009com.android.unit_tests.os

MemoryFileTest

public class MemoryFileTest extends TestCase

Fields Summary
private static final byte[]
testString
Constructors Summary
Methods Summary
private voidcompareBuffers(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");
            }
        }
    
public voidtestPurge()
Keep allocating new files till the system purges them.

        List<MemoryFile> files = new ArrayList<MemoryFile>();
        while (true) {
            MemoryFile newFile = new MemoryFile("MemoryFileTest", 1000000);
            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
                    for (MemoryFile fileToClose : files) {
                        fileToClose.close();
                    }
                    return;
                }
            }
        }
    
public voidtestRun()

        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();