在谷歌测试阵列的比较?(Comparison of arrays in google test?)

2019-07-19 00:11发布

我期待比较谷歌测试两个数组。 在单元测试+ +,这是通过CHECK_ARRAY_EQUAL完成。 你如何做到这一点在谷歌测试?

Answer 1:

我真的建议在看谷歌C ++模拟框架 。 即使你不想嘲笑什么,它允许你写轻松相当复杂的断言。

例如

//checks that vector v is {5, 10, 15}
ASSERT_THAT(v, ElementsAre(5, 10, 15));

//checks that map m only have elements 1 => 10, 2 => 20
ASSERT_THAT(m, ElementsAre(Pair(1, 10), Pair(2, 20)));

//checks that in vector v all the elements are greater than 10 and less than 20
ASSERT_THAT(v, Each(AllOf(Gt(10), Lt(20))));

//checks that vector v consist of 
//   5, number greater than 10, anything.
ASSERT_THAT(v, ElementsAre(5, Gt(10), _));

有大量的匹配器对每一个可能的情况下,你可以将它们结合起来,以实现几乎所有的东西。

我有没有告诉过你, ElementsAre只需要iteratorssize()在一个类的方法来工作? 因此,它不仅适用与STL而是使用自定义的容器也可以是任何的容器。

谷歌模拟声称是几乎一样谷歌测试便携式和坦白说,我不明白为什么你不会使用它。 这只是纯粹的真棒。



Answer 2:

如果你只需要检查数组相等,那么蛮力也可以工作:

int arr1[10];
int arr2[10];

// initialize arr1 and arr2

EXPECT_TRUE( 0 == std::memcmp( arr1, arr2, sizeof( arr1 ) ) );

然而,这并不能告诉你哪些元素不同。



Answer 3:

如果你想在C数组指针比较使用谷歌模拟一个数组,你可以通过的std ::向量。 例如:

uint8_t expect[] = {1, 2, 3, 42};
uint8_t * buffer = expect;
uint32_t buffer_size = sizeof(expect) / sizeof(expect[0]);
ASSERT_THAT(std::vector<uint8_t>(buffer, buffer + buffer_size), 
            ::testing::ElementsAreArray(expect));

谷歌莫克的ElementsAreArray也接受指针和长度,该长度允许两个C数组的指针进行比较。 例如:

ASSERT_THAT(std::vector<uint8_t>(buffer, buffer + buffer_size), 
            ::testing::ElementsAreArray(buffer, buffer_size));

我花了很多时间试图拼凑了一起。 由于这个StackOverlow岗位上的std ::矢量迭代器初始化提醒。 注意,此方法将在缓冲器阵列元素矢量比较之前复制到标准::。



Answer 4:

我有相同的问题,所以我写了一对夫妇做两个通用的容器之间的比较宏。 它是可扩展到具有任何容器const_iteratorbeginend 。 如果失败了,它会显示的阵列就错在何处,并会为每个失败要素做这么详细的消息; 这将确保它们是相同的长度; 在你的代码,它作为失败报告位置是你调用同一行EXPECT_ITERABLE_EQ( std::vector< double >, a, b)

//! Using the google test framework, check all elements of two containers
#define EXPECT_ITERABLE_BASE( PREDICATE, REFTYPE, TARTYPE, ref, target) \
    { \
    const REFTYPE& ref_(ref); \
    const TARTYPE& target_(target); \
    REFTYPE::const_iterator refIter = ref_.begin(); \
    TARTYPE::const_iterator tarIter = target_.begin(); \
    unsigned int i = 0; \
    while(refIter != ref_.end()) { \
        if ( tarIter == target_.end() ) { \
            ADD_FAILURE() << #target " has a smaller length than " #ref ; \
            break; \
        } \
        PREDICATE(* refIter, * tarIter) \
            << "Containers " #ref  " (refIter) and " #target " (tarIter)" \
               " differ at index " << i; \
        ++refIter; ++tarIter; ++i; \
    } \
    EXPECT_TRUE( tarIter == target_.end() ) \
        << #ref " has a smaller length than " #target ; \
    }

//! Check that all elements of two same-type containers are equal
#define EXPECT_ITERABLE_EQ( TYPE, ref, target) \
    EXPECT_ITERABLE_BASE( EXPECT_EQ, TYPE, TYPE, ref, target )

