示例#1
0
    // Read the records and verify the consistency.
    void readRecords (RandomAccessFile& file,
                      int numRecords,
                      HeapBlock <Record> const& records,
                      std::int64_t seedValue)
    {
        using namespace UnitTestUtilities;

        for (int i = 0; i < numRecords; ++i)
        {
            Record const& record (records [i]);

            int const bytes = record.bytes;

            Payload p1 (bytes);
            Payload p2 (bytes);

            p1.repeatableRandomFill (bytes, bytes, record.index + seedValue);

            file.setPosition (record.offset);

            Result result = file.read (p2.data.getData (), bytes);

            expect (result.wasOk (), "Should be ok");

            if (result.wasOk ())
            {
                p2.bytes = bytes;

                expect (p1 == p2, "Should be equal");
            }
        }
    }
示例#2
0
int _tmain(int argc, _TCHAR* argv[])
{
	char a = 't';
	RandomAccessFile* rf = new RandomAccessFile("tst.bin");
	rf->write(a);
	RandomAccessFile* rf2 = new RandomAccessFile("tst.bin");
	cout << rf2->read<char>(0) << endl;
}
示例#3
0
    // Perform the test at the given buffer size.
    void testFile (int const numRecords)
    {
        using namespace UnitTestUtilities;

        int const seedValue = 50;

        std::stringstream ss;
        ss << numRecords << " records";
        testcase (ss.str());

        // Calculate the path
        File const path (File::createTempFile ("RandomAccessFile"));

        // Create a predictable set of records
        HeapBlock <Record> records (numRecords);
        createRecords (records, numRecords, maxPayload, seedValue);

        Result result (Result::ok ());

        {
            // Create the file
            RandomAccessFile file;
            result = file.open (path, RandomAccessFile::readWrite);
            expect (result.wasOk (), "Should be ok");

            if (result.wasOk ())
            {
                writeRecords (file, numRecords, records, seedValue);

                readRecords (file, numRecords, records, seedValue);

                repeatableShuffle (numRecords, records, seedValue);

                readRecords (file, numRecords, records, seedValue);
            }
        }

        if (result.wasOk ())
        {
            // Re-open the file in read only mode
            RandomAccessFile file;
            result = file.open (path, RandomAccessFile::readOnly);
            expect (result.wasOk (), "Should be ok");

            if (result.wasOk ())
            {
                readRecords (file, numRecords, records, seedValue);
            }
        }
    }
示例#4
0
    // Write all the records to the file.
    // The payload is pseudo-randomly generated.
    void writeRecords (RandomAccessFile& file,
                       int numRecords,
                       HeapBlock <Record> const& records,
                       std::int64_t seedValue)
    {
        using namespace UnitTestUtilities;

        for (int i = 0; i < numRecords; ++i)
        {
            Payload p (records [i].bytes);

            p.repeatableRandomFill (records [i].bytes,
                                    records [i].bytes,
                                    records [i].index + seedValue);

            file.setPosition (records [i].offset);

            Result result = file.write (p.data.getData (), p.bytes);

            expect (result.wasOk (), "Should be ok");
        }
    }