Skip to content

Commit 2697913

Browse files
author
Cruz Monrreal
authored
Merge pull request #8990 from davidsaada/david_fat_tests_low_mem
Adjust FAT FS tests to low memory boards
2 parents 891427c + 713b470 commit 2697913

File tree

2 files changed

+66
-28
lines changed
  • features/storage/TESTS/filesystem

2 files changed

+66
-28
lines changed

features/storage/TESTS/filesystem/fat_filesystem/main.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,26 @@ using namespace utest::v1;
2929
#error [NOT_SUPPORTED] Filesystem tests not supported by default
3030
#endif
3131

32+
static const int mem_alloc_threshold = 32 * 1024;
33+
3234
// Test block device
3335
#define BLOCK_SIZE 512
34-
HeapBlockDevice bd(128 * BLOCK_SIZE, BLOCK_SIZE);
36+
#define BLOCK_COUNT 128
37+
HeapBlockDevice *bd = 0;
3538

3639

3740
// Test formatting
3841
void test_format()
3942
{
40-
int err = FATFileSystem::format(&bd);
43+
uint8_t *dummy = new (std::nothrow) uint8_t[mem_alloc_threshold];
44+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough heap memory to run test. Test skipped.");
45+
46+
delete[] dummy;
47+
48+
bd = new (std::nothrow) HeapBlockDevice(BLOCK_COUNT * BLOCK_SIZE, BLOCK_SIZE);
49+
TEST_SKIP_UNLESS_MESSAGE(bd, "Not enough heap memory to run test. Test skipped.");
50+
51+
int err = FATFileSystem::format(bd);
4152
TEST_ASSERT_EQUAL(0, err);
4253
}
4354

@@ -46,13 +57,15 @@ void test_format()
4657
template <ssize_t TEST_SIZE>
4758
void test_read_write()
4859
{
60+
TEST_SKIP_UNLESS_MESSAGE(bd, "Not enough heap memory to run test. Test skipped.");
61+
4962
FATFileSystem fs("fat");
5063

51-
int err = fs.mount(&bd);
64+
int err = fs.mount(bd);
5265
TEST_ASSERT_EQUAL(0, err);
5366

54-
uint8_t *buffer = (uint8_t *)malloc(TEST_SIZE);
55-
TEST_ASSERT(buffer);
67+
uint8_t *buffer = new (std::nothrow) uint8_t[TEST_SIZE];
68+
TEST_SKIP_UNLESS_MESSAGE(buffer, "Not enough heap memory to run test. Test skipped.");
5669

5770
// Fill with random sequence
5871
srand(1);
@@ -84,15 +97,19 @@ void test_read_write()
8497

8598
err = fs.unmount();
8699
TEST_ASSERT_EQUAL(0, err);
100+
101+
delete[] buffer;
87102
}
88103

89104

90105
// Simple test for iterating dir entries
91106
void test_read_dir()
92107
{
108+
TEST_SKIP_UNLESS_MESSAGE(bd, "Not enough heap memory to run test. Test skipped.");
109+
93110
FATFileSystem fs("fat");
94111

95-
int err = fs.mount(&bd);
112+
int err = fs.mount(bd);
96113
TEST_ASSERT_EQUAL(0, err);
97114

98115
err = fs.mkdir("test_read_dir", S_IRWXU | S_IRWXG | S_IRWXO);
@@ -146,6 +163,9 @@ void test_read_dir()
146163

147164
err = fs.unmount();
148165
TEST_ASSERT_EQUAL(0, err);
166+
167+
delete bd;
168+
bd = 0;
149169
}
150170

151171

features/storage/TESTS/filesystem/multipart_fat_filesystem/main.cpp

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,36 @@ using namespace utest::v1;
3030
#error [NOT_SUPPORTED] Filesystem tests not supported by default
3131
#endif
3232

33+
static const int mem_alloc_threshold = 32 * 1024;
34+
3335
// Test block device
3436
#define BLOCK_SIZE 512
3537
#define BLOCK_COUNT 512
36-
HeapBlockDevice bd(BLOCK_COUNT *BLOCK_SIZE, BLOCK_SIZE);
37-
38+
HeapBlockDevice *bd = 0;
3839

