Skip to content

Commit 36045c6

Browse files
committed
feat(storage/fatfs): Make host_test results easier to understand
1 parent f781970 commit 36045c6

File tree

1 file changed

+42
-88
lines changed

1 file changed

+42
-88
lines changed
Lines changed: 42 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -16,47 +16,39 @@
1616

1717
TEST_CASE("Create volume, open file, write and read back data", "[fatfs]")
1818
{
19-
FRESULT fr_result;
2019
BYTE pdrv;
2120
FATFS fs;
2221
FIL file;
2322
UINT bw;
2423

25-
esp_err_t esp_result;
26-
2724
const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, "storage");
2825

2926
// Mount wear-levelled partition
3027
wl_handle_t wl_handle;
31-
esp_result = wl_mount(partition, &wl_handle);
32-
REQUIRE(esp_result == ESP_OK);
28+
REQUIRE(wl_mount(partition, &wl_handle) == ESP_OK);
3329

3430
// Get a physical drive
35-
esp_result = ff_diskio_get_drive(&pdrv);
36-
REQUIRE(esp_result == ESP_OK);
31+
REQUIRE(ff_diskio_get_drive(&pdrv) == ESP_OK);
3732

3833
// Register physical drive as wear-levelled partition
39-
esp_result = ff_diskio_register_wl_partition(pdrv, wl_handle);
34+
REQUIRE(ff_diskio_register_wl_partition(pdrv, wl_handle) == ESP_OK);
4035

4136
// Create FAT volume on the entire disk
4237
LBA_t part_list[] = {100, 0, 0, 0};
4338
BYTE work_area[FF_MAX_SS];
4439

45-
fr_result = f_fdisk(pdrv, part_list, work_area);
46-
REQUIRE(fr_result == FR_OK);
40+
REQUIRE(f_fdisk(pdrv, part_list, work_area) == FR_OK);
4741

4842
// For host tests, include FM_SFD flag when formatting partitions smaller than 128KB.
4943
// if n_root field of MKFS_PARM is set to 128 => 1 root directory sec and if set to 0(default 512) => 4 root directory sectors.
5044
const MKFS_PARM opt = {(BYTE)(FM_ANY | FM_SFD), 0, 0, 128, 0};
51-
fr_result = f_mkfs("", &opt, work_area, sizeof(work_area)); // Use default volume
45+
REQUIRE(f_mkfs("", &opt, work_area, sizeof(work_area)) == FR_OK); // Use default volume
5246

5347
// Mount the volume
54-
fr_result = f_mount(&fs, "", 0);
55-
REQUIRE(fr_result == FR_OK);
48+
REQUIRE(f_mount(&fs, "", 0) == FR_OK);
5649

5750
// Open, write and read data
58-
fr_result = f_open(&file, "test.txt", FA_OPEN_ALWAYS | FA_READ | FA_WRITE);
59-
REQUIRE(fr_result == FR_OK);
51+
REQUIRE(f_open(&file, "test.txt", FA_OPEN_ALWAYS | FA_READ | FA_WRITE) == FR_OK);
6052

6153
// Generate data
6254
uint32_t data_size = 1000;
@@ -70,72 +62,59 @@ TEST_CASE("Create volume, open file, write and read back data", "[fatfs]")
7062
}
7163

7264
// Write generated data
73-
fr_result = f_write(&file, data, data_size, &bw);
74-
REQUIRE(fr_result == FR_OK);
65+
REQUIRE(f_write(&file, data, data_size, &bw) == FR_OK);
7566
REQUIRE(bw == data_size);
7667

7768
// Move to beginning of file
78-
fr_result = f_lseek(&file, 0);
79-
REQUIRE(fr_result == FR_OK);
69+
REQUIRE(f_lseek(&file, 0) == FR_OK);
8070

8171
// Read written data
82-
fr_result = f_read(&file, read, data_size, &bw);
83-
REQUIRE(fr_result == FR_OK);
72+
REQUIRE(f_read(&file, read, data_size, &bw) == FR_OK);
8473
REQUIRE(bw == data_size);
8574

