summaryrefslogtreecommitdiffstats
path: root/src/tests
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/tests/CMakeLists.txt3
-rw-r--r--src/tests/common/bit_field.cpp90
-rw-r--r--src/tests/common/bit_utils.cpp23
-rw-r--r--src/tests/common/multi_level_queue.cpp55
4 files changed, 171 insertions, 0 deletions
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
index 37f09ce5f..c7038b217 100644
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -1,4 +1,7 @@
add_executable(tests
+ common/bit_field.cpp
+ common/bit_utils.cpp
+ common/multi_level_queue.cpp
common/param_package.cpp
common/ring_buffer.cpp
core/arm/arm_test_common.cpp
diff --git a/src/tests/common/bit_field.cpp b/src/tests/common/bit_field.cpp
new file mode 100644
index 000000000..8ca1889f9
--- /dev/null
+++ b/src/tests/common/bit_field.cpp
@@ -0,0 +1,90 @@
+// Copyright 2019 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include <array>
+#include <cstring>
+#include <type_traits>
+#include <catch2/catch.hpp>
+#include "common/bit_field.h"
+
+TEST_CASE("BitField", "[common]") {
+ enum class TestEnum : u32 {
+ A = 0b10111101,
+ B = 0b10101110,
+ C = 0b00001111,
+ };
+
+ union LEBitField {
+ u32_le raw;
+ BitField<0, 6, u32> a;
+ BitField<6, 4, s32> b;
+ BitField<10, 8, TestEnum> c;
+ BitField<18, 14, u32> d;
+ } le_bitfield;
+
+ union BEBitField {
+ u32_be raw;
+ BitFieldBE<0, 6, u32> a;
+ BitFieldBE<6, 4, s32> b;
+ BitFieldBE<10, 8, TestEnum> c;
+ BitFieldBE<18, 14, u32> d;
+ } be_bitfield;
+
+ static_assert(sizeof(LEBitField) == sizeof(u32));
+ static_assert(sizeof(BEBitField) == sizeof(u32));
+ static_assert(std::is_trivially_copyable_v<LEBitField>);
+ static_assert(std::is_trivially_copyable_v<BEBitField>);
+
+ std::array<u8, 4> raw{{
+ 0b01101100,
+ 0b11110110,
+ 0b10111010,
+ 0b11101100,
+ }};
+
+ std::memcpy(&le_bitfield, &raw, sizeof(raw));
+ std::memcpy(&be_bitfield, &raw, sizeof(raw));
+
+ // bit fields: 11101100101110'10111101'1001'101100
+ REQUIRE(le_bitfield.raw == 0b11101100'10111010'11110110'01101100);
+ REQUIRE(le_bitfield.a == 0b101100);
+ REQUIRE(le_bitfield.b == -7); // 1001 as two's complement
+ REQUIRE(le_bitfield.c == TestEnum::A);
+ REQUIRE(le_bitfield.d == 0b11101100101110);
+
+ le_bitfield.a.Assign(0b000111);
+ le_bitfield.b.Assign(-1);
+ le_bitfield.c.Assign(TestEnum::C);
+ le_bitfield.d.Assign(0b01010101010101);
+ std::memcpy(&raw, &le_bitfield, sizeof(raw));
+ // bit fields: 01010101010101'00001111'1111'000111
+ REQUIRE(le_bitfield.raw == 0b01010101'01010100'00111111'11000111);
+ REQUIRE(raw == std::array<u8, 4>{{
+ 0b11000111,
+ 0b00111111,
+ 0b01010100,
+ 0b01010101,
+ }});
+
+ // bit fields: 01101100111101'10101110'1011'101100
+ REQUIRE(be_bitfield.raw == 0b01101100'11110110'10111010'11101100);
+ REQUIRE(be_bitfield.a == 0b101100);
+ REQUIRE(be_bitfield.b == -5); // 1011 as two's complement
+ REQUIRE(be_bitfield.c == TestEnum::B);
+ REQUIRE(be_bitfield.d == 0b01101100111101);
+
+ be_bitfield.a.Assign(0b000111);
+ be_bitfield.b.Assign(-1);
+ be_bitfield.c.Assign(TestEnum::C);
+ be_bitfield.d.Assign(0b01010101010101);
+ std::memcpy(&raw, &be_bitfield, sizeof(raw));
+ // bit fields: 01010101010101'00001111'1111'000111
+ REQUIRE(be_bitfield.raw == 0b01010101'01010100'00111111'11000111);
+ REQUIRE(raw == std::array<u8, 4>{{
+ 0b01010101,
+ 0b01010100,
+ 0b00111111,
+ 0b11000111,
+ }});
+}
diff --git a/src/tests/common/bit_utils.cpp b/src/tests/common/bit_utils.cpp
new file mode 100644
index 000000000..479b5995a
--- /dev/null
+++ b/src/tests/common/bit_utils.cpp
@@ -0,0 +1,23 @@
+// Copyright 2017 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include <catch2/catch.hpp>
+#include <math.h>
+#include "common/bit_util.h"
+
+namespace Common {
+
+TEST_CASE("BitUtils::CountTrailingZeroes", "[common]") {
+ REQUIRE(Common::CountTrailingZeroes32(0) == 32);
+ REQUIRE(Common::CountTrailingZeroes64(0) == 64);
+ REQUIRE(Common::CountTrailingZeroes32(9) == 0);
+ REQUIRE(Common::CountTrailingZeroes32(8) == 3);
+ REQUIRE(Common::CountTrailingZeroes32(0x801000) == 12);
+ REQUIRE(Common::CountTrailingZeroes64(9) == 0);
+ REQUIRE(Common::CountTrailingZeroes64(8) == 3);
+ REQUIRE(Common::CountTrailingZeroes64(0x801000) == 12);
+ REQUIRE(Common::CountTrailingZeroes64(0x801000000000UL) == 36);
+}
+
+} // namespace Common
diff --git a/src/tests/common/multi_level_queue.cpp b/src/tests/common/multi_level_queue.cpp
new file mode 100644
index 000000000..cca7ec7da
--- /dev/null
+++ b/src/tests/common/multi_level_queue.cpp
@@ -0,0 +1,55 @@
+// Copyright 2019 Yuzu Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include <catch2/catch.hpp>
+#include <math.h>
+#include "common/common_types.h"
+#include "common/multi_level_queue.h"
+
+namespace Common {
+
+TEST_CASE("MultiLevelQueue", "[common]") {
+ std::array<f32, 8> values = {0.0, 5.0, 1.0, 9.0, 8.0, 2.0, 6.0, 7.0};
+ Common::MultiLevelQueue<f32, 64> mlq;
+ REQUIRE(mlq.empty());
+ mlq.add(values[2], 2);
+ mlq.add(values[7], 7);
+ mlq.add(values[3], 3);
+ mlq.add(values[4], 4);
+ mlq.add(values[0], 0);
+ mlq.add(values[5], 5);
+ mlq.add(values[6], 6);
+ mlq.add(values[1], 1);
+ u32 index = 0;
+ bool all_set = true;
+ for (auto& f : mlq) {
+ all_set &= (f == values[index]);
+ index++;
+ }
+ REQUIRE(all_set);
+ REQUIRE(!mlq.empty());
+ f32 v = 8.0;
+ mlq.add(v, 2);
+ v = -7.0;
+ mlq.add(v, 2, false);
+ REQUIRE(mlq.front(2) == -7.0);
+ mlq.yield(2);
+ REQUIRE(mlq.front(2) == values[2]);
+ REQUIRE(mlq.back(2) == -7.0);
+ REQUIRE(mlq.empty(8));
+ v = 10.0;
+ mlq.add(v, 8);
+ mlq.adjust(v, 8, 9);
+ REQUIRE(mlq.front(9) == v);
+ REQUIRE(mlq.empty(8));
+ REQUIRE(!mlq.empty(9));
+ mlq.adjust(values[0], 0, 9);
+ REQUIRE(mlq.highest_priority_set() == 1);
+ REQUIRE(mlq.lowest_priority_set() == 9);
+ mlq.remove(values[1], 1);
+ REQUIRE(mlq.highest_priority_set() == 2);
+ REQUIRE(mlq.empty(1));
+}
+
+} // namespace Common