1 /*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "perfetto/ext/base/status_or.h"
18
19 #include "test/gtest_and_gmock.h"
20
21 namespace perfetto {
22 namespace base {
23
TEST(StatusOrTest,IntOk)24 TEST(StatusOrTest, IntOk) {
25 base::StatusOr<int> int_or = 1;
26 ASSERT_TRUE(int_or.ok());
27 ASSERT_TRUE(int_or.status().ok());
28 ASSERT_EQ(int_or.value(), 1);
29 ASSERT_EQ(*int_or, 1);
30 }
31
TEST(StatusOrTest,VecOk)32 TEST(StatusOrTest, VecOk) {
33 base::StatusOr<std::vector<int>> vec_or({0, 1, 100});
34 ASSERT_TRUE(vec_or.ok());
35 ASSERT_TRUE(vec_or.status().ok());
36
37 ASSERT_EQ(vec_or.value()[0], 0);
38 ASSERT_EQ(vec_or.value()[2], 100);
39
40 ASSERT_EQ((*vec_or)[0], 0);
41 ASSERT_EQ((*vec_or)[2], 100);
42
43 ASSERT_EQ(vec_or->at(0), 0);
44 ASSERT_EQ(vec_or->at(2), 100);
45 }
46
TEST(StatusOrTest,ErrStatus)47 TEST(StatusOrTest, ErrStatus) {
48 base::StatusOr<std::vector<int>> err(base::ErrStatus("Bad error"));
49 ASSERT_FALSE(err.ok());
50 ASSERT_FALSE(err.status().ok());
51 }
52
53 } // namespace base
54 } // namespace perfetto
55