How do I add checks for C style array pointers

6,737 views
Skip to first unread message

Shadowmind

unread,
Feb 8, 2011, 4:30:51 PM2/8/11
to Google C++ Mocking Framework
I have a mock class with a function:
write(unsigned char * txData, unsigned char * rxData, unsigned short
length, IGPIODriver * cs, bool deassert);

The callee calls this function with an array of unsigned chars. I
would like to add some sort of check to verify the correct data was
passed to this function. I haven't found any way of doing this with
Google Mock. What I would like to do is something like this but with
an STL container:

EXPECT_CALL(spi, write(ElementsAre(0x00, 0x01, 0x02), NotNull(), 3,
NotNull(), true));

When I try this I get the following linker errors:
n file included from staging/include/gmock/gmock.h:62:0,
from tests/common/ADAU1401Tests.cpp:14:
staging/include/gmock/gmock-generated-matchers.h: In member function
'testing::internal::ElementsAreMatcher3<T1, T2, T3>::operator
testing::Matcher<T>() const [with Container = unsigned char*, T1 =
int, T2 = int, T3 = int]':
tests/common/ADAU1401Tests.cpp:155:9: instantiated from here
staging/include/gmock/gmock-generated-matchers.h:378:9: error:
'unsigned char*' is not a class, struct, or union type
staging/include/gmock/gmock-generated-matchers.h:380:44: error:
'unsigned char*' is not a class, struct, or union type
staging/include/gmock/gmock-generated-matchers.h:380:44: error:
'unsigned char*' is not a class, struct, or union type
In file included from staging/include/gmock/gmock-spec-builders.h:
71:0,
from staging/include/gmock/gmock-generated-function-
mockers.h:41,
from staging/include/gmock/gmock.h:61,
from tests/common/ADAU1401Tests.cpp:14:
staging/include/gmock/gmock-matchers.h: At global scope:
staging/include/gmock/gmock-matchers.h: In instantiation of
'testing::internal::ElementsAreMatcherImpl<unsigned char*>':
staging/include/gmock/gmock-generated-matchers.h:386:74:
instantiated from 'testing::internal::ElementsAreMatcher3<T1, T2,
T3>::operator testing::Matcher<T>() const [with Container = unsigned
char*, T1 = int, T2 = int, T3 = int]'
tests/common/ADAU1401Tests.cpp:155:9: instantiated from here
staging/include/gmock/gmock-matchers.h:2196:45: error: 'unsigned
char*' is not a class, struct, or union type
staging/include/gmock/gmock-matchers.h:2299:41: error: 'unsigned
char*' is not a class, struct, or union type
staging/include/gmock/gmock-generated-matchers.h: In member function
'testing::internal::ElementsAreMatcher3<T1, T2, T3>::operator
testing::Matcher<T>() const [with Container = unsigned char*, T1 =
int, T2 = int, T3 = int]':
staging/include/gmock/gmock-generated-matchers.h:387:3: warning:
control reaches end of non-void function
make: *** [build/test/obj/tests/common/ADAU1401Tests.o] Error 1

Does GoogleMock support verification of C style array pointers?

Piotr Gorak

unread,
Feb 9, 2011, 3:57:24 AM2/9/11
to Google C++ Mocking Framework
Yes. Check the CookBook for ElementsAreArray.

Piotr

Vlad Losev

unread,
Feb 9, 2011, 2:56:38 PM2/9/11
to Shadowmind, Google C++ Mocking Framework
ElementsAre for a pointer/count pair is implemented as a multi-argument matcher and should be used in the With clause:

