1
0
mirror of synced 2024-11-12 02:00:52 +01:00

tests: Added operator test

This commit is contained in:
WerWolv 2021-09-12 21:54:18 +02:00
parent bed5361879
commit 7fbb540674
4 changed files with 79 additions and 4 deletions

View File

@ -14,6 +14,7 @@ set(AVAILABLE_TESTS
SucceedingAssert
FailingAssert
Bitfields
Math
)
@ -26,7 +27,7 @@ set_target_properties(unit_tests PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BIN
add_custom_command(TARGET unit_tests
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/test_data" ${CMAKE_BINARY_DIR})
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_CURRENT_SOURCE_DIR}/test_data" ${CMAKE_BINARY_DIR})
foreach (test IN LISTS AVAILABLE_TESTS)
add_test(NAME "${test}" COMMAND unit_tests "${test}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR})

View File

@ -0,0 +1,72 @@
#pragma once
#include "test_pattern.hpp"
namespace hex::test {
class TestPatternMath : public TestPattern {
public:
TestPatternMath() : TestPattern("Math") {
}
~TestPatternMath() override = default;
[[nodiscard]]
std::string getSourceCode() const override {
return R"(
// Compare operations
std::assert(123 == 123, "== operation error");
std::assert(123 != 567, "!= operation error");
std::assert(111 < 222, "< operation error");
std::assert(333 > 222, "> operation error");
std::assert(100 >= 100, ">= operation error");
std::assert(200 <= 200, "<= operation error");
// Boolean operations
std::assert(true, "true literal invalid");
std::assert(true && true, "&& operator error");
std::assert(false || true, "|| operator error");
std::assert(true ^^ false, "^^ operator error");
std::assert(!false, "! operator error");
// Bitwise operations
std::assert(0xFF00FF | 0x00AA00 == 0xFFAAFF, "| operator error");
std::assert(0xFFFFFF & 0x00FF00 == 0x00FF00, "& operator error");
std::assert(0xFFFFFF ^ 0x00AA00 == 0xFF55FF, "^ operator error");
std::assert(~0xFFFFFFFF == 0x00, "~ operator error");
std::assert(0xAA >> 4 == 0x0A, ">> operator error");
std::assert(0xAA << 4 == 0xAA0, "<< operator error");
// Basic operations
std::assert(100 + 200 == 300, "+ operator error");
std::assert(400 - 200 == 200, "- operator error");
std::assert(10 * 20 == 200, "* operator error");
std::assert(200 / 100 == 2, "/ operator error");
std::assert(100 % 2 == 0, "% operator error");
// Special operators
std::assert($ == 0, "$ operator error");
std::assert((10 == 20) ? 30 : 40 == 40, "?: operator error");
// Type operators
struct TypeTest { u32 x, y, z; };
TypeTest typeTest @ 0x100;
std::assert(addressof(typeTest) == 0x100, "addressof operator error");
std::assert(sizeof(typeTest) == 3 * 4, "sizeof operator error");
// Properties
std::assert(100 * 200 == 200 * 100, "+ operator commutativity error");
std::assert(100 * 200 == 200 * 100, "* operator commutativity error");
std::assert(100 * 200 == 200 * 100, "* operator commutativity error");
std::assert(10 + (20 + 30) == (10 + 20) + 30, "+ operator associativity error");
std::assert(10 * (20 * 30) == (10 * 20) * 30, "* operator associativity error");
std::assert(10 * (20 + 30) == 10 * 20 + 10 * 30, "Operator distributivity error");
)";
}
};
}

View File

@ -86,13 +86,13 @@ int test(int argc, char **argv) {
};
// Check if the right number of patterns have been produced
if (patterns->size() != currTest->getPatterns().size()) {
if (patterns->size() != currTest->getPatterns().size() && !currTest->getPatterns().empty()) {
hex::log::fatal("Source didn't produce expected number of patterns");
return EXIT_FAILURE;
}
// Check if the produced patterns are the ones expected
for (u32 i = 0; i < patterns->size(); i++) {
for (u32 i = 0; i < currTest->getPatterns().size(); i++) {
auto &left = *patterns->at(i);
auto &right = *currTest->getPatterns().at(i);

View File

@ -9,6 +9,7 @@
#include "test_patterns/test_pattern_succeeding_assert.hpp"
#include "test_patterns/test_pattern_failing_assert.hpp"
#include "test_patterns/test_pattern_bitfields.hpp"
#include "test_patterns/test_pattern_math.hpp"
std::array Tests = {
TEST(Placement),
@ -19,5 +20,6 @@ std::array Tests = {
TEST(Padding),
TEST(SucceedingAssert),
TEST(FailingAssert),
TEST(Bitfields)
TEST(Bitfields),
TEST(Math)
};