@@ -65,7 +65,7 @@ void basic_erase_program_read_test(BlockDevice *block_device, bd_size_t block_si
65
65
write_block[i_ind] = 0xff & rand ();
66
66
}
67
67
// Write, sync, and read the block
68
- utest_printf (" \n test %0*llx:%llu..." , addrwidth, block, block_size);
68
+ utest_printf (" test %0*llx:%llu...\n " , addrwidth, block, block_size);
69
69
_mutex->unlock ();
70
70
71
71
err = block_device->erase (block, block_size);
@@ -100,7 +100,7 @@ void test_random_program_read_erase()
100
100
101
101
BlockDevice *block_device = BlockDevice::get_default_instance ();
102
102
103
- TEST_SKIP_UNLESS_MESSAGE (block_device != NULL , " \n no block device found.\n " );
103
+ TEST_SKIP_UNLESS_MESSAGE (block_device != NULL , " no block device found." );
104
104
105
105
int err = block_device->init ();
106
106
TEST_ASSERT_EQUAL (0 , err);
@@ -123,7 +123,7 @@ void test_random_program_read_erase()
123
123
uint8_t *write_block = new (std::nothrow) uint8_t [block_size];
124
124
uint8_t *read_block = new (std::nothrow) uint8_t [block_size];
125
125
if (!write_block || !read_block) {
126
- utest_printf (" \n Not enough memory for test" );
126
+ utest_printf (" Not enough memory for test\n " );
127
127
goto end;
128
128
}
129
129
@@ -152,7 +152,7 @@ static void test_thread_job(void *block_device_ptr)
152
152
uint8_t *read_block = new (std::nothrow) uint8_t [block_size];
153
153
154
154
if (!write_block || !read_block) {
155
- utest_printf (" \n Not enough memory for test" );
155
+ utest_printf (" Not enough memory for test\n " );
156
156
goto end;
157
157
}
158
158
@@ -173,6 +173,10 @@ void test_multi_threads()
173
173
174
174
TEST_SKIP_UNLESS_MESSAGE (block_device != NULL , " \n no block device found.\n " );
175
175
176
+ char *dummy = new (std::nothrow) char [TEST_NUM_OF_THREADS * OS_STACK_SIZE];
177
+ TEST_SKIP_UNLESS_MESSAGE (dummy, " Not enough memory for test.\n " );
178
+ delete[] dummy;
179
+
176
180
int err = block_device->init ();
177
181
TEST_ASSERT_EQUAL (0 , err);
178
182
@@ -196,7 +200,7 @@ void test_multi_threads()
196
200
for (i_ind = 0 ; i_ind < TEST_NUM_OF_THREADS; i_ind++) {
197
201
threadStatus = bd_thread[i_ind].start (callback (test_thread_job, (void *)block_device));
198
202
if (threadStatus != 0 ) {
199
- utest_printf (" \n Thread %d Start Failed!" , i_ind + 1 );
203
+ utest_printf (" Thread %d Start Failed!\n " , i_ind + 1 );
200
204
}
201
205
}
202
206
@@ -219,15 +223,15 @@ void test_get_erase_value()
219
223
// 3. Read erased region and compare with get_erase_value()
220
224
221
225
BlockDevice *block_device = BlockDevice::get_default_instance ();
222
- TEST_SKIP_UNLESS_MESSAGE (block_device != NULL , " \n no block device found.\n " );
226
+ TEST_SKIP_UNLESS_MESSAGE (block_device != NULL , " no block device found." );
223
227
224
228
int err = block_device->init ();
225
229
TEST_ASSERT_EQUAL (0 , err);
226
230
227
231
// Check erase value
228
232
int erase_value_int = block_device->get_erase_value ();
229
- utest_printf (" \n block_device ->get_erase_value()=%d" , erase_value_int);
230
- TEST_SKIP_UNLESS_MESSAGE (erase_value_int >= 0 , " \n erase value is negative which means the erase value is unknown \n " );
233
+ utest_printf (" block_device ->get_erase_value()=%d\n " , erase_value_int);
234
+ TEST_SKIP_UNLESS_MESSAGE (erase_value_int >= 0 , " Erase not supported in this block device. Test skipped. " );
231
235
232
236
// Assuming that get_erase_value() returns byte value as documentation mentions
233
237
// "If get_erase_value() returns a non-negative byte value" for unknown case.
@@ -245,33 +249,33 @@ void test_get_erase_value()
245
249
start_address %= block_device->size () - data_buf_size - erase_size; // fit all data + alignment reserve
246
250
start_address += erase_size; // add alignment reserve
247
251
start_address -= start_address % erase_size; // align with erase_block
248
- utest_printf (" \n start_address =0x%016" PRIx64, start_address);
252
+ utest_printf (" start_address =0x%016" PRIx64 " \n " , start_address);
249
253
250
254
// Allocate buffer for read test data
251
255
uint8_t *data_buf = (uint8_t *)malloc (data_buf_size);
252
- TEST_ASSERT_NOT_NULL (data_buf);
256
+ TEST_SKIP_UNLESS_MESSAGE (data_buf, " Not enough memory for test. \n " );
253
257
254
258
// Write random data to selected region to make sure data is not accidentally set to "erased" value.
255
259
// With this pre-write, the test case will fail even if block_device->erase() is broken.
256
260
for (bd_size_t i = 0 ; i < data_buf_size; i++) {
257
261
data_buf[i] = (uint8_t ) rand ();
258
262
}
259
- utest_printf (" \n writing given memory region" );
263
+ utest_printf (" writing given memory region\n " );
260
264
err = block_device->program ((const void *)data_buf, start_address, data_buf_size);
261
265
TEST_ASSERT_EQUAL (0 , err);
262
266
263
267
// Erase given memory region
264
- utest_printf (" \n erasing given memory region" );
268
+ utest_printf (" erasing given memory region\n " );
265
269
err = block_device->erase (start_address, data_buf_size);
266
270
TEST_ASSERT_EQUAL (0 , err);
267
271
268
272
// Read erased memory region
269
- utest_printf (" \n reading erased memory region" );
273
+ utest_printf (" reading erased memory region\n " );
270
274
err = block_device->read ((void *)data_buf, start_address, data_buf_size);
271
275
TEST_ASSERT_EQUAL (0 , err);
272
276
273
277
// Verify erased memory region
274
- utest_printf (" \n verifying erased memory region" );
278
+ utest_printf (" verifying erased memory region\n " );
275
279
for (bd_size_t i = 0 ; i < data_buf_size; i++) {
276
280
TEST_ASSERT_EQUAL (erase_value, data_buf[i]);
277
281
}
@@ -295,7 +299,7 @@ void test_contiguous_erase_write_read()
295
299
// 3. Return step 2 for whole erase region
296
300
297
301
BlockDevice *block_device = BlockDevice::get_default_instance ();
298
- TEST_SKIP_UNLESS_MESSAGE (block_device != NULL , " \n no block device found.\n " );
302
+ TEST_SKIP_UNLESS_MESSAGE (block_device != NULL , " no block device found." );
299
303
300
304
// Initialize BlockDevice
301
305
int err = block_device->init ();
@@ -306,9 +310,9 @@ void test_contiguous_erase_write_read()
306
310
TEST_ASSERT (erase_size > 0 );
307
311
bd_size_t program_size = block_device->get_program_size ();
308
312
TEST_ASSERT (program_size > 0 );
309
- utest_printf (" \n erase_size=%d " , erase_size);
310
- utest_printf (" \n program_size=%d " , program_size);
311
- utest_printf (" \n block_device ->size()=%" PRId64, block_device->size ());
313
+ utest_printf (" erase_size=% " PRId64 " \n " , erase_size);
314
+ utest_printf (" program_size=% " PRId64 " \n " , program_size);
315
+ utest_printf (" block_device ->size()=%" PRId64 " \n " , block_device->size ());
312
316
313
317
// Determine write/read buffer size
314
318
// start write_read_buf_size from 1% block_device->size()
@@ -324,18 +328,18 @@ void test_contiguous_erase_write_read()
324
328
bd_size_t contiguous_write_read_blocks_per_region = write_read_buf_size /
325
329
program_size; // 2 is minimum to test contiguous write
326
330
write_read_buf_size = contiguous_write_read_blocks_per_region * program_size;
327
- utest_printf (" \n contiguous_write_read_blocks_per_region =%" PRIu64, contiguous_write_read_blocks_per_region);
328
- utest_printf (" \n write_read_buf_size =%" PRIu64, write_read_buf_size);
331
+ utest_printf (" contiguous_write_read_blocks_per_region =%" PRIu64 " \n " , contiguous_write_read_blocks_per_region);
332
+ utest_printf (" write_read_buf_size =%" PRIu64 " \n " , write_read_buf_size);
329
333
330
334
// Determine test region count
331
335
int contiguous_write_read_regions = TEST_BLOCK_COUNT;
332
- utest_printf (" \n contiguous_write_read_regions =%d" , contiguous_write_read_regions);
336
+ utest_printf (" contiguous_write_read_regions =%d\n " , contiguous_write_read_regions);
333
337
334
338
// Determine whole erase size
335
339
bd_size_t contiguous_erase_size = write_read_buf_size * contiguous_write_read_regions;
336
340
contiguous_erase_size -= contiguous_erase_size % erase_size; // aligned to erase_size
337
341
contiguous_erase_size += erase_size; // but larger than write/read size * regions
338
- utest_printf (" \n contiguous_erase_size =%" PRIu64, contiguous_erase_size);
342
+ utest_printf (" contiguous_erase_size =%" PRIu64 " \n " , contiguous_erase_size);
339
343
340
344
// Determine starting address
341
345
bd_addr_t start_address = rand (); // low 32 bytes
@@ -344,70 +348,70 @@ void test_contiguous_erase_write_read()
344
348
start_address += erase_size; // add alignment reserve
345
349
start_address -= start_address % erase_size; // align with erase_block
346
350
bd_addr_t stop_address = start_address + write_read_buf_size * contiguous_write_read_regions;
347
- utest_printf (" \n start_address =0x%016" PRIx64, start_address);
348
- utest_printf (" \n stop_address =0x%016" PRIx64, stop_address);
351
+ utest_printf (" start_address =0x%016" PRIx64 " \n " , start_address);
352
+ utest_printf (" stop_address =0x%016" PRIx64 " \n " , stop_address);
349
353
350
354
// Allocate write/read buffer
351
355
uint8_t *write_read_buf = (uint8_t *)malloc (write_read_buf_size);
352
356
if (write_read_buf == NULL ) {
353
357
block_device->deinit ();
354
- TEST_SKIP_MESSAGE (" \n not enough memory for test" );
358
+ TEST_SKIP_MESSAGE (" not enough memory for test" );
355
359
}
356
- utest_printf (" \n write_read_buf_size =%" PRIu64 " " , (uint64_t )write_read_buf_size);
360
+ utest_printf (" write_read_buf_size =%" PRIu64 " \n " , (uint64_t )write_read_buf_size);
357
361
358
362
// Pre-fill the to-be-erased region. By pre-filling the region,
359
363
// we can be sure the test will not pass if the erase doesn't work.
360
364
for (bd_size_t offset = 0 ; start_address + offset < stop_address; offset += write_read_buf_size) {
361
365
for (size_t i = 0 ; i < write_read_buf_size; i++) {
362
366
write_read_buf[i] = (uint8_t )rand ();
363
367
}
364
- utest_printf (" \n pre -filling memory, from 0x%" PRIx64 " of size 0x%" PRIx64, start_address + offset,
368
+ utest_printf (" pre -filling memory, from 0x%" PRIx64 " of size 0x%" PRIx64 " \n " , start_address + offset,
365
369
write_read_buf_size);
366
370
err = block_device->program ((const void *)write_read_buf, start_address + offset, write_read_buf_size);
367
371
TEST_ASSERT_EQUAL (0 , err);
368
372
}
369
373
370
374
// Erase the whole region first
371
- utest_printf (" \n erasing memory, from 0x%" PRIx64 " of size 0x%" PRIx64, start_address, contiguous_erase_size);
375
+ utest_printf (" erasing memory, from 0x%" PRIx64 " of size 0x%" PRIx64 " \n " , start_address, contiguous_erase_size);
372
376
err = block_device->erase (start_address, contiguous_erase_size);
373
377
TEST_ASSERT_EQUAL (0 , err);
374
378
375
379
// Loop through all write/read regions
376
380
int region = 0 ;
377
381
for (; start_address < stop_address; start_address += write_read_buf_size) {
378
- utest_printf (" \n\ n region #%d start_address=0x%016" PRIx64, region++, start_address);
382
+ utest_printf (" \n region #%d start_address=0x%016" PRIx64 " \n " , region++, start_address);
379
383
380
384
// Generate test data
381
385
unsigned int seed = rand ();
382
- utest_printf (" \n generating test data, seed=%u" , seed);
386
+ utest_printf (" generating test data, seed=%u\n " , seed);
383
387
srand (seed);
384
388
for (size_t i = 0 ; i < write_read_buf_size; i++) {
385
389
write_read_buf[i] = (uint8_t )rand ();
386
390
}
387
391
388
392
// Write test data
389
- utest_printf (" \n writing test data" );
393
+ utest_printf (" writing test data\n " );
390
394
err = block_device->program ((const void *)write_read_buf, start_address, write_read_buf_size);
391
395
TEST_ASSERT_EQUAL (0 , err);
392
396
393
397
// Read test data
394
398
memset (write_read_buf, 0 , (size_t )write_read_buf_size);
395
- utest_printf (" \n reading test data" );
399
+ utest_printf (" reading test data\n " );
396
400
err = block_device->read (write_read_buf, start_address, write_read_buf_size);
397
401
TEST_ASSERT_EQUAL (0 , err);
398
402
399
403
// Verify read data
400
- utest_printf (" \n verifying test data" );
404
+ utest_printf (" verifying test data\n " );
401
405
srand (seed);
402
406
for (size_t i = 0 ; i < write_read_buf_size; i++) {
403
407
uint8_t expected_value = (uint8_t )rand ();
404
408
if (write_read_buf[i] != expected_value) {
405
- utest_printf (" \n data verify failed, write_read_buf[%d]=%" PRIu8 " and not %" PRIu8 " \n " ,
409
+ utest_printf (" data verify failed, write_read_buf[%d]=%" PRIu8 " and not %" PRIu8 " \n " ,
406
410
i, write_read_buf[i], expected_value);
407
411
}
408
412
TEST_ASSERT_EQUAL (write_read_buf[i], expected_value);
409
413
}
410
- utest_printf (" \n verify OK" );
414
+ utest_printf (" verify OK\n " );
411
415
}
412
416
413
417
free (write_read_buf);
@@ -423,22 +427,34 @@ void test_program_read_small_data_sizes()
423
427
424
428
BlockDevice *bd = BlockDevice::get_default_instance ();
425
429
426
- TEST_SKIP_UNLESS_MESSAGE (bd != NULL , " \n no block device found.\n " );
430
+ TEST_SKIP_UNLESS_MESSAGE (bd != NULL , " no block device found." );
431
+
432
+ int err = bd->init ();
433
+ TEST_ASSERT_EQUAL (0 , err);
434
+
435
+ bd_size_t erase_size = bd->get_erase_size ();
436
+ bd_size_t program_size = bd->get_program_size ();
437
+ bd_size_t read_size = bd->get_read_size ();
438
+ TEST_ASSERT (program_size > 0 );
439
+
440
+ err = bd->deinit ();
441
+ TEST_ASSERT_EQUAL (0 , err);
442
+
443
+ // See that we have enough memory for buffered block device
444
+ char *dummy = new (std::nothrow) char [program_size + read_size];
445
+ TEST_SKIP_UNLESS_MESSAGE (dummy, " Not enough memory for test.\n " );
446
+ delete[] dummy;
427
447
428
448
// use BufferedBlockDevice for better handling of block devices program and read
429
449
BufferedBlockDevice *block_device = new BufferedBlockDevice (bd);
430
450
431
451
// BlockDevice initialization
432
- int err = block_device->init ();
452
+ err = block_device->init ();
433
453
TEST_ASSERT_EQUAL (0 , err);
434
454
435
455
const char write_buffer[] = " 1234567" ;
436
456
char read_buffer[7 ] = {};
437
457
438
- bd_size_t erase_size = block_device->get_erase_size ();
439
- bd_size_t program_size = block_device->get_program_size ();
440
- TEST_ASSERT (program_size > 0 );
441
-
442
458
// Determine starting address
443
459
bd_addr_t start_address = 0 ;
444
460
@@ -469,10 +485,7 @@ void test_program_read_small_data_sizes()
469
485
void test_get_type_functionality ()
470
486
{
471
487
BlockDevice *block_device = BlockDevice::get_default_instance ();
472
- if (block_device == NULL ) {
473
- TEST_SKIP_MESSAGE (" No block device component is defined for this target" );
474
- return ;
475
- }
488
+ TEST_SKIP_UNLESS_MESSAGE (block_device, " No block device component is defined for this target" );
476
489
const char *bd_type = block_device->get_type ();
477
490
TEST_ASSERT_NOT_EQUAL (0 , bd_type);
478
491
0 commit comments