From 81a9be9e30e81f69b483e62394de7e0016895bc8 Mon Sep 17 00:00:00 2001 From: Garth J Watney Date: Tue, 14 Mar 2023 17:14:59 +0000 Subject: [PATCH 1/2] Rework Listings rule for MicroFs unit test --- Os/MicroFs/test/ut/MyRules.cpp | 109 ++++++++++++++------------- Os/MicroFs/test/ut/MyRules.hpp | 19 +++-- Os/MicroFs/test/ut/Tester.cpp | 132 ++++++++++++++++++++------------- Os/MicroFs/test/ut/Tester.hpp | 11 ++- 4 files changed, 161 insertions(+), 110 deletions(-) diff --git a/Os/MicroFs/test/ut/MyRules.cpp b/Os/MicroFs/test/ut/MyRules.cpp index 976a62cc36..6c5247b3fe 100644 --- a/Os/MicroFs/test/ut/MyRules.cpp +++ b/Os/MicroFs/test/ut/MyRules.cpp @@ -81,7 +81,9 @@ { printf("--> Rule: %s %s\n", this->name, this->filename); - Os::File::Status stat = state.f.open(this->filename, Os::File::OPEN_CREATE); + I16 descIndex = state.getIndex(this->filename); + ASSERT_NE(descIndex, -1); + Os::File::Status stat = state.fileDesc[descIndex].open(this->filename, Os::File::OPEN_CREATE); ASSERT_EQ(Os::File::OP_OK, stat); } @@ -127,11 +129,13 @@ // // ------------------------------------------------------------------------------------------------------ - Os::Tester::WriteData::WriteData(NATIVE_INT_TYPE size, U8 value) : + Os::Tester::WriteData::WriteData(const char *filename, NATIVE_INT_TYPE size, U8 value) : STest::Rule("WriteData") { this->size = size; this->value = value; + this->filename = filename; + } @@ -149,23 +153,17 @@ { printf("--> Rule: %s \n", this->name); - ASSERT_LE(state.curPtr + this->size, Tester::BUFFER_SIZE); + I32 descIndex = state.getIndex(this->filename); + ASSERT_NE(descIndex, -1); + + ASSERT_LE(state.curPtr + this->size, Tester::FILE_SIZE); memset(state.buffOut + state.curPtr, this->value, this->size); NATIVE_INT_TYPE retSize = this->size; - Os::File::Status stat = state.f.write(state.buffOut + state.curPtr, retSize); + Os::File::Status stat = state.fileDesc[descIndex].write(state.buffOut + state.curPtr, retSize); state.curPtr = state.curPtr + this->size; ASSERT_EQ(stat, Os::File::OP_OK); ASSERT_EQ(retSize, this->size); - #if 0 - for (U16 i=0; i("ReadData") { this->size = size; + this->filename = filename; } @@ -198,17 +197,20 @@ { printf("--> Rule: %s \n", this->name); + I32 descIndex = state.getIndex(this->filename); + ASSERT_NE(descIndex, -1); + BYTE buffIn[state.testCfg.bins[0].fileSize]; NATIVE_INT_TYPE bufferSize = sizeof(buffIn); memset(buffIn,0xA5,sizeof(buffIn)); ASSERT_LE(this->size, sizeof(buffIn)); NATIVE_INT_TYPE retSize = this->size; - Os::File::Status stat = state.f.read(buffIn, retSize); + Os::File::Status stat = state.fileDesc[descIndex].read(buffIn, retSize); ASSERT_EQ(stat, Os::File::OP_OK); ASSERT_EQ(retSize, this->size); // Check the returned data - ASSERT_LE(state.curPtr + this->size, Tester::BUFFER_SIZE); + ASSERT_LE(state.curPtr + this->size, Tester::FILE_SIZE); ASSERT_EQ(0,memcmp(buffIn, state.buffOut+state.curPtr, this->size)); } @@ -222,9 +224,10 @@ // // ------------------------------------------------------------------------------------------------------ - Os::Tester::ResetFile::ResetFile() : + Os::Tester::ResetFile::ResetFile(const char *filename) : STest::Rule("ResetFile") { + this->filename = filename; } @@ -242,8 +245,11 @@ { printf("--> Rule: %s \n", this->name); + I32 descIndex = state.getIndex(this->filename); + ASSERT_NE(descIndex, -1); + // seek back to beginning - ASSERT_EQ(Os::File::OP_OK, state.f.seek(0)); + ASSERT_EQ(Os::File::OP_OK, state.fileDesc[descIndex].seek(0)); state.curPtr = 0; } @@ -256,9 +262,10 @@ // // ------------------------------------------------------------------------------------------------------ - Os::Tester::CloseFile::CloseFile() : + Os::Tester::CloseFile::CloseFile(const char *filename) : STest::Rule("CloseFile") { + this->filename = filename; } @@ -277,7 +284,9 @@ printf("--> Rule: %s \n", this->name); // close file - state.f.close(); + I32 descIndex = state.getIndex(this->filename); + ASSERT_NE(descIndex, -1); + state.fileDesc[descIndex].close(); } @@ -289,9 +298,11 @@ // // ------------------------------------------------------------------------------------------------------ - Os::Tester::Listings::Listings() : + Os::Tester::Listings::Listings(U16 numBins, U16 numFiles) : STest::Rule("Listings") { + this->numBins = numBins; + this->numFiles = numFiles; } @@ -311,43 +322,39 @@ Fw::String listDir; Fw::String expectedFile; - Fw::String files[1]; - U32 numFiles = 10; // oversize to check return + Fw::String bins[MAX_BINS]; + Fw::String files[MAX_FILES_PER_BIN]; + U32 totalBins = MAX_BINS + 1; // oversize to check return COMMENT("Listing /"); listDir = "/"; // get root directory listing ASSERT_EQ(Os::FileSystem::OP_OK, - Os::FileSystem::readDirectory(listDir.toChar(),1, files, numFiles)); - ASSERT_EQ(1,numFiles); - - expectedFile.format("/%s0", MICROFS_BIN_STRING); - ASSERT_EQ(0,strcmp(expectedFile.toChar(), files[0].toChar())); - - // get file listing - listDir.format("/%s0",MICROFS_BIN_STRING); - Fw::String msg; - msg.format("Listing %s",listDir.toChar()); - COMMENT(msg.toChar()); - numFiles = 10; - - ASSERT_EQ(Os::FileSystem::OP_OK, - Os::FileSystem::readDirectory(listDir.toChar(),1, files, numFiles)); - - expectedFile.format("/%s0/%s0",MICROFS_BIN_STRING,MICROFS_FILE_STRING); - printf("%s\n", expectedFile.toChar()); + Os::FileSystem::readDirectory(listDir.toChar(), MAX_BINS, bins, totalBins)); + ASSERT_EQ(this->numBins, totalBins); - ASSERT_EQ(0,strcmp(expectedFile.toChar(),files[0].toChar())); - - // check nonexistent bin - - listDir.format("/%s1",MICROFS_BIN_STRING); - msg.format("Listing nonexistent %s",listDir.toChar()); - COMMENT(msg.toChar()); - numFiles = 10; + for (U16 binIndex=0; binIndex < this->numBins; binIndex++) + { + printf("%s\n", bins[binIndex].toChar()); + expectedFile.format("/%s%d", MICROFS_BIN_STRING, binIndex); + ASSERT_EQ(0,strcmp(expectedFile.toChar(), bins[binIndex].toChar())); + } - ASSERT_EQ(Os::FileSystem::INVALID_PATH, - Os::FileSystem::readDirectory(listDir.toChar(),1, files, numFiles)); + U32 totalFiles = MAX_FILES_PER_BIN + 1; // oversize to check return + for (U16 binIndex=0; binIndex < this->numBins; binIndex++) + { + // get file listing + ASSERT_EQ(Os::FileSystem::OP_OK, + Os::FileSystem::readDirectory(bins[binIndex].toChar(), this->numBins, files, totalFiles)); + ASSERT_EQ(this->numFiles, totalFiles); + COMMENT(bins[binIndex].toChar()); + for (U16 fileIndex=0; fileIndex < this->numFiles; fileIndex++) + { + printf("%s\n", files[fileIndex].toChar()); + expectedFile.format("/%s%d/%s%d", MICROFS_BIN_STRING, binIndex, MICROFS_FILE_STRING, fileIndex); + ASSERT_EQ(0,strcmp(expectedFile.toChar(), files[fileIndex].toChar())); + } + } } diff --git a/Os/MicroFs/test/ut/MyRules.hpp b/Os/MicroFs/test/ut/MyRules.hpp index 06c2b222ed..cc657004b5 100644 --- a/Os/MicroFs/test/ut/MyRules.hpp +++ b/Os/MicroFs/test/ut/MyRules.hpp @@ -118,7 +118,7 @@ // ---------------------------------------------------------------------- //! Constructor - WriteData(NATIVE_INT_TYPE size, U8 value); + WriteData(const char *filename, NATIVE_INT_TYPE size, U8 value); // ---------------------------------------------------------------------- // Public member functions @@ -136,6 +136,7 @@ NATIVE_INT_TYPE size; U8 value; + const char* filename; }; @@ -154,7 +155,7 @@ // ---------------------------------------------------------------------- //! Constructor - ReadData(NATIVE_INT_TYPE size); + ReadData(const char *filename, NATIVE_INT_TYPE size); // ---------------------------------------------------------------------- // Public member functions @@ -171,6 +172,7 @@ ); NATIVE_INT_TYPE size; + const char *filename; }; @@ -189,7 +191,7 @@ // ---------------------------------------------------------------------- //! Constructor - ResetFile(); + ResetFile(const char *filename); // ---------------------------------------------------------------------- // Public member functions @@ -205,6 +207,8 @@ Os::Tester& state //!< The test state ); + const char *filename; + }; @@ -222,7 +226,7 @@ // ---------------------------------------------------------------------- //! Constructor - CloseFile(); + CloseFile(const char *filename); // ---------------------------------------------------------------------- // Public member functions @@ -238,6 +242,8 @@ Os::Tester& state //!< The test state ); + const char *filename; + }; @@ -255,7 +261,7 @@ // ---------------------------------------------------------------------- //! Constructor - Listings(); + Listings(U16 numBins, U16 numFiles); // ---------------------------------------------------------------------- // Public member functions @@ -271,6 +277,9 @@ Os::Tester& state //!< The test state ); + U16 numBins; + U16 numFiles; + }; \ No newline at end of file diff --git a/Os/MicroFs/test/ut/Tester.cpp b/Os/MicroFs/test/ut/Tester.cpp index e7f26009f5..e8b38b6943 100644 --- a/Os/MicroFs/test/ut/Tester.cpp +++ b/Os/MicroFs/test/ut/Tester.cpp @@ -33,23 +33,23 @@ namespace Os { void Tester :: OpenStressTest() { - const U16 NUMBER_BINS = 10; - const U16 NUMBER_FILES = 10; - const U16 FILE_SIZE = 100; - const U16 TOTAL_FILES = NUMBER_BINS * NUMBER_FILES; + const U16 NumberBins = MAX_BINS; + const U16 NumberFiles = MAX_FILES_PER_BIN; + + const U16 TotalFiles = NumberBins * NumberFiles; clearFileBuffer(); // Instantiate the Rules - OpenFile* openFile[TOTAL_FILES]; - InitFileSystem initFileSystem(NUMBER_BINS, FILE_SIZE, NUMBER_FILES); + OpenFile* openFile[TotalFiles]; + InitFileSystem initFileSystem(NumberBins, FILE_SIZE, NumberFiles); Cleanup cleanup; - char fileName[TOTAL_FILES][20]; + char fileName[TotalFiles][20]; U16 fileIndex = 0; - for (U16 bin=0; bin < NUMBER_BINS; bin++) + for (U16 bin=0; bin < NumberBins; bin++) { - for (U16 file=0; file < NUMBER_FILES; file++) + for (U16 file=0; file < NumberFiles; file++) { snprintf(fileName[fileIndex], 20, "/bin%d/file%d", bin, file); fileIndex++; @@ -60,7 +60,7 @@ namespace Os { // Run the Rules initFileSystem.apply(*this); - for (U16 i = 0; i < TOTAL_FILES; i++) + for (U16 i = 0; i < TotalFiles; i++) { openFile[i] = new OpenFile(fileName[i]); openFile[i]->apply(*this); @@ -79,10 +79,10 @@ namespace Os { clearFileBuffer(); // Instantiate the Rules - U32 numBins = 1; - U32 fileSize = 100; - U32 numFiles = 1; - InitFileSystem initFileSystem(numBins, fileSize, numFiles); + const U16 NumberBins = MAX_BINS; + const U16 NumberFiles = MAX_FILES_PER_BIN; + + InitFileSystem initFileSystem(NumberBins, FILE_SIZE, NumberFiles); Cleanup cleanup; // Run the Rules @@ -101,15 +101,16 @@ namespace Os { clearFileBuffer(); // Instantiate the Rules - U32 numBins = 1; - U32 fileSize = 100; - U32 numFiles = 1; - InitFileSystem initFileSystem(numBins, fileSize, numFiles); - OpenFile openFile("/bin0/file0"); - ResetFile resetFile; + const U16 NumberBins = MAX_BINS; + const U16 NumberFiles = MAX_FILES_PER_BIN; + const char* FileName = "/bin9/file9"; + + InitFileSystem initFileSystem(NumberBins, FILE_SIZE, NumberFiles); + OpenFile openFile(FileName); + ResetFile resetFile(FileName); Cleanup cleanup; - WriteData writeData(fileSize, 0xFF); - ReadData readData(fileSize); + WriteData writeData(FileName, FILE_SIZE, 0xFF); + ReadData readData(FileName, FILE_SIZE); // Run the Rules initFileSystem.apply(*this); @@ -132,16 +133,17 @@ namespace Os { clearFileBuffer(); // Instantiate the Rules - U32 numBins = 1; - U32 fileSize = 100; - U32 numFiles = 1; - InitFileSystem initFileSystem(numBins, fileSize, numFiles); - OpenFile openFile("/bin0/file0"); - ResetFile resetFile; + const U16 NumberBins = MAX_BINS; + const U16 NumberFiles = MAX_FILES_PER_BIN; + const char* FileName = "/bin0/file0"; + + InitFileSystem initFileSystem(NumberBins, FILE_SIZE, NumberFiles); + OpenFile openFile(FileName); + ResetFile resetFile(FileName); Cleanup cleanup; - WriteData writeData1(fileSize/2, 0x11); - WriteData writeData2(fileSize/2, 0x22); - ReadData readData(fileSize); + WriteData writeData1(FileName, FILE_SIZE/2, 0x11); + WriteData writeData2(FileName, FILE_SIZE/2, 0x22); + ReadData readData(FileName, FILE_SIZE); // Run the Rules initFileSystem.apply(*this); @@ -163,15 +165,16 @@ namespace Os { clearFileBuffer(); // Instantiate the Rules - U32 numBins = 1; - U32 fileSize = 100; - U32 numFiles = 1; - InitFileSystem initFileSystem(numBins, fileSize, numFiles); - OpenFile openFile("/bin0/file0"); - ResetFile resetFile; + const U16 NumberBins = MAX_BINS; + const U16 NumberFiles = MAX_FILES_PER_BIN; + const char* FileName = "/bin0/file0"; + + InitFileSystem initFileSystem(NumberBins, FILE_SIZE, NumberFiles); + OpenFile openFile(FileName); + ResetFile resetFile(FileName); Cleanup cleanup; - WriteData writeData(fileSize, 0xFF); - ReadData readData(fileSize/2); + WriteData writeData(FileName, FILE_SIZE, 0xFF); + ReadData readData(FileName, FILE_SIZE/2); // Run the Rules initFileSystem.apply(*this); @@ -194,21 +197,31 @@ namespace Os { clearFileBuffer(); // Instantiate the Rules - U32 numBins = 1; - U32 fileSize = 100; - U32 numFiles = 1; - InitFileSystem initFileSystem(numBins, fileSize, numFiles); - OpenFile openFile("/bin0/file0"); + const U16 NumberBins = 2; + const U16 NumberFiles = 2; + const char* File1 = "/bin0/file0"; + const char* File2 = "/bin0/file1"; + const char* File3 = "/bin1/file0"; + const char* File4 = "/bin1/file1"; + + InitFileSystem initFileSystem(NumberBins, FILE_SIZE, NumberFiles); + OpenFile openFile1(File1); + OpenFile openFile2(File2); + OpenFile openFile3(File3); + OpenFile openFile4(File4); Cleanup cleanup; - WriteData writeData(fileSize, 0xFF); - CloseFile closeFile; - Listings listings; + WriteData writeData(File1, FILE_SIZE, 0xFF); + CloseFile closeFile(File1); + Listings listings(NumberBins, NumberFiles); // Run the Rules initFileSystem.apply(*this); - openFile.apply(*this); - writeData.apply(*this); - closeFile.apply(*this); + openFile1.apply(*this); + openFile2.apply(*this); + openFile3.apply(*this); + openFile4.apply(*this); + //writeData.apply(*this); + //closeFile.apply(*this); listings.apply(*this); cleanup.apply(*this); @@ -219,7 +232,24 @@ namespace Os { void Tester::clearFileBuffer() { this->curPtr = 0; - memset(this->buffOut, 0xA5, BUFFER_SIZE); + memset(this->buffOut, 0xA5, FILE_SIZE); + } + + I16 Tester::getIndex(const char *fileName) + { + const char* filePathSpec = "/bin%d/file%d"; + + FwNativeUIntType binIndex = 0; + FwNativeUIntType fileIndex = 0; + I16 stat = sscanf(fileName, filePathSpec, &binIndex, &fileIndex); + if (stat != 2) + { + return -1; + + } else { + return binIndex * MAX_BINS + fileIndex; + } + } diff --git a/Os/MicroFs/test/ut/Tester.hpp b/Os/MicroFs/test/ut/Tester.hpp index 8594ccd80b..5f255d7eb1 100644 --- a/Os/MicroFs/test/ut/Tester.hpp +++ b/Os/MicroFs/test/ut/Tester.hpp @@ -22,7 +22,10 @@ namespace Os { #include "MyRules.hpp" enum { - BUFFER_SIZE = 100 + MAX_BINS = 10, + MAX_FILES_PER_BIN = 10, + MAX_TOTAL_FILES = MAX_BINS * MAX_FILES_PER_BIN, + FILE_SIZE = 100 }; // ---------------------------------------------------------------------- @@ -41,8 +44,8 @@ namespace Os { Fw::MallocAllocator alloc; Os::MicroFsConfig testCfg; - Os::File f; - BYTE buffOut[BUFFER_SIZE]; + Os::File fileDesc[MAX_TOTAL_FILES]; + BYTE buffOut[FILE_SIZE]; NATIVE_INT_TYPE curPtr; public: @@ -80,6 +83,8 @@ namespace Os { //! void initComponents(); + I16 getIndex(const char *fileName); + private: // ---------------------------------------------------------------------- From 2d7c3e7d0977254d945af67b9b01fc65875c51e2 Mon Sep 17 00:00:00 2001 From: Garth J Watney Date: Tue, 14 Mar 2023 18:31:27 +0000 Subject: [PATCH 2/2] Add OddTest to MicroFs --- Os/MicroFs/test/ut/MicroFsTest.cpp | 9 +++++++ Os/MicroFs/test/ut/MyRules.cpp | 4 +-- Os/MicroFs/test/ut/Tester.cpp | 40 ++++++++++++++++++++++++++++-- Os/MicroFs/test/ut/Tester.hpp | 1 + 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/Os/MicroFs/test/ut/MicroFsTest.cpp b/Os/MicroFs/test/ut/MicroFsTest.cpp index f0afea5631..749664c1b8 100644 --- a/Os/MicroFs/test/ut/MicroFsTest.cpp +++ b/Os/MicroFs/test/ut/MicroFsTest.cpp @@ -7,6 +7,7 @@ #include #include "Tester.hpp" + TEST(Initialization, InitTest) { Os::Tester tester; tester.InitTest(); @@ -37,6 +38,14 @@ TEST(FileOps, OpenStressTest) { tester.OpenStressTest(); } +#if 0 + +TEST(FileOps, OddTest) { + Os::Tester tester; + tester.OddTest(); +} +#endif + int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); diff --git a/Os/MicroFs/test/ut/MyRules.cpp b/Os/MicroFs/test/ut/MyRules.cpp index 6c5247b3fe..196c408b1b 100644 --- a/Os/MicroFs/test/ut/MyRules.cpp +++ b/Os/MicroFs/test/ut/MyRules.cpp @@ -281,7 +281,7 @@ Os::Tester& state //!< The test state ) { - printf("--> Rule: %s \n", this->name); + printf("--> Rule: %s %s\n", this->name, this->filename); // close file I32 descIndex = state.getIndex(this->filename); @@ -346,7 +346,7 @@ { // get file listing ASSERT_EQ(Os::FileSystem::OP_OK, - Os::FileSystem::readDirectory(bins[binIndex].toChar(), this->numBins, files, totalFiles)); + Os::FileSystem::readDirectory(bins[binIndex].toChar(), this->numFiles, files, totalFiles)); ASSERT_EQ(this->numFiles, totalFiles); COMMENT(bins[binIndex].toChar()); for (U16 fileIndex=0; fileIndex < this->numFiles; fileIndex++) diff --git a/Os/MicroFs/test/ut/Tester.cpp b/Os/MicroFs/test/ut/Tester.cpp index e8b38b6943..2bf2584fe9 100644 --- a/Os/MicroFs/test/ut/Tester.cpp +++ b/Os/MicroFs/test/ut/Tester.cpp @@ -26,6 +26,43 @@ namespace Os { // ---------------------------------------------------------------------- // Tests // ---------------------------------------------------------------------- +// ---------------------------------------------------------------------- + // OddTest + // ---------------------------------------------------------------------- + void Tester :: + OddTest() + { + const U16 NumberBins = 1; + const U16 NumberFiles = 2; + + const char* File1 = "/bin0/file0"; + const char* File2 = "/bin0/file2"; + // const char* File3 = "/bin1/file0"; + // const char* File4 = "/bin1/file2"; + + const U16 TotalFiles = NumberBins * NumberFiles; + clearFileBuffer(); + + // Instantiate the Rules + InitFileSystem initFileSystem(NumberBins, FILE_SIZE, NumberFiles); + OpenFile openFile1(File1); + OpenFile openFile2(File2); + // OpenFile openFile3(File3); + // OpenFile openFile4(File4); + Listings listings(NumberBins, NumberFiles); + + Cleanup cleanup; + + // Run the Rules + initFileSystem.apply(*this); + openFile1.apply(*this); + openFile2.apply(*this); + // openFile3.apply(*this); + // openFile4.apply(*this); + listings.apply(*this); + + cleanup.apply(*this); + } // ---------------------------------------------------------------------- // OpenStressTest @@ -67,9 +104,8 @@ namespace Os { } cleanup.apply(*this); - - } + // ---------------------------------------------------------------------- // InitTest // ---------------------------------------------------------------------- diff --git a/Os/MicroFs/test/ut/Tester.hpp b/Os/MicroFs/test/ut/Tester.hpp index 5f255d7eb1..c5e7cda933 100644 --- a/Os/MicroFs/test/ut/Tester.hpp +++ b/Os/MicroFs/test/ut/Tester.hpp @@ -61,6 +61,7 @@ namespace Os { void OpenWriteOnceReadTwiceTest(); void OneFileReadDirectory(); void OpenStressTest(); + void OddTest(); // Helper functions void clearFileBuffer();