aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/include/pwm.h
blob: e943df382ef72aaed220b03d4da5ee858c7ef573 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
    ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
                 2011,2012,2013 Giovanni Di Sirio.

    This file is part of ChibiOS/RT.

    ChibiOS/RT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ChibiOS/RT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file    pwm.h
 * @brief   PWM Driver macros and structures.
 *
 * @addtogroup PWM
 * @{
 */

#ifndef _PWM_H_
#define _PWM_H_

#if HAL_USE_PWM || defined(__DOXYGEN__)

/*===========================================================================*/
/* Driver constants.                                                         */
/*===========================================================================*/

/**
 * @name    PWM output mode macros
 * @{
 */
/**
 * @brief   Standard output modes mask.
 */
#define PWM_OUTPUT_MASK                         0x0F

/**
 * @brief   Output not driven, callback only.
 */
#define PWM_OUTPUT_DISABLED                     0x00

/**
 * @brief   Positive PWM logic, active is logic level one.
 */
#define PWM_OUTPUT_ACTIVE_HIGH                  0x01

/**
 * @brief   Inverse PWM logic, active is logic level zero.
 */
#define PWM_OUTPUT_ACTIVE_LOW                   0x02
/** @} */

/*===========================================================================*/
/* Driver pre-compile time settings.                                         */
/*===========================================================================*/

/*===========================================================================*/
/* Derived constants and error checks.                                       */
/*===========================================================================*/

/*===========================================================================*/
/* Driver data structures and types.                                         */
/*===========================================================================*/

/**
 * @brief   Driver state machine possible states.
 */
typedef enum {
  PWM_UNINIT = 0,                   /**< Not initialized.                   */
  PWM_STOP = 1,                     /**< Stopped.                           */
  PWM_READY = 2,                    /**< Ready.                             */
} pwmstate_t;

/**
 * @brief   Type of a structure representing a PWM driver.
 */
typedef struct PWMDriver PWMDriver;

/**
 * @brief   PWM notification callback type.
 *
 * @param[in] pwmp      pointer to a @p PWMDriver object
 */
typedef void (*pwmcallback_t)(PWMDriver *pwmp);

#include "pwm_lld.h"

/*===========================================================================*/
/* Driver macros.                                                            */
/*===========================================================================*/

/**
 * @name    PWM duty cycle conversion
 * @{
 */
/**
 * @brief   Converts from fraction to pulse width.
 * @note    Be careful with rounding errors, this is integer math not magic.
 *          You can specify tenths of thousandth but make sure you have the
 *          proper hardware resolution by carefully choosing the clock source
 *          and prescaler settings, see @p PWM_COMPUTE_PSC.
 *
 * @param[in] pwmp      pointer to a @p PWMDriver object
 * @param[in] denominator denominator of the fraction
 * @param[in] numerator numerator of the fraction
 * @return              The pulse width to be passed to @p pwmEnableChannel().
 *
 * @api
 */
#define PWM_FRACTION_TO_WIDTH(pwmp, denominator, numerator)                 \
  ((uint16_t)((((uint32_t)(pwmp)->period) *                                 \
               (uint32_t)(numerator)) / (uint32_t)(denominator)))

/**
 * @brief   Converts from degrees to pulse width.
 * @note    Be careful with rounding errors, this is integer math not magic.
 *          You can specify hundredths of degrees but make sure you have the
 *          proper hardware resolution by carefully choosing the clock source
 *          and prescaler settings, see @p PWM_COMPUTE_PSC.
 *
 * @param[in] pwmp      pointer to a @p PWMDriver object
 * @param[in] degrees   degrees as an integer between 0 and 36000
 * @return              The pulse width to be passed to @p pwmEnableChannel().
 *
 * @api
 */
#define PWM_DEGREES_TO_WIDTH(pwmp, degrees)                                 \
  PWM_FRACTION_TO_WIDTH(pwmp, 36000, degrees)

/**
 * @brief   Converts from percentage to pulse width.
 * @note    Be careful with rounding errors, this is integer math not magic.
 *          You can specify tenths of thousandth but make sure you have the
 *          proper hardware resolution by carefully choosing the clock source
 *          and prescaler settings, see @p PWM_COMPUTE_PSC.
 *
 * @param[in] pwmp      pointer to a @p PWMDriver object
 * @param[in] percentage percentage as an integer between 0 and 10000
 * @return              The pulse width to be passed to @p pwmEnableChannel().
 *
 * @api
 */
#define PWM_PERCENTAGE_TO_WIDTH(pwmp, percentage)                           \
  PWM_FRACTION_TO_WIDTH(pwmp, 10000, percentage)
/** @} */

/**
 * @name    Macro Functions
 * @{
 */
/**
 * @brief   Changes the period the PWM peripheral.
 * @details This function changes the period of a PWM unit that has already
 *          been activated using @p pwmStart().
 * @pre     The PWM unit must have been activated using @p pwmStart().
 * @post    The PWM unit period is changed to the new value.
 * @note    If a period is specified that is shorter than the pulse width
 *          programmed in one of the channels then the behavior is not
 *          guaranteed.
 *
 * @param[in] pwmp      pointer to a @p PWMDriver object
 * @param[in] value     new cycle time in ticks
 *
 * @iclass
 */
#define pwmChangePeriodI(pwmp, value) {                                     \
  (pwmp)->period = (value);                                                 \
  pwm_lld_change_period(pwmp, value);                                       \
}