//! Check that all elements of two different-type containers are equal
#define EXPECT_ITERABLE_EQ2( REFTYPE, TARTYPE, ref, target) \
    EXPECT_ITERABLE_BASE( EXPECT_EQ, REFTYPE, TARTYPE, ref, target )

//! Check that all elements of two same-type containers of doubles are equal
#define EXPECT_ITERABLE_DOUBLE_EQ( TYPE, ref, target) \
    EXPECT_ITERABLE_BASE( EXPECT_DOUBLE_EQ, TYPE, TYPE, ref, target )

希望这个作品对你(和你的问题已提交两个月后,你真正把这个答案)。



Answer 5:

我遇到了类似的问题,在谷歌测试比较阵列。

因为我需要基本的比较void*char* (低级别的代码测试),我不知道的事情无论是谷歌模拟 (这我还使用在项目中)或塞特的大宏能帮助我在特定情况。 我写了下面的宏:

#define EXPECT_ARRAY_EQ(TARTYPE, reference, actual, element_count) \
    {\
    TARTYPE* reference_ = static_cast<TARTYPE *> (reference); \
    TARTYPE* actual_ = static_cast<TARTYPE *> (actual); \
    for(int cmp_i = 0; cmp_i < element_count; cmp_i++ ){\
      EXPECT_EQ(reference_[cmp_i], actual_[cmp_i]);\
    }\
    }

石膏就在那儿进行比较时,宏使用void*到其他的东西:

  void* retrieved = ptr->getData();
  EXPECT_EQ(6, ptr->getSize());
  EXPECT_ARRAY_EQ(char, "data53", retrieved, 6)

在评论托拜厄斯建议铸造void*char*和使用EXPECT_STREQ ,宏我之前种种原因错过了-它看起来像一个更好的选择。



Answer 6:

下面是我写比较两个浮点阵列[片段]的断言:

/* See
http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
for thorough information about comparing floating point values.
For this particular application we know that the value range is -1 to 1 (audio signal),
so we can compare to absolute delta of 1/2^22 which is the smallest representable value in
a 22-bit recording.
*/
const float FLOAT_INEQUALITY_TOLERANCE = float(1.0 / (1 << 22));


template <class T>
::testing::AssertionResult AreFloatingPointArraysEqual(
                                const T* const expected,
                                const T* const actual,
                                unsigned long length)
{
    ::testing::AssertionResult result = ::testing::AssertionFailure();
    int errorsFound = 0;
    const char* separator = " ";
    for (unsigned long index = 0; index < length; index++)
    {
        if (fabs(expected[index] - actual[index]) > FLOAT_INEQUALITY_TOLERANCE)
        {
            if (errorsFound == 0)
            {
                result << "Differences found:";
            }
            if (errorsFound < 3)
            {
                result << separator
                        << expected[index] << " != " << actual[index]
                        << " @ " << index;
                separator = ", ";
            }
            errorsFound++;
        }
    }
    if (errorsFound > 0)
    {
        result << separator << errorsFound << " differences in total";
        return result;
    }
    return ::testing::AssertionSuccess();
}

谷歌的测试框架内的用法是这样的:

EXPECT_TRUE(AreFloatingPointArraysEqual(expectedArray, actualArray, lengthToCompare));

在发生错误的情况下,像如下输出产生:

..\MyLibraryTestMain.cpp:145: Failure
Value of: AreFloatingPointArraysEqual(expectedArray, actualArray, lengthToCompare)
  Actual: false (Differences found: 0.86119759082794189 != 0.86119747161865234 @ 14, -0.5552707314491272 != -0.55527061223983765 @ 24, 0.047732405364513397 != 0.04773232713341713 @ 36, 339 differences in total)
Expected: true

有关在一般比较浮点值深入的讨论,请参阅本 。



Answer 7:

ASSERT_EQ(x.size(), y.size()) << "Vectors x and y are of unequal length";

for (int i = 0; i < x.size(); ++i) {
  EXPECT_EQ(x[i], y[i]) << "Vectors x and y differ at index " << i;
}

资源



Answer 8:

我经历了所有的元素使用的经典的循环。 您可以使用SCOPED_TRACE读出其中迭代数组元素不同。 这为您提供更多的信息比起其他一些方法和易于阅读。

for (int idx=0; idx<ui16DataSize; idx++)
{
    SCOPED_TRACE(idx); //write to the console in which iteration the error occurred
    ASSERT_EQ(array1[idx],array2[idx]);
}


文章来源: Comparison of arrays in google test?