@@ -30,28 +30,36 @@ using namespace utest::v1;
30
30
#error [NOT_SUPPORTED] Filesystem tests not supported by default
31
31
#endif
32
32
33
+ static const int mem_alloc_threshold = 32 * 1024 ;
34
+
33
35
// Test block device
34
36
#define BLOCK_SIZE 512
35
37
#define BLOCK_COUNT 512
36
- HeapBlockDevice bd (BLOCK_COUNT *BLOCK_SIZE, BLOCK_SIZE);
37
-
38
+ HeapBlockDevice *bd = 0 ;
38
39
39
40
// Test formatting and partitioning
40
41
void test_format ()
41
42
{
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
+
42
50
// 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);
44
52
TEST_ASSERT_EQUAL (0 , err);
45
53
46
- err = MBRBlockDevice::partition (& bd, 2 , 0x83 , -(BLOCK_COUNT / 2 ) * BLOCK_SIZE);
54
+ err = MBRBlockDevice::partition (bd, 2 , 0x83 , -(BLOCK_COUNT / 2 ) * BLOCK_SIZE);
47
55
TEST_ASSERT_EQUAL (0 , err);
48
56
49
57
// Load both partitions
50
- MBRBlockDevice part1 (& bd, 1 );
58
+ MBRBlockDevice part1 (bd, 1 );
51
59
err = part1.init ();
52
60
TEST_ASSERT_EQUAL (0 , err);
53
61
54
- MBRBlockDevice part2 (& bd, 2 );
62
+ MBRBlockDevice part2 (bd, 2 );
55
63
err = part2.init ();
56
64
TEST_ASSERT_EQUAL (0 , err);
57
65
@@ -75,12 +83,14 @@ void test_format()
75
83
template <ssize_t TEST_SIZE>
76
84
void test_read_write ()
77
85
{
86
+ TEST_SKIP_UNLESS_MESSAGE (bd, " Not enough heap memory to run test. Test skipped." );
87
+
78
88
// Load both partitions
79
- MBRBlockDevice part1 (& bd, 1 );
89
+ MBRBlockDevice part1 (bd, 1 );
80
90
int err = part1.init ();
81
91
TEST_ASSERT_EQUAL (0 , err);
82
92
83
- MBRBlockDevice part2 (& bd, 2 );
93
+ MBRBlockDevice part2 (bd, 2 );
84
94
err = part2.init ();
85
95
TEST_ASSERT_EQUAL (0 , err);
86
96
@@ -94,11 +104,11 @@ void test_read_write()
94
104
err = fs2.mount (&part2);
95
105
TEST_ASSERT_EQUAL (0 , err);
96
106
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. " );
99
109
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. " );
102
112
103
113
// Fill with random sequence
104
114
srand (1 );
@@ -163,47 +173,55 @@ void test_read_write()
163
173
164
174
err = part2.deinit ();
165
175
TEST_ASSERT_EQUAL (0 , err);
176
+
177
+ delete[] buffer1;
178
+ delete[] buffer2;
166
179
}
167
180
168
181
void test_single_mbr ()
169
182
{
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 ();
171
186
TEST_ASSERT_EQUAL (0 , err);
172
187
173
188
const bd_addr_t MBR_OFFSET = 0 ;
174
189
const bd_addr_t FAT1_OFFSET = 1 ;
175
190
const bd_addr_t FAT2_OFFSET = BLOCK_COUNT / 2 ;
176
191
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. " );
179
194
180
195
// 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);
182
197
TEST_ASSERT_EQUAL (0 , err);
183
198
TEST_ASSERT (memcmp (&buffer[BLOCK_SIZE - 2 ], " \x55\xaa " , 2 ) == 0 );
184
199
185
- err = bd. read (buffer, FAT1_OFFSET * BLOCK_SIZE, BLOCK_SIZE);
200
+ err = bd-> read (buffer, FAT1_OFFSET * BLOCK_SIZE, BLOCK_SIZE);
186
201
TEST_ASSERT_EQUAL (0 , err);
187
202
TEST_ASSERT (memcmp (&buffer[BLOCK_SIZE - 2 ], " \x55\xaa " , 2 ) == 0 );
188
203
189
- err = bd. read (buffer, FAT2_OFFSET * BLOCK_SIZE, BLOCK_SIZE);
204
+ err = bd-> read (buffer, FAT2_OFFSET * BLOCK_SIZE, BLOCK_SIZE);
190
205
TEST_ASSERT_EQUAL (0 , err);
191
206
TEST_ASSERT (memcmp (&buffer[BLOCK_SIZE - 2 ], " \x55\xaa " , 2 ) == 0 );
192
207
193
208
// Check that the headers for both filesystems contain a jump code
194
209
// 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);
196
211
TEST_ASSERT_EQUAL (0 , err);
197
212
TEST_ASSERT (buffer[0 ] == 0xe9 || buffer[0 ] == 0xeb || buffer[0 ] == 0xe8 );
198
213
199
- err = bd. read (buffer, FAT2_OFFSET * BLOCK_SIZE, BLOCK_SIZE);
214
+ err = bd-> read (buffer, FAT2_OFFSET * BLOCK_SIZE, BLOCK_SIZE);
200
215
TEST_ASSERT_EQUAL (0 , err);
201
216
TEST_ASSERT (buffer[0 ] == 0xe9 || buffer[0 ] == 0xeb || buffer[0 ] == 0xe8 );
202
217
203
- free ( buffer) ;
218
+ delete[] buffer;
204
219
205
- bd. deinit ();
220
+ bd-> deinit ();
206
221
TEST_ASSERT_EQUAL (0 , err);
222
+
223
+ delete bd;
224
+ bd = 0 ;
207
225
}
208
226
209
227
0 commit comments