From 22ef265057be6428baaafd8d40d7ff35b9125524 Mon Sep 17 00:00:00 2001 From: Sandy Carter Date: Thu, 11 Jul 2019 10:26:59 -0400 Subject: [PATCH] [memory] Add Memory mapping view tests Add test for mapping and for mapping with access. --- src/xenia/base/testing/memory_test.cc | 55 +++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/xenia/base/testing/memory_test.cc b/src/xenia/base/testing/memory_test.cc index 5433dc118..66a365c8f 100644 --- a/src/xenia/base/testing/memory_test.cc +++ b/src/xenia/base/testing/memory_test.cc @@ -10,6 +10,9 @@ #include "xenia/base/memory.h" #include "third_party/catch/include/catch.hpp" +#include "third_party/fmt/include/fmt/format.h" + +#include "xenia/base/clock.h" namespace xe { namespace base { @@ -414,6 +417,58 @@ TEST_CASE("copy_and_swap_16_in_32_unaligned", "Copy and Swap") { REQUIRE(true == true); } +TEST_CASE("create_and_close_file_mapping", "Virtual Memory Mapping") { + auto path = fmt::format("Local\\xenia_test_{}", Clock::QueryHostTickCount()); + auto memory = xe::memory::CreateFileMappingHandle( + path, 0x100, xe::memory::PageAccess::kReadWrite, true); + REQUIRE(memory); + xe::memory::CloseFileMappingHandle(memory); +} + +TEST_CASE("map_view", "Virtual Memory Mapping") { + auto path = fmt::format("Local\\xenia_test_{}", Clock::QueryHostTickCount()); + const size_t length = 0x100; + auto memory = xe::memory::CreateFileMappingHandle( + path, length, xe::memory::PageAccess::kReadWrite, true); + REQUIRE(memory); + + uintptr_t address = 0x100000000; + auto view = + xe::memory::MapFileView(memory, reinterpret_cast(address), length, + xe::memory::PageAccess::kReadWrite, 0); + REQUIRE(reinterpret_cast(view) == address); + + xe::memory::UnmapFileView(memory, reinterpret_cast(address), length); + xe::memory::CloseFileMappingHandle(memory); +} + +TEST_CASE("read_write_view", "Virtual Memory Mapping") { + const size_t length = 0x100; + auto path = fmt::format("Local\\xenia_test_{}", Clock::QueryHostTickCount()); + auto memory = xe::memory::CreateFileMappingHandle( + path, length, xe::memory::PageAccess::kReadWrite, true); + REQUIRE(memory); + + uintptr_t address = 0x100000000; + auto view = + xe::memory::MapFileView(memory, reinterpret_cast(address), length, + xe::memory::PageAccess::kReadWrite, 0); + REQUIRE(reinterpret_cast(view) == address); + + for (uint32_t i = 0; i < length; i += sizeof(uint8_t)) { + auto p_value = reinterpret_cast(address + i); + *p_value = i; + } + for (uint32_t i = 0; i < length; i += sizeof(uint8_t)) { + auto p_value = reinterpret_cast(address + i); + uint8_t value = *p_value; + REQUIRE(value == i); + } + + xe::memory::UnmapFileView(memory, reinterpret_cast(address), length); + xe::memory::CloseFileMappingHandle(memory); +} + } // namespace test } // namespace base } // namespace xe