Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

c++ - gmock ElementsAreArray() function not found

I am trying to call the function ElementsAreArray() inside another function. I was using it normally inside my tests, but I wanted to make the tests reports better and organize it on a function. So I created the following files:

gUnitTest_Test_Array.cpp where I have my function definition:

#include "gUnitTest_Test_Array.h"

void gtest_array::expect_that_size1(volatile signed long arrayA[CIC8_TABLE_SIZE],volatile signed long arrayB[CIC8_TABLE_SIZE], std::string msg1, std::string msg2)
{
    using namespace testing;

            bool HasError = false; 
            EXPECT_THAT(arrayA, ElementsAreArray(arrayB)) << (HasError = true);

            if (HasError)
                std::cout << msg1; 
            else 
                std::cout << msg2;
}

gUnitTest_Test_Array.h where I have my class declaration:

#pragma once
#ifndef GUNITTEST_TEST_ARRAY_H
#define GUNITTEST_TEST_ARRAY_H
#include "gUnitTest_Common_Defs.h"


using namespace std;

class gtest_array
{
public:
    void expect_that_size1(volatile signed long arrayA[CIC8_TABLE_SIZE], volatile signed long arrayB[CIC8_TABLE_SIZE], std::string msg1, std::string msg2);
};


#endif

gUnitTest_Common_Defs.h where I include necessary libs and definitions

#pragma once
//#ifndef GUNITTEST_COMMON_DEFS_H
//#define   GUNITTEST_COMMON_DEFS_H
#include <stdio.h>
#include <string>
#include <gtest/gtest.h>
#include <gmock/gmock.h> 

#define ... //Whatever I need to define

Then in my Test file I call the function:

std::string msg1 = "failed";
std::string msg2 = "passed";
TestArray1.expect_that_size1(sG_Demodulation_Diagnostics_Buffers.WNLCPF_CIC8_Decim, s32L_CIC8_table, msg1, msg2);

The problem is that when I try to compile, the compiler complains that it does not find the function ElemetsAreArray(). which for me does not make sense since I included all the gtest necessary headers and before moving to the function I was using this function inline on my test as below:

EXPECT_THAT(sG_Demodulation_Diagnostics_Buffers.WNLCPF_CIC8_Decim, ElementsAreArray(s32L_CIC8_table)) << "The CIC8 block test failed. Failed at index: " << i;

Here are the error messages

Error C2672 'testing::ElementsAreArray': no matching overloaded function found GUnitTesting GUnitTestinggUnitTest_Test_Array.cpp 15

Error C2893 Failed to specialize function template 'testing::internal::ElementsAreArrayMatcherContainer::value_type testing::ElementsAreArray(const Container &)' GUnitTesting GUnitTestinggUnitTest_Test_Array.cpp 15

Error C2784 'testing::internal::ElementsAreArrayMatcher testing::ElementsAreArray(const T (&)[N])': could not deduce template argument for 'const T (&)[N]' from 'volatile long []' GUnitTesting GUnitTestinggUnitTest_Test_Array.cpp 15

Error C2780 'testing::internal::ElementsAreArrayMatcher testing::ElementsAreArray(const T *,size_t)': expects 2 arguments - 1 provided GUnitTesting GUnitTestinggUnitTest_Test_Array.cpp 15

Error C2780 'testing::internal::ElementsAreArrayMatcher<::std::iterator_traits<_Ty>::value_type> testing::ElementsAreArray(Iter,Iter)': expects 2 arguments - 1 provided GUnitTesting GUnitTestinggUnitTest_Test_Array.cpp 15

Error C2672 'testing::internal::MakePredicateFormatterFromMatcher': no matching overloaded function found GUnitTesting GUnitTestinggUnitTest_Test_Array.cpp 15

Error C2512 'testing::AssertionResult': no appropriate default constructor available GUnitTesting GUnitTestinggUnitTest_Test_Array.cpp 15


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The comments above were right about arrayA and arrayB not being arrays itself. I knew it, but I thought that ElementsAreArray() would be happy receiving the address from the pointers. It turns out, as also commented above, that it converts it to std::vectors, so the workaround I found was to create a local copy of my vectors inside the function (if anyone has any more efficient idea, please let me know):

#pragma once
#ifndef GUNITTEST_TEST_ARRAY_H
#define GUNITTEST_TEST_ARRAY_H
#include "gUnitTest_Common_Defs.h"


using namespace std;

class gtest_array
{
public:
    void expect_that_size1(volatile signed long (*arrayA), volatile signed long (*arrayB), uint16_t table_size, std::string msg1, std::string msg2);
};


#endif
#include "gUnitTest_Test_Array.h"

void gtest_array::expect_that_size1(volatile signed long (*arrayA),volatile signed long (*arrayB), uint16_t table_size, std::string msg1, std::string msg2)
{
    using namespace testing;
    signed long array1[CIC8_TABLE_SIZE];
    signed long array2[CIC8_TABLE_SIZE];

    std::copy(arrayA, arrayA + table_size, array1);
    std::copy(arrayB, arrayB + table_size, array2);

    bool HasError = false; 
    EXPECT_THAT(array1, ElementsAreArray(array2)) << (HasError = true);

    if (HasError)
        std::cout << msg1; 
    else 
        std::cout << msg2;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...