2013-07-24 15 views
4

我正在使用谷歌模擬單元測試我的代碼,我試圖返回,通過一個void *作爲輸出參數,一組值。如何在谷歌模擬中設置一個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的*。任何想法如何做到這一點?

回答

7

SetArgPointee和其他標準動作是類型安全的。他們不會允許你做這樣不安全的任務。你必須寫一個自定義動作,將做類型化:

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

... 

EXPECT_CALL(deviceWindow, get(_,sizeof(bigEndianTestValues[0]),_,_)) 
    .WillOnce(SetArg2ToInt32(bigEndianTestValues[i])) 
+0

感謝您的答覆。我正是這樣做的,但它只需要ACTION_P和reinterpret_cast。 – Peretz

+0

爲ACTION_P編輯 - 謝謝你的收穫!但是IIRC,你應該可以在這裏使用static_cast而沒有任何問題 - 而且它通常更安全。看到這個問題的一個很好的解釋:http://stackoverflow.com/questions/6855686/what-is-the-difference-between-static-cast-and-reinterprecast。 – VladLosev

相關問題