⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fs_apivalidation.c

📁 此为整套uC/FS程序
💻 C
📖 第 1 页 / 共 2 页
字号:
  // Mode "a+"
  //
  _Log("Mode 'a+'...");
  //
  // Check if append works
  //
  sString = "34567890";
  NumBytes = strlen(sString);
  r = _WriteFile("test.txt", "a+", sString, NumBytes);
  if (r != 0) {
    _ErrorOut("Failed\n");
  } 
  r = _ReadFile("test.txt", "r", _aBuffer, NumBytes);
  if (r != 0) {
    _ErrorOut("Failed\n");
  }
  if (FS_MEMCMP(_aBuffer, "1234567890", NumBytes) != 0) {
    _ErrorOut("Failed\n");
  }
  _Log("OK\n");
  if (FS_Remove("test.txt")) {
    _Log("Failed to remove file\n");
  }
}

/*********************************************************************
*
*       _FileAPITest
*
*  Description
*    Test different modes for opening a file.
*/
static void _FileAPITest(void)  {
  FS_FILE    * pFile;
  U32          NumBytes;
  int          i;

  //
  //  1. Create a file and write some bytes to it.
  //
  _Log("Simple file test\n");
  _Log("Creating file...");
  pFile = FS_FOpen("File.txt","w+");
  if (pFile == (FS_FILE *)NULL) {
    _ErrorOut("Failed\n");
  }
  _Log("Ok\n");
  _Log("Write some data...");
  NumBytes = FS_Write(pFile, "File.txt", 8);
  if (NumBytes != 8) {
    _ErrorOut("Failed\n");
  }
  _Log("Ok\n");
  //
  //  2. Verify that all data have been written and are correct.
  //
  _Log("Check file pos...");
  //
  // Seek to Pos. 0
  //
  FS_FSeek(pFile, 0, FS_SEEK_SET);
  if (FS_FTell(pFile)) {
    _ErrorOut("Failed\n");
  }
  _Log(".");
  //
  // Seek to Pos. 4 from beginning of the file 
  //
  FS_FSeek(pFile, 4, FS_SEEK_SET);
  if (FS_FTell(pFile) != 4) {
    _ErrorOut("Failed\n");
  }
  _Log(".");
  //
  // Seek to Pos. 200 from beginning of the file 
  //
  FS_FSeek(pFile, 200, FS_SEEK_SET);
  if (FS_FTell(pFile) != 200) {
    _ErrorOut("Failed\n");
  }
  //
  // Seek to end of file
  //
  FS_FSeek(pFile, 0, FS_SEEK_END);
  if (FS_FTell(pFile) != 8) {
    _ErrorOut("Failed\n");
  }
  _Log(".");
  //
  //  Check FS_FEof reports that we have reached end of file.
  //
  if (FS_FEof(pFile) == 0) {
    _ErrorOut("Failed\n");
  }
  _Log(".");
  _Log("Ok\n");
  //
  //  2. Verify that all data have been written and are correct.
  //
  _Log("Read written data back...");
  FS_FSeek(pFile, 0, FS_SEEK_SET);
  NumBytes = FS_Read(pFile, _aBuffer, 8);
  if ((NumBytes != 8) || (FS_MEMCMP(_aBuffer, "File.txt", 8))) {
    _ErrorOut("Failed\n");
  }
  _Log("Ok\n");
  //
  //  3. Write data to file using burst
  //
  _Log("Write Burst test...");
  _FillBuffer();
  NumBytes = FS_Write(pFile, _aBuffer, sizeof(_aBuffer));
  if (NumBytes != sizeof(_aBuffer)) {
    _ErrorOut("Failed\n");
  }
  _Log("Ok\n");
  //
  //  4. Read data from file using burst
  //
  _Log("Read Burst test...");
  FS_FSeek(pFile, 0, FS_SEEK_SET);
  NumBytes = FS_Read(pFile, _aBuffer, sizeof(_aBuffer));
  if (NumBytes != sizeof(_aBuffer)) {
    _ErrorOut("Failed\n");
  }
  if (FS_Remove("File.txt")) {
    _Log("Failed to remove file\n");
  }
  _Log("Ok\n");
  //
  //  4. Create a bunch of files
  //
  _Log("Creating a lot of file in root directory");
  for (i = 0; i < NUM_FILES; i++) {
    char acFileName[20];

    sprintf(acFileName, "file%d.txt", i);
    if (_WriteFile(acFileName, "w", _aBuffer, sizeof(_aBuffer)) == 0) {
      _Log(".");
    } else {
      _ErrorOut("Failed!\n");
    }
  }
  //
  // Remove all files that have been created
  //
  for (i = 0; i < NUM_FILES; i++) {
    char acFileName[20];

    sprintf(acFileName, "file%d.txt", i);
    if (FS_Remove(acFileName)) {
      _Log("Failed to remove file\n");
    }
  }
  _Log("Ok\n");
  _FileOpenModeTest();
  _Log("\n");
}