EXPECT_CALL(spi, write(_, NotNull(), _, NotNull, true)
    .With(WithArgs<0, 2>(ElementsAre(0x00, 0x01, 0x02)))

Shadowmind

unread,
Feb 9, 2011, 4:12:26 PM2/9/11
to Google C++ Mocking Framework
When I try this:
>EXPECT_CALL(spi, write(_, NotNull(), _, NotNull, true)
> .With(WithArgs<0, 2>(ElementsAre(0x00, 0x01, 0x02)))

I get the following compiler errors:
make -j 3 all
g++ -MMD -c -o build/test/obj/tests/common/ADAU1401Tests.o tests/
common/ADAU1401Tests.cpp -g -O0 -ffunction-sections -fdata-sections -
Wall -DBOARD=EVK1104 -D__AVR32_UC3A3256__= -DDEBUG_LOGGING= -Iinclude
-Istaging/include -Itests/mocks
In file included from staging/include/gmock/gmock-spec-builders.h:
71:0,
from staging/include/gmock/gmock-generated-function-
mockers.h:41,
from staging/include/gmock/gmock.h:61,
from tests/common/ADAU1401Tests.cpp:14:
staging/include/gmock/gmock-matchers.h:1373:0: warning: ignoring
#pragma warning
staging/include/gmock/gmock-matchers.h:1374:0: warning: ignoring
#pragma warning
staging/include/gmock/gmock-matchers.h:1378:0: warning: ignoring
#pragma warning
g++ -MMD -c -o build/test/obj/tests/tasks/MetricBufferTests.o tests/
tasks/MetricBufferTests.cpp -g -O0 -ffunction-sections -fdata-sections
-Wall -DBOARD=EVK1104 -D__AVR32_UC3A3256__= -DDEBUG_LOGGING= -
Iinclude -Istaging/include -Itests/mocks
tests/common/ADAU1401Tests.cpp: In member function 'virtual void
zeusTests::ADAU1401Tests_TESTBitRunning_Test::TestBody()':
tests/common/ADAU1401Tests.cpp:157:64: error: no matching function for
call to 'testing::internal::TypedExpectation<void(unsigned char*,
unsigned char*, long unsigned int, zeus::IGPIODriver*,
bool)>::With(testing::internal::WithArgsAction<testing::internal::ElementsAreMatcher3<int,
int, int>, 0, 2, -0x000000001, -0x000000001, -0x000000001,
-0x000000001, -0x000000001, -0x000000001, -0x000000001,
-0x000000001>)'
staging/include/gmock/gmock-spec-builders.h:758:21: note: candidate
is: testing::internal::TypedExpectation<F>&
testing::internal::TypedExpectation<F>::With(const
testing::Matcher<const typename
testing::internal::Function<F>::ArgumentTuple&>&) [with F =
void(unsigned char*, unsigned char*, long unsigned int,
zeus::IGPIODriver*, bool), testing::internal::TypedExpectation<F> =
testing::internal::TypedExpectation<void(unsigned char*, unsigned
char*, long unsigned int, zeus::IGPIODriver*, bool)>, typename
testing::internal::Function<F>::ArgumentTuple =
std::tr1::tuple<unsigned char*, unsigned char*, long unsigned int,
zeus::IGPIODriver*, bool>]

Zhanyong Wan (λx.x x)

unread,
Feb 9, 2011, 4:22:10 PM2/9/11
to Shadowmind, Google C++ Mocking Framework
On Wed, Feb 9, 2011 at 1:12 PM, Shadowmind <nlb...@gmail.com> wrote:
> When I try this:
>>EXPECT_CALL(spi, write(_, NotNull(), _, NotNull, true)
>>    .With(WithArgs<0, 2>(ElementsAre(0x00, 0x01, 0x02)))

The syntax should be

.With(Args<0, 2>(ElementsAre(0, 1, 2)))

See http://code.google.com/p/googlemock/wiki/CheatSheet#Multiargument_Matchers
and http://code.google.com/p/googlemock/wiki/CookBook#Matching_Multiple_Arguments_as_a_Whole

--
Zhanyong

Shadowmind

unread,
Feb 10, 2011, 2:25:08 PM2/10/11
to Google C++ Mocking Framework
I must have been unclear in my initial post. Lets say I have this
function

void foo(int * param, int length)

I want to verify this function is called with the following data:

int data[3] = {1, 2, 3};
foo(data, 3);

Now I want some method to verify data was passed in and points to 1,
2, 3. If I use this:
EXPECT_CALL(classA, foo(_, 3)
.With(Args<0,1>(ElementsAre(1, 2, 3)));

This attempts to match the first and second arguments with 1 and 2.
NOT what the first argument POINTS to.

Zhanyong Wan (λx.x x)

unread,
Feb 10, 2011, 2:36:15 PM2/10/11
to Shadowmind, Google C++ Mocking Framework
On Thu, Feb 10, 2011 at 11:25 AM, Shadowmind <nlb...@gmail.com> wrote:
> I must have been unclear in my initial post.

It was clear.

> Lets say I have this
> function
>
> void foo(int * param, int length)
>
> I want to verify this function is called with the following data:
>
> int data[3] = {1, 2, 3};
> foo(data, 3);

Understood.

> Now I want some method to verify data was passed in and points to 1,
> 2, 3. If I use this:
> EXPECT_CALL(classA, foo(_, 3)
>   .With(Args<0,1>(ElementsAre(1, 2, 3)));

Yes, it should do what you want.

> This attempts to match the first and second arguments with 1 and 2.

No, this is not what it does.

Did you try the snippet? What result did you get? If it doesn't
work, what's the error message?

> NOT what the first argument POINTS to.

--
Zhanyong

Piotr Gorak

unread,
Feb 11, 2011, 8:39:53 AM2/11/11
to Google C++ Mocking Framework
Zhanyong,

Your snippet works fine, but how does it relate to the documentation

<copy-paste from cook book>

int* const expected_vector3 = new int[count];
... fill expected_vector3 with values ...
EXPECT_CALL(mock, Foo(ElementsAreArray(expected_vector3, count)));

</copy-paste from cook book>

I was not able to express the same thing by writing ElementsAreArray
directly in method call (as in the cook book example above).

Piotr

Zhanyong Wan (λx.x x)

unread,
Feb 11, 2011, 1:17:26 PM2/11/11
to Piotr Gorak, Google C++ Mocking Framework

The recipe you cited is about a different use case.

Shadowmind needs to check that a C-style array (in the form of a
pointer + a size) contains the expected elements.

The recipe you mentioned checks that an STL container contains the
expected elements, where the expected elements happen to be described
as a C-style array.

--
Zhanyong

Shadowmind

unread,
Feb 15, 2011, 11:28:13 AM2/15/11
to Google C++ Mocking Framework
It has been a few days since I have been able to get back to this.

Here is my mock function:
MOCK_METHOD5(write, void(U8 * txBuf, U8 * rxBuf, U32 length,
IGPIODriver *cs, bool deassertAfterWrite));

I tried this test code:
U8 * expected_vector3 = new U8[3];
expected_vector3[0] = 0x01;
expected_vector3[1] = 0x00;
expected_vector3[2] = 0x00;
EXPECT_CALL(spi, write(ElementsAreArray(expected_vector3, 3),
NotNull(), 7, NotNull(), true));

This is the compiler error I get:

make -j 3 all
g++ -MMD -c -o build/test/obj/tests/common/ADAU1401Tests.o tests/
common/ADAU1401Tests.cpp -g -O0 -ffunction-sections -fdata-sections -
Wall -DBOARD=EVK1104 -D__AVR32_UC3A3256__= -DDEBUG_LOGGING= -Iinclude
-Istaging/include -Itests/mocks
In file included from staging/include/gmock/gmock-spec-builders.h:
71:0,
from staging/include/gmock/gmock-generated-function-
mockers.h:41,
from staging/include/gmock/gmock.h:61,
from tests/common/ADAU1401Tests.cpp:14:
staging/include/gmock/gmock-matchers.h: In member function
'testing::internal::ElementsAreArrayMatcher<T>::operator
testing::Matcher<T>() const [with Container = unsigned char*, T =
unsigned char]':
tests/common/ADAU1401Tests.cpp:168:8: instantiated from here
staging/include/gmock/gmock-matchers.h:2337:9: error: 'unsigned char*'
is not a class, struct, or union type
staging/include/gmock/gmock-matchers.h: At global scope:
staging/include/gmock/gmock-matchers.h: In instantiation of
'testing::internal::ElementsAreMatcherImpl<unsigned char*>':
staging/include/gmock/gmock-matchers.h:2339:77: instantiated from
'testing::internal::ElementsAreArrayMatcher<T>::operator
testing::Matcher<T>() const [with Container = unsigned char*, T =
unsigned char]'
tests/common/ADAU1401Tests.cpp:168:8: instantiated from here
staging/include/gmock/gmock-matchers.h:2200:45: error: 'unsigned
char*' is not a class, struct, or union type
staging/include/gmock/gmock-matchers.h:2303:41: error: 'unsigned
char*' is not a class, struct, or union type
staging/include/gmock/gmock-matchers.h: In constructor
'testing::internal::ElementsAreMatcherImpl<Container>::ElementsAreMatcherImpl(InputIter,
size_t) [with InputIter = const unsigned char*, Container = unsigned
char*, size_t = unsigned int]':
staging/include/gmock/gmock-matchers.h:2339:77: instantiated from
'testing::internal::ElementsAreArrayMatcher<T>::operator
testing::Matcher<T>() const [with Container = unsigned char*, T =
unsigned char]'
tests/common/ADAU1401Tests.cpp:168:8: instantiated from here
staging/include/gmock/gmock-matchers.h:2206:5: error: using invalid
field
'testing::internal::ElementsAreMatcherImpl<Container>::matchers_'
staging/include/gmock/gmock-matchers.h:2209:7: error: using invalid
field
'testing::internal::ElementsAreMatcherImpl<Container>::matchers_'
staging/include/gmock/gmock-matchers.h: In member function 'bool
testing::internal::ElementsAreMatcherImpl<Container>::MatchAndExplain(Container,
testing::MatchResultListener*) const [with Container = unsigned
char*]':
tests/common/ADAU1401Tests.cpp:179:1: instantiated from here
staging/include/gmock/gmock-matchers.h:2252:52: error: request for
member 'size' in 'stl_container', which is of non-class type 'unsigned
char* const'
tests/common/ADAU1401Tests.cpp:179:1: instantiated from here
staging/include/gmock/gmock-matchers.h:2264:43: error: 'unsigned
char*' is not a class, struct, or union type
tests/common/ADAU1401Tests.cpp:179:1: instantiated from here
staging/include/gmock/gmock-matchers.h:2264:43: error: 'unsigned
char*' is not a class, struct, or union type
staging/include/gmock/gmock-matchers.h:2269:7: error: using invalid
field
'testing::internal::ElementsAreMatcherImpl<Container>::matchers_'
staging/include/gmock/gmock-matchers.h:2264:43: error: 'unsigned
char*' is not a class, struct, or union type
staging/include/gmock/gmock-matchers.h: In member function 'void
testing::internal::ElementsAreMatcherImpl<Container>::DescribeTo(std::ostream*)
const [with Container = unsigned char*, std::ostream =
std::basic_ostream<char>]':
tests/common/ADAU1401Tests.cpp:179:1: instantiated from here
staging/include/gmock/gmock-matchers.h:2219:7: error: using invalid
field
'testing::internal::ElementsAreMatcherImpl<Container>::matchers_'
staging/include/gmock/gmock-matchers.h:2224:9: error: using invalid
field
'testing::internal::ElementsAreMatcherImpl<Container>::matchers_'
staging/include/gmock/gmock-matchers.h: In member function 'void
testing::internal::ElementsAreMatcherImpl<Container>::DescribeNegationTo(std::ostream*)
const [with Container = unsigned char*, std::ostream =
std::basic_ostream<char>]':
tests/common/ADAU1401Tests.cpp:179:1: instantiated from here
staging/include/gmock/gmock-matchers.h:2242:7: error: using invalid
field
'testing::internal::ElementsAreMatcherImpl<Container>::matchers_'
staging/include/gmock/gmock-matchers.h: In member function 'size_t
testing::internal::ElementsAreMatcherImpl<Container>::count() const
[with Container = unsigned char*, size_t = unsigned int]':
staging/include/gmock/gmock-matchers.h:2253:5: instantiated from
'bool
testing::internal::ElementsAreMatcherImpl<Container>::MatchAndExplain(Container,
testing::MatchResultListener*) const [with Container = unsigned
char*]'
tests/common/ADAU1401Tests.cpp:179:1: instantiated from here
staging/include/gmock/gmock-matchers.h:2302:48: error: using invalid
field
'testing::internal::ElementsAreMatcherImpl<Container>::matchers_'
staging/include/gmock/gmock-matchers.h:2302:51: warning: control
reaches end of non-void function
make: *** [build/test/obj/tests/common/ADAU1401Tests.o] Error 1

I suspect Google Mock can not match ElementsAreArray to an unsigned
char *, maybe it can match a int * or something like that, but that
doesn't help me. I am working on an embedded system and I can't define
everything as int to make life easier for Google Mock.

Here is a way I found to do it. It is ugly, but it works:
MATCHER(CorrectData, "")
{
bool correct = true;
correct &= arg[0] == 0x01 ? true : false;
correct &= arg[1] == 0x00 ? true : false;
correct &= arg[2] == 0x01 ? true : false;
return correct;
}

TEST_F(ADAU1401Tests, TESTBitRunning)
{
EXPECT_CALL(spi, isTransferInProgress())
.Times(AtLeast(1))
.WillRepeatedly(Return(false));

/* TODO: I want to verify what is passed to the write
function, but that doesn't
* appear possible with the current version of Google Mock
*/
EXPECT_CALL(spi, write(CorrectData(), NotNull(), 7, NotNull(),
true))
.Times(AtLeast(1));

ADAU1401 dsp(spi, cs, reset, 1000000, BIT);
dsp.runBIT();
}

I need to create a matcher for each array type I want to verify.

Zhanyong Wan (λx.x x)

unread,
Feb 15, 2011, 12:54:44 PM2/15/11
to Shadowmind, Google C++ Mocking Framework
On Tue, Feb 15, 2011 at 8:28 AM, Shadowmind <nlb...@gmail.com> wrote:
> It has been a few days since I have been able to get back to this.
>
> Here is my mock function:
>    MOCK_METHOD5(write, void(U8 * txBuf, U8 * rxBuf, U32 length,
> IGPIODriver *cs, bool deassertAfterWrite));
>
> I tried this test code:
>       U8 * expected_vector3 = new U8[3];
>       expected_vector3[0] = 0x01;
>       expected_vector3[1] = 0x00;
>       expected_vector3[2] = 0x00;
>       EXPECT_CALL(spi, write(ElementsAreArray(expected_vector3, 3),
> NotNull(), 7, NotNull(), true));

Have you tried the solution I posted earlier and read the wiki pages I
listed? Thanks,

--
Zhanyong

Shadowmind

unread,
Feb 24, 2011, 10:06:21 AM2/24/11
to Google C++ Mocking Framework
So I'm back looking at this. Here is my entire test code:

#include "gmock/gmock.h"
#include "gtest/gtest.h"
using ::testing::AtLeast;
using ::testing::Return;
using ::testing::_;
using ::testing::Le;
using ::testing::Ge;
using ::testing::Eq;
using ::testing::NotNull;
using ::testing::IsNull;
using ::testing::AllOf;
using ::testing::InSequence;
using ::testing::Args;
using ::testing::ElementsAre;
using ::testing::ElementsAreArray;
using ::testing::Pointee;

namespace Tests
{
class MockTest
{
public:
MOCK_METHOD2(write, bool(int * data, int length));
};


class MatcherTests: public ::testing::Test
{
protected:

MockTest test;

MatcherTests()
{
}

virtual ~MatcherTests()
{
}

virtual void SetUp()
{
}

virtual void TearDown()
{
}
};

TEST_F(MatcherTests, TestMatcher)
{
int expected[4] = {2, 0, 0xAA, 0x55};
int count = 4;

EXPECT_CALL(test, write(_, 4))
.With(Args<0>(ElementsAre(2, 0, 0xAA, 0x55)))
.Times(1);

test.write(expected, count);
}
}


Here is the compiler results:
make -j 3 all
g++ -MMD -c -o build/test/obj/tests/components/MatcherTest.o tests/
components/MatcherTest.cpp -g -pg --coverage -O0 -Wall -
Dprivate=public -Dprotected=public -DBOARD=ZEUS -D__AVR32_UC3A3256__= -
DSWVERSION=DEV -DPART_NUMBER=501010-000000 -DDEBUG_LOGGING= -Iinclude
-Istaging/include -Itests/mocks
In file included from staging/include/gmock/gmock.h:62:0,
from tests/components/MatcherTest.cpp:1:
staging/include/gmock/gmock-generated-matchers.h: In member function
'testing::internal::ElementsAreMatcher4<T1, T2, T3, T4>::operator
testing::Matcher<T>() const [with Container = const
std::tr1::tuple<int*>&, T1 = int, T2 = int, T3 = int, T4 = int]':
staging/include/gmock/gmock-matchers.h:407:42: instantiated from
'static testing::Matcher<A1> testing::SafeMatcherCastImpl<T>::Cast(M)
[with M = testing::internal::ElementsAreMatcher4<int, int, int, int>,
T = const std::tr1::tuple<int*>&]'
staging/include/gmock/gmock-matchers.h:445:58: instantiated from
'testing::Matcher<A1> testing::SafeMatcherCast(const M&) [with T =
const std::tr1::tuple<int*>&, M =
testing::internal::ElementsAreMatcher4<int, int, int, int>]'
staging/include/gmock/gmock-generated-matchers.h:232:75:
instantiated from 'testing::internal::ArgsMatcherImpl<ArgsTuple, k0,
k1, k2, k3, k4, k5, k6, k7, k8, k9>::ArgsMatcherImpl(const
InnerMatcher&) [with InnerMatcher =
testing::internal::ElementsAreMatcher4<int, int, int, int>, ArgsTuple
= const std::tr1::tuple<int*, int>&, int k0 = 0, int k1 =
-0x000000001, int k2 = -0x000000001, int k3 = -0x000000001, int k4 =
-0x000000001, int k5 = -0x000000001, int k6 = -0x000000001, int k7 =
-0x000000001, int k8 = -0x000000001, int k9 = -0x000000001]'
staging/include/gmock/gmock-generated-matchers.h:300:40:
instantiated from 'testing::internal::ArgsMatcher<InnerMatcher, k0,
k1, k2, k3, k4, k5, k6, k7, k8, k9>::operator testing::Matcher<T>()
const [with ArgsTuple = const std::tr1::tuple<int*, int>&,
InnerMatcher = testing::internal::ElementsAreMatcher4<int, int, int,
int>, int k0 = 0, int k1 = -0x000000001, int k2 = -0x000000001, int k3
= -0x000000001, int k4 = -0x000000001, int k5 = -0x000000001, int k6 =
-0x000000001, int k7 = -0x000000001, int k8 = -0x000000001, int k9 =
-0x000000001]'
tests/components/MatcherTest.cpp:56:57: instantiated from here
staging/include/gmock/gmock-generated-matchers.h:408:9: error: no type
named 'value_type' in 'class
testing::internal::StlContainerView<std::tr1::tuple<int*> >::type'
staging/include/gmock/gmock-generated-matchers.h:410:44: error: no
type named 'value_type' in 'class
testing::internal::StlContainerView<std::tr1::tuple<int*> >::type'
staging/include/gmock/gmock-generated-matchers.h:410:44: error: no
type named 'value_type' in 'class
testing::internal::StlContainerView<std::tr1::tuple<int*> >::type'
In file included from staging/include/gmock/gmock-spec-builders.h:
71:0,
from staging/include/gmock/gmock-generated-function-
mockers.h:41,
from staging/include/gmock/gmock.h:61,
from tests/components/MatcherTest.cpp:1:
staging/include/gmock/gmock-matchers.h: At global scope:
staging/include/gmock/gmock-matchers.h: In instantiation of
'testing::internal::ElementsAreMatcherImpl<const
std::tr1::tuple<int*>&>':
staging/include/gmock/gmock-generated-matchers.h:417:74:
instantiated from 'testing::internal::ElementsAreMatcher4<T1, T2, T3,
T4>::operator testing::Matcher<T>() const [with Container = const
std::tr1::tuple<int*>&, T1 = int, T2 = int, T3 = int, T4 = int]'
staging/include/gmock/gmock-matchers.h:407:42: instantiated from
'static testing::Matcher<A1> testing::SafeMatcherCastImpl<T>::Cast(M)
[with M = testing::internal::ElementsAreMatcher4<int, int, int, int>,
T = const std::tr1::tuple<int*>&]'
staging/include/gmock/gmock-matchers.h:445:58: instantiated from
'testing::Matcher<A1> testing::SafeMatcherCast(const M&) [with T =
const std::tr1::tuple<int*>&, M =
testing::internal::ElementsAreMatcher4<int, int, int, int>]'
staging/include/gmock/gmock-generated-matchers.h:232:75:
instantiated from 'testing::internal::ArgsMatcherImpl<ArgsTuple, k0,
k1, k2, k3, k4, k5, k6, k7, k8, k9>::ArgsMatcherImpl(const
InnerMatcher&) [with InnerMatcher =
testing::internal::ElementsAreMatcher4<int, int, int, int>, ArgsTuple
= const std::tr1::tuple<int*, int>&, int k0 = 0, int k1 =
-0x000000001, int k2 = -0x000000001, int k3 = -0x000000001, int k4 =
-0x000000001, int k5 = -0x000000001, int k6 = -0x000000001, int k7 =
-0x000000001, int k8 = -0x000000001, int k9 = -0x000000001]'
staging/include/gmock/gmock-generated-matchers.h:300:40:
instantiated from 'testing::internal::ArgsMatcher<InnerMatcher, k0,
k1, k2, k3, k4, k5, k6, k7, k8, k9>::operator testing::Matcher<T>()
const [with ArgsTuple = const std::tr1::tuple<int*, int>&,
InnerMatcher = testing::internal::ElementsAreMatcher4<int, int, int,
int>, int k0 = 0, int k1 = -0x000000001, int k2 = -0x000000001, int k3
= -0x000000001, int k4 = -0x000000001, int k5 = -0x000000001, int k6 =
-0x000000001, int k7 = -0x000000001, int k8 = -0x000000001, int k9 =
-0x000000001]'
tests/components/MatcherTest.cpp:56:57: instantiated from here
staging/include/gmock/gmock-matchers.h:2200:45: error: no type named
'value_type' in 'class testing::internal::ElementsAreMatcherImpl<const
std::tr1::tuple<int*>&>::StlContainer'
staging/include/gmock/gmock-matchers.h:2303:41: error: no type named
'value_type' in 'class
testing::internal::StlContainerView<std::tr1::tuple<int*> >::type'
staging/include/gmock/gmock-generated-matchers.h: In member function
'testing::internal::ElementsAreMatcher4<T1, T2, T3, T4>::operator
testing::Matcher<T>() const [with Container = const
std::tr1::tuple<int*>&, T1 = int, T2 = int, T3 = int, T4 = int]':
staging/include/gmock/gmock-generated-matchers.h:418:3: warning:
control reaches end of non-void function
make: *** [build/test/obj/tests/components/MatcherTest.o] Error 1

Any ideas why I'm getting these errors?

Shadowmind

unread,
Feb 24, 2011, 10:12:24 AM2/24/11
to Google C++ Mocking Framework
Zhanyong, Yes I've read and re-read the links you posted above. The
first link takes me to multi-argument matchers. Which for my test case
I don't care about. My function could have been written like this:

int foo(int * data);

I don't really need to match multiple arguments. The second link takes
me to "Matching Multiple Arguments as a Whole", which once again,
isn't really what I want to match. If I was to write my own mocking
class/function I would implemented it like this:

int _dataLength;
int _dataStorage[100];
inf foo(int * data, int length)
{
memcpy(_dataStorage, data, length);
_dataLength = length;
}

Then in the test code I would write something like this:
TEST_F(MatcherTests, TestMatcher)
{
int expected[4] = {1, 2, 3, 4};
int length = 4;
test.foo(expected, length);
EXPECT_EQ(memcmp(expected, test._dataStorage, length), 0);

}


On Feb 15, 11:54 am, Zhanyong Wan (λx.x x) <w...@google.com> wrote:

Zhanyong Wan (λx.x x)

unread,
Feb 24, 2011, 2:29:02 PM2/24/11
to Shadowmind, Google C++ Mocking Framework
On Thu, Feb 24, 2011 at 7:12 AM, Shadowmind <nlb...@gmail.com> wrote:
> Zhanyong, Yes I've read and re-read the links you posted above. The

Did you actually try the solution I posted before the links, instead
of just reading it?

> first link takes me to multi-argument matchers. Which for my test case
> I don't care about.

Actually, you do. The original code you posted has a buffer argument
and a length argument, and your matcher needs to know both. That is
exactly what multi-argument matcher is for.

> My function could have been written like this:
>
> int foo(int * data);

Then it's a totally different question. One at a time. ;-)

> I don't really need to match multiple arguments.

As said, you do want to (for the original problem you posted).

> The second link takes
> me to "Matching Multiple Arguments as a Whole", which once again,
> isn't really what I want to match.

Yes, you do. ;-)

