📄 position.c
字号:
/******************************************************************************Function Name : Seekables Description : Check the seekable positions as we write data to a file Parameters :******************************************************************************/static TestResult_t Seekables(int TestNo, TestVariant_t Variant){ TestResult_t Result; STAVFS_Handle_t DiskHandle; STAVFS_OpenMode_t OpenMode = STAVFS_WRITE_MODE | STAVFS_READ_MODE; switch (Variant) { case TEST_VARIANT_A: /* regular files; no special settings */ break; case TEST_VARIANT_B: /* cyclic files; just to check they do the same thing when not wrapping */ OpenMode |= STAVFS_CYCLIC_MODE; break; default: return TEST_NOT_WRITTEN; } Result = OpenPartition(0, 1, "Dev1", &DiskHandle); /* 1 sector per cluster */ if (TEST_PASSED == Result) { STAVFS_FileHandle_t FileHandle; U64 FileSize; U32 DataWritten, EOFPos = 0, ChunkSize = 100; /* EOFPos is the number of bytes written, but DataWritten is the name usually used with STAVFS_WriteFile to receive the byte count written in a particular operation */ I64_SetValue (500, 0, FileSize); if (ST_NO_ERROR != STAVFS_OpenFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, FILE_NAME, OpenMode, FileSize, &FileHandle)) { STTBX_Print (("Error opening file\n")); Result = TEST_FAILED; } else { S32 PosZero[] = { 0 }; /* seeks to this should succeed */ S32 PosBadInitial[] = { -1, 1, 200 }; /* seeks to these should fail */ S32 PosLastAndEOF[2], PosAfterEOF[1]; /* good and bad positions during loop below */ if (DOUBLE_PROBE_FAILS (FileHandle, PosZero, PosBadInitial, "New empty file: ")) { Result = TEST_FAILED; /* message already displayed */ } /* write four 100 byte chunks, checking certain seekabilities in each case */ while ((EOFPos < 4 * ChunkSize) && (Result == TEST_PASSED)) { if (ST_NO_ERROR != STAVFS_WriteFile (FileHandle, StdData, ChunkSize, &DataWritten)) { STTBX_Print (("Error writing from position %i\n", EOFPos)); Result = TEST_FAILED; } else { EOFPos += ChunkSize; PosLastAndEOF[0] = EOFPos -1; /* last byte written should be seekable */ PosLastAndEOF[1] = EOFPos; /* EOF should be seekable */ PosAfterEOF[0] = EOFPos + 1; /* any further should not */ if (DOUBLE_PROBE_FAILS (FileHandle, PosLastAndEOF, PosAfterEOF, "Probing near EOF as we write 100-byte chunks:\n ")) { Result = TEST_FAILED; /* message already displayed */ } /* success leaves the write position at EOF, where we want it for the next iteration */ } } /* close, and reopen if okay so far */ if (ST_NO_ERROR != STAVFS_CloseFile (FileHandle)) { STTBX_Print (("Error closing file (1)\n")); Result = TEST_FAILED; } if (Result == TEST_PASSED && ST_NO_ERROR != STAVFS_OpenFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, FILE_NAME, OpenMode, FileSize, &FileHandle)) { STTBX_Print (("Error reopening file\n")); Result = TEST_FAILED; } if (Result == TEST_PASSED) { if (OpenMode & STAVFS_CYCLIC_MODE) { /* cyclic case, whilst cyclic files are still defined to lose their contents on closing */ S32 PosBadReopen[] = { -1, 200, 399, 400, 401, 500, 512 }; /* positions that should be bad */ if (DOUBLE_PROBE_FAILS (FileHandle, PosZero, PosBadReopen, "After reopening: ")) { Result = TEST_FAILED; /* message already displayed */ } } else { /* normal case, which all files will follow eventually, but for now only non-cyclic files do */ S32 PosGoodReopen[] = { 0, 200, 399, 400 }; /* positions that should be good (last is EOF) */ S32 PosBadReopen[] = { -1, 401, 500, 512 }; /* positions that should be bad */ if (DOUBLE_PROBE_FAILS (FileHandle, PosGoodReopen, PosBadReopen, "After reopening: ")) { Result = TEST_FAILED; /* message already displayed */ } } if (ST_NO_ERROR != STAVFS_CloseFile (FileHandle)) { STTBX_Print (("Error closing file (2)\n")); Result = TEST_FAILED; } } /* delete file in all cases where we sucessfully created it */ if (ST_NO_ERROR != STAVFS_DeleteFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, FILE_NAME)) { STTBX_Print (("Error deleting file\n")); Result = TEST_FAILED; } } if (TEST_PASSED != ClosePartition("Dev1", DiskHandle)) { Result = TEST_FAILED; /* message already displayed */ } } return (Result);}/******************************************************************************Function Name : ReadStability Description : Confirm that reads after a seek really do come from where we specified Parameters :******************************************************************************/static TestResult_t ReadStability(int TestNo, TestVariant_t Variant){ TestResult_t Result; STAVFS_Handle_t DiskHandle; STAVFS_OpenMode_t OpenMode = STAVFS_WRITE_MODE | STAVFS_READ_MODE; switch (Variant) { case TEST_VARIANT_A: /* regular files; no special settings */ break; case TEST_VARIANT_B: /* cyclic files; just to check they do the same thing when not wrapping */ OpenMode |= STAVFS_CYCLIC_MODE; break; default: return TEST_NOT_WRITTEN; } Result = OpenPartition(0, 1, "Dev1", &DiskHandle); /* 1 sector per cluster */ if (TEST_PASSED == Result) { STAVFS_FileHandle_t FileHandle; ST_ErrorCode_t Error; U64 FileSize, Pos; U32 DataWritten, DataRead; U32 DataSize = 500, ReadPos = 250; U32 SeekReadChunkSize = 100; int i, j; I64_SetValue (1000, 0, FileSize); if (ST_NO_ERROR != STAVFS_OpenFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, FILE_NAME, OpenMode, FileSize, &FileHandle)) { STTBX_Print (("Error opening file\n")); Result = TEST_FAILED; } else { I64_SetValue (ReadPos, 0, Pos); /* for SeekFile below */ /* write some data */ if (ST_NO_ERROR != STAVFS_WriteFile (FileHandle, StdData, DataSize, &DataWritten)) { STTBX_Print (("Error writing file\n")); Result = TEST_FAILED; } else if (ST_NO_ERROR != STAVFS_SeekFile (FileHandle, Pos, STAVFS_START, FALSE)) { STTBX_Print (("Error read-seeking\n")); Result = TEST_FAILED; } else if (TEST_PASSED != CheckPos (FileHandle, ReadPos, DataSize, "After read seek: ")) { Result = TEST_FAILED; /* message already displayed */ } for (i = 0; (i < 2) && (Result == TEST_PASSED); ++i) { Error = STAVFS_ReadFile (FileHandle, Buffer, SeekReadChunkSize, &DataRead); if ((Error != ST_NO_ERROR) || (DataRead != SeekReadChunkSize)) { STTBX_Print (("Error on read #%i\n", i)); Result = TEST_FAILED; break; } /* update recorded read position and check GetFilePosition agrees */ if (TEST_PASSED != CheckPos (FileHandle, ReadPos+DataRead, DataSize, "After a read: ")) { Result = TEST_FAILED; /* message already displayed */ break; } /* Check the data retrieved; since StdData consists of a rolling-over one-byte count, this is easy */ for (j = 0; (j < DataRead) && (Result == TEST_PASSED); ++j) { if (Buffer[j] != (ReadPos + j) % 0x100) { STTBX_Print (("Wrong data read at position %i following seek\n", ReadPos + j)); Result = TEST_FAILED; } } ReadPos += DataRead; } /* Check the end conditions */ if (i == 2) { /* explicit zero to check ReadFile does not write data into our buffer beyond EOF position */ memset (Buffer, 0, SeekReadChunkSize); Error = STAVFS_ReadFile (FileHandle, Buffer, SeekReadChunkSize, &DataRead); if (OpenMode & STAVFS_CYCLIC_MODE) { if (Error != STAVFS_ERROR_UNDERFLOW) { STTBX_Print (("Read over end of written data failed to return STAVFS_ERROR_EOF\n")); Result = TEST_FAILED; } } else { if (Error != STAVFS_ERROR_EOF) { STTBX_Print (("Read over end of written data failed to return STAVFS_ERROR_EOF\n")); Result = TEST_FAILED; } } if (DataRead != DataSize - ReadPos) /* ie 500 - 450 = 50 */ { STTBX_Print (("Wrong DataRead returned by read over EOF\n")); Result = TEST_FAILED; } /* the data that is retrieved is checked below. The remaining bytes should have been left alone, and thus still be zero: */ for (j = DataRead; (j < SeekReadChunkSize) && (Result == TEST_PASSED); ++j) { if (Buffer[j] != 0) { STTBX_Print (("Bogus bytes returned by STAVFS_ReadFile past EOF\n")); Result = TEST_FAILED; } } } /* close and delete file in all cases where we sucessfully created it */ if (ST_NO_ERROR != STAVFS_CloseFile (FileHandle)) { STTBX_Print (("Error closing file\n")); Result = TEST_FAILED; } if (ST_NO_ERROR != STAVFS_DeleteFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, FILE_NAME)) { STTBX_Print (("Error deleting file\n")); Result = TEST_FAILED; } } if (TEST_PASSED != ClosePartition("Dev1", DiskHandle)) { Result = TEST_FAILED; /* message already displayed */ } } return (Result);}/******************************************************************************Function Name : WriteStability Description : Confirm that writes after a seek really do go where we specified Parameters :******************************************************************************/static TestResult_t WriteStability(int TestNo, TestVariant_t Variant){ TestResult_t Result; STAVFS_Handle_t DiskHandle;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -