From bfaad055a2ad2ed12de982374984fd55f902c9fc Mon Sep 17 00:00:00 2001 From: Joel Linn Date: Sat, 22 Jan 2022 14:17:29 +0100 Subject: [PATCH] [Base] Add easier to debug `copy_and_swap` tests --- src/xenia/base/testing/memory_test.cc | 48 ++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/xenia/base/testing/memory_test.cc b/src/xenia/base/testing/memory_test.cc index f78d9c3bb..1374d6c14 100644 --- a/src/xenia/base/testing/memory_test.cc +++ b/src/xenia/base/testing/memory_test.cc @@ -2,7 +2,7 @@ ****************************************************************************** * Xenia : Xbox 360 Emulator Research Project * ****************************************************************************** - * Copyright 2021 Ben Vanik. All rights reserved. * + * Copyright 2022 Ben Vanik. All rights reserved. * * Released under the BSD license - see LICENSE in the root for more details. * ****************************************************************************** */ @@ -14,6 +14,8 @@ #include "xenia/base/clock.h" +#include + namespace xe { namespace base { namespace test { @@ -113,6 +115,24 @@ TEST_CASE("copy_and_swap_16_unaligned", "[copy_and_swap]") { REQUIRE(c[2] == 0xAB89); REQUIRE(c[3] == 0xEFCD); + { + constexpr size_t count = 100; + std::array src{}; + std::array dst{}; + for (size_t i = 0; i < src.size(); ++i) { + src[i] = static_cast(i) + 1; // no zero in array + } + copy_and_swap_16_unaligned(dst.data(), src.data(), count); + for (size_t i = 0; i < src.size(); i += 2) { + // Check src is untouched + REQUIRE(static_cast(src[i + 0]) == i + 1); + REQUIRE(static_cast(src[i + 1]) == i + 2); + // Check swapped bytes + REQUIRE(static_cast(dst[i]) == static_cast(src[i + 1])); + REQUIRE(static_cast(dst[i + 1]) == static_cast(src[i])); + } + } + uint64_t e; copy_and_swap_16_unaligned(&e, d, 4); REQUIRE(e == 0xEFCDAB8967452301); @@ -221,6 +241,32 @@ TEST_CASE("copy_and_swap_32_unaligned", "[copy_and_swap]") { REQUIRE(c[2] == 0xEDEE87E8); REQUIRE(c[3] == 0x994151D8); + { + constexpr size_t count = 17; + std::array src{}; + std::array dst{}; + for (size_t i = 0; i < src.size(); ++i) { + src[i] = static_cast(i) + 1; // no zero in array + } + copy_and_swap_32_unaligned(dst.data(), src.data(), count); + for (size_t i = 0; i < src.size(); i += 4) { + // Check src is untouched + REQUIRE(static_cast(src[i + 0]) == i + 1); + REQUIRE(static_cast(src[i + 1]) == i + 2); + REQUIRE(static_cast(src[i + 2]) == i + 3); + REQUIRE(static_cast(src[i + 3]) == i + 4); + // Check swapped bytes + REQUIRE(static_cast(dst[i + 0]) == + static_cast(src[i + 3])); + REQUIRE(static_cast(dst[i + 1]) == + static_cast(src[i + 2])); + REQUIRE(static_cast(dst[i + 2]) == + static_cast(src[i + 1])); + REQUIRE(static_cast(dst[i + 3]) == + static_cast(src[i + 0])); + } + } + uint64_t e; copy_and_swap_32_unaligned(&e, d, 2); REQUIRE(e == 0xEFCDAB8967452301);