xref: /aosp_15_r20/external/pigweed/pw_bluetooth/result_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2022 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://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, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_bluetooth/result.h"
16 
17 #include "pw_unit_test/framework.h"
18 
19 namespace pw::bluetooth {
20 namespace {
21 
22 enum class TestError { kFailure0, kFailure1 };
23 
TEST(ResultTest,NoValue)24 TEST(ResultTest, NoValue) {
25   EXPECT_TRUE(Result<TestError>().ok());
26 
27   Result<TestError> error_result(TestError::kFailure0);
28   ASSERT_FALSE(error_result.ok());
29   EXPECT_EQ(error_result.error(), TestError::kFailure0);
30 }
31 
TEST(ResultTest,Value)32 TEST(ResultTest, Value) {
33   Result<TestError, int> ok_result(42);
34   ASSERT_TRUE(ok_result.ok());
35   EXPECT_EQ(ok_result.value(), 42);
36 
37   Result<TestError, int> error_result(TestError::kFailure1);
38   ASSERT_FALSE(error_result.ok());
39   EXPECT_EQ(error_result.error(), TestError::kFailure1);
40 }
41 
42 struct NonTrivialDestructor {
NonTrivialDestructorpw::bluetooth::__anona7f74f980111::NonTrivialDestructor43   explicit NonTrivialDestructor(int* destructor_counter)
44       : destructor_counter_(destructor_counter) {}
~NonTrivialDestructorpw::bluetooth::__anona7f74f980111::NonTrivialDestructor45   ~NonTrivialDestructor() {
46     EXPECT_NE(nullptr, destructor_counter_);
47     (*destructor_counter_)++;
48   }
49   NonTrivialDestructor(const NonTrivialDestructor&) = delete;
50   NonTrivialDestructor(NonTrivialDestructor&&) = delete;
51 
52   int* destructor_counter_;
53 };
54 
TEST(ResultTest,NonTrivialDestructorTest)55 TEST(ResultTest, NonTrivialDestructorTest) {
56   int counter = 0;
57   {
58     Result<TestError, NonTrivialDestructor> ret(std::in_place, &counter);
59     ASSERT_TRUE(ret.ok());
60   }
61   EXPECT_EQ(counter, 1);
62   {
63     Result<TestError, NonTrivialDestructor> ret(TestError::kFailure0);
64     ASSERT_FALSE(ret.ok());
65   }
66   EXPECT_EQ(counter, 1);
67 }
68 
69 }  // namespace
70 }  // namespace pw::bluetooth
71