Skip to content

Commit 6d9255a

Browse files
committed
Some cleanup on file system test, add reset test
1 parent 266962f commit 6d9255a

File tree

3 files changed

+47
-50
lines changed

3 files changed

+47
-50
lines changed

tests/host/common/spiffs_mock.cpp

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,69 +24,48 @@
2424

2525
extern "C"
2626
{
27-
uint32_t mock_spiffs_phys_addr = 0;
28-
uint32_t mock_spiffs_phys_size = 0;
29-
uint32_t mock_spiffs_phys_page = 0;
30-
uint32_t mock_spiffs_phys_block = 0;
31-
uint8_t* mock_spiffs_phys_data = nullptr;
27+
static uint32_t s_phys_addr = 0;
28+
uint32_t s_phys_size = 0;
29+
uint32_t s_phys_page = 0;
30+
uint32_t s_phys_block = 0;
31+
uint8_t* s_phys_data = nullptr;
3232
}
3333

3434
FS SPIFFS(nullptr);
3535

36-
37-
3836
SpiffsMock::SpiffsMock(size_t fs_size, size_t fs_block, size_t fs_page)
3937
{
4038
m_fs.resize(fs_size, 0xff);
41-
mock_spiffs_phys_addr = 0;
42-
mock_spiffs_phys_size = fs_size;
43-
mock_spiffs_phys_page = fs_page;
44-
mock_spiffs_phys_block = fs_block;
45-
mock_spiffs_phys_data = m_fs.data();
46-
SPIFFS = FS(FSImplPtr(new SPIFFSImpl(0, fs_size, fs_page, fs_block, 5)));
39+
s_phys_addr = 0;
40+
s_phys_size = static_cast<uint32_t>(fs_size);
41+
s_phys_page = static_cast<uint32_t>(fs_page);
42+
s_phys_block = static_cast<uint32_t>(fs_block);
43+
s_phys_data = m_fs.data();
44+
reset();
45+
}
46+
47+
void SpiffsMock::reset()
48+
{
49+
SPIFFS = FS(FSImplPtr(new SPIFFSImpl(0, s_phys_size, s_phys_page, s_phys_block, 5)));
4750
}
4851

4952
SpiffsMock::~SpiffsMock()
5053
{
51-
mock_spiffs_phys_addr = 0;
52-
mock_spiffs_phys_size = 0;
53-
mock_spiffs_phys_page = 0;
54-
mock_spiffs_phys_block = 0;
55-
mock_spiffs_phys_data = nullptr;
54+
s_phys_addr = 0;
55+
s_phys_size = 0;
56+
s_phys_page = 0;
57+
s_phys_block = 0;
58+
s_phys_data = nullptr;
5659
SPIFFS = FS(FSImplPtr(nullptr));
5760
}
5861

59-
/*
60-
spi_flash_read function requires flash address to be aligned on word boundary.
61-
We take care of this by reading first and last words separately and memcpy
62-
relevant bytes into result buffer.
63-
64-
alignment: 012301230123012301230123
65-
bytes requested: -------***********------
66-
read directly: --------xxxxxxxx--------
67-
read pre: ----aaaa----------------
68-
read post: ----------------bbbb----
69-
alignedBegin: ^
70-
alignedEnd: ^
71-
*/
72-
7362
int32_t spiffs_hal_read(uint32_t addr, uint32_t size, uint8_t *dst) {
74-
memcpy(dst, mock_spiffs_phys_data + addr, size);
63+
memcpy(dst, s_phys_data + addr, size);
7564
return SPIFFS_OK;
7665
}
7766

78-
/*
79-
Like spi_flash_read, spi_flash_write has a requirement for flash address to be
80-
aligned. However it also requires RAM address to be aligned as it reads data
81-
in 32-bit words. Flash address (mis-)alignment is handled much the same way
82-
as for reads, but for RAM alignment we have to copy data into a temporary
83-
buffer. The size of this buffer is a tradeoff between number of writes required
84-
and amount of stack required. This is chosen to be 512 bytes here, but might
85-
be adjusted in the future if there are good reasons to do so.
86-
*/
87-
8867
int32_t spiffs_hal_write(uint32_t addr, uint32_t size, uint8_t *src) {
89-
memcpy(mock_spiffs_phys_data + addr, src, size);
68+
memcpy(s_phys_data + addr, src, size);
9069
return SPIFFS_OK;
9170
}
9271

@@ -98,7 +77,7 @@ int32_t spiffs_hal_erase(uint32_t addr, uint32_t size) {
9877
const uint32_t sector = addr / FLASH_SECTOR_SIZE;
9978
const uint32_t sectorCount = size / FLASH_SECTOR_SIZE;
10079
for (uint32_t i = 0; i < sectorCount; ++i) {
101-
memset(mock_spiffs_phys_data + (sector + i) * FLASH_SECTOR_SIZE, 0xff, FLASH_SECTOR_SIZE);
80+
memset(s_phys_data + (sector + i) * FLASH_SECTOR_SIZE, 0xff, FLASH_SECTOR_SIZE);
10281
}
10382
return SPIFFS_OK;
10483
}

tests/host/common/spiffs_mock.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@
2424
class SpiffsMock {
2525
public:
2626
SpiffsMock(size_t fs_size, size_t fs_block, size_t fs_page);
27+
void reset();
2728
~SpiffsMock();
2829

2930
protected:
3031
std::vector<uint8_t> m_fs;
3132
};
3233

34+
#define SPIFFS_MOCK_DECLARE(size_kb, block_kb, page_b) SpiffsMock spiffs_mock(size_kb * 1024, block_kb * 1024, page_b)
35+
#define SPIFFS_MOCK_RESET() spiffs_mock.reset()
36+
3337

3438
#endif /* spiffs_mock_hpp */

tests/host/fs/test_fs.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
#include <FS.h>
1818
#include "../common/spiffs_mock.h"
1919

20-
#define SPIFFS_SIZE (64*1024)
21-
#define SPIFFS_BLOCK (2*4096)
22-
#define SPIFFS_PAGE (512)
23-
#define SPIFFS_MOCK_DECLARE(size_kb, block_kb, page_b) SpiffsMock mock(size_kb * 1024, block_kb * 1024, page_b)
2420

2521
TEST_CASE("FS can begin","[fs]")
2622
{
@@ -45,8 +41,8 @@ TEST_CASE("Files can be written and appended to","[fs]")
4541
REQUIRE(SPIFFS.begin());
4642
{
4743
File f = SPIFFS.open("config1.txt", "w");
48-
f.println("file 1");
4944
REQUIRE(f);
45+
f.println("file 1");
5046
}
5147
{
5248
File f = SPIFFS.open("config1.txt", "a");
@@ -63,4 +59,22 @@ TEST_CASE("Files can be written and appended to","[fs]")
6359
}
6460
}
6561

62+
TEST_CASE("Files persist after reset", "[fs]")
63+
{
64+
SPIFFS_MOCK_DECLARE(1024, 8, 512);
65+
REQUIRE(SPIFFS.begin());
66+
{
67+
File f = SPIFFS.open("config1.txt", "w");
68+
REQUIRE(f);
69+
f.println("file 1");
70+
}
71+
SPIFFS_MOCK_RESET();
72+
REQUIRE(SPIFFS.begin());
73+
{
74+
File f = SPIFFS.open("config1.txt", "r");
75+
REQUIRE(f);
76+
REQUIRE(f.readString() == "file 1\r\n");
77+
}
78+
}
79+
6680

0 commit comments

Comments
 (0)