aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tests
diff options
context:
space:
mode:
authorameerj <[email protected]>2022-12-19 22:40:50 -0500
committerameerj <[email protected]>2022-12-19 22:40:50 -0500
commitc6590ad07b384762fd90ee8852796ec681a69286 (patch)
treec0d8d2d157f3bb4be01331f2da459c0f68ca4d7b /src/tests
parent64869807e2e4604f3d6334feeaf890515e9edb81 (diff)
downloadyuzu-mainline-c6590ad07b384762fd90ee8852796ec681a69286.tar.gz
yuzu-mainline-c6590ad07b384762fd90ee8852796ec681a69286.zip
scratch_buffer: Explicitly defing resize and resize_destructive functions
resize keeps previous data intact when the buffer grows resize_destructive destroys the previous data when the buffer grows
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/common/scratch_buffer.cpp78
1 files changed, 75 insertions, 3 deletions
diff --git a/src/tests/common/scratch_buffer.cpp b/src/tests/common/scratch_buffer.cpp
index a59490f55..b602c8d0a 100644
--- a/src/tests/common/scratch_buffer.cpp
+++ b/src/tests/common/scratch_buffer.cpp
@@ -29,7 +29,7 @@ TEST_CASE("ScratchBuffer: Basic Test", "[common]") {
}
}
-TEST_CASE("ScratchBuffer: Resize Grow", "[common]") {
+TEST_CASE("ScratchBuffer: resize_destructive Grow", "[common]") {
std::array<u8, 10> payload;
payload.fill(66);
@@ -38,14 +38,86 @@ TEST_CASE("ScratchBuffer: Resize Grow", "[common]") {
REQUIRE(buf.capacity() == payload.size());
// Increasing the size should reallocate the buffer
- buf.resize(payload.size() * 2);
+ buf.resize_destructive(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]") {
+TEST_CASE("ScratchBuffer: resize_destructive 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_destructive(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: resize Grow u8", "[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]);
+ }
+
+ // Increasing the size should reallocate the buffer
+ buf.resize(payload.size() * 2);
+ REQUIRE(buf.size() == payload.size() * 2);
+ REQUIRE(buf.capacity() == payload.size() * 2);
+
+ // resize() keeps the previous data intact
+ for (size_t i = 0; i < payload.size(); ++i) {
+ REQUIRE(buf[i] == payload[i]);
+ }
+}
+
+TEST_CASE("ScratchBuffer: resize Grow u64", "[common]") {
+ std::array<u64, 10> payload;
+ payload.fill(6666);
+
+ ScratchBuffer<u64> buf(payload.size());
+ REQUIRE(buf.size() == payload.size());
+ REQUIRE(buf.capacity() == payload.size());
+
+ std::memcpy(buf.data(), payload.data(), payload.size() * sizeof(u64));
+ for (size_t i = 0; i < payload.size(); ++i) {
+ REQUIRE(buf[i] == payload[i]);
+ }
+
+ // Increasing the size should reallocate the buffer
+ buf.resize(payload.size() * 2);
+ REQUIRE(buf.size() == payload.size() * 2);
+ REQUIRE(buf.capacity() == payload.size() * 2);
+
+ // resize() keeps the previous data intact
+ for (size_t i = 0; i < payload.size(); ++i) {
+ REQUIRE(buf[i] == payload[i]);
+ }
+}
+
+TEST_CASE("ScratchBuffer: resize Shrink", "[common]") {
std::array<u8, 10> payload;
payload.fill(66);