/*********************************************************************
*
*       _DirAPITestTest
*/
static void _DirAPITestTest(void)  {
  FS_FIND_DATA fd;
  int          r;
  unsigned     i;
  
  _Log("Directory API test\n");
  _Log("Checking FS_MkDir()...");
  if (FS_MkDir("SubDir")) {
    _ErrorOut("Failed\n");
  }
  _Log("Ok\n");
  _Log("Checking FS_CreateDir()...");
  if (FS_CreateDir("SubDir1\\SubDir2\\SubDir3") < 0) {
    _ErrorOut("Failed\n");
  }
  _Log("Ok\n");
  //
  // Verify that FS_FindFirstFile()//FS_FindNextFile() 
  // gets the correct number of entries available.
  //
  _Log("Checking FS_FindFirstFile()/FS_FindNextFile...");
  r = FS_FindFirstFile(&fd, "", _aBuffer, sizeof(_aBuffer));
  if (r == 0) {
    i = 1;
    do {
      i++;
    } while (FS_FindNextFile(&fd));
    if (i > 3) {
     _ErrorOut("Failed\n");
    }
  } else {
    _ErrorOut("Failed\n");
  }
  _Log("Ok\n");
  _Log("Checking FS_RmDir()...");
  //
  // Remove an empty directory
  //
  if (FS_RmDir("SubDir")) {
    _ErrorOut("Failed\n");
  }
  _Log(".");
  //
  // Remove a directory that is not empty, FS_RmDir should indicate an error
  //
  if (FS_RmDir("SubDir1") == 0) {
    _ErrorOut("Failed\n");
  }
  _Log("Ok\n\n");
}

/*********************************************************************
*
*       _PrepareExtTest
*
*  Return value
*    0   - OK.
*    1   - Error, preparation Failed.
*/
static int _PrepareExtTest(void)  {
  int r;

  _FillBuffer();
  //
  // Create a file in root directory
  //
  r = _WriteFile("file.txt", "w", _aBuffer, sizeof(_aBuffer));
  if (r == 0) {
    //
    //  Create a directory in root directory
    //
    r = FS_MkDir("SubDir");
  }
  return r;
}

/*********************************************************************
*
*       _ExtendedAPITest
*/
static void _ExtendedAPITest(void)  {
  FS_FILE * pFile;

  _Log("Extended API test\n");
  _Log("Preparing test...");
  if (_PrepareExtTest()) {
    _ErrorOut("Failed\n");
  }
  _Log("Ok\n");
  //
  //  Extended file operations
  //
  _Log("Checking FS_CopyFile()...");
  if (FS_CopyFile("file.txt", "copy.txt")) {
    _ErrorOut("Failed\n");
  }
  _Log("Ok\n");
  _Log("Checking FS_Move()...");
  FS_Move("File.txt", "SubDir\\File.txt");
  if ((_FileExist("file.txt")) && (_FileExist("SubDir\\File.txt")) == 0) {
    _ErrorOut("Failed\n");
  }
  _Log("Ok\n");
  //
  //  Check deleting a file
  //
  _Log("Checking FS_Remove()...");
  FS_Remove     ("copy.txt");
  if (_FileExist("copy.txt")) {
    _ErrorOut("Failed\n");
  }
  _Log("Ok\n");
  //
  //  Renaming a file
  //
  _Log("Checking FS_Rename()...");
  FS_Rename("SubDir\\File.txt", "Data.txt");
  if ((_FileExist("SubDir\\File.txt")) && ((_FileExist("SubDir\\data.txt")) == 0)) {
    _ErrorOut("Failed\n");
  }
  _Log("Ok\n");
  //
  //  Verify file
  //
  pFile = FS_FOpen ("SubDir\\Data.txt", "r");
  if (FS_Verify(pFile, _aBuffer, sizeof(_aBuffer))) {
    _ErrorOut("Failed\n");
  }
  FS_FClose(pFile);
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/

/*********************************************************************
*
*       MainTask
*/
void MainTask(void);
void MainTask(void) {
  //
  // Initialize file system
  //
  FS_Init();
  //
  // Check if low-level format is required
  //
  FS_FormatLLIfRequired("");
  _FormatTest();
  //
  //
  //
  _FileAPITest();
  //
  //
  //
  _DirAPITestTest();
  //
  //
  //
  _ExtendedAPITest();
  printf("Finished\n");
  while(1);
}

/****** EOF *********************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -