Bug 1404972 - To Result add operator==. r=glandium

This is needed for using Result with gtest matchers.

Differential Revision: https://phabricator.services.mozilla.com/D205958
This commit is contained in:
Andreas Pehrson 2024-05-06 19:54:00 +00:00
Родитель 4783bbe42c
Коммит 5ec78bee07
2 изменённых файлов: 54 добавлений и 0 удалений

Просмотреть файл

@ -834,6 +834,15 @@ class [[nodiscard]] Result final {
constexpr auto andThen(F f) -> std::invoke_result_t<F, V&&> {
return MOZ_LIKELY(isOk()) ? f(unwrap()) : propagateErr();
}
bool operator==(const Result<V, E>& aOther) const {
return (isOk() && aOther.isOk() && inspect() == aOther.inspect()) ||
(isErr() && aOther.isErr() && inspectErr() == aOther.inspectErr());
}
bool operator!=(const Result<V, E>& aOther) const {
return !(*this == aOther);
}
};
/**

Просмотреть файл

@ -852,6 +852,50 @@ void UpcastTest() {
}
}
void EqualityTest() {
{
Result<int, bool> result(1);
Result<int, bool> other(1);
MOZ_RELEASE_ASSERT(result == other);
}
{
Result<int, bool> result(true);
Result<int, bool> other(true);
MOZ_RELEASE_ASSERT(result == other);
}
{
Result<int, bool> result(1);
Result<int, bool> other(2);
MOZ_RELEASE_ASSERT(result != other);
}
{
Result<int, bool> result(true);
Result<int, bool> other(false);
MOZ_RELEASE_ASSERT(result != other);
}
{
Result<int, bool> result(0);
Result<int, bool> other(false);
MOZ_RELEASE_ASSERT(result != other);
}
{
Result<int, unsigned int> result(1);
Result<int, unsigned int> other(1u);
MOZ_RELEASE_ASSERT(result != other);
}
}
/* * */
int main() {
@ -866,5 +910,6 @@ int main() {
UniquePtrTest();
ZeroIsEmptyErrorTest();
UpcastTest();
EqualityTest();
return 0;
}