【发布时间】:2018-05-28 22:27:59
【问题描述】:
今天我将我的矩阵类重写为constexpr。我对这个类有 100% 的单元测试覆盖率,但我注意到在将几乎所有函数转换为 constexpr 后,构造函数的一部分在 lcov 中被标记为根本不再覆盖。
这是只有构造函数的类。
template<typename T, std::size_t m, std::size_t n>
class Matrix
{
static_assert(std::is_arithmetic<T>::value,
"Matrix can only be declared with a type where "
"std::is_arithmetic is true.");
public:
constexpr Matrix(
std::initializer_list<std::initializer_list<T>> matrix_data)
{
if (matrix_data.size() != m)
{
throw std::invalid_argument("Invalid amount of rows.");
}
for (const auto& col : matrix_data)
{
if (col.size() != n)
{
throw std::invalid_argument("Invalid amount of columns.");
}
}
std::size_t pos_i = 0;
std::size_t pos_j = 0;
for (auto i = matrix_data.begin(); i != matrix_data.end(); ++i)
{
for (auto j = i->begin(); j != i->end(); ++j)
{
this->data[pos_i][pos_j] = *j;
++pos_j;
}
++pos_i;
pos_j = 0;
}
}
private:
std::array<std::array<T, n>, m> data{};
};
int main()
{
Matrix<double, 2, 2> mat = {
{1, 2},
{3, 4}
};
return 0;
}
我正在使用 gcc 7.2 和 lcov 1.13
【问题讨论】:
-
这里问了什么?
-
@ÖöTiib 为什么
constexpr代码在启用gcov时不会产生任何覆盖率数据。
标签: c++ unit-testing code-coverage gcov lcov