2015-12-03 36 views
0

我有一个方法为什么我需要第二个参数?

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

测试需要验证阵列的{1, 2, 3, 4, 5},其具有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))); 
      ^ 
+0

第一个参数是一个指针“uint8_t *”,而不是一个数组。 – molbdnilo

+0

@molbdnilo你是什么意思?第一个参数是一个指向数组的指针,你可以详细说明你的评论吗? – user1135541

+1

这是一个指向* one *'uint8_t'的指针,而不是数组。它指向的'uint8_t'恰好是数组的第一个元素,但编译器完全不知道这一点。 – molbdnilo

回答

1

指针是不阵列;它只是一个地址在内存中的一个点。系统没有办法知道您为阵列预留的内存有多长。第二个论据是专门为此目的而设计的。你知道当你创建它多久时,你必须一起传递这些信息。

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

+0

查看gmock CheatSheet,<>中的参数标识参数号,数组长度由以下内容中的5指定:'ElementsAreArray(arr,5)'。我可能会错过一些东西,但我相信是这样。我正在研究低级别代码的测试,其中不使用std :: array,这不取决于我。 – user1135541

相关问题