24
24
#include " mbed_trace.h"
25
25
#include < inttypes.h>
26
26
#include < stdlib.h>
27
+ #include " BufferedBlockDevice.h"
27
28
28
29
using namespace utest ::v1;
29
30
@@ -99,10 +100,7 @@ void test_random_program_read_erase()
99
100
100
101
BlockDevice *block_device = BlockDevice::get_default_instance ();
101
102
102
- if (!block_device) {
103
- utest_printf (" \n no block device found.\n " );
104
- return ;
105
- }
103
+ TEST_SKIP_UNLESS_MESSAGE (block_device != NULL , " \n no block device found.\n " );
106
104
107
105
int err = block_device->init ();
108
106
TEST_ASSERT_EQUAL (0 , err);
@@ -173,10 +171,7 @@ void test_multi_threads()
173
171
174
172
BlockDevice *block_device = BlockDevice::get_default_instance ();
175
173
176
- if (!block_device) {
177
- utest_printf (" \n no block device found.\n " );
178
- return ;
179
- }
174
+ TEST_SKIP_UNLESS_MESSAGE (block_device != NULL , " \n no block device found.\n " );
180
175
181
176
int err = block_device->init ();
182
177
TEST_ASSERT_EQUAL (0 , err);
@@ -297,7 +292,7 @@ void test_contiguous_erase_write_read()
297
292
// - Tests contiguous erase
298
293
// 2. Write smaller memory area
299
294
// - Tests contiguous sector writes
300
- // 3. Rerun step 2 for whole erase region
295
+ // 3. Return step 2 for whole erase region
301
296
302
297
BlockDevice *block_device = BlockDevice::get_default_instance ();
303
298
TEST_SKIP_UNLESS_MESSAGE (block_device != NULL , " \n no block device found.\n " );
@@ -326,7 +321,8 @@ void test_contiguous_erase_write_read()
326
321
if (write_read_buf_size < program_size * 2 ) {
327
322
write_read_buf_size = program_size * 2 ; // going over 10k
328
323
}
329
- bd_size_t contiguous_write_read_blocks_per_region = write_read_buf_size / program_size; // 2 is minimum to test contiguous write
324
+ bd_size_t contiguous_write_read_blocks_per_region = write_read_buf_size /
325
+ program_size; // 2 is minimum to test contiguous write
330
326
write_read_buf_size = contiguous_write_read_blocks_per_region * program_size;
331
327
utest_printf (" \n contiguous_write_read_blocks_per_region=%" PRIu64, contiguous_write_read_blocks_per_region);
332
328
utest_printf (" \n write_read_buf_size=%" PRIu64, write_read_buf_size);
@@ -365,7 +361,8 @@ void test_contiguous_erase_write_read()
365
361
for (size_t i = 0 ; i < write_read_buf_size; i++) {
366
362
write_read_buf[i] = (uint8_t )rand ();
367
363
}
368
- utest_printf (" \n pre-filling memory, from 0x%" PRIx64 " of size 0x%" PRIx64, start_address + offset, write_read_buf_size);
364
+ utest_printf (" \n pre-filling memory, from 0x%" PRIx64 " of size 0x%" PRIx64, start_address + offset,
365
+ write_read_buf_size);
369
366
err = block_device->program ((const void *)write_read_buf, start_address + offset, write_read_buf_size);
370
367
TEST_ASSERT_EQUAL (0 , err);
371
368
}
@@ -420,6 +417,61 @@ void test_contiguous_erase_write_read()
420
417
TEST_ASSERT_EQUAL (0 , err);
421
418
}
422
419
420
+ void test_program_read_small_data_sizes ()
421
+ {
422
+ utest_printf (" \n Test program-read small data sizes, from 1 to 7 bytes..\n " );
423
+
424
+ BlockDevice *bd = BlockDevice::get_default_instance ();
425
+
426
+ TEST_SKIP_UNLESS_MESSAGE (bd != NULL , " \n no block device found.\n " );
427
+
428
+ // use BufferedBlockDevice for better handling of block devices program and read
429
+ BufferedBlockDevice *block_device = new BufferedBlockDevice (bd);
430
+
431
+ // BlockDevice initialization
432
+ int err = block_device->init ();
433
+ TEST_ASSERT_EQUAL (0 , err);
434
+
435
+ const char write_buffer[] = " 1234567" ;
436
+ char read_buffer[7 ] = {};
437
+
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
+ // Determine starting address
443
+ bd_addr_t start_address = 0 ;
444
+
445
+ for (int i = 1 ; i <= 7 ; i++) {
446
+ err = block_device->erase (start_address, erase_size);
447
+ TEST_ASSERT_EQUAL (0 , err);
448
+
449
+ err = block_device->program ((const void *)write_buffer, start_address, i);
450
+ TEST_ASSERT_EQUAL (0 , err);
451
+
452
+ err = block_device->sync ();
453
+ TEST_ASSERT_EQUAL (0 , err);
454
+
455
+ err = block_device->read (read_buffer, start_address, i);
456
+ TEST_ASSERT_EQUAL (0 , err);
457
+
458
+ err = memcmp (write_buffer, read_buffer, i);
459
+ TEST_ASSERT_EQUAL (0 , err);
460
+ }
461
+
462
+ // BlockDevice deinitialization
463
+ err = block_device->deinit ();
464
+ TEST_ASSERT_EQUAL (0 , err);
465
+
466
+ delete block_device;
467
+ }
468
+
469
+ utest::v1::status_t greentea_failure_handler (const Case *const source, const failure_t reason)
470
+ {
471
+ greentea_case_failure_abort_handler (source, reason);
472
+ return STATUS_CONTINUE;
473
+ }
474
+
423
475
// Test setup
424
476
utest::v1::status_t test_setup (const size_t number_of_cases)
425
477
{
@@ -428,10 +480,11 @@ utest::v1::status_t test_setup(const size_t number_of_cases)
428
480
}
429
481
430
482
Case cases[] = {
431
- Case (" Testing read write random blocks" , test_random_program_read_erase),
432
- Case (" Testing Multi Threads Erase Program Read" , test_multi_threads),
433
- Case (" Testing contiguous erase, write and read" , test_contiguous_erase_write_read),
434
- Case (" Test BlockDevice::get_erase_value()" , test_get_erase_value)
483
+ Case (" Testing read write random blocks" , test_random_program_read_erase, greentea_failure_handler),
484
+ Case (" Testing multi threads erase program read" , test_multi_threads, greentea_failure_handler),
485
+ Case (" Testing contiguous erase, write and read" , test_contiguous_erase_write_read, greentea_failure_handler),
486
+ Case (" Testing BlockDevice::get_erase_value()" , test_get_erase_value, greentea_failure_handler),
487
+ Case (" Testing program read small data sizes" , test_program_read_small_data_sizes, greentea_failure_handler)
435
488
};
436
489
437
490
Specification specification (test_setup, cases);
0 commit comments