From c401662941ead07be5255ec10fb35962620efb47 Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Tue, 30 Oct 2018 17:13:45 +0200 Subject: [PATCH] Rework fseek/ftell tests ARM C library is really good at optimising out calls to underlying seek. The only ones we were actually detecting in the empty file case were the ones that the default FileHandle::size() made itself during the SEEK_END case. When we implement TestFile::size() directly, we will no longer see a single seek call from the ARM C library in the empty file case, so remove those tests. Beef up the non-empty file case, adding checks that we are making underlying read+write calls in the correct position, as a proxy for direct checks for underlying seek being called. --- TESTS/mbed_platform/FileHandle/main.cpp | 34 ++++++++++--------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/TESTS/mbed_platform/FileHandle/main.cpp b/TESTS/mbed_platform/FileHandle/main.cpp index 7d5a2c21f5..0bf9d2c18b 100644 --- a/TESTS/mbed_platform/FileHandle/main.cpp +++ b/TESTS/mbed_platform/FileHandle/main.cpp @@ -384,17 +384,15 @@ void test_fprintf_fscanf() /** Test fseek and ftell * - * Given already opened file is empty - * - * When set the file position indicator via fseek - * Then underneath retargeting layer seek function is called - * fseek return with succeed and ftell return already set position + * ARM library is quite good at optimising out unnecessary calls to underlying + * seek, so only test real non empty files. * * Given already opened file is not empty * * When set the file position indicator via fseek * Then underneath retargeting layer seek function is called * fseek return with succeed and ftell return already set position + * Check actual character read or written. * */ void test_fseek_ftell() @@ -413,19 +411,6 @@ void test_fseek_ftell() ftell_ret = std::ftell(file); TEST_ASSERT_EQUAL(0, ftell_ret); - TestFile::resetFunctionCallHistory(); - fssek_ret = std::fseek(file, 0, SEEK_CUR); - TEST_ASSERT_EQUAL(0, fssek_ret); - - TestFile::resetFunctionCallHistory(); - fssek_ret = std::fseek(file, 0, SEEK_SET); - TEST_ASSERT_EQUAL(0, fssek_ret); - - TestFile::resetFunctionCallHistory(); - fssek_ret = std::fseek(file, 0, SEEK_END); - TEST_ASSERT_TRUE(TestFile::functionCalled(TestFile::fnSeek)); - TEST_ASSERT_EQUAL(0, fssek_ret); - const char *str = "Hello world"; const std::size_t size = std::strlen(str); @@ -440,19 +425,28 @@ void test_fseek_ftell() TEST_ASSERT_EQUAL(0, fssek_ret); ftell_ret = std::ftell(file); TEST_ASSERT_EQUAL(5, ftell_ret); + int c = std::fgetc(file); + TEST_ASSERT_TRUE(TestFile::functionCalled(TestFile::fnRead)); + TEST_ASSERT_EQUAL(c, str[5]); TestFile::resetFunctionCallHistory(); - fssek_ret = std::fseek(file, -5, SEEK_CUR); + fssek_ret = std::fseek(file, -6, SEEK_CUR); TEST_ASSERT_EQUAL(0, fssek_ret); ftell_ret = std::ftell(file); TEST_ASSERT_EQUAL(0, ftell_ret); + c = std::fgetc(file); + TEST_ASSERT_TRUE(TestFile::functionCalled(TestFile::fnRead)); + TEST_ASSERT_EQUAL(c, str[0]); TestFile::resetFunctionCallHistory(); fssek_ret = std::fseek(file, 0, SEEK_END); - TEST_ASSERT_TRUE(TestFile::functionCalled(TestFile::fnSeek)); TEST_ASSERT_EQUAL(0, fssek_ret); ftell_ret = std::ftell(file); TEST_ASSERT_EQUAL(size, ftell_ret); + c = std::fputc('!', file); + TEST_ASSERT_TRUE(TestFile::functionCalled(TestFile::fnWrite)); + TEST_ASSERT_EQUAL(c, '!'); + TEST_ASSERT_EQUAL(fh.size(), size + 1); std::fclose(file); }