3940
// Test formatting and partitioning
4041
void test_format()
4142
{
43+
uint8_t *dummy = new (std::nothrow) uint8_t[mem_alloc_threshold];
44+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough heap memory to run test. Test skipped.");
45+
delete[] dummy;
46+
47+
bd = new (std::nothrow) HeapBlockDevice(BLOCK_COUNT * BLOCK_SIZE, BLOCK_SIZE);
48+
TEST_SKIP_UNLESS_MESSAGE(bd, "Not enough heap memory to run test. Test skipped.");
49+
4250
// Create two partitions splitting device in ~half
43-
int err = MBRBlockDevice::partition(&bd, 1, 0x83, 0, (BLOCK_COUNT / 2) * BLOCK_SIZE);
51+
int err = MBRBlockDevice::partition(bd, 1, 0x83, 0, (BLOCK_COUNT / 2) * BLOCK_SIZE);
4452
TEST_ASSERT_EQUAL(0, err);
4553

46-
err = MBRBlockDevice::partition(&bd, 2, 0x83, -(BLOCK_COUNT / 2) * BLOCK_SIZE);
54+
err = MBRBlockDevice::partition(bd, 2, 0x83, -(BLOCK_COUNT / 2) * BLOCK_SIZE);
4755
TEST_ASSERT_EQUAL(0, err);
4856

4957
// Load both partitions
50-
MBRBlockDevice part1(&bd, 1);
58+
MBRBlockDevice part1(bd, 1);
5159
err = part1.init();
5260
TEST_ASSERT_EQUAL(0, err);
5361

54-
MBRBlockDevice part2(&bd, 2);
62+
MBRBlockDevice part2(bd, 2);
5563
err = part2.init();
5664
TEST_ASSERT_EQUAL(0, err);
5765

@@ -75,12 +83,14 @@ void test_format()
7583
template <ssize_t TEST_SIZE>
7684
void test_read_write()
7785
{
86+
TEST_SKIP_UNLESS_MESSAGE(bd, "Not enough heap memory to run test. Test skipped.");
87+
7888
// Load both partitions
79-
MBRBlockDevice part1(&bd, 1);
89+
MBRBlockDevice part1(bd, 1);
8090
int err = part1.init();
8191
TEST_ASSERT_EQUAL(0, err);
8292

83-
MBRBlockDevice part2(&bd, 2);
93+
MBRBlockDevice part2(bd, 2);
8494
err = part2.init();
8595
TEST_ASSERT_EQUAL(0, err);
8696

@@ -94,11 +104,11 @@ void test_read_write()
94104
err = fs2.mount(&part2);
95105
TEST_ASSERT_EQUAL(0, err);
96106

97-
uint8_t *buffer1 = (uint8_t *)malloc(TEST_SIZE);
98-
TEST_ASSERT(buffer1);
107+
uint8_t *buffer1 = new (std::nothrow) uint8_t[TEST_SIZE];
108+
TEST_SKIP_UNLESS_MESSAGE(buffer1, "Not enough heap memory to run test. Test skipped.");
99109

100-
uint8_t *buffer2 = (uint8_t *)malloc(TEST_SIZE);
101-
TEST_ASSERT(buffer2);
110+
uint8_t *buffer2 = new (std::nothrow) uint8_t[TEST_SIZE];
111+
TEST_SKIP_UNLESS_MESSAGE(buffer2, "Not enough heap memory to run test. Test skipped.");
102112

103113
// Fill with random sequence
104114
srand(1);
@@ -163,47 +173,55 @@ void test_read_write()
163173

164174
err = part2.deinit();
165175
TEST_ASSERT_EQUAL(0, err);
176+
177+
delete[] buffer1;
178+
delete[] buffer2;
166179
}
167180

168181
void test_single_mbr()
169182
{
170-
int err = bd.init();
183+
TEST_SKIP_UNLESS_MESSAGE(bd, "Not enough heap memory to run test. Test skipped.");
184+
185+
int err = bd->init();
171186
TEST_ASSERT_EQUAL(0, err);
172187

173188
const bd_addr_t MBR_OFFSET = 0;
174189
const bd_addr_t FAT1_OFFSET = 1;
175190
const bd_addr_t FAT2_OFFSET = BLOCK_COUNT / 2;
176191

177-
uint8_t *buffer = (uint8_t *)malloc(BLOCK_SIZE);
178-
TEST_ASSERT(buffer);
192+
uint8_t *buffer = new (std::nothrow) uint8_t[BLOCK_SIZE];
193+
TEST_SKIP_UNLESS_MESSAGE(buffer, "Not enough heap memory to run test. Test skipped.");
179194

180195
// Check that all three header blocks have the 0x55aa signature
181-
err = bd.read(buffer, MBR_OFFSET * BLOCK_SIZE, BLOCK_SIZE);
196+
err = bd->read(buffer, MBR_OFFSET * BLOCK_SIZE, BLOCK_SIZE);
182197
TEST_ASSERT_EQUAL(0, err);
183198
TEST_ASSERT(memcmp(&buffer[BLOCK_SIZE - 2], "\x55\xaa", 2) == 0);
184199

185-
err = bd.read(buffer, FAT1_OFFSET * BLOCK_SIZE, BLOCK_SIZE);
200+
err = bd->read(buffer, FAT1_OFFSET * BLOCK_SIZE, BLOCK_SIZE);
186201
TEST_ASSERT_EQUAL(0, err);
187202
TEST_ASSERT(memcmp(&buffer[BLOCK_SIZE - 2], "\x55\xaa", 2) == 0);
188203

189-
err = bd.read(buffer, FAT2_OFFSET * BLOCK_SIZE, BLOCK_SIZE);
204+
err = bd->read(buffer, FAT2_OFFSET * BLOCK_SIZE, BLOCK_SIZE);
190205
TEST_ASSERT_EQUAL(0, err);
191206
TEST_ASSERT(memcmp(&buffer[BLOCK_SIZE - 2], "\x55\xaa", 2) == 0);
192207

193208
// Check that the headers for both filesystems contain a jump code
194209
// indicating they are actual FAT superblocks and not an extra MBR
195-
err = bd.read(buffer, FAT1_OFFSET * BLOCK_SIZE, BLOCK_SIZE);
210+
err = bd->read(buffer, FAT1_OFFSET * BLOCK_SIZE, BLOCK_SIZE);
196211
TEST_ASSERT_EQUAL(0, err);
197212
TEST_ASSERT(buffer[0] == 0xe9 || buffer[0] == 0xeb || buffer[0] == 0xe8);
198213

199-
err = bd.read(buffer, FAT2_OFFSET * BLOCK_SIZE, BLOCK_SIZE);
214+
err = bd->read(buffer, FAT2_OFFSET * BLOCK_SIZE, BLOCK_SIZE);
200215
TEST_ASSERT_EQUAL(0, err);
201216
TEST_ASSERT(buffer[0] == 0xe9 || buffer[0] == 0xeb || buffer[0] == 0xe8);
202217

203-
free(buffer);
218+
delete[] buffer;
204219

205-
bd.deinit();
220+
bd->deinit();
206221
TEST_ASSERT_EQUAL(0, err);
222+
223+
delete bd;
224+
bd = 0;
207225
}
208226

209227

0 commit comments

Comments
 (0)