Skip to content

Commit

Permalink
Merge 01ba156 into cbdccfe
Browse files Browse the repository at this point in the history
  • Loading branch information
srz-zumix committed Jun 30, 2022
2 parents cbdccfe + 01ba156 commit 3bf43a9
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 19 deletions.
13 changes: 6 additions & 7 deletions include/internal/iutest_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ class StdioFile : public IFile
{
return GetSizeBySeekSet(fp);
}
// FIXME: https://github.com/srz-zumix/iutest/issues/227
return static_cast<size_t>(st.st_size);
return st.st_size;
#else
return GetSizeBySeekSet(fp);
#endif
Expand All @@ -271,12 +270,12 @@ class StdioFile : public IFile
{
return 0;
}
const long pre = ftell(fp);
if( (pre != -1) && (fseek(fp, 0, SEEK_END) == 0) )
const off_t pre = internal::posix::FileTell(fp);
if( (pre != -1) && (internal::posix::FileSeek(fp, 0, SEEK_END) == 0) )
{
const size_t size = static_cast<size_t>(ftell(fp));
IUTEST_UNUSED_RETURN(fseek(fp, pre, SEEK_SET));
return size;
const off_t size = internal::posix::FileTell(fp);
IUTEST_UNUSED_RETURN(internal::posix::FileSeek(fp, pre, SEEK_SET));
return static_cast<size_t>(size);
}
return 0;
}
Expand Down
26 changes: 26 additions & 0 deletions include/internal/iutest_port.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,32 @@ IUTEST_ATTRIBUTE_NORETURN_ void Abort();
inline void Abort() { abort(); }
#endif

#if IUTEST_HAS_FOPEN

inline off_t FileSeek(FILE* fp, off_t pos, int origin)
{
#if defined(_MSC_VER) || defined(IUTEST_OS_WINDOWS_MINGW)
return _fseeki64(fp, pos, origin);
#elif (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) || (defined(defined) && _POSIX_C_SOURCE >= 200112L)
return fseeko(fp, pos, origin);
#else
return fseek(fp, pos, origin);
#endif
}

inline off_t FileTell(FILE* fp)
{
#if defined(_MSC_VER) || defined(IUTEST_OS_WINDOWS_MINGW)
return _ftelli64(fp);
#elif (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) || (defined(defined) && _POSIX_C_SOURCE >= 200112L)
return ftello(fp);
#else
return ftell(fp);
#endif
}

#endif

#if IUTEST_HAS_HDR_UNISTD

#if defined(_MSC_VER) || defined(IUTEST_OS_WINDOWS_MINGW)
Expand Down
45 changes: 33 additions & 12 deletions test/unit_file_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,35 +75,56 @@ IUTEST(NoEffectFileUnitTest, Call)
IUTEST_EXPECT_EQ(0u, file.GetSize());
}

#if IUTEST_HAS_PARAM_TEST

#if IUTEST_HAS_STD_FILESYSTEM

const std::filesystem::path largefile("./testdata/largefile.bin");

class FileSystemTest : public ::iuutil::backward::Test<FileSystemTest>
class FileSystemTest : public ::iutest::TestWithParam<uintmax_t>
{
public:
static void SetUpTestCase()
void SetUp() IUTEST_CXX_OVERRIDE
{
IUTEST_ASSERT_TRUE(::std::filesystem::copy_file("./testdata/empty.bin", largefile, ::std::filesystem::copy_options::overwrite_existing));
::std::filesystem::resize_file(largefile, 0x100000000ull);
::std::filesystem::resize_file(largefile, GetParam());
}
static void TearDownTestCase()
void TearDown() IUTEST_CXX_OVERRIDE
{
::std::filesystem::remove(largefile);
}
};

#if IUTEST_HAS_FOPEN

// FIXME: 64bit GetSizeBySeekSet
// IUTEST_F(FileSystemTest, FileSize64bit)
// {
// IUTEST_ASSUME_EQ(0x100000000ull, ::std::filesystem::file_size(largefile));
IUTEST_P(FileSystemTest, GetSizeBySeekSet)
{
IUTEST_ASSUME_EQ(0x100000000ull, ::std::filesystem::file_size(largefile));

FILE* fp = fopen(largefile.string().c_str(), "rb");
IUTEST_ASSUME_NOTNULL(fp);
IUTEST_EXPECT_EQ(0x100000000ull, ::iutest::StdioFile::GetSizeBySeekSet(fp)) << ": " << sizeof(size_t);

const off_t pre = ::iutest::internal::posix::FileTell(fp);
IUTEST_EXPECT_EQ(0, pre);
IUTEST_EXPECT_EQ(0, ::iutest::internal::posix::FileSeek(fp, 0, SEEK_END));
const off_t size = ::iutest::internal::posix::FileTell(fp);
IUTEST_EXPECT_EQ(0x100000000ll, size);
IUTEST_EXPECT_EQ(0, ::iutest::internal::posix::FileSeek(fp, pre, SEEK_SET));
}

IUTEST_P(FileSystemTest, FileSize64bit)
{
IUTEST_ASSUME_EQ(0x100000000ull, ::std::filesystem::file_size(largefile));

::iutest::StdioFile file;
IUTEST_ASSERT_TRUE( file.Open(largefile, iutest::IFile::OpenRead) );
IUTEST_EXPECT_EQ(0x100000000ull, file.GetSize());
}

#endif

// ::iutest::StdioFile file;
// IUTEST_ASSERT_TRUE( file.Open(largefile, iutest::IFile::OpenRead) );
// IUTEST_EXPECT_EQ(0x100000000ull, file.GetSize());
// }
IUTEST_INSTANTIATE_TEST_SUITE_P(A, FileSystemTest, ::iutest::Values(0x100000000ull, 0x10000ull));

#endif

Expand Down
Empty file added unit_file_tests.cpp
Empty file.

0 comments on commit 3bf43a9

Please sign in to comment.