Skip to content

Commit

Permalink
refactor(hal): apply clang format
Browse files Browse the repository at this point in the history
refactor(hal): apply clang format

refactor(hal): apply clang format
  • Loading branch information
Jan committed Feb 13, 2025
1 parent 9522d32 commit afb8fb0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 34 deletions.
11 changes: 6 additions & 5 deletions src/filesystem/FileSystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ void filesystemAddNewFileSystemEntry(filesystemConfiguration_t *filesystemConfig
filesystemConfig->fileSystem[filesystemConfig->numberOfEntries].entry.isConfig = isConfig;

uint32_t numberOfUsedSectors = (size_t)ceilf(
(float)filesystemConfig->fileSystem[filesystemConfig->numberOfEntries].entry.size /
(float)flashGetBytesPerSector(filesystemConfig->flash));

filesystemConfig->fileSystem[filesystemConfig->numberOfEntries].entry.numberOfSectors = numberOfUsedSectors;
(float)filesystemConfig->fileSystem[filesystemConfig->numberOfEntries].entry.size /
(float)flashGetBytesPerSector(filesystemConfig->flash));

filesystemConfig->fileSystem[filesystemConfig->numberOfEntries].entry.numberOfSectors =
numberOfUsedSectors;

// set all used sectors to 0 (used)
if (isConfig == 2) {
Expand All @@ -113,7 +113,8 @@ void filesystemAddNewFileSystemEntry(filesystemConfiguration_t *filesystemConfig
// update number of entries
++filesystemConfig->numberOfEntries;

filesystemConfig->numberOfFreeSectors = filesystemConfig->numberOfFreeSectors - numberOfUsedSectors;
filesystemConfig->numberOfFreeSectors =
filesystemConfig->numberOfFreeSectors - numberOfUsedSectors;
writeFileSystemToFlash(filesystemConfig);
}

Expand Down
75 changes: 46 additions & 29 deletions test/hardware/AutomatedHWTests/TestFileSystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
#include <stdlib.h>

#define paramTest(fn, param) \
void fn##param(void) { \
fn(param); \
}
void fn##param(void) { \
fn(param); \
}

static size_t entrySize = sizeof(fileSystemEntry_t);

