我正试图在另一个函数中调用函数ElementsAreArray((。我通常在测试中使用它,但我想让测试报告更好,并将其组织成一个函数。所以我创建了以下文件:
gUnitTest_Test_Array.cpp这里有我的函数定义:
#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其中我有类声明:
#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其中我包括必要的库和定义
#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
然后在我的测试文件中,我调用函数:
std::string msg1 = "failed";
std::string msg2 = "passed";
TestArray1.expect_that_size1(sG_Demodulation_Diagnostics_Buffers.WNLCPF_CIC8_Decim, s32L_CIC8_table, msg1, msg2);
问题是,当我尝试编译时,编译器会抱怨找不到函数ElemetsAreArray((。这对我来说没有意义,因为我包含了所有gtest必需的头,在转到函数之前,我在测试中内联使用了这个函数,如下所示:
EXPECT_THAT(sG_Demodulation_Diagnostics_Buffers.WNLCPF_CIC8_Decim, ElementsAreArray(s32L_CIC8_table)) << "The CIC8 block test failed. Failed at index: " << i;
以下是错误消息
错误C2672"testing::ElementsAreArray":没有匹配重载函数已找到GUnitTesting\gUnitTest\gUnitTest_Test_Array.cpp 15
错误C2893无法专门化函数模板'测试::内部::ElementsAreArrayMatcherContainer::value_type测试::ElementsAreArray(const Container&('GUnitTesting\gUnitTest\gUnitTest_Test_Array.cpp 15
错误C2784'测试::internal::ElementsAreArrayMatcher测试::ElementsAreArray(const T(&([N](":无法推导模板来自"volatile long"的"const T(&([N]"的参数[]'GUnitTesting\GUnitTests\gUnitTest_Test_Array.cpp 15
错误C2780'测试:内部::ElementsAreArrayMatchertesting::ElementsAreArray(const T*,size_T(':需要2个参数-1提供GUnitTesting\gUnitTest\gUnitTest_Test_Array.cpp 15
错误C2780'测试:内部::ElementsAreArrayMatcher<:std::iterator_traits<_Ty>;:value_type>测试::ElementsAreArray(Iter,Iter(":需要2个参数-1提供GUnitTesting\gUnitTest\gUnitTest_Test_Array.cpp 15
错误C2672"测试::internal::MakePredicateFormatterFromMatcher":否匹配重载函数找到GUnitTesting\gUnitTest\gUnitTest_Test_Array.cpp 15
错误C2512"testing::AssertionResult":没有适当的默认值构造函数可用GUnitTesting\gUnitTest\gUnitTest_Test_Array.cpp 15
上面的评论是正确的,arrayA和arrayB本身不是数组。我知道,但我认为ElementsAreArray((会很高兴从指针中收到地址。事实证明,正如上面所评论的,它将其转换为std::vectors,所以我找到的解决方法是在函数中创建我的vectors的本地副本(如果有人有更有效的想法,请告诉我(:
#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;
}