44
55#include " openvino/runtime/shared_buffer.hpp"
66
7+ #include < algorithm>
78#include < filesystem>
89#include < fstream>
910#include < sstream>
@@ -416,40 +417,48 @@ TEST(MappedMemory, get_id_same_for_same_file) {
416417 std::filesystem::remove (file_path);
417418}
418419
419- using PartialMappingTestRegions = // offset_1, size_1, offset_2, size_2
420- std::tuple<size_t , size_t , size_t , size_t >;
421- using PartialMappingTestParams = // offset-size pairs, use file path (true) or file handle (false)
422- std::tuple<PartialMappingTestRegions , bool >;
420+ using RangedMappingTestRegions = // offset_1, size_1, offset_2, size_2, file_size (0 means auto-calculate)
421+ std::tuple<size_t , size_t , size_t , size_t , size_t >;
422+ using RangedMappingTestParams = // offset-size pairs, use file path (true) or file handle (false)
423+ std::tuple<RangedMappingTestRegions , bool >;
423424
424- class PartialMappingTest : public ::testing::TestWithParam<PartialMappingTestParams > {
425+ class RangedMappingTest : public ::testing::TestWithParam<RangedMappingTestParams > {
425426protected:
426427 std::filesystem::path m_file_path;
427428 std::vector<char > m_data_1, m_data_2;
428429
429430 void SetUp () override {
430431 const auto & [regions, use_file_path] = GetParam ();
431- const auto & [offset_1, size_1, offset_2, size_2] = regions;
432- // todo - add test with overlapping regions when it's supported by implementation
433- ASSERT_TRUE (offset_2 >= offset_1 + size_1 || (offset_2 == 0 && size_2 == 0 ));
432+ const auto & [offset_1, size_1, offset_2, size_2, file_size] = regions;
434433
435- m_data_1.resize (size_1);
436- m_data_2.resize (size_2);
437- for (size_t i = 0 ; i < size_1; ++i) {
434+ const auto actual_file_size = file_size ? file_size : std::max (offset_1 + size_1, offset_2 + size_2);
435+ const auto actual_size_1 = size_1 ? size_1 : actual_file_size - offset_1;
436+ const auto actual_size_2 = size_2 ? size_2 : actual_file_size - offset_2;
437+ ASSERT_LE (offset_1 + actual_size_1, actual_file_size);
438+ ASSERT_LE (offset_2 + actual_size_2, actual_file_size);
439+
440+ m_data_1.resize (actual_size_1);
441+ m_data_2.resize (actual_size_2);
442+ for (size_t i = 0 ; i < actual_size_1; ++i) {
438443 m_data_1[i] = static_cast <char >(' A' + i % 26 );
439444 }
440- for (size_t i = 0 ; i < size_2 ; ++i) {
445+ for (size_t i = 0 ; i < actual_size_2 ; ++i) {
441446 m_data_2[i] = static_cast <char >(' a' + i % 26 );
442447 }
443448
444- std::vector<char > buffer (offset_2 + size_2 , ' _' );
445- for (size_t i = 0 ; i < size_1 ; ++i) {
449+ std::vector<char > buffer (actual_file_size , ' _' );
450+ for (size_t i = 0 ; i < actual_size_1 ; ++i) {
446451 buffer[offset_1 + i] = m_data_1[i];
447452 }
448- for (size_t i = 0 ; i < size_2 ; ++i) {
453+ for (size_t i = 0 ; i < actual_size_2 ; ++i) {
449454 buffer[offset_2 + i] = m_data_2[i];
450455 }
456+ // In case the ranges overlap
457+ for (size_t i = 0 ; i < actual_size_1; ++i) {
458+ m_data_1[i] = buffer[offset_1 + i];
459+ }
451460
452- m_file_path = utils::generateTestFilePrefix () + " _partial_mapping " ;
461+ m_file_path = utils::generateTestFilePrefix ();
453462 std::ofstream s (m_file_path, std::ios::binary);
454463 ASSERT_TRUE (s.is_open ());
455464 s.write (buffer.data (), buffer.size ());
@@ -461,12 +470,12 @@ class PartialMappingTest : public ::testing::TestWithParam<PartialMappingTestPar
461470 }
462471
463472public:
464- static std::string test_name (const testing::TestParamInfo<PartialMappingTestParams >& info) {
473+ static std::string test_name (const testing::TestParamInfo<RangedMappingTestParams >& info) {
465474 const auto & [regions, use_file_path] = info.param ;
466- const auto & [offset_1, size_1, offset_2, size_2] = regions;
475+ const auto & [offset_1, size_1, offset_2, size_2, file_size ] = regions;
467476 std::ostringstream ss;
468477 ss << " offset1_" << offset_1 << " _size1_" << size_1 << " _offset2_" << offset_2 << " _size2_" << size_2
469- << (use_file_path ? " _file_path" : " _file_handle" );
478+ << " _file_size_ " << file_size << (use_file_path ? " _file_path" : " _file_handle" );
470479 return ss.str ();
471480 }
472481};
@@ -492,9 +501,9 @@ FileHandle open_file(const std::filesystem::path& path) {
492501#endif
493502} // namespace
494503
495- TEST_P (PartialMappingTest , compare_data) {
504+ TEST_P (RangedMappingTest , compare_data) {
496505 const auto & [regions, use_file_path] = GetParam ();
497- const auto & [offset_1, size_1, offset_2, size_2] = regions;
506+ const auto & [offset_1, size_1, offset_2, size_2, file_size ] = regions;
498507 std::shared_ptr<MappedMemory> mm_1, mm_2;
499508
500509 if (use_file_path) {
@@ -509,20 +518,20 @@ TEST_P(PartialMappingTest, compare_data) {
509518 ASSERT_NE (mm_1, nullptr );
510519 ASSERT_NE (mm_2, nullptr );
511520
512- EXPECT_EQ (mm_1->size (), size_1 );
513- EXPECT_EQ (mm_2->size (), size_2 );
521+ EXPECT_EQ (mm_1->size (), m_data_1. size () );
522+ EXPECT_EQ (mm_2->size (), m_data_2. size () );
514523 EXPECT_EQ (m_data_1, std::vector<char >(mm_1->data (), mm_1->data () + mm_1->size ()));
515524 EXPECT_EQ (m_data_2, std::vector<char >(mm_2->data (), mm_2->data () + mm_2->size ()));
516525}
517526
518- TEST_P (PartialMappingTest , compare_id) {
527+ TEST_P (RangedMappingTest , compare_id) {
519528 const auto & [regions, use_file_path] = GetParam ();
520529 if (!use_file_path) {
521530 GTEST_SKIP (); // CVS-182260
522531 }
523- const auto & [offset_1, size_1, offset_2, size_2] = regions;
532+ const auto & [offset_1, size_1, offset_2, size_2, file_size ] = regions;
524533
525- std::filesystem::path other_file_path = utils::generateTestFilePrefix () + " _partial_mapping " ;
534+ std::filesystem::path other_file_path = utils::generateTestFilePrefix ();
526535 std::error_code ec;
527536 std::filesystem::copy_file (m_file_path, other_file_path, ec);
528537 ASSERT_FALSE (ec) << " Failed to copy file \" " << m_file_path << " \" for test setup: " << ec.message ();
@@ -560,14 +569,18 @@ TEST_P(PartialMappingTest, compare_id) {
560569
561570static auto pg_sz = util::get_system_page_size();
562571INSTANTIATE_TEST_SUITE_P (MappedMemory,
563- PartialMappingTest,
564- ::testing::Combine (::testing::ValuesIn(std::vector<PartialMappingTestRegions>{
565- // {pg_sz, pg_sz / 2, 0, 0},
566- {pg_sz, 127 , 2 * pg_sz, 101 },
567- {0 , 3 * pg_sz, 7 * pg_sz, 1024 },
568- {0 , pg_sz, pg_sz, pg_sz}}),
572+ RangedMappingTest,
573+ ::testing::Combine (::testing::ValuesIn(std::vector<RangedMappingTestRegions>{
574+ {pg_sz, 127 , 2 * pg_sz, 101 , 0 },
575+ {0 , 3 * pg_sz, 7 * pg_sz, 1024 , 0 },
576+ {0 , pg_sz, pg_sz, pg_sz, 0 },
577+ {100 , 50 , 150 , 30 , 180 },
578+ {1 , 0 , 0 , 0 , pg_sz},
579+ {0 , 40 , 0 , 41 , 42 },
580+ {11 , 0 , 17 , 80 , 101 },
581+ {10 , 0 , 0 , 90 , 100 }}),
569582 ::testing::ValuesIn(std::vector<bool >{true , false })),
570- PartialMappingTest ::test_name);
583+ RangedMappingTest ::test_name);
571584
572585// ==================== SharedBuffer with explicit descriptor Tests ====================
573586
0 commit comments