Adjust the default behavior of result.

This commit is contained in:
mutouyun 2023-09-09 12:27:15 +08:00
parent 3d4e92a9c5
commit d0af950071
4 changed files with 10 additions and 15 deletions

View File

@ -42,7 +42,7 @@ struct generic_traits {
/// \brief Custom initialization.
constexpr static void init_code(storage_t &code) noexcept {
code = {0, std::make_error_code(std::errc::invalid_argument)};
code = {0, std::error_code(-1, std::generic_category())};
}
constexpr static void init_code(storage_t &code, T value, std::error_code const &ec) noexcept {
code = {value, ec};
@ -73,7 +73,7 @@ struct default_traits<void, ___> {
/// \brief Custom initialization.
constexpr static void init_code(storage_t &code) noexcept {
code = std::make_error_code(std::errc::invalid_argument);
code = std::error_code(-1, std::generic_category());
}
constexpr static void init_code(storage_t &code, std::error_code const &ec) noexcept {
code = ec;
@ -96,7 +96,7 @@ struct default_traits<T, std::enable_if_t<std::is_integral<T>::value>> : generic
/// \brief Custom initialization.
constexpr static void init_code(typename generic_traits<T>::storage_t &code,
T value, bool ok) noexcept {
code = {value, ok ? std::error_code() : std::make_error_code(std::errc::invalid_argument)};
code = {value, ok ? std::error_code() : std::error_code(-1, std::generic_category())};
}
using generic_traits<T>::init_code;
@ -113,7 +113,7 @@ struct default_traits<T, std::enable_if_t<std::is_pointer<T>::value>> : generic_
}
constexpr static void init_code(typename generic_traits<T>::storage_t &code,
std::nullptr_t) noexcept {
code = {nullptr, std::make_error_code(std::errc::invalid_argument)};
code = {nullptr, std::error_code(-1, std::generic_category())};
}
using generic_traits<T>::init_code;

View File

@ -10,5 +10,5 @@ TEST(error, error_code) {
std::error_code ecode;
EXPECT_FALSE(ecode);
std::cout << ecode.message() << "\n";
EXPECT_EQ(ecode.message(), "[generic: 0, \"success\"]");
EXPECT_EQ(ecode.message(), "Success");
}

View File

@ -102,11 +102,11 @@ TEST(result, fmt) {
imp::result<int *> r3 {&aaa};
EXPECT_EQ(imp::fmt(r3), imp::fmt("succ, value = ", (void *)&aaa));
imp::result<int *> r4 {nullptr};
EXPECT_EQ(imp::fmt(r4), imp::fmt("fail, value = ", nullptr, ", error = [generic: 0, \"failure\"]"));
EXPECT_EQ(imp::fmt(r4), imp::fmt("fail, value = ", nullptr, ", error = [-1: Unknown error -1]"));
r4 = {nullptr, std::error_code{1234, std::generic_category()}};
EXPECT_EQ(imp::fmt(r4), imp::fmt("fail, value = ", nullptr, ", error = [generic: 1234, \"failure\"]"));
EXPECT_EQ(imp::fmt(r4), imp::fmt("fail, value = ", nullptr, ", error = [1234: Unknown error 1234]"));
imp::result<int *> r5;
EXPECT_EQ(imp::fmt(r5), "fail, value = null, error = [generic: 0, \"failure\"]");
EXPECT_EQ(imp::fmt(r5), "fail, value = null, error = [-1: Unknown error -1]");
}
{
imp::result<std::int64_t> r1 {-123};
@ -114,9 +114,9 @@ TEST(result, fmt) {
}
{
imp::result<void> r1;
EXPECT_EQ(imp::fmt(r1), "fail, error = [generic: 0, \"failure\"]");
EXPECT_EQ(imp::fmt(r1), "fail, error = [-1: Unknown error -1]");
r1 = std::error_code{};
EXPECT_TRUE(r1);
EXPECT_EQ(imp::fmt(r1), "succ, error = [generic: 0, \"success\"]");
EXPECT_EQ(imp::fmt(r1), "succ, error = [0: Success]");
}
}

View File

@ -9,11 +9,6 @@
#include "libimp/codecvt.h"
#include "libimp/fmt.h"
#if defined(LIBIMP_OS_WIN)
#include <Windows.h>
#else
#endif
TEST(system, conf) {
auto ret = imp::sys::conf(imp::sys::info::page_size);
EXPECT_TRUE(ret);