diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/scratch_buffer.h | 14 | ||||
| -rw-r--r-- | src/tests/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/tests/common/scratch_buffer.cpp | 127 |
3 files changed, 137 insertions, 5 deletions
diff --git a/src/common/scratch_buffer.h b/src/common/scratch_buffer.h index afbe2eee1..59bb8a9ea 100644 --- a/src/common/scratch_buffer.h +++ b/src/common/scratch_buffer.h | |||
| @@ -19,16 +19,16 @@ public: | |||
| 19 | ScratchBuffer() = default; | 19 | ScratchBuffer() = default; |
| 20 | 20 | ||
| 21 | explicit ScratchBuffer(size_t initial_capacity) | 21 | explicit ScratchBuffer(size_t initial_capacity) |
| 22 | : last_requested_size{initial_capacity}, capacity{initial_capacity}, | 22 | : last_requested_size{initial_capacity}, buffer_capacity{initial_capacity}, |
| 23 | buffer{Common::make_unique_for_overwrite<T[]>(initial_capacity)} {} | 23 | buffer{Common::make_unique_for_overwrite<T[]>(initial_capacity)} {} |
| 24 | 24 | ||
| 25 | ~ScratchBuffer() = default; | 25 | ~ScratchBuffer() = default; |
| 26 | 26 | ||
| 27 | /// This will only grow the buffer's capacity if size is greater than the current capacity. | 27 | /// This will only grow the buffer's capacity if size is greater than the current capacity. |
| 28 | void resize(size_t size) { | 28 | void resize(size_t size) { |
| 29 | if (size > capacity) { | 29 | if (size > buffer_capacity) { |
| 30 | capacity = size; | 30 | buffer_capacity = size; |
| 31 | buffer = Common::make_unique_for_overwrite<T[]>(capacity); | 31 | buffer = Common::make_unique_for_overwrite<T[]>(buffer_capacity); |
| 32 | } | 32 | } |
| 33 | last_requested_size = size; | 33 | last_requested_size = size; |
| 34 | } | 34 | } |
| @@ -65,9 +65,13 @@ public: | |||
| 65 | return last_requested_size; | 65 | return last_requested_size; |
| 66 | } | 66 | } |
| 67 | 67 | ||
| 68 | [[nodiscard]] size_t capacity() const noexcept { | ||
| 69 | return buffer_capacity; | ||
| 70 | } | ||
| 71 | |||
| 68 | private: | 72 | private: |
| 69 | size_t last_requested_size{}; | 73 | size_t last_requested_size{}; |
| 70 | size_t capacity{}; | 74 | size_t buffer_capacity{}; |
| 71 | std::unique_ptr<T[]> buffer{}; | 75 | std::unique_ptr<T[]> buffer{}; |
| 72 | }; | 76 | }; |
| 73 | 77 | ||
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 348d1edf4..6a4022e45 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt | |||
| @@ -8,6 +8,7 @@ add_executable(tests | |||
| 8 | common/host_memory.cpp | 8 | common/host_memory.cpp |
| 9 | common/param_package.cpp | 9 | common/param_package.cpp |
| 10 | common/ring_buffer.cpp | 10 | common/ring_buffer.cpp |
| 11 | common/scratch_buffer.cpp | ||
| 11 | common/unique_function.cpp | 12 | common/unique_function.cpp |
| 12 | core/core_timing.cpp | 13 | core/core_timing.cpp |
| 13 | core/internal_network/network.cpp | 14 | core/internal_network/network.cpp |
diff --git a/src/tests/common/scratch_buffer.cpp b/src/tests/common/scratch_buffer.cpp new file mode 100644 index 000000000..a59490f55 --- /dev/null +++ b/src/tests/common/scratch_buffer.cpp | |||
| @@ -0,0 +1,127 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #include <algorithm> | ||
| 5 | #include <array> | ||
| 6 | #include <span> | ||
| 7 | #include <catch2/catch.hpp> | ||
| 8 | #include "common/common_types.h" | ||
| 9 | #include "common/scratch_buffer.h" | ||
| 10 | |||
| 11 | namespace Common { | ||
| 12 | |||
| 13 | TEST_CASE("ScratchBuffer: Basic Test", "[common]") { | ||
| 14 | ScratchBuffer<u8> buf; | ||
| 15 | |||
| 16 | REQUIRE(buf.size() == 0U); | ||
| 17 | REQUIRE(buf.capacity() == 0U); | ||
| 18 | |||
| 19 | std::array<u8, 10> payload; | ||
| 20 | payload.fill(66); | ||
| 21 | |||
| 22 | buf.resize(payload.size()); | ||
| 23 | REQUIRE(buf.size() == payload.size()); | ||
| 24 | REQUIRE(buf.capacity() == payload.size()); | ||
| 25 | |||
| 26 | std::memcpy(buf.data(), payload.data(), payload.size()); | ||
| 27 | for (size_t i = 0; i < payload.size(); ++i) { | ||
| 28 | REQUIRE(buf[i] == payload[i]); | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | TEST_CASE("ScratchBuffer: Resize Grow", "[common]") { | ||
| 33 | std::array<u8, 10> payload; | ||
| 34 | payload.fill(66); | ||
| 35 | |||
| 36 | ScratchBuffer<u8> buf(payload.size()); | ||
| 37 | REQUIRE(buf.size() == payload.size()); | ||
| 38 | REQUIRE(buf.capacity() == payload.size()); | ||
| 39 | |||
| 40 | // Increasing the size should reallocate the buffer | ||
| 41 | buf.resize(payload.size() * 2); | ||
| 42 | REQUIRE(buf.size() == payload.size() * 2); | ||
| 43 | REQUIRE(buf.capacity() == payload.size() * 2); | ||
| 44 | |||
| 45 | // Since the buffer is not value initialized, reading its data will be garbage | ||
| 46 | } | ||
| 47 | |||
| 48 | TEST_CASE("ScratchBuffer: Resize Shrink", "[common]") { | ||
| 49 | std::array<u8, 10> payload; | ||
| 50 | payload.fill(66); | ||
| 51 | |||
| 52 | ScratchBuffer<u8> buf(payload.size()); | ||
| 53 | REQUIRE(buf.size() == payload.size()); | ||
| 54 | REQUIRE(buf.capacity() == payload.size()); | ||
| 55 | |||
| 56 | std::memcpy(buf.data(), payload.data(), payload.size()); | ||
| 57 | for (size_t i = 0; i < payload.size(); ++i) { | ||
| 58 | REQUIRE(buf[i] == payload[i]); | ||
| 59 | } | ||
| 60 | |||
| 61 | // Decreasing the size should not cause a buffer reallocation | ||
| 62 | // This can be tested by ensuring the buffer capacity and data has not changed, | ||
| 63 | buf.resize(1U); | ||
| 64 | REQUIRE(buf.size() == 1U); | ||
| 65 | REQUIRE(buf.capacity() == payload.size()); | ||
| 66 | |||
| 67 | for (size_t i = 0; i < payload.size(); ++i) { | ||
| 68 | REQUIRE(buf[i] == payload[i]); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | TEST_CASE("ScratchBuffer: Span Size", "[common]") { | ||
| 73 | std::array<u8, 10> payload; | ||
| 74 | payload.fill(66); | ||
| 75 | |||
| 76 | ScratchBuffer<u8> buf(payload.size()); | ||
| 77 | REQUIRE(buf.size() == payload.size()); | ||
| 78 | REQUIRE(buf.capacity() == payload.size()); | ||
| 79 | |||
| 80 | std::memcpy(buf.data(), payload.data(), payload.size()); | ||
| 81 | for (size_t i = 0; i < payload.size(); ++i) { | ||
| 82 | REQUIRE(buf[i] == payload[i]); | ||
| 83 | } | ||
| 84 | |||
| 85 | buf.resize(3U); | ||
| 86 | REQUIRE(buf.size() == 3U); | ||
| 87 | REQUIRE(buf.capacity() == payload.size()); | ||
| 88 | |||
| 89 | const auto buf_span = std::span<u8>(buf); | ||
| 90 | // The span size is the last requested size of the buffer, not its capacity | ||
| 91 | REQUIRE(buf_span.size() == buf.size()); | ||
| 92 | |||
| 93 | for (size_t i = 0; i < buf_span.size(); ++i) { | ||
| 94 | REQUIRE(buf_span[i] == buf[i]); | ||
| 95 | REQUIRE(buf_span[i] == payload[i]); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | TEST_CASE("ScratchBuffer: Span Writes", "[common]") { | ||
| 100 | std::array<u8, 10> payload; | ||
| 101 | payload.fill(66); | ||
| 102 | |||
| 103 | ScratchBuffer<u8> buf(payload.size()); | ||
| 104 | REQUIRE(buf.size() == payload.size()); | ||
| 105 | REQUIRE(buf.capacity() == payload.size()); | ||
| 106 | |||
| 107 | std::memcpy(buf.data(), payload.data(), payload.size()); | ||
| 108 | for (size_t i = 0; i < payload.size(); ++i) { | ||
| 109 | REQUIRE(buf[i] == payload[i]); | ||
| 110 | } | ||
| 111 | |||
| 112 | buf.resize(3U); | ||
| 113 | REQUIRE(buf.size() == 3U); | ||
| 114 | REQUIRE(buf.capacity() == payload.size()); | ||
| 115 | |||
| 116 | const auto buf_span = std::span<u8>(buf); | ||
| 117 | REQUIRE(buf_span.size() == buf.size()); | ||
| 118 | |||
| 119 | for (size_t i = 0; i < buf_span.size(); ++i) { | ||
| 120 | const auto new_value = static_cast<u8>(i + 1U); | ||
| 121 | // Writes to a span of the scratch buffer will propogate to the buffer itself | ||
| 122 | buf_span[i] = new_value; | ||
| 123 | REQUIRE(buf[i] == new_value); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | } // namespace Common | ||