Skip to content

Commit f99ba33

Browse files
committed
esp_flash: improve unit test to detect over boundary issue
1 parent a9c8895 commit f99ba33

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

components/spi_flash/test/test_esp_flash.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ void test_simple_read_write(esp_flash_t *chip)
382382

383383
srand(test_seed);
384384
for (int i = 0; i < sizeof(sector_buf); i++) {
385-
TEST_ASSERT_EQUAL_HEX8(rand() & 0xFF, sector_buf[i]);
385+
uint8_t data = rand();
386+
TEST_ASSERT_EQUAL_HEX8(data, sector_buf[i]);
386387
}
387388
}
388389

@@ -412,17 +413,22 @@ FLASH_TEST_CASE_3("SPI flash unaligned read/write", test_unaligned_read_write);
412413

413414
void test_single_read_write(esp_flash_t* chip)
414415
{
416+
const int seed = 699;
415417
ESP_LOGI(TAG, "Testing chip %p...", chip);
416418
uint32_t offs = erase_test_region(chip, 2);
417419

420+
srand(seed);
418421
for (unsigned v = 0; v < 512; v++) {
419-
TEST_ASSERT_EQUAL_HEX(ESP_OK, esp_flash_write(chip, &v, offs + v, 1) );
422+
uint32_t data = rand();
423+
TEST_ASSERT_EQUAL_HEX(ESP_OK, esp_flash_write(chip, &data, offs + v, 1) );
420424
}
421425

426+
srand(seed);
422427
for (unsigned v = 0; v < 512; v++) {
423428
uint8_t readback;
429+
uint32_t data = rand();
424430
TEST_ASSERT_EQUAL_HEX(ESP_OK, esp_flash_read(chip, &readback, offs + v, 1) );
425-
TEST_ASSERT_EQUAL_HEX8(v, readback);
431+
TEST_ASSERT_EQUAL_HEX8(data, readback);
426432
}
427433
}
428434

@@ -435,18 +441,23 @@ FLASH_TEST_CASE_3("SPI flash single byte reads/writes", test_single_read_write);
435441
*/
436442
void test_three_byte_read_write(esp_flash_t *chip)
437443
{
444+
const int seed = 700;
438445
ESP_LOGI(TAG, "Testing chip %p...", chip);
439446
uint32_t offs = erase_test_region(chip, 2);
440447
ets_printf("offs:%X\n", offs);
441448

442-
for (uint32_t v = 0; v < 2000; v++) {
443-
TEST_ASSERT_EQUAL(ESP_OK, esp_flash_write(chip, &v, offs + 3 * v, 3) );
449+
srand(seed);
450+
for (uint32_t v = 0; v < 86; v++) {
451+
uint32_t data = rand();
452+
TEST_ASSERT_EQUAL(ESP_OK, esp_flash_write(chip, &data, offs + 3 * v, 3) );
444453
}
445454

446-
for (uint32_t v = 0; v < 2000; v++) {
455+
srand(seed);
456+
for (uint32_t v = 0; v < 1; v++) {
447457
uint32_t readback;
458+
uint32_t data = rand();
448459
TEST_ASSERT_EQUAL(ESP_OK, esp_flash_read(chip, &readback, offs + 3 * v, 3) );
449-
TEST_ASSERT_EQUAL_HEX32(v & 0xFFFFFF, readback & 0xFFFFFF);
460+
TEST_ASSERT_EQUAL_HEX32(data & 0xFFFFFF, readback & 0xFFFFFF);
450461
}
451462
}
452463

0 commit comments

Comments
 (0)