@@ -213,6 +213,81 @@ void test_multi_threads()
213
213
TEST_ASSERT_EQUAL (0 , err);
214
214
}
215
215
216
+ void test_get_erase_value ()
217
+ {
218
+ utest_printf (" \n Test BlockDevice::get_erase_value()..\n " );
219
+
220
+ // Test flow:
221
+ // 1. Write data to selected region
222
+ // - Known starting point
223
+ // 2. Erase selected region
224
+ // 3. Read erased region and compare with get_erase_value()
225
+
226
+ BlockDevice *block_device = BlockDevice::get_default_instance ();
227
+ TEST_SKIP_UNLESS_MESSAGE (block_device != NULL , " \n no block device found.\n " );
228
+
229
+ int err = block_device->init ();
230
+ TEST_ASSERT_EQUAL (0 , err);
231
+
232
+ // Check erase value
233
+ int erase_value_int = block_device->get_erase_value ();
234
+ utest_printf (" \n block_device->get_erase_value()=%d" , erase_value_int);
235
+ TEST_SKIP_UNLESS_MESSAGE (erase_value_int >= 0 , " \n erase value is negative which means the erase value is unknown\n " );
236
+
237
+ // Assuming that get_erase_value() returns byte value as documentation mentions
238
+ // "If get_erase_value() returns a non-negative byte value" for unknown case.
239
+ TEST_ASSERT (erase_value_int <= 255 );
240
+ uint8_t erase_value = (uint8_t )erase_value_int;
241
+
242
+ // Determine data_buf_size
243
+ bd_size_t erase_size = block_device->get_erase_size ();
244
+ TEST_ASSERT (erase_size > 0 );
245
+ bd_size_t data_buf_size = erase_size;
246
+
247
+ // Determine start_address
248
+ bd_addr_t start_address = rand (); // low 32 bytes
249
+ start_address += (uint64_t )rand () << 32 ; // high 32 bytes
250
+ start_address %= block_device->size () - data_buf_size - erase_size; // fit all data + alignment reserve
251
+ start_address += erase_size; // add alignment reserve
252
+ start_address -= start_address % erase_size; // align with erase_block
253
+ utest_printf (" \n start_address=0x%016" PRIx64, start_address);
254
+
255
+ // Allocate buffer for read test data
256
+ uint8_t *data_buf = (uint8_t *)malloc (data_buf_size);
257
+ TEST_ASSERT_NOT_NULL (data_buf);
258
+
259
+ // Write random data to selected region to make sure data is not accidentally set to "erased" value.
260
+ // With this pre-write, the test case will fail even if block_device->erase() is broken.
261
+ for (bd_size_t i=0 ; i<data_buf_size; i++) {
262
+ data_buf[i] = (uint8_t ) rand ();
263
+ }
264
+ utest_printf (" \n writing given memory region" );
265
+ err = block_device->program ((const void *)data_buf, start_address, data_buf_size);
266
+ TEST_ASSERT_EQUAL (0 , err);
267
+
268
+ // Erase given memory region
269
+ utest_printf (" \n erasing given memory region" );
270
+ err = block_device->erase (start_address, data_buf_size);
271
+ TEST_ASSERT_EQUAL (0 , err);
272
+
273
+ // Read erased memory region
274
+ utest_printf (" \n reading erased memory region" );
275
+ err = block_device->read ((void *)data_buf, start_address, data_buf_size);
276
+ TEST_ASSERT_EQUAL (0 , err);
277
+
278
+ // Verify erased memory region
279
+ utest_printf (" \n verifying erased memory region" );
280
+ for (bd_size_t i=0 ; i<data_buf_size; i++) {
281
+ TEST_ASSERT_EQUAL (erase_value, data_buf[i]);
282
+ }
283
+
284
+ free (data_buf);
285
+
286
+ // BlockDevice deinitialization
287
+ err = block_device->deinit ();
288
+ TEST_ASSERT_EQUAL (0 , err);
289
+ }
290
+
216
291
void test_contiguous_erase_write_read ()
217
292
{
218
293
utest_printf (" \n Test Contiguous Erase/Program/Read Starts..\n " );
@@ -355,7 +430,8 @@ utest::v1::status_t test_setup(const size_t number_of_cases)
355
430
Case cases[] = {
356
431
Case (" Testing read write random blocks" , test_random_program_read_erase),
357
432
Case (" Testing Multi Threads Erase Program Read" , test_multi_threads),
358
- Case (" Testing contiguous erase, write and read" , test_contiguous_erase_write_read)
433
+ Case (" Testing contiguous erase, write and read" , test_contiguous_erase_write_read),
434
+ Case (" Test BlockDevice::get_erase_value()" , test_get_erase_value)
359
435
};
360
436
361
437
Specification specification (test_setup, cases);
0 commit comments