nsCRT::memset(writeBuf[i], i, 256 * i);
memset(writeBuf[i], i, 256 * i);
StressTest(nsILocalFile *  localFile, PRInt32  testNumber, PRBool readWrite){    nsresult  rv = NS_OK;#define ITERATIONS      1024#define MAX_ALLOCATIONS 256    Allocation  block[MAX_ALLOCATIONS];    PRInt32     currentAllocations = 0;    PRInt32     i;    PRUint32    a;    char * writeBuf[4];    char   readBuf[256 * 4];            if (readWrite) {        for (i = 0; i < 4; i++) {            writeBuf[i] = new char[256 * i];            if (!writeBuf[i]) {                printf("Test %d: failed - out of memory\n", testNumber);                rv = NS_ERROR_OUT_OF_MEMORY;                goto exit;            }                        nsCRT::memset(writeBuf[i], i, 256 * i);        }    }    nsDiskCacheBlockFile * blockFile = new nsDiskCacheBlockFile;    if (!blockFile) {        printf("Test %d failed (unable to allocate nsDiskCacheBlockFile", testNumber);        rv = NS_ERROR_OUT_OF_MEMORY;        goto exit;    }    rv = blockFile->Open(localFile, 256);    if (NS_FAILED(rv)) {        printf("Test %d: failed (Open returned: 0x%.8x)\n", testNumber, rv);        goto exit;    }    i = ITERATIONS;    while (i > 0) {        if ((currentAllocations >= MAX_ALLOCATIONS) ||            ((currentAllocations > 0) && (rand() % 4 == 0))) {            // deallocate if we've reached the limit, or 25% of the time we have allocations            a = rand() % currentAllocations;                        if (readWrite) {                // read verify deallocation                rv = blockFile->ReadBlocks(readBuf, block[a].start, block[a].count);                if (NS_FAILED(rv)) {                    printf("Test %d: failed (ReadBlocks() returned 0x%.8x)\n", testNumber, rv);                    goto exit;                }                // Verify buffer                for (i = 0; i < 256 * block[a].count; i++) {                    if (readBuf[i] != block[a].count) {                        printf("Test %d: failed (verifying buffer 1)\n", testNumber);                        rv = NS_ERROR_FAILURE;                        goto exit;                    }                }            }                        rv = blockFile->DeallocateBlocks(block[a].start, block[a].count);            if (NS_FAILED(rv)) {                printf("Test %d: failed (DeallocateBlocks() returned %d)\n", testNumber, rv);                goto exit;            }                        --currentAllocations;            if (currentAllocations > 0)                block[a] = block[currentAllocations];                    } else {            // allocate blocks            --i;            a = currentAllocations++;            block[a].count = rand() % 4 + 1; // allocate 1 to 4 blocks            block[a].start = blockFile->AllocateBlocks(block[a].count);            if (block[a].start < 0) {                printf("Test %d: failed (AllocateBlocks() failed.)\n", testNumber);                goto exit;            }                        if (readWrite) {                // write buffer                rv = blockFile->WriteBlocks(writeBuf[block[a].count], block[a].start, block[a].count);                if (NS_FAILED(rv)) {                    printf("Test %d: failed (WriteBlocks() returned 0x%.8x)\n",testNumber, rv);                    goto exit;                }            }        }    }        // now deallocate remaining allocations    i = currentAllocations;    while (i--) {         if (readWrite) {            // read verify deallocation            rv = blockFile->ReadBlocks(readBuf, block[a].start, block[a].count);            if (NS_FAILED(rv)) {                printf("Test %d: failed (ReadBlocks(1) returned 0x%.8x)\n", testNumber, rv);                goto exit;            }            // Verify buffer            for (i = 0; i < 256 * block[a].count; i++) {                if (readBuf[i] != block[a].count) {                    printf("Test %d: failed (verifying buffer 1)\n", testNumber);                    rv = NS_ERROR_FAILURE;                    goto exit;                }            }        }        rv = blockFile->DeallocateBlocks(block[i].start, block[i].count);        if (NS_FAILED(rv)) {            printf("Test %d: failed (DeallocateBlocks() returned %d)\n", testNumber, rv);            goto exit;        }    }exit:    nsresult rv2 = blockFile->Close();    if (NS_FAILED(rv2)) {        printf("Test %d: failed (Close returned: 0x%.8x)\n", testNumber, rv2);    }    return rv ? rv : rv2;}    