diff options
author | ameerj <52414509+ameerj@users.noreply.github.com> | 2022-12-19 18:30:52 +0100 |
---|---|---|
committer | ameerj <52414509+ameerj@users.noreply.github.com> | 2022-12-20 00:08:04 +0100 |
commit | 64869807e2e4604f3d6334feeaf890515e9edb81 (patch) | |
tree | 5af972e67d4713d1a10919e100985e2383225680 /src/tests | |
parent | dma_pusher: Rework command_headers usage (diff) | |
download | yuzu-64869807e2e4604f3d6334feeaf890515e9edb81.tar yuzu-64869807e2e4604f3d6334feeaf890515e9edb81.tar.gz yuzu-64869807e2e4604f3d6334feeaf890515e9edb81.tar.bz2 yuzu-64869807e2e4604f3d6334feeaf890515e9edb81.tar.lz yuzu-64869807e2e4604f3d6334feeaf890515e9edb81.tar.xz yuzu-64869807e2e4604f3d6334feeaf890515e9edb81.tar.zst yuzu-64869807e2e4604f3d6334feeaf890515e9edb81.zip |
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/tests/common/scratch_buffer.cpp | 127 |
2 files changed, 128 insertions, 0 deletions
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 common/host_memory.cpp common/param_package.cpp common/ring_buffer.cpp + common/scratch_buffer.cpp common/unique_function.cpp core/core_timing.cpp 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 @@ +// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include <algorithm> +#include <array> +#include <span> +#include <catch2/catch.hpp> +#include "common/common_types.h" +#include "common/scratch_buffer.h" + +namespace Common { + +TEST_CASE("ScratchBuffer: Basic Test", "[common]") { + ScratchBuffer<u8> buf; + + REQUIRE(buf.size() == 0U); + REQUIRE(buf.capacity() == 0U); + + std::array<u8, 10> payload; + payload.fill(66); + + buf.resize(payload.size()); + REQUIRE(buf.size() == payload.size()); + REQUIRE(buf.capacity() == payload.size()); + + std::memcpy(buf.data(), payload.data(), payload.size()); + for (size_t i = 0; i < payload.size(); ++i) { + REQUIRE(buf[i] == payload[i]); + } +} + +TEST_CASE("ScratchBuffer: Resize Grow", "[common]") { + std::array<u8, 10> payload; + payload.fill(66); + + ScratchBuffer<u8> buf(payload.size()); + REQUIRE(buf.size() == payload.size()); + REQUIRE(buf.capacity() == payload.size()); + + // Increasing the size should reallocate the buffer + buf.resize(payload.size() * 2); + REQUIRE(buf.size() == payload.size() * 2); + REQUIRE(buf.capacity() == payload.size() * 2); + + // Since the buffer is not value initialized, reading its data will be garbage +} + +TEST_CASE("ScratchBuffer: Resize Shrink", "[common]") { + std::array<u8, 10> payload; + payload.fill(66); + + ScratchBuffer<u8> buf(payload.size()); + REQUIRE(buf.size() == payload.size()); + REQUIRE(buf.capacity() == payload.size()); + + std::memcpy(buf.data(), payload.data(), payload.size()); + for (size_t i = 0; i < payload.size(); ++i) { + REQUIRE(buf[i] == payload[i]); + } + + // Decreasing the size should not cause a buffer reallocation + // This can be tested by ensuring the buffer capacity and data has not changed, + buf.resize(1U); + REQUIRE(buf.size() == 1U); + REQUIRE(buf.capacity() == payload.size()); + + for (size_t i = 0; i < payload.size(); ++i) { + REQUIRE(buf[i] == payload[i]); + } +} + +TEST_CASE("ScratchBuffer: Span Size", "[common]") { + std::array<u8, 10> payload; + payload.fill(66); + + ScratchBuffer<u8> buf(payload.size()); + REQUIRE(buf.size() == payload.size()); + REQUIRE(buf.capacity() == payload.size()); + + std::memcpy(buf.data(), payload.data(), payload.size()); + for (size_t i = 0; i < payload.size(); ++i) { + REQUIRE(buf[i] == payload[i]); + } + + buf.resize(3U); + REQUIRE(buf.size() == 3U); + REQUIRE(buf.capacity() == payload.size()); + + const auto buf_span = std::span<u8>(buf); + // The span size is the last requested size of the buffer, not its capacity + REQUIRE(buf_span.size() == buf.size()); + + for (size_t i = 0; i < buf_span.size(); ++i) { + REQUIRE(buf_span[i] == buf[i]); + REQUIRE(buf_span[i] == payload[i]); + } +} + +TEST_CASE("ScratchBuffer: Span Writes", "[common]") { + std::array<u8, 10> payload; + payload.fill(66); + + ScratchBuffer<u8> buf(payload.size()); + REQUIRE(buf.size() == payload.size()); + REQUIRE(buf.capacity() == payload.size()); + + std::memcpy(buf.data(), payload.data(), payload.size()); + for (size_t i = 0; i < payload.size(); ++i) { + REQUIRE(buf[i] == payload[i]); + } + + buf.resize(3U); + REQUIRE(buf.size() == 3U); + REQUIRE(buf.capacity() == payload.size()); + + const auto buf_span = std::span<u8>(buf); + REQUIRE(buf_span.size() == buf.size()); + + for (size_t i = 0; i < buf_span.size(); ++i) { + const auto new_value = static_cast<u8>(i + 1U); + // Writes to a span of the scratch buffer will propogate to the buffer itself + buf_span[i] = new_value; + REQUIRE(buf[i] == new_value); + } +} + +} // namespace Common |