> If I was to write my own mocking
> class/function I would implemented it like this:
>
> int _dataLength;
> int _dataStorage[100];
> inf foo(int * data, int length)
> {
>   memcpy(_dataStorage, data, length);
>   _dataLength = length;
> }
>
> Then in the test code I would write something like this:
> TEST_F(MatcherTests, TestMatcher)
> {
>   int expected[4] = {1, 2, 3, 4};
>   int length = 4;
>   test.foo(expected, length);
>   EXPECT_EQ(memcmp(expected, test._dataStorage, length), 0);

What if foo() is called with a buffer whose length is shorter than 4?
Your EXPECT_EQ will have a buffer overrun.

--
Zhanyong

Zhanyong Wan (λx.x x)

unread,
Feb 24, 2011, 2:30:19 PM2/24/11
to Shadowmind, Google C++ Mocking Framework

Args<0, 1>

--
Zhanyong

Shadowmind

unread,
Feb 24, 2011, 6:34:31 PM2/24/11
to Google C++ Mocking Framework
I hang my head in shame. It DOES work. Here is what I have that works:
/* Write Status */
EXPECT_CALL(spi, write(NotNull(), IsNull(), 2, Eq(&cs), true))
.With(Args<0, 2>(ElementsAre(0x01, 0x0C)))
.Times(1);

This correctly verifies that the first argument is passed in an 2
element array and the values are 0x01 and 0x0C. Thank you for your
help!!!!

Zhanyong Wan (λx.x x)

unread,
Feb 24, 2011, 6:35:48 PM2/24/11
to Shadowmind, Google C++ Mocking Framework

Glad that we sorted it out finally! ;-)

--
Zhanyong

Vyass R

unread,
Sep 4, 2014, 8:43:21 PM9/4/14
to googl...@googlegroups.com, nlb...@gmail.com
Zhangyong and Shadowmind,
This is quite amazing. I really appreciate that. 
The documentation is quite confusing as to what they refer to as and they do not give an example of  .With(Args< >(Elements()).
Now my test looks really good. Thank you very much!
Reply all
Reply to author
Forward
0 new messages