8675
REQUIRE(memcmp(data, read, data_size) == 0);
8776

8877
// Close file
89-
fr_result = f_close(&file);
90-
REQUIRE(fr_result == FR_OK);
78+
REQUIRE(f_close(&file) == FR_OK);
9179

9280
// Unmount default volume
93-
fr_result = f_mount(0, "", 0);
94-
REQUIRE(fr_result == FR_OK);
81+
REQUIRE(f_mount(0, "", 0) == FR_OK);
9582

9683
// Clear
9784
free(read);
9885
free(data);
9986
ff_diskio_unregister(pdrv);
10087
ff_diskio_clear_pdrv_wl(wl_handle);
101-
esp_result = wl_unmount(wl_handle);
102-
REQUIRE(esp_result == ESP_OK);
88+
REQUIRE(wl_unmount(wl_handle) == ESP_OK);
10389
}
10490

10591
static void prepare_fatfs(const char* partition_label, const esp_partition_t** partition, wl_handle_t* wl_handle, BYTE* pdrv)
10692
{
107-
FRESULT fr_result;
108-
esp_err_t esp_result;
109-
11093
*partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, partition_label);
11194
REQUIRE(partition != NULL);
11295
printf("partition address=0x%x\n", (*partition)->address);
11396
printf("partition size=0x%x\n", (*partition)->size);
11497

11598
// Mount wear-levelled partition
116-
esp_result = wl_mount(*partition, wl_handle);
117-
REQUIRE(esp_result == ESP_OK);
99+
REQUIRE(wl_mount(*partition, wl_handle) == ESP_OK);
118100

119101
// Get a physical drive
120102
BYTE _pdrv;
121-
esp_result = ff_diskio_get_drive(&_pdrv);
122-
REQUIRE(esp_result == ESP_OK);
103+
REQUIRE(ff_diskio_get_drive(&_pdrv) == ESP_OK);
123104
printf("using pdrv=%i\n", _pdrv);
124105
char drv[3] = {(char)('0' + _pdrv), ':', 0};
125106
*pdrv = _pdrv;
126107

127108
// Register physical drive as wear-levelled partition
128-
esp_result = ff_diskio_register_wl_partition(_pdrv, *wl_handle);
109+
REQUIRE(ff_diskio_register_wl_partition(_pdrv, *wl_handle) == ESP_OK);
129110

130111
// Create FAT volume on the entire disk
131112
LBA_t part_list[] = {100, 0, 0, 0};
132113
BYTE work_area[FF_MAX_SS];
133114

134-
fr_result = f_fdisk(_pdrv, part_list, work_area);
135-
REQUIRE(fr_result == FR_OK);
115+
REQUIRE(f_fdisk(_pdrv, part_list, work_area) == FR_OK);
136116
const MKFS_PARM opt = {(BYTE)(FM_ANY | FM_SFD), 0, 0, 128, 0};
137-
fr_result = f_mkfs(drv, &opt, work_area, sizeof(work_area)); // Use default volume
138-
REQUIRE(fr_result == FR_OK);
117+
REQUIRE(f_mkfs(drv, &opt, work_area, sizeof(work_area)) == FR_OK); // Use default volume
139118
}
140119

