diff options
author | Kirill Zhdanovich <kzhdanovich@gmail.com> | 2013-01-21 18:58:22 +0400 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 01:49:01 +0300 |
commit | 5d0bde083b0ce93fc908fec0115af724de369a58 (patch) | |
tree | a1cb1c0c8b46a60c66e3488e32aa96dd40bbcb2b /coding | |
parent | afd954e47fd96984431a91961f591131b102c7a6 (diff) |
function that compares files added
function tests
Diffstat (limited to 'coding')
-rw-r--r-- | coding/coding_tests/file_data_test.cpp | 61 | ||||
-rw-r--r-- | coding/internal/file_data.cpp | 28 | ||||
-rw-r--r-- | coding/internal/file_data.hpp | 1 |
3 files changed, 90 insertions, 0 deletions
diff --git a/coding/coding_tests/file_data_test.cpp b/coding/coding_tests/file_data_test.cpp index 69a1511638..85b0bcd272 100644 --- a/coding/coding_tests/file_data_test.cpp +++ b/coding/coding_tests/file_data_test.cpp @@ -17,6 +17,12 @@ namespace f.Write(name.c_str(), name.size()); } + void MakeFile(string const & name, size_t const size, const char c) + { + my::FileData f(name, my::FileData::OP_WRITE_TRUNCATE); + f.Write(string(size, c).c_str(), size); + } + void CheckFileOK(string const & name) { my::FileData f(name, my::FileData::OP_READ); @@ -106,3 +112,58 @@ UNIT_TEST(FileData_SharingAV_Windows) TEST(my::DeleteFileX(name2), ()); } #endif + +UNIT_TEST(Equal_Function_Test) +{ + MakeFile(name1); + MakeFile(name2); + TEST(my::IsEqualFiles(name1, name1), ()); + TEST(my::IsEqualFiles(name2, name2), ()); + TEST(!my::IsEqualFiles(name1, name2), ()); + + TEST(my::DeleteFileX(name1), ()); + TEST(my::DeleteFileX(name2), ()); +} + +UNIT_TEST(Equal_Function_Test_For_Big_Files) +{ + { + MakeFile(name1, 1024 * 1024, 'a'); + MakeFile(name2, 1024 * 1024, 'a'); + TEST(my::IsEqualFiles(name1, name2), ()); + TEST(my::DeleteFileX(name1), ()); + TEST(my::DeleteFileX(name2), ()); + } + { + MakeFile(name1, 1024 * 1024 + 512, 'a'); + MakeFile(name2, 1024 * 1024 + 512, 'a'); + TEST(my::IsEqualFiles(name1, name2), ()); + TEST(my::DeleteFileX(name1), ()); + TEST(my::DeleteFileX(name2), ()); + } + { + MakeFile(name1, 1024 * 1024 + 1, 'a'); + MakeFile(name2, 1024 * 1024 + 1, 'b'); + TEST(my::IsEqualFiles(name1, name1), ()); + TEST(my::IsEqualFiles(name2, name2), ()); + TEST(!my::IsEqualFiles(name1, name2), ()); + TEST(my::DeleteFileX(name1), ()); + TEST(my::DeleteFileX(name2), ()); + } + { + MakeFile(name1, 1024 * 1024, 'a'); + MakeFile(name2, 1024 * 1024, 'b'); + TEST(my::IsEqualFiles(name1, name1), ()); + TEST(my::IsEqualFiles(name2, name2), ()); + TEST(!my::IsEqualFiles(name1, name2), ()); + TEST(my::DeleteFileX(name1), ()); + TEST(my::DeleteFileX(name2), ()); + } + { + MakeFile(name1, 1024 * 1024, 'a'); + MakeFile(name2, 1024 * 1024 + 1, 'b'); + TEST(!my::IsEqualFiles(name1, name2), ()); + TEST(my::DeleteFileX(name1), ()); + TEST(my::DeleteFileX(name2), ()); + } +} diff --git a/coding/internal/file_data.cpp b/coding/internal/file_data.cpp index cfdeb62fde..d437376599 100644 --- a/coding/internal/file_data.cpp +++ b/coding/internal/file_data.cpp @@ -263,4 +263,32 @@ bool CopyFile(string const & fOld, string const & fNew) return false; } +bool IsEqualFiles(string const & firstFile, string const & secondFile) +{ + my::FileData first(firstFile, my::FileData::OP_READ); + my::FileData second(secondFile, my::FileData::OP_READ); + if (first.Size() != second.Size()) + return false; + + size_t const bufSize = 512 * 1024; + vector<char> buf1, buf2; + buf1.resize(bufSize); + buf2.resize(bufSize); + size_t const fileSize = first.Size(); + size_t currSize = 0; + + while (currSize < fileSize) + { + size_t toRead = fileSize - currSize; + if (toRead > bufSize) + toRead = bufSize; + first.Read(step * bufSize, &buf1[0], readingLength); + second.Read(step * bufSize, &buf2[0], readingLength); + if (buf1 != buf2) + return false; + currSize += toRead; + } + return true; +} + } diff --git a/coding/internal/file_data.hpp b/coding/internal/file_data.hpp index 8dd47d83d2..e5ec462ecb 100644 --- a/coding/internal/file_data.hpp +++ b/coding/internal/file_data.hpp @@ -49,5 +49,6 @@ bool DeleteFileX(string const & fName); bool RenameFileX(string const & fOld, string const & fNew); /// @return false if copy fails. DO NOT THROWS exceptions bool CopyFile(string const & fOld, string const & fNew); +bool IsEqualFiles(string const & firstFile, string const & secondFile); } |