1 /******************************************************************************
2  *
3  *  Copyright 2014 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #pragma once
20 
21 #include "osi/include/osi.h"
22 
23 // Helper macros for stubbing out functions and modules for testing.
24 
25 // Stub out a function, with call counting and mode awareness
26 #define STUB_FUNCTION(ret, name, params)                 \
27   UNUSED_ATTR static int name##_callcount;               \
28   static ret name params {                               \
29     UNUSED_ATTR int _local_callcount = name##_callcount; \
30     name##_callcount++;
31 
32 // Expect a certain number of calls to the specified stub function
33 #define EXPECT_CALL_COUNT(name, count)   \
34   EXPECT_EQ((count), (name##_callcount)) \
35       << "expected " #name " to be called " #count " times"
36 
37 // Reset the call count for the specificed stub function
38 #define RESET_CALL_COUNT(name) ((name##_callcount) = 0)
39 
40 // Use this in a stub function to catch unexpected calls.
41 // Prints out a nice message including the call count, the
42 // stub function name, and the mode index (sadly no mode name)
43 #define UNEXPECTED_CALL                                                  \
44   EXPECT_TRUE(false) << "unexpected call " << _local_callcount << " to " \
45                      << __func__ << " during mode " << (int)_current_mode
46 
47 #define MODE_IS(mode) (_current_mode == (mode))
48 
49 // Macro selection helpers
50 #define OVERLOAD_CAT(A, B) A##B
51 #define OVERLOAD_SELECT(NAME, NUM) OVERLOAD_CAT(NAME##_, NUM)
52 #define OVERLOAD_GET_COUNT(_1, _2, _3, _4, _5, _6, COUNT, ...) COUNT
53 #define OVERLOAD_VA_SIZE(...) OVERLOAD_GET_COUNT(__VA_ARGS__, 6, 5, 4, 3, 2, 1)
54 #define OVERLOAD_OF(NAME, ...) \
55   OVERLOAD_SELECT(NAME, OVERLOAD_VA_SIZE(__VA_ARGS__))(__VA_ARGS__)
56 
57 // Use this to branch stub function execution to a specific mode or modes.
58 // Treat it like an if statement. For example:
59 //
60 // DURING(dinner) EXPECT_EQ(bread_pudding, food);
61 // DURING(midday_snack, midnight_snack) EXPECT_EQ(chocolate, food);
62 #define DURING(...) OVERLOAD_OF(DURING, __VA_ARGS__)
63 
64 #define DURING_1(mode0) if (MODE_IS(mode0))
65 #define DURING_2(mode0, mode1) if (MODE_IS(mode0) || MODE_IS(mode1))
66 #define DURING_3(mode0, mode1, mode2) \
67   if (MODE_IS(mode0) || MODE_IS(mode1) || MODE_IS(mode2))
68 #define DURING_4(mode0, mode1, mode2, mode3) \
69   if (MODE_IS(mode0) || MODE_IS(mode1) || MODE_IS(mode2) || MODE_IS(mode3))
70 #define DURING_5(mode0, mode1, mode2, mode3, mode4)                           \
71   if (MODE_IS(mode0) || MODE_IS(mode1) || MODE_IS(mode2) || MODE_IS(mode3) || \
72       MODE_IS(mode4))
73 #define DURING_6(mode0, mode1, mode2, mode3, mode4, mode5)                    \
74   if (MODE_IS(mode0) || MODE_IS(mode1) || MODE_IS(mode2) || MODE_IS(mode3) || \
75       MODE_IS(mode4) || MODE_IS(mode5))
76 
77 // Use this to branch stub function exeuction to a specific call
78 // count index (zero based). Treat it like an if statement.
79 // Usually most helpful following a DURING clause. For example:
80 //
81 // DURING (breakfast) AT_CALL(0) EXPECT_EQ(bacon, food);
82 //
83 // or
84 //
85 // DURING (three_course_meal) {
86 //   AT_CALL(0) EXPECT_EQ(shrimp_cocktail, food);
87 //   AT_CALL(1) EXPECT_EQ(bacon_wrapped_bacon, food);
88 //   AT_CALL(1) EXPECT_EQ(chocolate_covered_fake_blueberries, food);
89 // }
90 #define AT_CALL(index) if (_local_callcount == (index))
91 
92 // Declare all the available test modes for the DURING clauses
93 // For example:
94 //
95 // DECLARE_TEST_MODES(breakfast, lunch, dinner);
96 #define DECLARE_TEST_MODES(...)               \
97   typedef enum { __VA_ARGS__ } _test_modes_t; \
98   static _test_modes_t _current_mode;
99 
100 // Get the current test mode
101 #define CURRENT_TEST_MODE _current_mode
102 
103 #define TEST_MODES_T _test_modes_t
104