/**
 * @brief   Enables a PWM channel.
 * @pre     The PWM unit must have been activated using @p pwmStart().
 * @post    The channel is active using the specified configuration.
 * @note    Depending on the hardware implementation this function has
 *          effect starting on the next cycle (recommended implementation)
 *          or immediately (fallback implementation).
 *
 * @param[in] pwmp      pointer to a @p PWMDriver object
 * @param[in] channel   PWM channel identifier (0...PWM_CHANNELS-1)
 * @param[in] width     PWM pulse width as clock pulses number
 *
 * @iclass
 */
#define pwmEnableChannelI(pwmp, channel, width)                             \
  pwm_lld_enable_channel(pwmp, channel, width)

/**
 * @brief   Disables a PWM channel.
 * @pre     The PWM unit must have been activated using @p pwmStart().
 * @post    The channel is disabled and its output line returned to the
 *          idle state.
 * @note    Depending on the hardware implementation this function has
 *          effect starting on the next cycle (recommended implementation)
 *          or immediately (fallback implementation).
 *
 * @param[in] pwmp      pointer to a @p PWMDriver object
 * @param[in] channel   PWM channel identifier (0...PWM_CHANNELS-1)
 *
 * @iclass
 */
#define pwmDisableChannelI(pwmp, channel)                                   \
  pwm_lld_disable_channel(pwmp, channel)

/**
 * @brief   Returns a PWM channel status.
 * @pre     The PWM unit must have been activated using @p pwmStart().
 *
 * @param[in] pwmp      pointer to a @p PWMDriver object
 * @param[in] channel   PWM channel identifier (0...PWM_CHANNELS-1)
 *
 * @iclass
 */
#define pwmIsChannelEnabledI(pwmp, channel)                                 \
  pwm_lld_is_channel_enabled(pwmp, channel)
/** @} */

/*===========================================================================*/
/* External declarations.                                                    */
/*===========================================================================*/

#ifdef __cplusplus
extern "C" {
#endif
  void pwmInit(void);
  void pwmObjectInit(PWMDriver *pwmp);
  void pwmStart(PWMDriver *pwmp, const PWMConfig *config);
  void pwmStop(PWMDriver *pwmp);
  void pwmChangePeriod(PWMDriver *pwmp, pwmcnt_t period);
  void pwmEnableChannel(PWMDriver *pwmp,
                        pwmchannel_t channel,
                        pwmcnt_t width);
  void pwmDisableChannel(PWMDriver *pwmp, pwmchannel_t channel);
#ifdef __cplusplus
}
#endif

#endif /* HAL_USE_PWM */

#endif /* _PWM_H_ */

