|
| 1 | +#include <algorithm> |
| 2 | +#include <array> |
| 3 | +#include <cstdint> |
| 4 | +#include <string> |
| 5 | +#include <string_view> |
| 6 | +#include <system_error> |
| 7 | +#include <type_traits> |
| 8 | + |
| 9 | +#include <Catch2/single_include/catch2/catch.hpp> |
| 10 | + |
| 11 | +#include "../src/clp/error_handling/ErrorCode.hpp" |
| 12 | + |
| 13 | +using clp::error_handling::ErrorCategory; |
| 14 | +using clp::error_handling::ErrorCode; |
| 15 | +using std::string; |
| 16 | +using std::string_view; |
| 17 | + |
| 18 | +namespace { |
| 19 | +enum class AlwaysSuccessErrorCodeEnum : uint8_t { |
| 20 | + Success = 0 |
| 21 | +}; |
| 22 | + |
| 23 | +enum class BinaryErrorCodeEnum : uint8_t { |
| 24 | + Success = 0, |
| 25 | + Failure |
| 26 | +}; |
| 27 | + |
| 28 | +using AlwaysSuccessErrorCode = ErrorCode<AlwaysSuccessErrorCodeEnum>; |
| 29 | +using AlwaysSuccessErrorCategory = ErrorCategory<AlwaysSuccessErrorCodeEnum>; |
| 30 | +using BinaryErrorCode = ErrorCode<BinaryErrorCodeEnum>; |
| 31 | +using BinaryErrorCategory = ErrorCategory<BinaryErrorCodeEnum>; |
| 32 | + |
| 33 | +constexpr string_view cAlwaysSuccessErrorCategoryName{"Always Success Error Code"}; |
| 34 | +constexpr string_view cBinaryTestErrorCategoryName{"Binary Error Code"}; |
| 35 | +constexpr string_view cSuccessErrorMsg{"Success"}; |
| 36 | +constexpr string_view cFailureErrorMsg{"Failure"}; |
| 37 | +constexpr string_view cUnrecognizedErrorCode{"Unrecognized Error Code"}; |
| 38 | +constexpr std::array cFailureConditions{std::errc::not_connected, std::errc::timed_out}; |
| 39 | +constexpr std::array cNoneFailureConditions{std::errc::broken_pipe, std::errc::address_in_use}; |
| 40 | +} // namespace |
| 41 | + |
| 42 | +CLP_ERROR_HANDLING_MARK_AS_ERROR_CODE_ENUM(AlwaysSuccessErrorCodeEnum); |
| 43 | +CLP_ERROR_HANDLING_MARK_AS_ERROR_CODE_ENUM(BinaryErrorCodeEnum); |
| 44 | + |
| 45 | +template <> |
| 46 | +auto AlwaysSuccessErrorCategory::name() const noexcept -> char const* { |
| 47 | + return cAlwaysSuccessErrorCategoryName.data(); |
| 48 | +} |
| 49 | + |
| 50 | +template <> |
| 51 | +auto AlwaysSuccessErrorCategory::message(AlwaysSuccessErrorCodeEnum error_enum) const -> string { |
| 52 | + switch (error_enum) { |
| 53 | + case AlwaysSuccessErrorCodeEnum::Success: |
| 54 | + return string{cSuccessErrorMsg}; |
| 55 | + default: |
| 56 | + return string{cUnrecognizedErrorCode}; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +template <> |
| 61 | +auto BinaryErrorCategory::name() const noexcept -> char const* { |
| 62 | + return cBinaryTestErrorCategoryName.data(); |
| 63 | +} |
| 64 | + |
| 65 | +template <> |
| 66 | +auto BinaryErrorCategory::message(BinaryErrorCodeEnum error_enum) const -> string { |
| 67 | + switch (error_enum) { |
| 68 | + case BinaryErrorCodeEnum::Success: |
| 69 | + return string{cSuccessErrorMsg}; |
| 70 | + case BinaryErrorCodeEnum::Failure: |
| 71 | + return string{cFailureErrorMsg}; |
| 72 | + default: |
| 73 | + return string{cUnrecognizedErrorCode}; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +template <> |
| 78 | +auto BinaryErrorCategory::equivalent( |
| 79 | + BinaryErrorCodeEnum error_enum, |
| 80 | + std::error_condition const& condition |
| 81 | +) const noexcept -> bool { |
| 82 | + switch (error_enum) { |
| 83 | + case BinaryErrorCodeEnum::Failure: |
| 84 | + return std::any_of( |
| 85 | + cFailureConditions.cbegin(), |
| 86 | + cFailureConditions.cend(), |
| 87 | + [&](auto failure_condition) -> bool { return condition == failure_condition; } |
| 88 | + ); |
| 89 | + default: |
| 90 | + return false; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +TEST_CASE("test_error_code_implementation", "[error_handling][ErrorCode]") { |
| 95 | + // Test error codes within the same error category |
| 96 | + BinaryErrorCode const success{BinaryErrorCodeEnum::Success}; |
| 97 | + std::error_code const success_error_code{success}; |
| 98 | + REQUIRE((success == success_error_code)); |
| 99 | + REQUIRE((cSuccessErrorMsg == success_error_code.message())); |
| 100 | + REQUIRE((BinaryErrorCode::get_category() == success_error_code.category())); |
| 101 | + REQUIRE((cBinaryTestErrorCategoryName == success_error_code.category().name())); |
| 102 | + |
| 103 | + BinaryErrorCode const failure{BinaryErrorCodeEnum::Failure}; |
| 104 | + std::error_code const failure_error_code{failure}; |
| 105 | + REQUIRE((failure == failure_error_code)); |
| 106 | + REQUIRE((cFailureErrorMsg == failure_error_code.message())); |
| 107 | + REQUIRE((BinaryErrorCode::get_category() == failure_error_code.category())); |
| 108 | + REQUIRE((cBinaryTestErrorCategoryName == failure_error_code.category().name())); |
| 109 | + std::for_each( |
| 110 | + cFailureConditions.cbegin(), |
| 111 | + cFailureConditions.cend(), |
| 112 | + [&](auto failure_condition) { REQUIRE((failure_error_code == failure_condition)); } |
| 113 | + ); |
| 114 | + std::for_each( |
| 115 | + cNoneFailureConditions.cbegin(), |
| 116 | + cNoneFailureConditions.cend(), |
| 117 | + [&](auto none_failure_condition) { |
| 118 | + REQUIRE((failure_error_code != none_failure_condition)); |
| 119 | + } |
| 120 | + ); |
| 121 | + |
| 122 | + REQUIRE((success_error_code != failure_error_code)); |
| 123 | + REQUIRE((success_error_code.category() == failure_error_code.category())); |
| 124 | + |
| 125 | + AlwaysSuccessErrorCode const always_success{AlwaysSuccessErrorCodeEnum::Success}; |
| 126 | + std::error_code const always_success_error_code{always_success}; |
| 127 | + REQUIRE((always_success_error_code == always_success)); |
| 128 | + REQUIRE((cSuccessErrorMsg == always_success_error_code.message())); |
| 129 | + REQUIRE((AlwaysSuccessErrorCode::get_category() == always_success_error_code.category())); |
| 130 | + REQUIRE((cAlwaysSuccessErrorCategoryName == always_success_error_code.category().name())); |
| 131 | + |
| 132 | + // Compare error codes from different error category |
| 133 | + // Error codes that have the same value or message won't be the same with each other if they are |
| 134 | + // from different error categories. |
| 135 | + REQUIRE((success_error_code.value() == always_success_error_code.value())); |
| 136 | + REQUIRE((success_error_code.message() == always_success_error_code.message())); |
| 137 | + REQUIRE((success_error_code.category() != always_success_error_code.category())); |
| 138 | + REQUIRE((success_error_code != always_success_error_code)); |
| 139 | + REQUIRE((AlwaysSuccessErrorCode{AlwaysSuccessErrorCodeEnum::Success} != success_error_code)); |
| 140 | + REQUIRE((BinaryErrorCode{BinaryErrorCodeEnum::Success} != always_success_error_code)); |
| 141 | +} |
0 commit comments