简体   繁体   English

如何使用 google-test 检查向量中的内容?

[英]How do I check the contents in a vector using google-test?

std::vector<uint8_t> vector1(10);
std::vector<uint8_t> vector2(10);

std::fill(vector1.begin(), vector1.end(), 2);
std::fill(vector2.begin(), vector2.end(), 2);

EXPECT_EQ(vector1, vector2);

Does the EXPECT_EQ() above check that the contents in vector1 and vector2 are equal?上面的 EXPECT_EQ() 是否检查 vector1 和 vector2 中的内容是否相等? If not, how do I check that that the contents in vector1 and vector2 are equal using a googletest EXPECT_* function?如果不是,我如何使用 googletest EXPECT_* 函数检查 vector1 和 vector2 中的内容是否相等?

The documentation for googletest explains how to test the contents of C strings and c++ string objects, but not how to check the contents of a c++ vector. googletest 的文档解释了如何测试 C 字符串和 c++ 字符串对象的内容,但没有解释如何检查 c++ 向量的内容。

EXPECT_EQ uses operator == to compare objects, andoperator == for std::vector (quote from cppreference): EXPECT_EQ使用operator ==比较对象,operator ==用于std::vector (引用自 cppreference):

Checks if the contents of lhs and rhs are equal, that is, they have the same number of elements and each element in lhs compares equal with the element in rhs at the same position.检查lhsrhs的内容是否相等,即它们具有相同数量的元素,并且lhs每个元素与rhs中相同位置的元素比较相等。

So the answer is yes , it will compare the contents of the vectors.所以答案是肯定的,它将比较向量的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM