为什么我需要第二个参数?

时间:2015-12-03 15:45:19

标签: c++ gmock

我有一个方法

class FooInterface {
    bool put(uint8_t* array, unsigned array_length);
}

测试需要验证具有5个元素的{1, 2, 3, 4, 5}数组是否已传递到put在我的TEST_F()中,我有以下代码。

uint8_t arr[5] = {1, 2, 3, 4, 5};  // Values for 'array' the out parameter

MockFoo foo;
FooInterface* fooI = &foo;

EXPECT_CALL(foo, put(_, 5))
    .With(Args<0,1>(ElementsAreArray(arr, 5)));

这似乎有效,但它让我发疯,因为,似乎而不是Args<0,1>,我应该Args<0>,因为我匹配第一个参数的数组并且数组大小已设置到5。改为:

EXPECT_CALL(BFO, put(_, 5))
    .With(Args<0>(ElementsAreArray(arr, 5)));  // Here is 'Args<0>'

产生以下错误:

/home/sporty/ws-ccs/googletest/googlemock/include/gmock/gmock-matchers.h:3114:34: error: no type named 'value_type' in 'std::tr1::tuple<const unsigned char *>'                                                                                                                                                                                                              
  typedef typename StlContainer::value_type Element;                                                                                                                                                                                                                                                                                                                         
          ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~                                                                                                                                                                                                                                                                                                                                  
/home/sporty/ws-ccs/googletest/googlemock/include/gmock/gmock-matchers.h:3532:28: note: in instantiation of template class 'testing::internal::ElementsAreMatcherImpl<const std::tr1::tuple<const unsigned char *> &>' requested here                                                                                                                                        
    return MakeMatcher(new ElementsAreMatcherImpl<Container>(                                                                                                                                                                                                                                                                                                                
                           ^                                                                                                                                                                                                                                                                                                                                                 
/home/sporty/ws-ccs/googletest/googlemock/include/gmock/gmock-matchers.h:555:12: note: in instantiation of function template specialization 'testing::internal::ElementsAreArrayMatcher<unsigned char>::operator Matcher<const std::tr1::tuple<const unsigned char *> &>' requested here                                                                                     
    return polymorphic_matcher_or_value;                                                                                                                                                                                                                                                                                                                                     
           ^                                                                                                                                                                                                                                                                                                                                                                 
/home/sporty/ws-ccs/googletest/googlemock/include/gmock/gmock-matchers.h:531:12: note: in instantiation of member function 'testing::internal::MatcherCastImpl<const std::tr1::tuple<const unsigned char *> &, testing::internal::ElementsAreArrayMatcher<unsigned char> >::CastImpl' requested here                                                                         
    return CastImpl(                                                                                                                                                                                                                                                                                                                                                         
           ^                                                                                                                                                                                                                                                                                                                                                                 
/home/sporty/ws-ccs/googletest/googlemock/include/gmock/gmock-matchers.h:628:45: note: in instantiation of member function 'testing::internal::MatcherCastImpl<const std::tr1::tuple<const unsigned char *> &, testing::internal::ElementsAreArrayMatcher<unsigned char> >::Cast' requested here                                                                             
    return internal::MatcherCastImpl<T, M>::Cast(polymorphic_matcher_or_value);                                                                                                                                                                                                                                                                                              
                                            ^                                                                                                                                                                                                                                                                                                                                
/home/sporty/ws-ccs/googletest/googlemock/include/gmock/gmock-matchers.h:666:34: note: in instantiation of function template specialization 'testing::SafeMatcherCastImpl<const std::tr1::tuple<const unsigned char *> &>::Cast<testing::internal::ElementsAreArrayMatcher<unsigned char> >' requested here                                                                  
  return SafeMatcherCastImpl<T>::Cast(polymorphic_matcher);                                                                                                                                                                                                                                                                                                                  
                                 ^                                                                                                                                                                                                                                                                                                                                           
/home/sporty/ws-ccs/googletest/googlemock/include/gmock/gmock-generated-matchers.h:221:24: note: in instantiation of function template specialization 'testing::SafeMatcherCast<const std::tr1::tuple<const unsigned char *> &, testing::internal::ElementsAreArrayMatcher<unsigned char> >' requested here                                                                  
      : inner_matcher_(SafeMatcherCast<const SelectedArgs&>(inner_matcher)) {}                                                                                                                                                                                                                                                                                               
                       ^                                                                                                                                                                                                                                                                                                                                                     
/home/sporty/ws-ccs/googletest/googlemock/include/gmock/gmock-generated-matchers.h:288:28: note: in instantiation of function template specialization 'testing::internal::ArgsMatcherImpl<const std::tr1::tuple<const unsigned char *, unsigned int> &, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1>::ArgsMatcherImpl<testing::internal::ElementsAreArrayMatcher<unsigned char> >' 
    return MakeMatcher(new ArgsMatcherImpl<ArgsTuple, k0, k1, k2, k3, k4, k5,                                                                                                                                                                                                                                                                                                
                           ^                                                                                                                                                                                                                                                                                                                                                 
/home/sporty/ws-ccs/hw_1_5/miwt-os/coap/unittest/cbor_encoder_test.cpp:176:15: note: in instantiation of function template specialization 'testing::internal::ArgsMatcher<testing::internal::ElementsAreArrayMatcher<unsigned char>, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1>::operator Matcher<const std::tr1::tuple<const unsigned char *, unsigned int> &>' requested here  
        .With(Args<0>(ElementsAreArray(arr, 5)));
              ^  

1 个答案:

答案 0 :(得分:1)

指针不是数组;它只是内存中某个地点的地址。系统无需知道您为阵列保留的内存有多长。第二个论点是专门为此目的。你知道什么时候创建它需要多长时间,所以你必须传递信息。

但是,除非您被禁止这样做,否则我建议您花时间学习如何使用模板来处理数组和数组类型结构。 std :: array非常好用,你可以使用各种铃声和口哨声。最重要的是它可以处理维护阵列所带来的所有麻烦。