Skip to content

Commit eed3fb1

Browse files
author
Cruz Monrreal
authored
Merge pull request #9109 from kjbracey-arm/fseek_test_rework
Rework fseek/ftell tests
2 parents 463a453 + ff7a316 commit eed3fb1

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

TESTS/mbed_platform/FileHandle/main.cpp

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -384,17 +384,15 @@ void test_fprintf_fscanf()
384384

385385
/** Test fseek and ftell
386386
*
387-
* Given already opened file is empty
388-
*
389-
* When set the file position indicator via fseek
390-
* Then underneath retargeting layer seek function is called
391-
* fseek return with succeed and ftell return already set position
387+
* ARM library is quite good at optimising out unnecessary calls to underlying
388+
* seek, so only test real non empty files.
392389
*
393390
* Given already opened file is not empty
394391
*
395392
* When set the file position indicator via fseek
396393
* Then underneath retargeting layer seek function is called
397394
* fseek return with succeed and ftell return already set position
395+
* Check actual character read or written.
398396
*
399397
*/
400398
void test_fseek_ftell()
@@ -413,19 +411,6 @@ void test_fseek_ftell()
413411
ftell_ret = std::ftell(file);
414412
TEST_ASSERT_EQUAL(0, ftell_ret);
415413

416-
TestFile<FS>::resetFunctionCallHistory();
417-
fssek_ret = std::fseek(file, 0, SEEK_CUR);
418-
TEST_ASSERT_EQUAL(0, fssek_ret);
419-
420-
TestFile<FS>::resetFunctionCallHistory();
421-
fssek_ret = std::fseek(file, 0, SEEK_SET);
422-
TEST_ASSERT_EQUAL(0, fssek_ret);
423-
424-
TestFile<FS>::resetFunctionCallHistory();
425-
fssek_ret = std::fseek(file, 0, SEEK_END);
426-
TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnSeek));
427-
TEST_ASSERT_EQUAL(0, fssek_ret);
428-
429414
const char *str = "Hello world";
430415
const std::size_t size = std::strlen(str);
431416

@@ -440,19 +425,28 @@ void test_fseek_ftell()
440425
TEST_ASSERT_EQUAL(0, fssek_ret);
441426
ftell_ret = std::ftell(file);
442427
TEST_ASSERT_EQUAL(5, ftell_ret);
428+
int c = std::fgetc(file);
429+
TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnRead));
430+
TEST_ASSERT_EQUAL(c, str[5]);
443431

444432
TestFile<FS>::resetFunctionCallHistory();
445-
fssek_ret = std::fseek(file, -5, SEEK_CUR);
433+
fssek_ret = std::fseek(file, -6, SEEK_CUR);
446434
TEST_ASSERT_EQUAL(0, fssek_ret);
447435
ftell_ret = std::ftell(file);
448436
TEST_ASSERT_EQUAL(0, ftell_ret);
437+
c = std::fgetc(file);
438+
TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnRead));
439+
TEST_ASSERT_EQUAL(c, str[0]);
449440

450441
TestFile<FS>::resetFunctionCallHistory();
451442
fssek_ret = std::fseek(file, 0, SEEK_END);
452-
TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnSeek));
453443
TEST_ASSERT_EQUAL(0, fssek_ret);
454444
ftell_ret = std::ftell(file);
455445
TEST_ASSERT_EQUAL(size, ftell_ret);
446+
c = std::fputc('!', file);
447+
TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnWrite));
448+
TEST_ASSERT_EQUAL(c, '!');
449+
TEST_ASSERT_EQUAL(fh.size(), size + 1);
456450

457451
std::fclose(file);
458452
}

0 commit comments

Comments
 (0)