/** @} */
efine GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS #endif // !GTEST_OS_WINDOWS || (_MSC_VER >= 1500) namespace testing { namespace gmock_generated_function_mockers_test { using testing::internal::string; using testing::_; using testing::A; using testing::An; using testing::AnyNumber; using testing::Const; using testing::DoDefault; using testing::Eq; using testing::Lt; using testing::MockFunction; using testing::Ref; using testing::Return; using testing::ReturnRef; using testing::TypedEq; class FooInterface { public: virtual ~FooInterface() {} virtual void VoidReturning(int x) = 0; virtual int Nullary() = 0; virtual bool Unary(int x) = 0; virtual long Binary(short x, int y) = 0; // NOLINT virtual int Decimal(bool b, char c, short d, int e, long f, // NOLINT float g, double h, unsigned i, char* j, const string& k) = 0; virtual bool TakesNonConstReference(int& n) = 0; // NOLINT virtual string TakesConstReference(const int& n) = 0; #ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS virtual bool TakesConst(const int x) = 0; #endif // GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS virtual int OverloadedOnArgumentNumber() = 0; virtual int OverloadedOnArgumentNumber(int n) = 0; virtual int OverloadedOnArgumentType(int n) = 0; virtual char OverloadedOnArgumentType(char c) = 0; virtual int OverloadedOnConstness() = 0; virtual char OverloadedOnConstness() const = 0; virtual int TypeWithHole(int (*func)()) = 0; virtual int TypeWithComma(const std::map<int, string>& a_map) = 0; #if GTEST_OS_WINDOWS STDMETHOD_(int, CTNullary)() = 0; STDMETHOD_(bool, CTUnary)(int x) = 0; STDMETHOD_(int, CTDecimal)(bool b, char c, short d, int e, long f, // NOLINT float g, double h, unsigned i, char* j, const string& k) = 0; STDMETHOD_(char, CTConst)(int x) const = 0; #endif // GTEST_OS_WINDOWS }; // Const qualifiers on arguments were once (incorrectly) considered // significant in determining whether two virtual functions had the same // signature. This was fixed in Visual Studio 2008. However, the compiler // still emits a warning that alerts about this change in behavior. #ifdef _MSC_VER # pragma warning(push) # pragma warning(disable : 4373) #endif class MockFoo : public FooInterface { public: MockFoo() {} // Makes sure that a mock function parameter can be named. MOCK_METHOD1(VoidReturning, void(int n)); // NOLINT MOCK_METHOD0(Nullary, int()); // NOLINT // Makes sure that a mock function parameter can be unnamed. MOCK_METHOD1(Unary, bool(int)); // NOLINT MOCK_METHOD2(Binary, long(short, int)); // NOLINT MOCK_METHOD10(Decimal, int(bool, char, short, int, long, float, // NOLINT double, unsigned, char*, const string& str)); MOCK_METHOD1(TakesNonConstReference, bool(int&)); // NOLINT MOCK_METHOD1(TakesConstReference, string(const int&)); #ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS MOCK_METHOD1(TakesConst, bool(const int)); // NOLINT #endif // Tests that the function return type can contain unprotected comma. MOCK_METHOD0(ReturnTypeWithComma, std::map<int, string>()); MOCK_CONST_METHOD1(ReturnTypeWithComma, std::map<int, string>(int)); // NOLINT MOCK_METHOD0(OverloadedOnArgumentNumber, int()); // NOLINT MOCK_METHOD1(OverloadedOnArgumentNumber, int(int)); // NOLINT MOCK_METHOD1(OverloadedOnArgumentType, int(int)); // NOLINT MOCK_METHOD1(OverloadedOnArgumentType, char(char)); // NOLINT MOCK_METHOD0(OverloadedOnConstness, int()); // NOLINT MOCK_CONST_METHOD0(OverloadedOnConstness, char()); // NOLINT MOCK_METHOD1(TypeWithHole, int(int (*)())); // NOLINT MOCK_METHOD1(TypeWithComma, int(const std::map<int, string>&)); // NOLINT #if GTEST_OS_WINDOWS MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, CTNullary, int()); MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, CTUnary, bool(int)); MOCK_METHOD10_WITH_CALLTYPE(STDMETHODCALLTYPE, CTDecimal, int(bool b, char c, short d, int e, long f, float g, double h, unsigned i, char* j, const string& k)); MOCK_CONST_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, CTConst, char(int)); // Tests that the function return type can contain unprotected comma. MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, CTReturnTypeWithComma, std::map<int, string>()); #endif // GTEST_OS_WINDOWS private: GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo); }; #ifdef _MSC_VER # pragma warning(pop) #endif class FunctionMockerTest : public testing::Test { protected: FunctionMockerTest() : foo_(&mock_foo_) {} FooInterface* const foo_; MockFoo mock_foo_; }; // Tests mocking a void-returning function. TEST_F(FunctionMockerTest, MocksVoidFunction) { EXPECT_CALL(mock_foo_, VoidReturning(Lt(100))); foo_->VoidReturning(0); } // Tests mocking a nullary function. TEST_F(FunctionMockerTest, MocksNullaryFunction) { EXPECT_CALL(mock_foo_, Nullary()) .WillOnce(DoDefault()) .WillOnce(Return(1)); EXPECT_EQ(0, foo_->Nullary()); EXPECT_EQ(1, foo_->Nullary()); } // Tests mocking a unary function. TEST_F(FunctionMockerTest, MocksUnaryFunction) { EXPECT_CALL(mock_foo_, Unary(Eq(2))) .Times(2) .WillOnce(Return(true)); EXPECT_TRUE(foo_->Unary(2)); EXPECT_FALSE(foo_->Unary(2)); } // Tests mocking a binary function. TEST_F(FunctionMockerTest, MocksBinaryFunction) { EXPECT_CALL(mock_foo_, Binary(2, _)) .WillOnce(Return(3)); EXPECT_EQ(3, foo_->Binary(2, 1)); } // Tests mocking a decimal function. TEST_F(FunctionMockerTest, MocksDecimalFunction) { EXPECT_CALL(mock_foo_, Decimal(true, 'a', 0, 0, 1L, A<float>(), Lt(100), 5U, NULL, "hi")) .WillOnce(Return(5)); EXPECT_EQ(5, foo_->Decimal(true, 'a', 0, 0, 1, 0, 0, 5, NULL, "hi")); } // Tests mocking a function that takes a non-const reference. TEST_F(FunctionMockerTest, MocksFunctionWithNonConstReferenceArgument) { int a = 0; EXPECT_CALL(mock_foo_, TakesNonConstReference(Ref(a))) .WillOnce(Return(true)); EXPECT_TRUE(foo_->TakesNonConstReference(a)); } // Tests mocking a function that takes a const reference. TEST_F(FunctionMockerTest, MocksFunctionWithConstReferenceArgument) { int a = 0; EXPECT_CALL(mock_foo_, TakesConstReference(Ref(a))) .WillOnce(Return("Hello")); EXPECT_EQ("Hello", foo_->TakesConstReference(a)); } #ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS // Tests mocking a function that takes a const variable. TEST_F(FunctionMockerTest, MocksFunctionWithConstArgument) { EXPECT_CALL(mock_foo_, TakesConst(Lt(10))) .WillOnce(DoDefault()); EXPECT_FALSE(foo_->TakesConst(5)); } #endif // GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS // Tests mocking functions overloaded on the number of arguments. TEST_F(FunctionMockerTest, MocksFunctionsOverloadedOnArgumentNumber) { EXPECT_CALL(mock_foo_, OverloadedOnArgumentNumber()) .WillOnce(Return(1)); EXPECT_CALL(mock_foo_, OverloadedOnArgumentNumber(_)) .WillOnce(Return(2)); EXPECT_EQ(2, foo_->OverloadedOnArgumentNumber(1)); EXPECT_EQ(1, foo_->OverloadedOnArgumentNumber()); } // Tests mocking functions overloaded on the types of argument. TEST_F(FunctionMockerTest, MocksFunctionsOverloadedOnArgumentType) { EXPECT_CALL(mock_foo_, OverloadedOnArgumentType(An<int>())) .WillOnce(Return(1)); EXPECT_CALL(mock_foo_, OverloadedOnArgumentType(TypedEq<char>('a'))) .WillOnce(Return('b')); EXPECT_EQ(1, foo_->OverloadedOnArgumentType(0)); EXPECT_EQ('b', foo_->OverloadedOnArgumentType('a')); } // Tests mocking functions overloaded on the const-ness of this object. TEST_F(FunctionMockerTest, MocksFunctionsOverloadedOnConstnessOfThis) { EXPECT_CALL(mock_foo_, OverloadedOnConstness()); EXPECT_CALL(Const(mock_foo_), OverloadedOnConstness()) .WillOnce(Return('a')); EXPECT_EQ(0, foo_->OverloadedOnConstness()); EXPECT_EQ('a', Const(*foo_).OverloadedOnConstness()); } TEST_F(FunctionMockerTest, MocksReturnTypeWithComma) { const std::map<int, string> a_map; EXPECT_CALL(mock_foo_, ReturnTypeWithComma()) .WillOnce(Return(a_map)); EXPECT_CALL(mock_foo_, ReturnTypeWithComma(42)) .WillOnce(Return(a_map)); EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma()); EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma(42)); } #if GTEST_OS_WINDOWS // Tests mocking a nullary function with calltype. TEST_F(FunctionMockerTest, MocksNullaryFunctionWithCallType) { EXPECT_CALL(mock_foo_, CTNullary()) .WillOnce(Return(-1)) .WillOnce(Return(0)); EXPECT_EQ(-1, foo_->CTNullary()); EXPECT_EQ(0, foo_->CTNullary()); } // Tests mocking a unary function with calltype. TEST_F(FunctionMockerTest, MocksUnaryFunctionWithCallType) { EXPECT_CALL(mock_foo_, CTUnary(Eq(2))) .Times(2) .WillOnce(Return(true)) .WillOnce(Return(false)); EXPECT_TRUE(foo_->CTUnary(2)); EXPECT_FALSE(foo_->CTUnary(2)); } // Tests mocking a decimal function with calltype. TEST_F(FunctionMockerTest, MocksDecimalFunctionWithCallType) { EXPECT_CALL(mock_foo_, CTDecimal(true, 'a', 0, 0, 1L, A<float>(), Lt(100), 5U, NULL, "hi")) .WillOnce(Return(10)); EXPECT_EQ(10, foo_->CTDecimal(true, 'a', 0, 0, 1, 0, 0, 5, NULL, "hi")); } // Tests mocking functions overloaded on the const-ness of this object. TEST_F(FunctionMockerTest, MocksFunctionsConstFunctionWithCallType) { EXPECT_CALL(Const(mock_foo_), CTConst(_)) .WillOnce(Return('a')); EXPECT_EQ('a', Const(*foo_).CTConst(0)); } TEST_F(FunctionMockerTest, MocksReturnTypeWithCommaAndCallType) { const std::map<int, string> a_map; EXPECT_CALL(mock_foo_, CTReturnTypeWithComma()) .WillOnce(Return(a_map)); EXPECT_EQ(a_map, mock_foo_.CTReturnTypeWithComma()); } #endif // GTEST_OS_WINDOWS class MockB { public: MockB() {} MOCK_METHOD0(DoB, void()); private: GTEST_DISALLOW_COPY_AND_ASSIGN_(MockB); }; // Tests that functions with no EXPECT_CALL() ruls can be called any // number of times. TEST(ExpectCallTest, UnmentionedFunctionCanBeCalledAnyNumberOfTimes) { { MockB b; } { MockB b; b.DoB(); } { MockB b; b.DoB(); b.DoB(); } } // Tests mocking template interfaces. template <typename T> class StackInterface { public: virtual ~StackInterface() {} // Template parameter appears in function parameter. virtual void Push(const T& value) = 0; virtual void Pop() = 0; virtual int GetSize() const = 0; // Template parameter appears in function return type. virtual const T& GetTop() const = 0; }; template <typename T> class MockStack : public StackInterface<T> { public: MockStack() {} MOCK_METHOD1_T(Push, void(const T& elem)); MOCK_METHOD0_T(Pop, void()); MOCK_CONST_METHOD0_T(GetSize, int()); // NOLINT MOCK_CONST_METHOD0_T(GetTop, const T&()); // Tests that the function return type can contain unprotected comma. MOCK_METHOD0_T(ReturnTypeWithComma, std::map<int, int>()); MOCK_CONST_METHOD1_T(ReturnTypeWithComma, std::map<int, int>(int)); // NOLINT private: GTEST_DISALLOW_COPY_AND_ASSIGN_(MockStack); }; // Tests that template mock works. TEST(TemplateMockTest, Works) { MockStack<int> mock; EXPECT_CALL(mock, GetSize()) .WillOnce(Return(0)) .WillOnce(Return(1)) .WillOnce(Return(0)); EXPECT_CALL(mock, Push(_)); int n = 5; EXPECT_CALL(mock, GetTop()) .WillOnce(ReturnRef(n)); EXPECT_CALL(mock, Pop()) .Times(AnyNumber()); EXPECT_EQ(0, mock.GetSize()); mock.Push(5); EXPECT_EQ(1, mock.GetSize()); EXPECT_EQ(5, mock.GetTop()); mock.Pop(); EXPECT_EQ(0, mock.GetSize()); } TEST(TemplateMockTest, MethodWithCommaInReturnTypeWorks) { MockStack<int> mock; const std::map<int, int> a_map; EXPECT_CALL(mock, ReturnTypeWithComma()) .WillOnce(Return(a_map)); EXPECT_CALL(mock, ReturnTypeWithComma(1)) .WillOnce(Return(a_map)); EXPECT_EQ(a_map, mock.ReturnTypeWithComma()); EXPECT_EQ(a_map, mock.ReturnTypeWithComma(1)); } #if GTEST_OS_WINDOWS // Tests mocking template interfaces with calltype. template <typename T> class StackInterfaceWithCallType { public: virtual ~StackInterfaceWithCallType() {} // Template parameter appears in function parameter. STDMETHOD_(void, Push)(const T& value) = 0; STDMETHOD_(void, Pop)() = 0; STDMETHOD_(int, GetSize)() const = 0; // Template parameter appears in function return type. STDMETHOD_(const T&, GetTop)() const = 0; }; template <typename T> class MockStackWithCallType : public StackInterfaceWithCallType<T> { public: MockStackWithCallType() {} MOCK_METHOD1_T_WITH_CALLTYPE(STDMETHODCALLTYPE, Push, void(const T& elem)); MOCK_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, Pop, void()); MOCK_CONST_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, GetSize, int()); MOCK_CONST_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, GetTop, const T&()); private: GTEST_DISALLOW_COPY_AND_ASSIGN_(MockStackWithCallType); }; // Tests that template mock with calltype works. TEST(TemplateMockTestWithCallType, Works) { MockStackWithCallType<int> mock; EXPECT_CALL(mock, GetSize()) .WillOnce(Return(0)) .WillOnce(Return(1)) .WillOnce(Return(0)); EXPECT_CALL(mock, Push(_)); int n = 5; EXPECT_CALL(mock, GetTop()) .WillOnce(ReturnRef(n)); EXPECT_CALL(mock, Pop()) .Times(AnyNumber()); EXPECT_EQ(0, mock.GetSize()); mock.Push(5); EXPECT_EQ(1, mock.GetSize()); EXPECT_EQ(5, mock.GetTop()); mock.Pop(); EXPECT_EQ(0, mock.GetSize()); } #endif // GTEST_OS_WINDOWS #define MY_MOCK_METHODS1_ \ MOCK_METHOD0(Overloaded, void()); \ MOCK_CONST_METHOD1(Overloaded, int(int n)); \ MOCK_METHOD2(Overloaded, bool(bool f, int n)) class MockOverloadedOnArgNumber { public: MockOverloadedOnArgNumber() {} MY_MOCK_METHODS1_; private: GTEST_DISALLOW_COPY_AND_ASSIGN_(MockOverloadedOnArgNumber); }; TEST(OverloadedMockMethodTest, CanOverloadOnArgNumberInMacroBody) { MockOverloadedOnArgNumber mock; EXPECT_CALL(mock, Overloaded()); EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2)); EXPECT_CALL(mock, Overloaded(true, 1)).WillOnce(Return(true)); mock.Overloaded(); EXPECT_EQ(2, mock.Overloaded(1)); EXPECT_TRUE(mock.Overloaded(true, 1)); } #define MY_MOCK_METHODS2_ \ MOCK_CONST_METHOD1(Overloaded, int(int n)); \ MOCK_METHOD1(Overloaded, int(int n)); class MockOverloadedOnConstness { public: MockOverloadedOnConstness() {} MY_MOCK_METHODS2_; private: GTEST_DISALLOW_COPY_AND_ASSIGN_(MockOverloadedOnConstness); }; TEST(OverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) { MockOverloadedOnConstness mock; const MockOverloadedOnConstness* const_mock = &mock; EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2)); EXPECT_CALL(*const_mock, Overloaded(1)).WillOnce(Return(3)); EXPECT_EQ(2, mock.Overloaded(1)); EXPECT_EQ(3, const_mock->Overloaded(1)); } TEST(MockFunctionTest, WorksForVoidNullary) { MockFunction<void()> foo; EXPECT_CALL(foo, Call()); foo.Call(); } TEST(MockFunctionTest, WorksForNonVoidNullary) { MockFunction<int()> foo; EXPECT_CALL(foo, Call()) .WillOnce(Return(1)) .WillOnce(Return(2)); EXPECT_EQ(1, foo.Call()); EXPECT_EQ(2, foo.Call()); } TEST(MockFunctionTest, WorksForVoidUnary) { MockFunction<void(int)> foo; EXPECT_CALL(foo, Call(1)); foo.Call(1); } TEST(MockFunctionTest, WorksForNonVoidBinary) { MockFunction<int(bool, int)> foo; EXPECT_CALL(foo, Call(false, 42)) .WillOnce(Return(1)) .WillOnce(Return(2)); EXPECT_CALL(foo, Call(true, Ge(100))) .WillOnce(Return(3)); EXPECT_EQ(1, foo.Call(false, 42)); EXPECT_EQ(2, foo.Call(false, 42)); EXPECT_EQ(3, foo.Call(true, 120)); } TEST(MockFunctionTest, WorksFor10Arguments) { MockFunction<int(bool a0, char a1, int a2, int a3, int a4, int a5, int a6, char a7, int a8, bool a9)> foo; EXPECT_CALL(foo, Call(_, 'a', _, _, _, _, _, _, _, _)) .WillOnce(Return(1)) .WillOnce(Return(2)); EXPECT_EQ(1, foo.Call(false, 'a', 0, 0, 0, 0, 0, 'b', 0, true)); EXPECT_EQ(2, foo.Call(true, 'a', 0, 0, 0, 0, 0, 'b', 1, false)); } #if GTEST_HAS_STD_FUNCTION_ TEST(MockFunctionTest, AsStdFunction) { MockFunction<int(int)> foo; auto call = [](const std::function<int(int)> &f, int i) { return f(i); }; EXPECT_CALL(foo, Call(1)).WillOnce(Return(-1)); EXPECT_CALL(foo, Call(2)).WillOnce(Return(-2)); EXPECT_EQ(-1, call(foo.AsStdFunction(), 1)); EXPECT_EQ(-2, call(foo.AsStdFunction(), 2)); } TEST(MockFunctionTest, AsStdFunctionReturnsReference) { MockFunction<int&()> foo; int value = 1; EXPECT_CALL(foo, Call()).WillOnce(ReturnRef(value)); int& ref = foo.AsStdFunction()(); EXPECT_EQ(1, ref); value = 2; EXPECT_EQ(2, ref); } #endif // GTEST_HAS_STD_FUNCTION_ } // namespace gmock_generated_function_mockers_test } // namespace testing