1 /* 2 * Copyright 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 <ftl/fake_guard.h> 18 #include <gtest/gtest.h> 19 20 #include <functional> 21 #include <mutex> 22 23 namespace android::test { 24 25 // Keep in sync with example usage in header file. TEST(FakeGuard,Example)26TEST(FakeGuard, Example) { 27 struct { 28 std::mutex mutex; 29 int x FTL_ATTRIBUTE(guarded_by(mutex)) = -1; 30 31 int f() { 32 { 33 ftl::FakeGuard guard(mutex); 34 x = 0; 35 } 36 37 return FTL_FAKE_GUARD(mutex, x + 1); 38 } 39 40 std::function<int()> g() const { 41 return [this]() FTL_FAKE_GUARD(mutex) { return x; }; 42 } 43 } s; 44 45 EXPECT_EQ(s.f(), 1); 46 EXPECT_EQ(s.g()(), 0); 47 } 48 49 } // namespace android::test 50