1 /*
2  * Copyright (C) 2016 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 #ifndef CHRE_PLATFORM_LINUX_ASSERT_H_
18 #define CHRE_PLATFORM_LINUX_ASSERT_H_
19 
20 #include <cassert>
21 
22 #ifdef GTEST
23 
24 #include "chre/platform/log.h"
25 #include "gmock/gmock.h"
26 
27 class MockAssert;
28 extern MockAssert *gMockAssert;
29 
30 class AssertInterface {
31  public:
32   virtual void doAssert() = 0;
33 };
34 
35 class MockAssert : public AssertInterface {
36  public:
MockAssert()37   MockAssert() {
38     gMockAssert = this;
39   }
~MockAssert()40   ~MockAssert() {
41     gMockAssert = nullptr;
42   }
43 
44   MOCK_METHOD0(doAssert, void());
45 };
46 
47 /**
48  * Helper macro that wraps a statement in a block that sets up the mock for
49  * CHRE_ASSERT and expects it to be called at least once. This allows for
50  * verification that the code to be tested throws an expected assertion failure,
51  * and also handles the failure gracefully when assertions are compiled out.
52  * Triggered assertions are logged using LOGI, so they can be manually checked
53  * in the test output.
54  *
55  * Example:
56  * @code{.cpp}
57  *   TEST(DynamicVector, InsertToSparseIndexFails) {
58  *     DynamicVector<int> vector;
59  *     EXPECT_CHRE_ASSERT(EXPECT_FALSE(vector.insert(5));
60  *   }
61  * @endcode
62  */
63 #define EXPECT_CHRE_ASSERT(statement)                                     \
64   do {                                                                    \
65     ASSERT_EQ(gMockAssert, nullptr);                                      \
66     MockAssert chreMockAssert;                                            \
67     EXPECT_CALL(chreMockAssert, doAssert()).Times(::testing::AtLeast(1)); \
68     statement;                                                            \
69   } while (0)
70 
71 #define CHRE_ASSERT(condition)                           \
72   do {                                                   \
73     if (gMockAssert != nullptr && !(condition)) {        \
74       LOGI("Mocked assertion " #condition " triggered"); \
75       gMockAssert->doAssert();                           \
76     } else {                                             \
77       assert(condition);                                 \
78     }                                                    \
79   } while (0)
80 
81 #else  // if !defined(GTEST)
82 
83 #define CHRE_ASSERT(condition) assert(condition)
84 
85 #endif  // GTEST
86 
87 #endif  // CHRE_PLATFORM_LINUX_ASSERT_H_
88