Skip to content

Commit cf84615

Browse files
committed
Add block device small data size test
1 parent 3147545 commit cf84615

File tree

1 file changed

+68
-15
lines changed
  • features/storage/TESTS/blockdevice/general_block_device

1 file changed

+68
-15
lines changed

features/storage/TESTS/blockdevice/general_block_device/main.cpp

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "mbed_trace.h"
2525
#include <inttypes.h>
2626
#include <stdlib.h>
27+
#include "BufferedBlockDevice.h"
2728

2829
using namespace utest::v1;
2930

@@ -99,10 +100,7 @@ void test_random_program_read_erase()
99100

100101
BlockDevice *block_device = BlockDevice::get_default_instance();
101102

102-
if (!block_device) {
103-
utest_printf("\nno block device found.\n");
104-
return;
105-
}
103+
TEST_SKIP_UNLESS_MESSAGE(block_device != NULL, "\nno block device found.\n");
106104

107105
int err = block_device->init();
108106
TEST_ASSERT_EQUAL(0, err);
@@ -173,10 +171,7 @@ void test_multi_threads()
173171

174172
BlockDevice *block_device = BlockDevice::get_default_instance();
175173

176-
if (!block_device) {
177-
utest_printf("\nno block device found.\n");
178-
return;
179-
}
174+
TEST_SKIP_UNLESS_MESSAGE(block_device != NULL, "\nno block device found.\n");
180175

181176
int err = block_device->init();
182177
TEST_ASSERT_EQUAL(0, err);
@@ -297,7 +292,7 @@ void test_contiguous_erase_write_read()
297292
// - Tests contiguous erase
298293
// 2. Write smaller memory area
299294
// - Tests contiguous sector writes
300-
// 3. Rerun step 2 for whole erase region
295+
// 3. Return step 2 for whole erase region
301296

302297
BlockDevice *block_device = BlockDevice::get_default_instance();
303298
TEST_SKIP_UNLESS_MESSAGE(block_device != NULL, "\nno block device found.\n");
@@ -326,7 +321,8 @@ void test_contiguous_erase_write_read()
326321
if (write_read_buf_size < program_size * 2) {
327322
write_read_buf_size = program_size * 2; // going over 10k
328323
}
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
330326
write_read_buf_size = contiguous_write_read_blocks_per_region * program_size;
331327
utest_printf("\ncontiguous_write_read_blocks_per_region=%" PRIu64, contiguous_write_read_blocks_per_region);
332328
utest_printf("\nwrite_read_buf_size=%" PRIu64, write_read_buf_size);
@@ -365,7 +361,8 @@ void test_contiguous_erase_write_read()
365361
for (size_t i = 0; i < write_read_buf_size; i++) {
366362
write_read_buf[i] = (uint8_t)rand();
367363
}
368-
utest_printf("\npre-filling memory, from 0x%" PRIx64 " of size 0x%" PRIx64, start_address + offset, write_read_buf_size);
364+
utest_printf("\npre-filling memory, from 0x%" PRIx64 " of size 0x%" PRIx64, start_address + offset,
365+
write_read_buf_size);
369366
err = block_device->program((const void *)write_read_buf, start_address + offset, write_read_buf_size);
370367
TEST_ASSERT_EQUAL(0, err);
371368
}
@@ -420,6 +417,61 @@ void test_contiguous_erase_write_read()
420417
TEST_ASSERT_EQUAL(0, err);
421418
}
422419

420+
void test_program_read_small_data_sizes()
421+
{
422+
utest_printf("\nTest 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, "\nno 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+
423475
// Test setup
424476
utest::v1::status_t test_setup(const size_t number_of_cases)
425477
{
@@ -428,10 +480,11 @@ utest::v1::status_t test_setup(const size_t number_of_cases)
428480
}
429481

430482
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)
435488
};
436489

437490
Specification specification(test_setup, cases);

0 commit comments

Comments
 (0)