• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "utils/expected.h"
17 
18 #include <gtest/gtest.h>
19 
20 #include <type_traits>
21 
22 namespace ark::test::expected {
23 
24 // NOLINTBEGIN(readability-magic-numbers)
25 
26 enum class ErrorCode { FIRST, SECOND };
27 
Helper(int v)28 static Expected<int, ErrorCode> Helper(int v)
29 {
30     switch (v) {
31         case 0U:
32             return Unexpected {ErrorCode::FIRST};
33         case 1U:
34             return {42U};
35         default:
36             return Unexpected {ErrorCode::SECOND};
37     }
38 }
39 
40 struct Default {
41     int v;
42 };
43 
44 struct NonDefaultConstructible {
45     NonDefaultConstructible() = delete;
46 };
47 
TEST(Expected,Unexpected)48 TEST(Expected, Unexpected)
49 {
50     int e = 1;
51     auto u = Unexpected(e);
52     EXPECT_EQ(Unexpected<int>(1U).Value(), 1U);
53     EXPECT_EQ(u.Value(), 1U);
54     EXPECT_EQ(static_cast<const Unexpected<int> &>(u).Value(), 1U);
55 }
56 
TEST(Expected,Ctor)57 TEST(Expected, Ctor)
58 {
59     int v = 1;
60     auto e = Expected<int, ErrorCode>(v);
61     EXPECT_TRUE(e);
62     EXPECT_EQ(e.Value(), 1U);
63     EXPECT_EQ(*e, 1U);
64 
65     auto e0 = Expected<int, ErrorCode>();
66     EXPECT_EQ(*e0, 0U);
67 
68     auto e1 = Expected<int, ErrorCode>(2U);
69     EXPECT_EQ(e1.Value(), 2U);
70 
71     auto e2 = Expected<int, ErrorCode>(Unexpected(ErrorCode::FIRST));
72     auto u = Unexpected(ErrorCode::SECOND);
73     auto e3 = Expected<int, ErrorCode>(u);
74     EXPECT_FALSE(e2);
75     EXPECT_EQ(e2.Error(), ErrorCode::FIRST);
76     EXPECT_EQ(e3.Error(), ErrorCode::SECOND);
77 
78     // Default constructor is only enabled if T is default constructible.
79     EXPECT_FALSE((std::is_default_constructible_v<Expected<NonDefaultConstructible, ErrorCode>>));
80 }
81 
TEST(Expected,Access)82 TEST(Expected, Access)
83 {
84     const auto e1 = Expected<int, ErrorCode>(Unexpected(ErrorCode::FIRST));
85     EXPECT_EQ(e1.Error(), ErrorCode::FIRST);
86     EXPECT_EQ((Expected<int, ErrorCode>(Unexpected(ErrorCode::SECOND)).Error()), ErrorCode::SECOND);
87     const auto e2 = Expected<int, ErrorCode>(1U);
88     EXPECT_EQ(e2.Value(), 1U);
89     EXPECT_EQ(*e2, 1U);
90     EXPECT_EQ((*Expected<int, ErrorCode>(2U)), 2U);
91     EXPECT_EQ((Expected<int, ErrorCode>(3U).Value()), 3U);
92 }
93 
TEST(Expected,Assignment)94 TEST(Expected, Assignment)
95 {
96     auto d = Default {1U};
97     Expected<Default, ErrorCode> t = d;
98     t.Value() = Default {2U};
99     EXPECT_TRUE(t);
100     EXPECT_EQ((*t).v, 2U);
101     t = Unexpected(ErrorCode::FIRST);
102     EXPECT_FALSE(t);
103     EXPECT_EQ(t.Error(), ErrorCode::FIRST);
104 }
105 
TEST(Expected,Basic)106 TEST(Expected, Basic)
107 {
108     auto res1 = Helper(0U);
109     auto res2 = Helper(1U);
110     auto res3 = Helper(2U);
111     EXPECT_FALSE(res1);
112     EXPECT_TRUE(res2);
113     EXPECT_FALSE(res3);
114     EXPECT_EQ(res1.Error(), ErrorCode::FIRST);
115     EXPECT_EQ(*res2, 42U);
116     EXPECT_EQ(res3.Error(), ErrorCode::SECOND);
117 }
118 
TEST(Expected,ValueOr)119 TEST(Expected, ValueOr)
120 {
121     auto res1 = Helper(0U).ValueOr(1U);
122     auto res2 = Helper(res1).ValueOr(res1);
123     auto e = Expected<int, ErrorCode>(1U);
124     EXPECT_EQ(res1, 1U);
125     EXPECT_EQ(res2, 42U);
126     EXPECT_EQ(e.ValueOr(0U), 1U);
127 }
128 // NOLINTEND(readability-magic-numbers)
129 
130 }  // namespace ark::test::expected
131