Expand Down Expand Up @@ -51,8 +51,8 @@ void testFileSystemExists() {
uint8_t data[entrySize];
data_t entryBuffer = {.data = data, .length = entrySize};

for (int i = filesystemConfig.filesystemStartSector;
i <= filesystemConfig.filesystemEndSector; i++) {
for (int i = filesystemConfig.filesystemStartSector; i <= filesystemConfig.filesystemEndSector;
i++) {
uint32_t sectorStartAddress = i * flashGetBytesPerSector(filesystemConfig.flash);
flashReadData(filesystemConfig.flash, sectorStartAddress, &entryBuffer);

Expand All @@ -63,13 +63,14 @@ void testFileSystemExists() {
}
TEST_ASSERT_EQUAL(true, filesystemExists);
}

void testFileSystemDoesntExist() {
bool filesystemExists = false;
uint8_t data[entrySize];
data_t entryBuffer = {.data = data, .length = entrySize};

for (int i = filesystemConfig.filesystemStartSector;
i <= filesystemConfig.filesystemEndSector; i++) {
for (int i = filesystemConfig.filesystemStartSector; i <= filesystemConfig.filesystemEndSector;
i++) {
uint32_t sectorStartAddress = i * flashGetBytesPerSector(filesystemConfig.flash);
flashReadData(filesystemConfig.flash, sectorStartAddress, &entryBuffer);

Expand All @@ -83,8 +84,10 @@ void testFileSystemDoesntExist() {

void testFindFittingStartSector() {
// test behaviour if file is too large (sectors between start and end of filesystem are blocked)
uint32_t numberOfRequiredBytes = filesystemConfig.filesystemStartSector * flashGetBytesPerSector(filesystemConfig.flash);
uint32_t startSector = filesystemFindFittingStartSector(&filesystemConfig, numberOfRequiredBytes);
uint32_t numberOfRequiredBytes =
filesystemConfig.filesystemStartSector * flashGetBytesPerSector(filesystemConfig.flash);
uint32_t startSector =
filesystemFindFittingStartSector(&filesystemConfig, numberOfRequiredBytes);
TEST_ASSERT_EQUAL(-1, startSector);

// check, that all other sizes fit into an empty flash
Expand All @@ -106,16 +109,17 @@ void testCorrectEntryAfterNewFile() {
// writes mock data to sector
uint32_t sizeOfEntry = 1 * flashGetBytesPerSector(filesystemConfig.flash);
uint8_t testData[10] = {1};
uint32_t startSector =
filesystemFindFittingStartSector(&filesystemConfig, sizeOfEntry);
uint32_t startSector = filesystemFindFittingStartSector(&filesystemConfig, sizeOfEntry);
uint16_t numberOfRequiredPages = sizeOfEntry / flashGetBytesPerPage(&flashConfig);
uint16_t numberOfRequiredSectors = ceil((double)sizeOfEntry / flashGetBytesPerSector(&flashConfig));
uint16_t numberOfRequiredSectors =
ceil((double)sizeOfEntry / flashGetBytesPerSector(&flashConfig));

// mock data should always fit
TEST_ASSERT_NOT_EQUAL(-1, startSector);

for (int i = 0; i < numberOfRequiredPages; i++) {
flashWritePage(&flashConfig, startSector + i * flashGetBytesPerPage(&flashConfig), testData, sizeof(testData));
flashWritePage(&flashConfig, startSector + i * flashGetBytesPerPage(&flashConfig), testData,
sizeof(testData));
}

filesystemAddNewFileSystemEntry(&filesystemConfig, startSector, sizeOfEntry, 1);
Expand All @@ -131,7 +135,8 @@ void testCorrectEntryAfterNewFile() {
// checks if filesystemGet... functions dont return null with correct values
TEST_ASSERT_NOT_EQUAL(NULL, filesystemGetEntryByIndex(&filesystemConfig, 0));
TEST_ASSERT_NOT_EQUAL(NULL, filesystemGetEntryBySector(&filesystemConfig, startSector));
TEST_ASSERT_NOT_EQUAL(NULL, filesystemGetEntryByID(&filesystemConfig, filesystemConfig.fileID -1));
TEST_ASSERT_NOT_EQUAL(NULL,
filesystemGetEntryByID(&filesystemConfig, filesystemConfig.fileID - 1));

// checks if wrong values lead to null return
TEST_ASSERT_EQUAL(NULL, filesystemGetEntryByIndex(&filesystemConfig, 0 + 1));
Expand All @@ -143,12 +148,16 @@ void testCorrectEntryAfterNewFile() {
TEST_ASSERT_EQUAL(101, filesystemConfig.fileID);
TEST_ASSERT_EQUAL(1020, filesystemConfig.nextFileSystemSector);
}

void testMovingFile(uint16_t newSector) {
// move most recent file to given sector
fileSystemEntry_t *oldEntry = filesystemGetEntryByID(&filesystemConfig, filesystemConfig.fileID - 1);
fileSystemEntry_t *oldEntry =
filesystemGetEntryByID(&filesystemConfig, filesystemConfig.fileID - 1);
uint16_t oldStartSector = oldEntry->entry.startSector;
bool moveSuccessful = filesystemMoveFileToSector(&filesystemConfig, filesystemConfig.fileID - 1 ,newSector);
fileSystemEntry_t *newEntry = filesystemGetEntryByID(&filesystemConfig, filesystemConfig.fileID - 1);
bool moveSuccessful =
filesystemMoveFileToSector(&filesystemConfig, filesystemConfig.fileID - 1, newSector);
fileSystemEntry_t *newEntry =
filesystemGetEntryByID(&filesystemConfig, filesystemConfig.fileID - 1);

// moving to empty sector should succeed
TEST_ASSERT_EQUAL(true, moveSuccessful);
Expand All @@ -166,60 +175,67 @@ void testMovingFile(uint16_t newSector) {
TEST_ASSERT_EQUAL(newSector, newEntry->entry.startSector);

// moving a file to protected sector (i.e. filesystem sector) should always fail
moveSuccessful = filesystemMoveFileToSector(&filesystemConfig, filesystemConfig.fileID - 1, filesystemConfig.filesystemStartSector);
moveSuccessful = filesystemMoveFileToSector(&filesystemConfig, filesystemConfig.fileID - 1,
filesystemConfig.filesystemStartSector);
TEST_ASSERT_EQUAL(false, moveSuccessful);
}

void testCorrectEntryAfterDelete(uint16_t id) {
fileSystemEntry_t *entry = filesystemGetEntryByID(&filesystemConfig, id-1);
fileSystemEntry_t *entry = filesystemGetEntryByID(&filesystemConfig, id - 1);
filesystemEraseFileByID(&filesystemConfig, filesystemConfig.fileID - 1);

// test that number of entries is updated
TEST_ASSERT_EQUAL(0, filesystemConfig.numberOfEntries);

// check that all Get... functions behave accordingly when passed non-existent id
TEST_ASSERT_EQUAL(NULL, filesystemGetEntryByIndex(&filesystemConfig, 0));
TEST_ASSERT_EQUAL(NULL, filesystemGetEntryBySector(&filesystemConfig, entry->entry.startSector));
TEST_ASSERT_EQUAL(NULL,
filesystemGetEntryBySector(&filesystemConfig, entry->entry.startSector));
TEST_ASSERT_EQUAL(NULL, filesystemGetEntryByID(&filesystemConfig, entry->entry.id));

// make sure that all sectors of deleted file are cleared
for (int i = 0; i < entry->entry.numberOfSectors; i++) {
TEST_ASSERT_EQUAL(1, filesystemConfig.sectorFree[entry->entry.startSector + i]);

}
}

void testCorrectNumberOfFreeSectors(int expectedNumberOfFreeSectors) {
TEST_ASSERT_EQUAL(expectedNumberOfFreeSectors, filesystemConfig.numberOfFreeSectors);
}

void testCorrectNumberOfBlockedSectors(uint16_t startSector) {
// block 2 sectors
uint32_t numberOfRequiredBytes = flashGetBytesPerSector(filesystemConfig.flash);
bool blockSuccessful = filesystemBlockBytesForFPGA(&filesystemConfig, startSector, numberOfRequiredBytes);
bool blockSuccessful =
filesystemBlockBytesForFPGA(&filesystemConfig, startSector, numberOfRequiredBytes);

TEST_ASSERT_EQUAL(true, blockSuccessful);
}

void testBlockSectorsFailsIfSectorIsOccupied(uint16_t startSector) {
// block 2 sectors
uint32_t numberOfRequiredBytes = flashGetBytesPerSector(filesystemConfig.flash);
bool blockSuccessful = filesystemBlockBytesForFPGA(&filesystemConfig, startSector, numberOfRequiredBytes);
bool blockSuccessful =
filesystemBlockBytesForFPGA(&filesystemConfig, startSector, numberOfRequiredBytes);

TEST_ASSERT_EQUAL(false, blockSuccessful);
}

void testCorrectNumberOfBlockedSectorsAfterFree() {
filesystemFreeBlockedFPGASectors(&filesystemConfig);
TEST_ASSERT_EQUAL(0, filesystemConfig.numberOfBlockedSectors);
}

paramTest(testMovingFile, 100)

paramTest(testCorrectNumberOfFreeSectors, 1018)
paramTest(testCorrectNumberOfFreeSectors, 1019)
paramTest(testCorrectNumberOfFreeSectors, 1018) paramTest(testCorrectNumberOfFreeSectors, 1019)

paramTest(testCorrectNumberOfBlockedSectors, 0)
paramTest(testBlockSectorsFailsIfSectorIsOccupied, 0)
paramTest(testCorrectNumberOfBlockedSectors, 0)
paramTest(testBlockSectorsFailsIfSectorIsOccupied, 0)

void setUp() {}
void setUp() {}
void tearDown(){};

void deInit() {
filesystemEraseAllEntries(&filesystemConfig);
rom_reset_usb_boot(0, 0);
Expand Down Expand Up @@ -255,7 +271,8 @@ int main() {
// test behaviour of free blocked sectors
RUN_TEST(testCorrectNumberOfBlockedSectorsAfterFree);

// check that no filesystem exists after all entries are deleted and all blocked sectors are free
// check that no filesystem exists after all entries are deleted and all blocked sectors are
// free
RUN_TEST(testFileSystemDoesntExist);

UNITY_END();
Expand Down

0 comments on commit afb8fb0

Please sign in to comment.