/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ #include #include "mozilla/ResultVariant.h" #include "mozilla/UniquePtr.h" using mozilla::Err; using mozilla::GenericErrorResult; using mozilla::Ok; using mozilla::Result; using mozilla::UniquePtr; enum struct TestUnusedZeroEnum : int16_t { Ok = 0, NotOk = 1 }; namespace mozilla::detail { template <> struct UnusedZero : UnusedZeroEnum {}; } // namespace mozilla::detail struct Failed {}; namespace mozilla::detail { template <> struct UnusedZero { using StorageType = uintptr_t; static constexpr bool value = true; static constexpr StorageType nullValue = 0; static constexpr StorageType GetDefaultValue() { return 2; } static constexpr void AssertValid(StorageType aValue) {} static constexpr Failed Inspect(const StorageType& aValue) { return Failed{}; } static constexpr Failed Unwrap(StorageType aValue) { return Failed{}; } static constexpr StorageType Store(Failed aValue) { return GetDefaultValue(); } }; } // namespace mozilla::detail // V is trivially default-constructible, and E has UnusedZero::value == true, // for a reference type and for a non-reference type static_assert(mozilla::detail::SelectResultImpl::value == mozilla::detail::PackingStrategy::NullIsOk); static_assert( mozilla::detail::SelectResultImpl::value == mozilla::detail::PackingStrategy::NullIsOk); static_assert(mozilla::detail::SelectResultImpl::value == mozilla::detail::PackingStrategy::LowBitTagIsError); static_assert(std::is_trivially_destructible_v>); static_assert(std::is_trivially_destructible_v>); static_assert(std::is_trivially_destructible_v>); static_assert( sizeof(Result) <= sizeof(uintptr_t), "Result with bool value type should not be larger than pointer-sized"); static_assert(sizeof(Result) == sizeof(uint8_t), "Result with empty value type should be size 1"); static_assert(sizeof(Result) == sizeof(uintptr_t), "Result with two aligned pointer types should be pointer-sized"); static_assert( sizeof(Result) > sizeof(char*), "Result with unaligned success type `char*` must not be pointer-sized"); static_assert( sizeof(Result) > sizeof(char*), "Result with unaligned error type `char*` must not be pointer-sized"); enum Foo8 : uint8_t {}; enum Foo16 : uint16_t {}; enum Foo32 : uint32_t {}; static_assert(sizeof(Result) <= sizeof(uintptr_t), "Result with small types should be pointer-sized"); static_assert(sizeof(Result) <= sizeof(uintptr_t), "Result with small types should be pointer-sized"); static_assert(sizeof(Foo32) >= sizeof(uintptr_t) || sizeof(Result) <= sizeof(uintptr_t), "Result with small types should be pointer-sized"); static_assert(sizeof(Result) <= sizeof(uintptr_t), "Result with small types should be pointer-sized"); static_assert(sizeof(Result) <= sizeof(uintptr_t), "Result with small types should be pointer-sized"); static_assert(sizeof(Foo32) >= sizeof(uintptr_t) || sizeof(Result) <= sizeof(uintptr_t), "Result with small types should be pointer-sized"); static_assert(sizeof(Foo32) >= sizeof(uintptr_t) || sizeof(Result) <= sizeof(uintptr_t), "Result with small types should be pointer-sized"); static GenericErrorResult Fail() { static Failed failed; return Err(failed); } static GenericErrorResult FailTestUnusedZeroEnum() { return Err(TestUnusedZeroEnum::NotOk); } static Result Task1(bool pass) { if (!pass) { return Fail(); // implicit conversion from GenericErrorResult to Result } return Ok(); } static Result Task1UnusedZeroEnumErr(bool pass) { if (!pass) { return FailTestUnusedZeroEnum(); // implicit conversion from // GenericErrorResult to Result } return Ok(); } static Result Task2(bool pass, int value) { MOZ_TRY( Task1(pass)); // converts one type of result to another in the error case return value; // implicit conversion from T to Result } static Result Task2UnusedZeroEnumErr(bool pass, int value) { MOZ_TRY(Task1UnusedZeroEnumErr( pass)); // converts one type of result to another in the error case return value; // implicit conversion from T to Result } static Result Task3(bool pass1, bool pass2, int value) { int x, y; MOZ_TRY_VAR(x, Task2(pass1, value)); MOZ_TRY_VAR(y, Task2(pass2, value)); return x + y; } static void BasicTests() { MOZ_RELEASE_ASSERT(Task1(true).isOk()); MOZ_RELEASE_ASSERT(!Task1(true).isErr()); MOZ_RELEASE_ASSERT(!Task1(false).isOk()); MOZ_RELEASE_ASSERT(Task1(false).isErr()); MOZ_RELEASE_ASSERT(Task1UnusedZeroEnumErr(true).isOk()); MOZ_RELEASE_ASSERT(!Task1UnusedZeroEnumErr(true).isErr()); MOZ_RELEASE_ASSERT(!Task1UnusedZeroEnumErr(false).isOk()); MOZ_RELEASE_ASSERT(Task1UnusedZeroEnumErr(false).isErr()); MOZ_RELEASE_ASSERT(TestUnusedZeroEnum::NotOk == Task1UnusedZeroEnumErr(false).inspectErr()); MOZ_RELEASE_ASSERT(TestUnusedZeroEnum::NotOk == Task1UnusedZeroEnumErr(false).unwrapErr()); // MOZ_TRY works. MOZ_RELEASE_ASSERT(Task2(true, 3).isOk()); MOZ_RELEASE_ASSERT(Task2(true, 3).unwrap() == 3); MOZ_RELEASE_ASSERT(Task2(true, 3).unwrapOr(6) == 3); MOZ_RELEASE_ASSERT(Task2(false, 3).isErr()); MOZ_RELEASE_ASSERT(Task2(false, 3).unwrapOr(6) == 6); MOZ_RELEASE_ASSERT(Task2UnusedZeroEnumErr(true, 3).isOk()); MOZ_RELEASE_ASSERT(Task2UnusedZeroEnumErr(true, 3).unwrap() == 3); MOZ_RELEASE_ASSERT(Task2UnusedZeroEnumErr(true, 3).unwrapOr(6) == 3); MOZ_RELEASE_ASSERT(Task2UnusedZeroEnumErr(false, 3).isErr()); MOZ_RELEASE_ASSERT(Task2UnusedZeroEnumErr(false, 3).unwrapOr(6) == 6); // MOZ_TRY_VAR works. MOZ_RELEASE_ASSERT(Task3(true, true, 3).isOk()); MOZ_RELEASE_ASSERT(Task3(true, true, 3).unwrap() == 6); MOZ_RELEASE_ASSERT(Task3(true, false, 3).isErr()); MOZ_RELEASE_ASSERT(Task3(false, true, 3).isErr()); MOZ_RELEASE_ASSERT(Task3(false, true, 3).unwrapOr(6) == 6); // Lvalues should work too. { Result res = Task1(true); MOZ_RELEASE_ASSERT(res.isOk()); MOZ_RELEASE_ASSERT(!res.isErr()); res = Task1(false); MOZ_RELEASE_ASSERT(!res.isOk()); MOZ_RELEASE_ASSERT(res.isErr()); } { Result res = Task2(true, 3); MOZ_RELEASE_ASSERT(res.isOk()); MOZ_RELEASE_ASSERT(res.unwrap() == 3); res = Task2(false, 4); MOZ_RELEASE_ASSERT(res.isErr()); } // Some tests for pointer tagging. { int i = 123; Result res = &i; static_assert(sizeof(res) == sizeof(uintptr_t), "should use pointer tagging to fit in a word"); MOZ_RELEASE_ASSERT(res.isOk()); MOZ_RELEASE_ASSERT(*res.unwrap() == 123); res = Err(Failed()); MOZ_RELEASE_ASSERT(res.isErr()); } } struct NonCopyableNonMovable { explicit NonCopyableNonMovable(uint32_t aValue) : mValue(aValue) {} NonCopyableNonMovable(const NonCopyableNonMovable&) = delete; NonCopyableNonMovable(NonCopyableNonMovable&&) = delete; NonCopyableNonMovable& operator=(const NonCopyableNonMovable&) = delete; NonCopyableNonMovable& operator=(NonCopyableNonMovable&&) = delete; uint32_t mValue; }; static void InPlaceConstructionTests() { { // PackingStrategy == NullIsOk static_assert(mozilla::detail::SelectResultImpl::value == mozilla::detail::PackingStrategy::NullIsOk); const Result result{std::in_place, 42}; MOZ_RELEASE_ASSERT(42 == result.inspect().mValue); } { // PackingStrategy == Variant static_assert( mozilla::detail::SelectResultImpl::value == mozilla::detail::PackingStrategy::Variant); const Result result{std::in_place, 42}; MOZ_RELEASE_ASSERT(42 == result.inspect().mValue); } } /* * */ struct Snafu : Failed {}; static Result Explode() { static Snafu snafu; return Err(&snafu); } static Result ErrorGeneralization() { MOZ_TRY(Explode()); // change error type from Snafu* to more general Failed* return Ok(); } static void TypeConversionTests() { MOZ_RELEASE_ASSERT(ErrorGeneralization().isErr()); { const Result res = Explode(); MOZ_RELEASE_ASSERT(res.isErr()); } { const Result res = Result{Ok{}}; MOZ_RELEASE_ASSERT(res.isOk()); } } static void EmptyValueTest() { struct Fine {}; mozilla::Result res((Fine())); res.unwrap(); MOZ_RELEASE_ASSERT(res.isOk()); static_assert(sizeof(res) == sizeof(uint8_t), "Result with empty value and error types should be size 1"); } static void MapTest() { struct MyError { int x; explicit MyError(int y) : x(y) {} }; // Mapping over success values, to the same success type. { Result res(5); bool invoked = false; auto res2 = res.map([&invoked](int x) { MOZ_RELEASE_ASSERT(x == 5); invoked = true; return 6; }); MOZ_RELEASE_ASSERT(res2.isOk()); MOZ_RELEASE_ASSERT(invoked); MOZ_RELEASE_ASSERT(res2.unwrap() == 6); } // Mapping over success values, to a different success type. { Result res(5); bool invoked = false; auto res2 = res.map([&invoked](int x) { MOZ_RELEASE_ASSERT(x == 5); invoked = true; return "hello"; }); MOZ_RELEASE_ASSERT(res2.isOk()); MOZ_RELEASE_ASSERT(invoked); MOZ_RELEASE_ASSERT(strcmp(res2.unwrap(), "hello") == 0); } // Mapping over error values. { MyError err(1); Result res(err); MOZ_RELEASE_ASSERT(res.isErr()); Result res2 = res.map([](int x) { MOZ_RELEASE_ASSERT(false); return 'a'; }); MOZ_RELEASE_ASSERT(res2.isErr()); MOZ_RELEASE_ASSERT(res2.unwrapErr().x == err.x); } // Function pointers instead of lambdas as the mapping function. { Result res("hello"); auto res2 = res.map(strlen); MOZ_RELEASE_ASSERT(res2.isOk()); MOZ_RELEASE_ASSERT(res2.unwrap() == 5); } } static void MapErrTest() { struct MyError { int x; explicit MyError(int y) : x(y) {} }; struct MyError2 { int a; explicit MyError2(int b) : a(b) {} }; // Mapping over error values, to the same error type. { MyError err(1); Result res(err); MOZ_RELEASE_ASSERT(res.isErr()); bool invoked = false; auto res2 = res.mapErr([&invoked](const auto err) { MOZ_RELEASE_ASSERT(err.x == 1); invoked = true; return MyError(2); }); MOZ_RELEASE_ASSERT(res2.isErr()); MOZ_RELEASE_ASSERT(invoked); MOZ_RELEASE_ASSERT(res2.unwrapErr().x == 2); } // Mapping over error values, to a different error type. { MyError err(1); Result res(err); MOZ_RELEASE_ASSERT(res.isErr()); bool invoked = false; auto res2 = res.mapErr([&invoked](const auto err) { MOZ_RELEASE_ASSERT(err.x == 1); invoked = true; return MyError2(2); }); MOZ_RELEASE_ASSERT(res2.isErr()); MOZ_RELEASE_ASSERT(invoked); MOZ_RELEASE_ASSERT(res2.unwrapErr().a == 2); } // Mapping over success values. { Result res(5); auto res2 = res.mapErr([](const auto err) { MOZ_RELEASE_ASSERT(false); return MyError(1); }); MOZ_RELEASE_ASSERT(res2.isOk()); MOZ_RELEASE_ASSERT(res2.unwrap() == 5); } // Function pointers instead of lambdas as the mapping function. { Result res("hello"); auto res2 = res.mapErr(strlen); MOZ_RELEASE_ASSERT(res2.isErr()); MOZ_RELEASE_ASSERT(res2.unwrapErr() == 5); } } static Result strlen_ResultWrapper(const char* aValue) { return Err(strlen(aValue)); } static void OrElseTest() { struct MyError { int x; explicit MyError(int y) : x(y) {} }; struct MyError2 { int a; explicit MyError2(int b) : a(b) {} }; // `orElse`ing over error values, to Result (the same error type) error // variant. { MyError err(1); Result res(err); MOZ_RELEASE_ASSERT(res.isErr()); bool invoked = false; auto res2 = res.orElse([&invoked](const auto err) -> Result { MOZ_RELEASE_ASSERT(err.x == 1); invoked = true; if (err.x != 42) { return Err(MyError(2)); } return 'a'; }); MOZ_RELEASE_ASSERT(res2.isErr()); MOZ_RELEASE_ASSERT(invoked); MOZ_RELEASE_ASSERT(res2.unwrapErr().x == 2); } // `orElse`ing over error values, to Result (the same error type) // success variant. { MyError err(42); Result res(err); MOZ_RELEASE_ASSERT(res.isErr()); bool invoked = false; auto res2 = res.orElse([&invoked](const auto err) -> Result { MOZ_RELEASE_ASSERT(err.x == 42); invoked = true; if (err.x != 42) { return Err(MyError(2)); } return 'a'; }); MOZ_RELEASE_ASSERT(res2.isOk()); MOZ_RELEASE_ASSERT(invoked); MOZ_RELEASE_ASSERT(res2.unwrap() == 'a'); } // `orElse`ing over error values, to Result (a different error type) // error variant. { MyError err(1); Result res(err); MOZ_RELEASE_ASSERT(res.isErr()); bool invoked = false; auto res2 = res.orElse([&invoked](const auto err) -> Result { MOZ_RELEASE_ASSERT(err.x == 1); invoked = true; if (err.x != 42) { return Err(MyError2(2)); } return 'a'; }); MOZ_RELEASE_ASSERT(res2.isErr()); MOZ_RELEASE_ASSERT(invoked); MOZ_RELEASE_ASSERT(res2.unwrapErr().a == 2); } // `orElse`ing over error values, to Result (a different error type) // success variant. { MyError err(42); Result res(err); MOZ_RELEASE_ASSERT(res.isErr()); bool invoked = false; auto res2 = res.orElse([&invoked](const auto err) -> Result { MOZ_RELEASE_ASSERT(err.x == 42); invoked = true; if (err.x != 42) { return Err(MyError2(2)); } return 'a'; }); MOZ_RELEASE_ASSERT(res2.isOk()); MOZ_RELEASE_ASSERT(invoked); MOZ_RELEASE_ASSERT(res2.unwrap() == 'a'); } // `orElse`ing over success values. { Result res(5); auto res2 = res.orElse([](const auto err) -> Result { MOZ_RELEASE_ASSERT(false); return Err(MyError(1)); }); MOZ_RELEASE_ASSERT(res2.isOk()); MOZ_RELEASE_ASSERT(res2.unwrap() == 5); } // Function pointers instead of lambdas as the `orElse`ing function. { Result res("hello"); auto res2 = res.orElse(strlen_ResultWrapper); MOZ_RELEASE_ASSERT(res2.isErr()); MOZ_RELEASE_ASSERT(res2.unwrapErr() == 5); } } static void AndThenTest() { // `andThen`ing over success results. { Result r1(10); Result r2 = r1.andThen([](int x) { return Result(x + 1); }); MOZ_RELEASE_ASSERT(r2.isOk()); MOZ_RELEASE_ASSERT(r2.unwrap() == 11); } // `andThen`ing over error results. { Result r3("error"); Result r4 = r3.andThen([](int x) { MOZ_RELEASE_ASSERT(false); return Result(1); }); MOZ_RELEASE_ASSERT(r4.isErr()); MOZ_RELEASE_ASSERT(r3.unwrapErr() == r4.unwrapErr()); } // andThen with a function accepting an rvalue { Result r1(10); Result r2 = r1.andThen([](int&& x) { return Result(x + 1); }); MOZ_RELEASE_ASSERT(r2.isOk()); MOZ_RELEASE_ASSERT(r2.unwrap() == 11); } } using UniqueResult = Result, const char*>; static UniqueResult UniqueTask() { return mozilla::MakeUnique(3); } static UniqueResult UniqueTaskError() { return Err("bad"); } using UniqueErrorResult = Result>; static UniqueErrorResult UniqueError() { return Err(mozilla::MakeUnique(4)); } static Result> TryUniqueErrorResult() { MOZ_TRY(UniqueError()); return Ok(); } static void UniquePtrTest() { { auto result = UniqueTask(); MOZ_RELEASE_ASSERT(result.isOk()); auto ptr = result.unwrap(); MOZ_RELEASE_ASSERT(ptr); MOZ_RELEASE_ASSERT(*ptr == 3); auto moved = result.unwrap(); MOZ_RELEASE_ASSERT(!moved); } { auto err = UniqueTaskError(); MOZ_RELEASE_ASSERT(err.isErr()); auto ptr = err.unwrapOr(mozilla::MakeUnique(4)); MOZ_RELEASE_ASSERT(ptr); MOZ_RELEASE_ASSERT(*ptr == 4); } { auto result = UniqueTaskError(); result = UniqueResult(mozilla::MakeUnique(6)); MOZ_RELEASE_ASSERT(result.isOk()); MOZ_RELEASE_ASSERT(result.inspect() && *result.inspect() == 6); } { auto result = UniqueError(); MOZ_RELEASE_ASSERT(result.isErr()); MOZ_RELEASE_ASSERT(result.inspectErr()); MOZ_RELEASE_ASSERT(*result.inspectErr() == 4); auto err = result.unwrapErr(); MOZ_RELEASE_ASSERT(!result.inspectErr()); MOZ_RELEASE_ASSERT(err); MOZ_RELEASE_ASSERT(*err == 4); result = UniqueErrorResult(0); MOZ_RELEASE_ASSERT(result.isOk() && result.unwrap() == 0); } { auto result = TryUniqueErrorResult(); MOZ_RELEASE_ASSERT(result.isErr()); auto err = result.unwrapErr(); MOZ_RELEASE_ASSERT(err && *err == 4); MOZ_RELEASE_ASSERT(!result.inspectErr()); } } /* * */ int main() { BasicTests(); InPlaceConstructionTests(); TypeConversionTests(); EmptyValueTest(); MapTest(); MapErrTest(); OrElseTest(); AndThenTest(); UniquePtrTest(); return 0; }