xref: /aosp_15_r20/external/pigweed/pw_sync/borrow_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2021 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_sync/borrow.h"
16 
17 #include "pw_sync/lock_testing.h"
18 #include "pw_sync_private/borrow_lockable_tests.h"
19 #include "pw_unit_test/framework.h"
20 
21 namespace pw::sync::test {
22 namespace {
23 
24 // Test fixtures.
25 
26 class Base {
27  public:
28   virtual ~Base() = default;
value() const29   int value() const { return value_; }
30 
31  protected:
Base(int value)32   Base(int value) : value_(value) {}
33 
34  private:
35   int value_ = 0;
36 };
37 
38 class Derived : public Base {
39  public:
Derived(int value)40   Derived(int value) : Base(value) {}
41 };
42 
43 // Unit tests.
44 
TEST(BorrowedPointerTest,MoveConstruct)45 TEST(BorrowedPointerTest, MoveConstruct) {
46   Derived derived(1);
47   FakeBasicLockable lock;
48   Borrowable<Derived, FakeBasicLockable> borrowable(derived, lock);
49   BorrowedPointer<Base, VirtualBasicLockable> borrowed(borrowable.acquire());
50   EXPECT_EQ(borrowed->value(), 1);
51 }
52 
TEST(BorrowedPointerTest,MoveAssign)53 TEST(BorrowedPointerTest, MoveAssign) {
54   Derived derived(2);
55   FakeBasicLockable lock;
56   Borrowable<Derived, FakeBasicLockable> borrowable(derived, lock);
57   BorrowedPointer<Base, VirtualBasicLockable> borrowed = borrowable.acquire();
58   EXPECT_EQ(borrowed->value(), 2);
59 }
60 
61 PW_SYNC_ADD_BORROWABLE_LOCK_TESTS(FakeBasicLockable);
62 PW_SYNC_ADD_BORROWABLE_LOCK_TESTS(FakeLockable);
63 PW_SYNC_ADD_BORROWABLE_TIMED_LOCK_TESTS(FakeTimedLockable, FakeClock);
64 
65 }  // namespace
66 }  // namespace pw::sync::test
67