如何在谷歌模拟中将 void* 参数设置为一组值?

2024-02-01

我正在使用 google mock 对我的代码进行单元测试,并且我试图通过 void* 作为输出参数返回一组值。

uint32_t bigEndianTestValues[BIG_ENDIAN_FIELD_MAX_ELEMENTS] = {0xDEADBEEF, 0xFFFF0000, 0x00000000, 0x00A00F10, 0x11234211};

for (int i = 0; i < BIG_ENDIAN_FIELD_MAX_ELEMENTS; ++i)
{
   EXPECT_CALL( deviceWindow, get(_,sizeof(bigEndianTestValues[0]),_,_) )
   .WillOnce(SetArgPointee<2>(bigEndianTestValues[i]))
   .RetiresOnSaturation();
}

我的模拟包含一个方法调用,如下所示

MOCK_METHOD4(get, void(const size_t, const size_t, void*, nNIAPALS100::tStatus&));

但我收到以下错误

....\include\gmock/gmock-actions.h(693) : error C2100: illegal indirection
....\gmock/gmock-actions.h(369) : see reference to function template instantiation 'void testing::internal::SetArgumentPointeeAction<N,A,kIsProto>::Perform<void,std::tr1::tuple<T0,T1,T2,T3>>(const ArgumentTuple &) const' being compiled with
            [
                N=0x02,
                A=uint32_t,
                kIsProto=false,
                T0=size_t,
                T1=size_t,
                T2=void *,
                T3=nNIAPALS100::tStatus &,
                ArgumentTuple=std::tr1::tuple<size_t,size_t,void *,nNIAPALS100::tStatus &>
            ]
            z:\Perforce\jperetzm_JPERETZM-DT\ThirdPartyExports\googlemock\export\1.6\1.6.0a0\includes\gmock\include\gmock/gmock-actions.h(368) : while compiling class template member function 'void testing::Polymor
    phicAction<Impl>::MonomorphicImpl<F>::Perform(const std::tr1::tuple<T0,T1,T2,T3> &)'
            with
            [
                Impl=testing::internal::SetArgumentPointeeAction<0x02,uint32_t,false>,
                F=void (size_t,size_t,void *,nNIAPALS100::tStatus &),
                T0=size_t,
                T1=size_t,
                T2=void *,
                T3=nNIAPALS100::tStatus &
            ]
....\include\gmock/gmock-actions.h(356) : see reference to class template instantiation 'testing::PolymorphicAction<Impl>::MonomorphicImpl<F>' being compiled
            with
            [
                Impl=testing::internal::SetArgumentPointeeAction<0x02,uint32_t,false>,
                F=void (size_t,size_t,void *,nNIAPALS100::tStatus &)
            ]
....\StorageMapRaw_Test.cpp(471) : see reference to function template instantiation 'testing::PolymorphicAction<Impl>::operator testing::Action<F>(void) const<F>' being compiled with
            [
                Impl=testing::internal::SetArgumentPointeeAction<0x02,uint32_t,false>,
                F=void (size_t,size_t,void *,nNIAPALS100::tStatus &)
            ]
....include\gmock/gmock-actions.h(693) : error C2440: '=' : cannot convert from 'const uint32_t' to 'void *const 'Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast storageMap_test_win64U_x64_msvc90_debug - 2 error(s), 0 warning(s)

看来我需要将 void* 转换为 uint32_t*。知道怎么做吗?


SetArgPointee和其他标准操作是类型安全的。他们不会允许你做这样不安全的任务。您必须编写一个自定义操作来执行类型转换:

ACTION_P(SetArg2ToInt32, value) { *static_cast<uint32_t*>(arg2) = value; }

...

EXPECT_CALL(deviceWindow, get(_,sizeof(bigEndianTestValues[0]),_,_))
  .WillOnce(SetArg2ToInt32(bigEndianTestValues[i]))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在谷歌模拟中将 void* 参数设置为一组值? 的相关文章

随机推荐