2015-12-03 1 views
0

J'ai une méthodePourquoi ai-je besoin du second argument?

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

Le test doit vérifier que tableau de {1, 2, 3, 4, 5}, qui dispose de 5 éléments est d'être transmis au put Dans mon TEST_F(), j'ai le code suivant.

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))); 

Cela semble fonctionner, mais il me rend fou parce que, il semble que au lieu de Args<0,1>, je devrais avoir Args<0> depuis que je suis tableau correspondant pour le premier paramètre et la taille du tableau est mis à 5. Le passage à:

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

produit ces erreurs:

/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

Le premier paramètre est un pointeur, 'uint8_t *', pas un tableau. – molbdnilo

+0

@molbdnilo Que voulez-vous dire? Le premier argument est un pointeur sur le tableau, pouvez-vous développer votre commentaire? – user1135541

+1

C'est un pointeur sur * one * 'uint8_t', pas sur un tableau. Le 'uint8_t' qu'il pointe à se trouve être le premier élément d'un tableau, mais le compilateur est totalement inconscient de cela. – molbdnilo

Répondre

1

Un pointeur est pas un tableau; c'est simplement une adresse à un endroit en mémoire. Le système n'a aucun moyen de savoir combien de temps la mémoire que vous avez réservée pour le tableau est. Le deuxième argument est spécifiquement à cette fin. Vous savez quand vous le créez combien de temps il faut donc transmettre cette information.

Cependant, sauf si cela vous est interdit, je vous recommande de prendre le temps d'apprendre à utiliser des modèles pour gérer vos tableaux et structures de type tableau. Le std :: array est très sympa et a toutes sortes de cloches et de sifflets que vous pouvez utiliser. Le meilleur de tous, il gère tous les tracas qui vont avec la maintenance de votre tableau.

+0

En regardant gmock CheatSheet, les paramètres à l'intérieur de <> identifient le numéro d'argument, la longueur du tableau est spécifiée par le 5 dans ce qui suit: 'ElementsAreArray (arr, 5)'. Il se peut que je manque quelque chose, mais je crois que c'est le cas. Je travaille sur les tests pour le code bas niveau où std :: array n'a pas été utilisé, ça ne dépend pas de moi. – user1135541