Skip to content

Commit a224ca7

Browse files
author
David Saada
committed
Skip a few tests if not enough memory can be allocated for them.
1 parent be215a3 commit a224ca7

File tree

5 files changed

+124
-38
lines changed

5 files changed

+124
-38
lines changed

TESTS/mbed_platform/error_handling/main.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ void test_error_logging()
199199
}
200200

201201
#define NUM_TEST_THREADS 5
202+
#define THREAD_STACK_SIZE 512
202203

203204
//Error logger threads
204205
void err_thread_func(mbed_error_status_t *error_status)
@@ -211,16 +212,20 @@ void err_thread_func(mbed_error_status_t *error_status)
211212
*/
212213
void test_error_logging_multithread()
213214
{
215+
uint8_t *dummy = new (std::nothrow) uint8_t[NUM_TEST_THREADS * THREAD_STACK_SIZE];
216+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory for test");
217+
delete[] dummy;
218+
214219
mbed_error_ctx error_ctx = {0};
215-
int i=0;
220+
int i;
216221
Thread *errThread[NUM_TEST_THREADS];
217222
mbed_error_status_t error_status[NUM_TEST_THREADS] = {
218223
MBED_ERROR_INVALID_ARGUMENT, MBED_ERROR_INVALID_DATA_DETECTED, MBED_ERROR_INVALID_FORMAT, MBED_ERROR_INVALID_SIZE, MBED_ERROR_INVALID_OPERATION
219224
};
220225

221226

222-
for(; i<NUM_TEST_THREADS; i++) {
223-
errThread[i] = new Thread(osPriorityNormal1, 512, NULL, NULL);
227+
for(i=0; i<NUM_TEST_THREADS; i++) {
228+
errThread[i] = new Thread(osPriorityNormal1, THREAD_STACK_SIZE, NULL, NULL);
224229
errThread[i]->start(callback(err_thread_func, &error_status[i]));
225230
}
226231
wait(2.0);

features/TESTS/filesystem/buffered_block_device/main.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,21 @@ static const bd_size_t num_blocks = 4;
3030

3131
void functionality_test()
3232
{
33+
uint8_t *dummy = new (std::nothrow) uint8_t[num_blocks * heap_erase_size + heap_prog_size];
34+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory for test");
35+
delete[] dummy;
36+
3337
HeapBlockDevice heap_bd(num_blocks * heap_erase_size, heap_read_size, heap_prog_size, heap_erase_size);
3438
BufferedBlockDevice bd(&heap_bd);
3539

3640
int err = bd.init();
3741
TEST_ASSERT_EQUAL(0, err);
3842

3943
uint8_t *read_buf, *write_buf;
40-
read_buf = new uint8_t[heap_prog_size];
41-
write_buf = new uint8_t[heap_prog_size];
44+
read_buf = new (std::nothrow) uint8_t[heap_prog_size];
45+
TEST_SKIP_UNLESS_MESSAGE(read_buf, "Not enough memory for test");
46+
write_buf = new (std::nothrow) uint8_t[heap_prog_size];
47+
TEST_SKIP_UNLESS_MESSAGE(write_buf, "Not enough memory for test");
4248

4349
TEST_ASSERT_EQUAL(1, bd.get_read_size());
4450
TEST_ASSERT_EQUAL(1, bd.get_program_size());

features/TESTS/filesystem/heap_block_device/main.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ const struct {
4646

4747
// Simple test that read/writes random set of blocks
4848
void test_read_write() {
49+
uint8_t *dummy = new (std::nothrow) uint8_t[TEST_BLOCK_DEVICE_SIZE];
50+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory for test");
51+
delete[] dummy;
52+
4953
HeapBlockDevice bd(TEST_BLOCK_DEVICE_SIZE, TEST_BLOCK_SIZE);
5054

5155
int err = bd.init();
@@ -63,12 +67,18 @@ void test_read_write() {
6367
}
6468
}
6569

66-
bd_size_t block_size = bd.get_erase_size();
67-
uint8_t *write_block = new uint8_t[block_size];
68-
uint8_t *read_block = new uint8_t[block_size];
69-
uint8_t *error_mask = new uint8_t[TEST_ERROR_MASK];
7070
unsigned addrwidth = ceil(log(float(bd.size()-1)) / log(float(16)))+1;
7171

72+
bd_size_t block_size = bd.get_erase_size();
73+
uint8_t *write_block = new (std::nothrow) uint8_t[block_size];
74+
uint8_t *read_block = new (std::nothrow) uint8_t[block_size];
75+
uint8_t *error_mask = new (std::nothrow) uint8_t[TEST_ERROR_MASK];
76+
if (!write_block || !read_block || !error_mask) {
77+
printf("Not enough memory for test");
78+
goto end;
79+
}
80+
81+
7282
for (int b = 0; b < TEST_BLOCK_COUNT; b++) {
7383
// Find a random block
7484
bd_addr_t block = (rand()*block_size) % bd.size();
@@ -135,6 +145,12 @@ void test_read_write() {
135145

136146
err = bd.deinit();
137147
TEST_ASSERT_EQUAL(0, err);
148+
149+
end:
150+
delete[] write_block;
151+
delete[] read_block;
152+
delete[] error_mask;
153+
138154
}
139155

140156

features/TESTS/filesystem/mbr_block_device/main.cpp

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ HeapBlockDevice bd(BLOCK_COUNT*BLOCK_SIZE, BLOCK_SIZE);
3737
// Testing formatting of master boot record
3838
void test_mbr_format()
3939
{
40+
uint8_t *dummy = new (std::nothrow) uint8_t[BLOCK_COUNT * BLOCK_SIZE];
41+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory for test");
42+
delete[] dummy;
43+
4044
// Create two partitions splitting device in ~half
4145
int err = MBRBlockDevice::partition(&bd, 1, 0x83, 0, (BLOCK_COUNT/2)*BLOCK_SIZE);
4246
TEST_ASSERT_EQUAL(0, err);
@@ -68,6 +72,10 @@ void test_mbr_format()
6872
// Testing mbr attributes
6973
void test_mbr_attr()
7074
{
75+
uint8_t *dummy = new (std::nothrow) uint8_t[BLOCK_COUNT * BLOCK_SIZE];
76+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory for test");
77+
delete[] dummy;
78+
7179
// Load partitions
7280
MBRBlockDevice part1(&bd, 1);
7381
int err = part1.init();
@@ -123,18 +131,28 @@ void test_mbr_attr()
123131
// Testing mbr read write
124132
void test_mbr_read_write()
125133
{
134+
uint8_t *dummy = new (std::nothrow) uint8_t[BLOCK_COUNT * BLOCK_SIZE];
135+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory for test");
136+
delete[] dummy;
137+
138+
int err;
139+
126140
// Load partitions
127141
MBRBlockDevice part1(&bd, 1);
128-
int err = part1.init();
142+
err = part1.init();
129143
TEST_ASSERT_EQUAL(0, err);
130144

131145
MBRBlockDevice part2(&bd, 2);
132146
err = part2.init();
133147
TEST_ASSERT_EQUAL(0, err);
134148

135149
// Test reading/writing the partitions
136-
uint8_t *write_block = new uint8_t[BLOCK_SIZE];
137-
uint8_t *read_block = new uint8_t[BLOCK_SIZE];
150+
uint8_t *write_block = new (std::nothrow) uint8_t[BLOCK_SIZE];
151+
uint8_t *read_block = new (std::nothrow) uint8_t[BLOCK_SIZE];
152+
if (!write_block || !read_block) {
153+
printf("Not enough memory for test");
154+
goto end;
155+
}
138156

139157
// Fill with random sequence
140158
srand(1);
@@ -200,15 +218,16 @@ void test_mbr_read_write()
200218
TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
201219
}
202220

203-
// Clean up
204-
delete[] write_block;
205-
delete[] read_block;
206-
207221
err = part1.deinit();
208222
TEST_ASSERT_EQUAL(0, err);
209223

210224
err = part2.deinit();
211225
TEST_ASSERT_EQUAL(0, err);
226+
227+
end:
228+
// Clean up
229+
delete[] write_block;
230+
delete[] read_block;
212231
}
213232

214233

features/TESTS/filesystem/util_block_device/main.cpp

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,32 @@ using namespace utest::v1;
3737

3838
// Simple test which read/writes blocks on a sliced block device
3939
void test_slicing() {
40+
uint8_t *dummy = new (std::nothrow) uint8_t[BLOCK_COUNT * BLOCK_SIZE];
41+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory for test");
42+
delete[] dummy;
43+
44+
int err;
45+
4046
HeapBlockDevice bd(BLOCK_COUNT*BLOCK_SIZE, BLOCK_SIZE);
41-
uint8_t *write_block = new uint8_t[BLOCK_SIZE];
42-
uint8_t *read_block = new uint8_t[BLOCK_SIZE];
4347

44-
// Test with first slice of block device
4548
SlicingBlockDevice slice1(&bd, 0, (BLOCK_COUNT/2)*BLOCK_SIZE);
49+
SlicingBlockDevice slice2(&bd, -(BLOCK_COUNT/2)*BLOCK_SIZE);
4650

47-
int err = slice1.init();
51+
// Test with first slice of block device
52+
err = slice1.init();
4853
TEST_ASSERT_EQUAL(0, err);
4954

5055
TEST_ASSERT_EQUAL(BLOCK_SIZE, slice1.get_program_size());
5156
TEST_ASSERT_EQUAL(BLOCK_SIZE, slice1.get_erase_size(BLOCK_SIZE));
5257
TEST_ASSERT_EQUAL((BLOCK_COUNT/2)*BLOCK_SIZE, slice1.size());
5358

59+
uint8_t *write_block = new (std::nothrow) uint8_t[BLOCK_SIZE];
60+
uint8_t *read_block = new (std::nothrow) uint8_t[BLOCK_SIZE];
61+
if (!write_block || !read_block) {
62+
printf("Not enough memory for test");
63+
goto end;
64+
}
65+
5466
// Fill with random sequence
5567
srand(1);
5668
for (int i = 0; i < BLOCK_SIZE; i++) {
@@ -85,8 +97,6 @@ void test_slicing() {
8597

8698

8799
// Test with second slice of block device
88-
SlicingBlockDevice slice2(&bd, -(BLOCK_COUNT/2)*BLOCK_SIZE);
89-
90100
err = slice2.init();
91101
TEST_ASSERT_EQUAL(0, err);
92102

@@ -122,24 +132,38 @@ void test_slicing() {
122132
TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
123133
}
124134

125-
delete[] write_block;
126-
delete[] read_block;
127135
err = slice2.deinit();
128136
TEST_ASSERT_EQUAL(0, err);
137+
138+
end:
139+
delete[] write_block;
140+
delete[] read_block;
129141
}
130142

131143
// Simple test which read/writes blocks on a chain of block devices
132144
void test_chaining() {
145+
uint8_t *dummy = new (std::nothrow) uint8_t[BLOCK_COUNT * BLOCK_SIZE];
146+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory for test");
147+
delete[] dummy;
148+
149+
int err;
150+
133151
HeapBlockDevice bd1((BLOCK_COUNT/2)*BLOCK_SIZE, BLOCK_SIZE);
134152
HeapBlockDevice bd2((BLOCK_COUNT/2)*BLOCK_SIZE, BLOCK_SIZE);
135-
uint8_t *write_block = new uint8_t[BLOCK_SIZE];
136-
uint8_t *read_block = new uint8_t[BLOCK_SIZE];
137153

138154
// Test with chain of block device
139155
BlockDevice *bds[] = {&bd1, &bd2};
140156
ChainingBlockDevice chain(bds);
141157

142-
int err = chain.init();
158+
uint8_t *write_block = new (std::nothrow) uint8_t[BLOCK_SIZE];
159+
uint8_t *read_block = new (std::nothrow) uint8_t[BLOCK_SIZE];
160+
161+
if (!write_block || !read_block) {
162+
printf("Not enough memory for test");
163+
goto end;
164+
}
165+
166+
err = chain.init();
143167
TEST_ASSERT_EQUAL(0, err);
144168

145169
TEST_ASSERT_EQUAL(BLOCK_SIZE, chain.get_program_size());
@@ -178,27 +202,41 @@ void test_chaining() {
178202
TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
179203
}
180204

181-
delete[] write_block;
182-
delete[] read_block;
183205
err = chain.deinit();
184206
TEST_ASSERT_EQUAL(0, err);
207+
208+
end:
209+
delete[] write_block;
210+
delete[] read_block;
185211
}
186212

187213
// Simple test which read/writes blocks on a chain of block devices
188214
void test_profiling() {
189-
HeapBlockDevice bd(BLOCK_COUNT*BLOCK_SIZE, BLOCK_SIZE);
190-
uint8_t *write_block = new uint8_t[BLOCK_SIZE];
191-
uint8_t *read_block = new uint8_t[BLOCK_SIZE];
215+
uint8_t *dummy = new (std::nothrow) uint8_t[BLOCK_COUNT * BLOCK_SIZE];
216+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory for test");
217+
delete[] dummy;
192218

219+
int err;
220+
bd_size_t read_count, program_count, erase_count;
221+
222+
HeapBlockDevice bd(BLOCK_COUNT*BLOCK_SIZE, BLOCK_SIZE);
193223
// Test under profiling
194224
ProfilingBlockDevice profiler(&bd);
195225

196-
int err = profiler.init();
226+
err = profiler.init();
197227
TEST_ASSERT_EQUAL(0, err);
198228

199229
TEST_ASSERT_EQUAL(BLOCK_SIZE, profiler.get_erase_size());
200230
TEST_ASSERT_EQUAL(BLOCK_COUNT*BLOCK_SIZE, profiler.size());
201231

232+
uint8_t *write_block = new (std::nothrow) uint8_t[BLOCK_SIZE];
233+
uint8_t *read_block = new (std::nothrow) uint8_t[BLOCK_SIZE];
234+
235+
if (!write_block || !read_block) {
236+
printf("Not enough memory for test");
237+
goto end;
238+
}
239+
202240
// Fill with random sequence
203241
srand(1);
204242
for (int i = 0; i < BLOCK_SIZE; i++) {
@@ -231,18 +269,20 @@ void test_profiling() {
231269
TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
232270
}
233271

234-
delete[] write_block;
235-
delete[] read_block;
236272
err = profiler.deinit();
237273
TEST_ASSERT_EQUAL(0, err);
238274

239275
// Check that profiled operations match expectations
240-
bd_size_t read_count = profiler.get_read_count();
276+
read_count = profiler.get_read_count();
241277
TEST_ASSERT_EQUAL(BLOCK_SIZE, read_count);
242-
bd_size_t program_count = profiler.get_program_count();
278+
program_count = profiler.get_program_count();
243279
TEST_ASSERT_EQUAL(BLOCK_SIZE, program_count);
244-
bd_size_t erase_count = profiler.get_erase_count();
280+
erase_count = profiler.get_erase_count();
245281
TEST_ASSERT_EQUAL(BLOCK_SIZE, erase_count);
282+
283+
end:
284+
delete[] write_block;
285+
delete[] read_block;
246286
}
247287

248288

0 commit comments

Comments
 (0)