Skip to content

Commit 24a89bf

Browse files
Update Os File UTs (#3619)
* bugfixes to uts. the old way was generating out of range offsets * bugfixes * adding more helpful messages and bugfix to ut for when mode is OPEN_APPEND * removed unnecessary prints * ci fixes and updates based on review * fix for static analyzer * sp * should appease ci * append moves the file pointer before write not on open * typo * cleaned up code * open with append will still have the state position to 0
1 parent 2542b60 commit 24a89bf

File tree

8 files changed

+90
-11
lines changed

8 files changed

+90
-11
lines changed

Os/test/ut/directory/DirectoryRules.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ bool Os::Test::Directory::Tester::Open::precondition(const Os::Test::Directory::
2222

2323
void Os::Test::Directory::Tester::Open::action(Os::Test::Directory::Tester &state) {
2424
Os::Directory::OpenMode mode = STest::Pick::lowerUpper(0, 1) == 1 ? Os::Directory::READ : Os::Directory::CREATE_IF_MISSING;
25+
printf("--> Rule: %s pathname %s mode %d\n", this->getName(), state.m_path.c_str(), mode);
2526
Os::Directory::Status status = state.m_directory.open(state.m_path.c_str(), mode);
2627
ASSERT_EQ(status, Os::Directory::Status::OP_OK);
2728
state.m_state = Os::Test::Directory::Tester::DirectoryState::OPEN;
@@ -40,6 +41,7 @@ bool Os::Test::Directory::Tester::OpenAlreadyExistsError::precondition(const Os:
4041
}
4142

4243
void Os::Test::Directory::Tester::OpenAlreadyExistsError::action(Os::Test::Directory::Tester &state) {
44+
printf("--> Rule: %s pathname %s\n", this->getName(), state.m_path.c_str());
4345
Os::Directory new_directory;
4446
Os::Directory::Status status = new_directory.open(state.m_path.c_str(), Os::Directory::OpenMode::CREATE_EXCLUSIVE);
4547
ASSERT_EQ(status, Os::Directory::Status::ALREADY_EXISTS);
@@ -100,8 +102,9 @@ bool Os::Test::Directory::Tester::ReadOneFile::precondition(const Os::Test::Dire
100102
}
101103

102104
void Os::Test::Directory::Tester::ReadOneFile::action(Os::Test::Directory::Tester &state) {
105+
printf("--> Rule: %s\n", this->getName());
103106
ASSERT_TRUE(state.m_directory.isOpen());
104-
char filename[100];
107+
char filename[100] = {0};
105108
Os::Directory::Status status = state.m_directory.read(filename, 100);
106109
// If seek is at the end of the directory, expect NO_MORE_FILES - otherwise expect normal read and valid filename
107110
if (state.m_seek_position < static_cast<FwIndexType>(state.m_filenames.size())) {

Os/test/ut/file/CommonTests.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,31 @@ TEST_F(Functionality, WriteInvalidModes) {
181181
write_rule.apply(*tester);
182182
}
183183

184+
// Ensure a write followed by a read produces valid data
185+
TEST_F(FunctionalIO, WriteSeekAppendReadBack) {
186+
Os::Test::File::Tester::OpenFileCreate open_rule(false);
187+
Os::Test::File::Tester::OpenForAppend open_append_rule(false);
188+
Os::Test::File::Tester::Write write_rule;
189+
Os::Test::File::Tester::Seek seek_rule;
190+
Os::Test::File::Tester::CloseFile close_rule;
191+
Os::Test::File::Tester::OpenForRead open_read;
192+
Os::Test::File::Tester::Read read_rule;
193+
194+
open_rule.apply(*tester);
195+
write_rule.apply(*tester);
196+
close_rule.apply(*tester);
197+
open_read.apply(*tester);
198+
close_rule.apply(*tester);
199+
open_append_rule.apply(*tester);
200+
write_rule.apply(*tester);
201+
seek_rule.apply(*tester);
202+
write_rule.apply(*tester);
203+
close_rule.apply(*tester);
204+
open_read.apply(*tester);
205+
read_rule.apply(*tester);
206+
close_rule.apply(*tester);
207+
}
208+
184209
// Ensure a write followed by a read produces valid data
185210
TEST_F(FunctionalIO, WriteReadBack) {
186211
Os::Test::File::Tester::OpenFileCreate open_rule(false);

Os/test/ut/file/FileRules.cpp

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ void Os::Test::File::Tester::assert_file_opened(const std::string &path, Os::Fil
171171
// When the open mode has been specified assert that is in an exact state
172172
if (not path.empty() && Os::File::Mode::OPEN_NO_MODE != newly_opened_mode) {
173173
// Assert file pointer always at beginning when functional
174-
if (functional() ) {
174+
if (functional()) {
175175
FwSizeType file_position = std::numeric_limits<FwSizeType>::max();
176176
ASSERT_EQ(this->m_file.position(file_position), Os::File::Status::OP_OK);
177177
ASSERT_EQ(file_position, 0);
@@ -258,7 +258,7 @@ bool Os::Test::File::Tester::OpenBaseRule::precondition(const Os::Test::File::Te
258258

259259
void Os::Test::File::Tester::OpenBaseRule::action(Os::Test::File::Tester &state //!< The test state
260260
) {
261-
printf("--> Rule: %s \n", this->getName());
261+
printf("--> Rule: %s mode %d\n", this->getName(), this->m_mode);
262262
// Initial variables used for this test
263263
std::shared_ptr<const std::string> filename = state.get_filename(this->m_random);
264264

@@ -322,6 +322,25 @@ Os::Test::File::Tester::OpenForWrite::OpenForWrite(const bool randomize_filename
322322

323323
}
324324

325+
// ------------------------------------------------------------------------------------------------------
326+
// Rule: OpenForAppend
327+
//
328+
// ------------------------------------------------------------------------------------------------------
329+
330+
Os::Test::File::Tester::OpenForAppend::OpenForAppend(const bool randomize_filename)
331+
: Os::Test::File::Tester::OpenBaseRule("OpenForAppend",
332+
// Randomized write mode
333+
Os::File::Mode::OPEN_APPEND,
334+
// Randomized overwrite
335+
false,
336+
randomize_filename) {
337+
// Ensures that a random write mode will work correctly
338+
static_assert((Os::File::Mode::OPEN_SYNC_WRITE - 1) == Os::File::Mode::OPEN_WRITE, "Write modes not contiguous");
339+
static_assert((Os::File::Mode::OPEN_APPEND - 1) == Os::File::Mode::OPEN_SYNC_WRITE,
340+
"Write modes not contiguous");
341+
342+
}
343+
325344
// ------------------------------------------------------------------------------------------------------
326345
// Rule: OpenForRead
327346
//
@@ -419,7 +438,12 @@ void Os::Test::File::Tester::Write::action(
419438
printf("--> Rule: %s \n", this->getName());
420439
U8 buffer[FILE_DATA_MAXIMUM];
421440
state.assert_file_consistent();
422-
FwSizeType size_desired = static_cast<FwSizeType>(STest::Pick::lowerUpper(0, FILE_DATA_MAXIMUM));
441+
FwSizeType current_position = 0;
442+
state.m_file.position(current_position);
443+
if(state.m_mode == Os::File::Mode::OPEN_APPEND) {
444+
state.m_file.size(current_position);
445+
}
446+
FwSizeType size_desired = static_cast<FwSizeType>(STest::Pick::lowerUpper(0, FILE_DATA_MAXIMUM - current_position));
423447
FwSizeType size_written = size_desired;
424448
bool wait = static_cast<bool>(STest::Pick::lowerUpper(0, 1));
425449
for (FwSizeType i = 0; i < size_desired; i++) {
@@ -435,7 +459,7 @@ void Os::Test::File::Tester::Write::action(
435459
}
436460

437461
// ------------------------------------------------------------------------------------------------------
438-
// Rule: Read
462+
// Rule: Seek
439463
//
440464
// ------------------------------------------------------------------------------------------------------
441465

@@ -464,7 +488,8 @@ void Os::Test::File::Tester::Seek::action(
464488
if (absolute) {
465489
seek_offset = STest::Pick::lowerUpper(0, FILE_DATA_MAXIMUM);
466490
} else {
467-
seek_offset = STest::Pick::lowerUpper(0, original_file_state.position + FILE_DATA_MAXIMUM) - original_file_state.position;
491+
seek_offset = STest::Pick::lowerUpper(0, FILE_DATA_MAXIMUM);
492+
seek_offset -= original_file_state.position;
468493
}
469494
Os::File::Status status = state.m_file.seek(seek_offset, absolute ? Os::File::SeekType::ABSOLUTE : Os::File::SeekType::RELATIVE);
470495
ASSERT_EQ(status, Os::File::Status::OP_OK);
@@ -496,8 +521,8 @@ void Os::Test::File::Tester::Preallocate::action(
496521
printf("--> Rule: %s \n", this->getName());
497522
state.assert_file_consistent();
498523
FileState original_file_state = state.current_file_state();
499-
FwSizeType offset = static_cast<FwSizeType>(STest::Pick::lowerUpper(0, FILE_DATA_MAXIMUM));
500-
FwSizeType length = static_cast<FwSizeType>(STest::Pick::lowerUpper(1, FILE_DATA_MAXIMUM));
524+
FwSizeType offset = static_cast<FwSizeType>(STest::Pick::lowerUpper(0, FILE_DATA_MAXIMUM - 1));
525+
FwSizeType length = static_cast<FwSizeType>(STest::Pick::lowerUpper(1, FILE_DATA_MAXIMUM - offset));
501526
Os::File::Status status = state.m_file.preallocate(offset, length);
502527
ASSERT_EQ(Os::File::Status::OP_OK, status);
503528
state.shadow_preallocate(offset, length);

Os/test/ut/file/FileRules.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ struct OpenFileCreateOverwrite : public OpenBaseRule {
5858
};
5959

6060

61+
// ------------------------------------------------------------------------------------------------------
62+
// Rule: OpenForAppend
63+
//
64+
// ------------------------------------------------------------------------------------------------------
65+
struct OpenForAppend : public OpenBaseRule {
66+
// ----------------------------------------------------------------------
67+
// Construction
68+
// ----------------------------------------------------------------------
69+
70+
//! Constructor
71+
explicit OpenForAppend(const bool randomize_filename = false);
72+
};
73+
6174
// ------------------------------------------------------------------------------------------------------
6275
// Rule: OpenForWrite
6376
//

Os/test/ut/file/SyntheticFileSystem.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
namespace Os {
99
namespace Test {
1010

11+
SyntheticFile::~SyntheticFile() {
12+
this->close();
13+
}
14+
1115
SyntheticFileSystem::OpenData SyntheticFileSystem::open(const CHAR* char_path, const Os::File::Mode open_mode, const File::OverwriteType overwrite) {
1216
SyntheticFileSystem::OpenData return_value;
1317
std::string path = char_path;
@@ -36,7 +40,9 @@ SyntheticFileSystem::OpenData SyntheticFileSystem::open(const CHAR* char_path, c
3640
if (truncate) {
3741
return_value.file->m_data.clear();
3842
}
43+
3944
return_value.file->m_pointer = 0;
45+
4046
return_value.file->m_mode = open_mode;
4147
return_value.file->m_path = path;
4248
// Checks on the shadow data to ensure consistency
@@ -83,9 +89,11 @@ void SyntheticFile::close() {
8389
if (this->m_data != nullptr) {
8490
this->m_data->m_mode = Os::File::Mode::OPEN_NO_MODE;
8591
this->m_data->m_path.clear();
92+
this->m_data->m_pointer = 0;
8693
// Checks on the shadow data to ensure consistency
8794
FW_ASSERT(this->m_data->m_mode == Os::File::Mode::OPEN_NO_MODE);
8895
FW_ASSERT(this->m_data->m_path.empty());
96+
FW_ASSERT(this->m_data->m_pointer == 0);
8997
}
9098
}
9199

Os/test/ut/file/SyntheticFileSystem.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class SyntheticFile : public FileInterface {
3636
public:
3737
friend class SyntheticFileSystem;
3838

39+
//! \brief Destructor in order to correctly close file
40+
virtual ~SyntheticFile();
41+
3942
//! \brief check if file exists
4043
static bool exists(const CHAR* path);
4144

@@ -59,7 +62,7 @@ class SyntheticFile : public FileInterface {
5962

6063
//! \brief close the file
6164
//!
62-
void close() override;
65+
void close() final;
6366

6467
//! \brief read data from the file
6568
//!

Os/test/ut/filesystem/FileSystemRules.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ bool Os::Test::FileSystem::Tester::DirectoryExists::precondition(const Os::Test:
5454

5555
void Os::Test::FileSystem::Tester::DirectoryExists::action(Os::Test::FileSystem::Tester &state) {
5656
std::string dirPath = state.get_random_directory().path;
57+
printf("--> Rule: %s directory path %s\n", this->getName(), dirPath.c_str());
5758
ASSERT_TRUE(Os::FileSystem::getSingleton().exists(dirPath.c_str())) << "exists() failed for directory";
5859
}
5960

Svc/FpySequencer/test/ut/FpySequencerTester.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,9 @@ void FpySequencerTester::addStmt(const Fpy::Statement& stmt) {
108108
// if fails, cannot add a new stmt (out of space)
109109
FW_ASSERT(seq.getheader().getstatementCount() < std::numeric_limits<U16>::max());
110110

111-
seq.getstatements()[seq.getheader().getstatementCount()] = stmt;
112-
seq.getheader().setstatementCount(seq.getheader().getstatementCount() + 1);
111+
U16 stateCount = seq.getheader().getstatementCount();
112+
seq.getstatements()[stateCount] = stmt;
113+
seq.getheader().setstatementCount(static_cast<U16>(stateCount + 1));
113114
}
114115

115116
void FpySequencerTester::addCmd(FwOpcodeType opcode) {

0 commit comments

Comments
 (0)