141120
/*
@@ -146,9 +125,6 @@ static void prepare_fatfs(const char* partition_label, const esp_partition_t** p
146125
*/
147126
TEST_CASE("Test mounting 2 volumes, writing data and formatting the 2nd one, reading data", "[fatfs]")
148127
{
149-
FRESULT fr_result;
150-
esp_err_t esp_result;
151-
152128
const char* partition_label0 = "storage";
153129
const esp_partition_t *partition0 = NULL;
154130
BYTE pdrv0 = UINT8_MAX;
@@ -170,27 +146,22 @@ TEST_CASE("Test mounting 2 volumes, writing data and formatting the 2nd one, rea
170146
REQUIRE(wl_handle0 != WL_INVALID_HANDLE);
171147
REQUIRE(pdrv0 == 0);
172148
char drv0[3] = {(char)('0' + pdrv0), ':', 0};
173-
fr_result = f_mount(&fs0, drv0, 0);
174-
REQUIRE(fr_result == FR_OK);
149+
REQUIRE(f_mount(&fs0, drv0, 0) == FR_OK);
175150

176151
// Open file and write data
177152
FIL file0;
178153
UINT bw0;
179-
fr_result = f_open(&file0, "0:/test0.txt", FA_OPEN_ALWAYS | FA_WRITE);
180-
REQUIRE(fr_result == FR_OK);
154+
REQUIRE(f_open(&file0, "0:/test0.txt", FA_OPEN_ALWAYS | FA_WRITE) == FR_OK);
181155
// Write data
182156
const char *data0 = "123456789";
183157
char read0[10] = {0};
184-
fr_result = f_write(&file0, data0, data_size, &bw0);
185-
REQUIRE(fr_result == FR_OK);
158+
REQUIRE(f_write(&file0, data0, data_size, &bw0) == FR_OK);
186159
REQUIRE(bw0 == data_size);
187160
// Close file
188-
fr_result = f_close(&file0);
189-
REQUIRE(fr_result == FR_OK);
161+
REQUIRE(f_close(&file0) == FR_OK);
190162

191163
// Unmount volume 0
192-
fr_result = f_mount(0, drv0, 0);
193-
REQUIRE(fr_result == FR_OK);
164+
REQUIRE(f_mount(0, drv0, 0) == FR_OK);
194165

195166

196167
// Mount the volume 1
@@ -199,89 +170,72 @@ TEST_CASE("Test mounting 2 volumes, writing data and formatting the 2nd one, rea
199170
REQUIRE(wl_handle1 != WL_INVALID_HANDLE);
200171
REQUIRE(pdrv1 == 1);
201172
char drv1[3] = {(char)('0' + pdrv1), ':', 0};
202-
fr_result = f_mount(&fs1, drv1, 0);
203-
REQUIRE(fr_result == FR_OK);
173+
REQUIRE(f_mount(&fs1, drv1, 0) == FR_OK);
204174

205175
// Open file and write data
206176
FIL file1;
207177
UINT bw1;
208-
fr_result = f_open(&file1, "1:/test1.txt", FA_OPEN_ALWAYS | FA_WRITE);
209-
REQUIRE(fr_result == FR_OK);
178+
REQUIRE(f_open(&file1, "1:/test1.txt", FA_OPEN_ALWAYS | FA_WRITE) == FR_OK);
210179
// Write data
211180
const char* data1 = "987654321";
212181
char read1[10] = {0};
213-
fr_result = f_write(&file1, data1, data_size, &bw1);
214-
REQUIRE(fr_result == FR_OK);
182+
REQUIRE(f_write(&file1, data1, data_size, &bw1) == FR_OK);
215183
REQUIRE(bw1 == data_size);
216184
// Close file
217-
fr_result = f_close(&file1);
218-
REQUIRE(fr_result == FR_OK);
185+
REQUIRE(f_close(&file1) == FR_OK);
219186

220187
// Unmount volume 1
221-
fr_result = f_mount(0, drv1, 0);
222-
REQUIRE(fr_result == FR_OK);
188+
REQUIRE(f_mount(0, drv1, 0) == FR_OK);
223189

224190
// Format the volume 1
225191
const size_t workbuf_size = 4096;
226192
void *workbuf = ff_memalloc(workbuf_size);
227193
REQUIRE(workbuf != NULL);
228194
const MKFS_PARM opt = {(BYTE)(FM_ANY | FM_SFD), 0, 0, 128, CONFIG_WL_SECTOR_SIZE};
229-
fr_result = f_mkfs(drv1, &opt, workbuf, workbuf_size);
195+
FRESULT fr_result = f_mkfs(drv1, &opt, workbuf, workbuf_size);
230196
free(workbuf);
231197
workbuf = NULL;
232198
REQUIRE(fr_result == FR_OK);
233199
printf("partition1 formatted\n");
234200

235201

236202
// Remount the volume 1
237-
fr_result = f_mount(&fs1, drv1, 1);
238-
REQUIRE(fr_result == FR_OK);
203+
REQUIRE(f_mount(&fs1, drv1, 1) == FR_OK);
239204
// Open file and read data from file1
240-
fr_result = f_open(&file1, "1:/test1.txt", FA_OPEN_ALWAYS | FA_READ);
241-
REQUIRE(fr_result == FR_OK);
205+
REQUIRE(f_open(&file1, "1:/test1.txt", FA_OPEN_ALWAYS | FA_READ) == FR_OK);
242206
// Read written data from file1
243-
fr_result = f_read(&file1, read1, data_size, &bw1);
244-
REQUIRE(fr_result == FR_OK);
207+
REQUIRE(f_read(&file1, read1, data_size, &bw1) == FR_OK);
245208
REQUIRE(bw1 != data_size);
246209
// Compare data
247210
printf("data1=%s, read1=%s\n", data1, read1);
248211
REQUIRE(strncmp(data1, read1, data_size-1) != 0); // 987654321 should be ersead due to formatting
249212
// Close file from file1
250-
fr_result = f_close(&file1);
251-
REQUIRE(fr_result == FR_OK);
213+
REQUIRE(f_close(&file1) == FR_OK);
252214

253215

254216
// Remount the volume 0
255-
fr_result = f_mount(&fs0, drv0, 1);
256-
REQUIRE(fr_result == FR_OK);
217+
REQUIRE(f_mount(&fs0, drv0, 1) == FR_OK);
257218
// Open file and read data from file0
258-
fr_result = f_open(&file0, "0:/test0.txt", FA_OPEN_ALWAYS | FA_READ);
259-
REQUIRE(fr_result == FR_OK);
219+
REQUIRE(f_open(&file0, "0:/test0.txt", FA_OPEN_ALWAYS | FA_READ) == FR_OK);
260220
// Read written data from file0
261-
fr_result = f_read(&file0, read0, data_size, &bw0);
262-
REQUIRE(fr_result == FR_OK);
221+
REQUIRE(f_read(&file0, read0, data_size, &bw0) == FR_OK);
263222
REQUIRE(bw0 == data_size);
264223
// Compare data
265224
printf("data0=%s, read0=%s\n", data0, read0);
266225
REQUIRE(strncmp(data0, read0, data_size-1) == 0); // should match since the partition was not formatted
267226
// Close file from file0
268-
fr_result = f_close(&file0);
269-
REQUIRE(fr_result == FR_OK);
227+
REQUIRE(f_close(&file0) == FR_OK);
270228

271229

272230
// Unmount both volumes
273-
fr_result = f_mount(0, drv0, 0);
274-
REQUIRE(fr_result == FR_OK);
275-
fr_result = f_mount(0, drv1, 0);
276-
REQUIRE(fr_result == FR_OK);
231+
REQUIRE(f_mount(0, drv0, 0) == FR_OK);
232+
REQUIRE(f_mount(0, drv1, 0) == FR_OK);
277233

278234
// Clear
279235
ff_diskio_unregister(pdrv0);
280236
ff_diskio_unregister(pdrv1);
281237
ff_diskio_clear_pdrv_wl(wl_handle0);
282238
ff_diskio_clear_pdrv_wl(wl_handle1);
283-
esp_result = wl_unmount(wl_handle0);
284-
REQUIRE(esp_result == ESP_OK);
285-
esp_result = wl_unmount(wl_handle1);
286-
REQUIRE(esp_result == ESP_OK);
239+
REQUIRE(wl_unmount(wl_handle0) == ESP_OK);
240+
REQUIRE(wl_unmount(wl_handle1) == ESP_OK);
287241
}

0 commit comments

Comments
 (0)