diff --git a/tests/algorithms/CMakeLists.txt b/tests/algorithms/CMakeLists.txt index e4cf17932..c9a5ede62 100644 --- a/tests/algorithms/CMakeLists.txt +++ b/tests/algorithms/CMakeLists.txt @@ -5,12 +5,22 @@ project(algorithms_test) # Add new tests here # set(AVAILABLE_TESTS - TestSucceeding - TestFailing + # Common + TestSucceeding + TestFailing + + # Endian + 32BitIntegerEndianSwap + 64BitFloatEndianSwap ) -add_executable(algorithms_test source/hash.cpp source/main.cpp include/tests.hpp) +add_executable(algorithms_test + source/main.cpp + + source/common.cpp + source/endian.cpp +) target_include_directories(algorithms_test PRIVATE include) target_link_libraries(algorithms_test libimhex) diff --git a/tests/algorithms/include/tests.hpp b/tests/algorithms/include/tests.hpp index e8c2cedf9..064125932 100644 --- a/tests/algorithms/include/tests.hpp +++ b/tests/algorithms/include/tests.hpp @@ -9,9 +9,10 @@ #include #define TEST_SEQUENCE(...) static auto ANONYMOUS_VARIABLE(TEST_SEQUENCE) = ::hex::test::TestSequenceExecutor(__VA_ARGS__) + []() -> int -#define TEST_FAIL() return EXIT_FAILURE; -#define TEST_SUCCESS() return EXIT_SUCCESS; +#define TEST_FAIL() return EXIT_FAILURE +#define TEST_SUCCESS() return EXIT_SUCCESS #define FAILING true +#define TEST_ASSERT(x) return (x) ? EXIT_SUCCESS : EXIT_FAILURE namespace hex::test { diff --git a/tests/algorithms/source/hash.cpp b/tests/algorithms/source/common.cpp similarity index 100% rename from tests/algorithms/source/hash.cpp rename to tests/algorithms/source/common.cpp diff --git a/tests/algorithms/source/endian.cpp b/tests/algorithms/source/endian.cpp new file mode 100644 index 000000000..de17ad022 --- /dev/null +++ b/tests/algorithms/source/endian.cpp @@ -0,0 +1,17 @@ +#include +#include "test_provider.hpp" +#include "tests.hpp" + +TEST_SEQUENCE("32BitIntegerEndianSwap") { + TEST_ASSERT(hex::changeEndianess(0xAABBCCDD, std::endian::big) == 0xDDCCBBAA); +}; + +TEST_SEQUENCE("64BitFloatEndianSwap", FAILING) { + double floatValue = 1234.5; + u64 integerValue = reinterpret_cast(floatValue); + + double swappedFloatValue = hex::changeEndianess(floatValue, std::endian::big); + u64 swappedIntegerValue = hex::changeEndianess(integerValue, std::endian::big); + + TEST_ASSERT(std::memcmp(&floatValue, &integerValue, 8) && std::memcmp(&swappedFloatValue, &swappedIntegerValue, 8)); +}; \ No newline at end of file