/** * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "utils/expected.h" #include #include namespace panda::test::expected { enum class ErrorCode { First, Second }; static Expected helper(int v) { switch (v) { case 0: return Unexpected {ErrorCode::First}; case 1: return {42}; default: return Unexpected {ErrorCode::Second}; } } struct Default { int v; }; struct NonDefaultConstructible { NonDefaultConstructible() = delete; }; TEST(Expected, Unexpected) { int e = 1; auto u = Unexpected(e); EXPECT_EQ(Unexpected(1).Value(), 1); EXPECT_EQ(u.Value(), 1); EXPECT_EQ(static_cast &>(u).Value(), 1); } TEST(Expected, Ctor) { int v = 1; auto e = Expected(v); EXPECT_TRUE(e); EXPECT_EQ(e.Value(), 1); EXPECT_EQ(*e, 1); auto e0 = Expected(); EXPECT_EQ(*e0, 0); auto e1 = Expected(2); EXPECT_EQ(e1.Value(), 2); auto e2 = Expected(Unexpected(ErrorCode::First)); auto u = Unexpected(ErrorCode::Second); auto e3 = Expected(u); EXPECT_FALSE(e2); EXPECT_EQ(e2.Error(), ErrorCode::First); EXPECT_EQ(e3.Error(), ErrorCode::Second); // Default constructor is only enabled if T is default constructible. EXPECT_FALSE((std::is_default_constructible_v>)); } TEST(Expected, Access) { const auto e1 = Expected(Unexpected(ErrorCode::First)); EXPECT_EQ(e1.Error(), ErrorCode::First); EXPECT_EQ((Expected(Unexpected(ErrorCode::Second)).Error()), ErrorCode::Second); const auto e2 = Expected(1); EXPECT_EQ(e2.Value(), 1); EXPECT_EQ(*e2, 1); EXPECT_EQ((*Expected(2)), 2); EXPECT_EQ((Expected(3).Value()), 3); } TEST(Expected, Assignment) { auto d = Default {1}; Expected t = d; t.Value() = Default {2}; EXPECT_TRUE(t); EXPECT_EQ((*t).v, 2); t = Unexpected(ErrorCode::First); EXPECT_FALSE(t); EXPECT_EQ(t.Error(), ErrorCode::First); } TEST(Expected, Basic) { auto res1 = helper(0); auto res2 = helper(1); auto res3 = helper(2); EXPECT_FALSE(res1); EXPECT_TRUE(res2); EXPECT_FALSE(res3); EXPECT_EQ(res1.Error(), ErrorCode::First); EXPECT_EQ(*res2, 42); EXPECT_EQ(res3.Error(), ErrorCode::Second); } TEST(Expected, ValueOr) { auto res1 = helper(0).ValueOr(1); auto res2 = helper(res1).ValueOr(res1); auto e = Expected(1); EXPECT_EQ(res1, 1); EXPECT_EQ(res2, 42); EXPECT_EQ(e.ValueOr(0), 1); } } // namespace panda::test::expected