From 3d14850beaad172a91a9ab4f3e5ac3ef4cdd82cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 18 Nov 2020 17:59:49 +0100 Subject: [PATCH] test: Add TypedExecutionResult --- test/unittests/execute_numeric_test.cpp | 3 +- test/utils/asserts.hpp | 52 ++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/test/unittests/execute_numeric_test.cpp b/test/unittests/execute_numeric_test.cpp index 48ea72afba..3041c3bf90 100644 --- a/test/unittests/execute_numeric_test.cpp +++ b/test/unittests/execute_numeric_test.cpp @@ -42,7 +42,8 @@ auto create_unary_operation_executor(Instr instr) auto instance = instantiate(std::move(module)); - return [instance = std::move(instance)](Value arg) { return execute(*instance, 0, {arg}); }; + return + [instance = std::move(instance)](Value arg) { return typed_execute(*instance, 0, {arg}); }; } auto create_binary_operation_executor(Instr instr) diff --git a/test/utils/asserts.hpp b/test/utils/asserts.hpp index 993fb5f114..c49cdd523b 100644 --- a/test/utils/asserts.hpp +++ b/test/utils/asserts.hpp @@ -10,6 +10,28 @@ #include #include #include +#include + +namespace fizzy::test +{ +struct TypedExecutionResult : ExecutionResult +{ + ValType type; + TypedExecutionResult(ExecutionResult result, ValType t) noexcept + : ExecutionResult{result}, type{t} + {} +}; + +inline TypedExecutionResult typed_execute( + Instance& instance, FuncIdx func_idx, std::initializer_list args) +{ + const auto& func_type = instance.module->get_function_type(func_idx); + assert(args.size() == func_type.inputs.size()); + const auto result = execute(instance, func_idx, args.begin()); + const auto result_type = func_type.outputs.empty() ? ValType{} : func_type.outputs[0]; + return {result, result_type}; +} +} // namespace fizzy::test MATCHER(Traps, "") // NOLINT(readability-redundant-string-init) { @@ -21,13 +43,41 @@ MATCHER(Result, "empty result") return !arg.trapped && !arg.has_value; } +template +constexpr bool is_any_of = std::disjunction_v...>; + MATCHER_P(Result, value, "") // NOLINT(readability-redundant-string-init) { + using namespace fizzy; + static_assert(is_any_of); + if (arg.trapped || !arg.has_value) return false; + using result_type = std::remove_cv_t>; + if constexpr (std::is_same_v) + { + // Type safe checks. + if constexpr (is_any_of) + { + return arg.type == ValType::i32 && arg.value.i64 == static_cast(value); + } + else if constexpr (is_any_of) + { + return arg.type == ValType::i64 && arg.value.i64 == static_cast(value); + } + else if constexpr (std::is_same_v) + { + return arg.type == ValType::f32 && arg.value.f32 == test::FP{value}; + } + else if constexpr (std::is_same_v) + { + return arg.type == ValType::f64 && arg.value.f64 == test::FP{value}; + } + } + if constexpr (std::is_floating_point_v) - return arg.value.template as() == fizzy::test::FP{value}; + return arg.value.template as() == test::FP{value}; else // always check 64 bit of result for all integers, including 32-bit results return arg.value.i64 == static_cast>(value); }