mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
fix singed integer overflow UB in integer_types and other trivial compiler warnings
libeigen/eigen!2380
This commit is contained in:
@@ -70,17 +70,6 @@ struct adjoint_specific<false> {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename MatrixType, typename Scalar = typename MatrixType::Scalar>
|
||||
MatrixType RandomMatrix(Index rows, Index cols, Scalar min, Scalar max) {
|
||||
MatrixType M = MatrixType(rows, cols);
|
||||
for (Index i = 0; i < rows; ++i) {
|
||||
for (Index j = 0; j < cols; ++j) {
|
||||
M(i, j) = Eigen::internal::random<Scalar>(min, max);
|
||||
}
|
||||
}
|
||||
return M;
|
||||
}
|
||||
|
||||
template <typename MatrixType>
|
||||
void adjoint(const MatrixType& m) {
|
||||
/* this test covers the following files:
|
||||
|
||||
@@ -505,7 +505,6 @@ template <typename Scalar>
|
||||
void cholesky_rowmajor_boundary() {
|
||||
typedef typename NumTraits<Scalar>::Real RealScalar;
|
||||
typedef Matrix<Scalar, Dynamic, Dynamic, RowMajor> RowMatrixType;
|
||||
typedef Matrix<Scalar, Dynamic, 1> VectorType;
|
||||
|
||||
const Index sizes[] = {7, 8, 9, 15, 16, 17, 31, 32, 33};
|
||||
for (Index si = 0; si < Index(sizeof(sizes) / sizeof(sizes[0])); ++si) {
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
template <typename MatrixType>
|
||||
void signed_integer_type_tests(const MatrixType& m) {
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
constexpr Scalar kMax = (Scalar(1) << ((8 * sizeof(Scalar) - 2) / 2)) - 1;
|
||||
|
||||
enum { is_signed = (Scalar(-1) > Scalar(0)) ? 0 : 1 };
|
||||
VERIFY(is_signed == 1);
|
||||
@@ -24,26 +25,12 @@ void signed_integer_type_tests(const MatrixType& m) {
|
||||
Index rows = m.rows();
|
||||
Index cols = m.cols();
|
||||
|
||||
MatrixType m1(rows, cols), m2 = MatrixType::Random(rows, cols), mzero = MatrixType::Zero(rows, cols);
|
||||
|
||||
{
|
||||
int guard = 0;
|
||||
do {
|
||||
m1 = MatrixType::Random(rows, cols);
|
||||
} while ((m1 == mzero || m1 == m2) && (++guard) < 100);
|
||||
VERIFY(guard < 100);
|
||||
}
|
||||
MatrixType m1 = RandomMatrix<MatrixType>(rows, cols, Scalar(0), kMax);
|
||||
MatrixType m2 = RandomMatrix<MatrixType>(rows, cols, Scalar(0), kMax);
|
||||
|
||||
// check linear structure
|
||||
|
||||
Scalar s1;
|
||||
{
|
||||
int guard = 0;
|
||||
do {
|
||||
s1 = internal::random<Scalar>();
|
||||
} while (s1 == 0 && (++guard) < 100);
|
||||
VERIFY(guard < 100);
|
||||
}
|
||||
Scalar s1 = internal::random<Scalar>(1, kMax);
|
||||
|
||||
VERIFY_IS_EQUAL(-(-m1), m1);
|
||||
VERIFY_IS_EQUAL(-m2 + m1 + m2, m1);
|
||||
@@ -53,46 +40,31 @@ void signed_integer_type_tests(const MatrixType& m) {
|
||||
template <typename MatrixType>
|
||||
void integer_type_tests(const MatrixType& m) {
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
constexpr Scalar kMax = (Scalar(1) << ((8 * sizeof(Scalar) - 2) / 2)) - 1;
|
||||
|
||||
VERIFY(NumTraits<Scalar>::IsInteger);
|
||||
enum { is_signed = (Scalar(-1) > Scalar(0)) ? 0 : 1 };
|
||||
VERIFY(int(NumTraits<Scalar>::IsSigned) == is_signed);
|
||||
|
||||
typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
|
||||
typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
|
||||
|
||||
Index rows = m.rows();
|
||||
Index cols = m.cols();
|
||||
|
||||
// this test relies a lot on Random.h, and there's not much more that we can do
|
||||
// to test it, hence I consider that we will have tested Random.h
|
||||
MatrixType m1(rows, cols), m2 = MatrixType::Random(rows, cols), m3(rows, cols), mzero = MatrixType::Zero(rows, cols);
|
||||
|
||||
typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
|
||||
SquareMatrixType identity = SquareMatrixType::Identity(rows, rows), square = SquareMatrixType::Random(rows, rows);
|
||||
VectorType v1(rows), v2 = VectorType::Random(rows), vzero = VectorType::Zero(rows);
|
||||
|
||||
{
|
||||
int guard = 0;
|
||||
do {
|
||||
m1 = MatrixType::Random(rows, cols);
|
||||
} while ((m1 == mzero || m1 == m2) && (++guard) < 100);
|
||||
VERIFY(guard < 100);
|
||||
}
|
||||
|
||||
{
|
||||
int guard = 0;
|
||||
do {
|
||||
v1 = VectorType::Random(rows);
|
||||
} while ((v1 == vzero || v1 == v2) && (++guard) < 100);
|
||||
VERIFY(guard < 100);
|
||||
}
|
||||
MatrixType m1 = RandomMatrix<MatrixType>(rows, cols, Scalar(0), kMax);
|
||||
MatrixType m2 = RandomMatrix<MatrixType>(rows, cols, Scalar(0), kMax);
|
||||
MatrixType m3 = RandomMatrix<MatrixType>(rows, cols, Scalar(0), kMax);
|
||||
SquareMatrixType square = RandomMatrix<SquareMatrixType>(rows, rows, Scalar(0), kMax);
|
||||
VectorType v1 = RandomMatrix<VectorType, Scalar>(rows, Index(1), Scalar(0), NumTraits<Scalar>::highest() / Scalar(2));
|
||||
|
||||
VERIFY_IS_APPROX(v1, v1);
|
||||
VERIFY_IS_NOT_APPROX(v1, 2 * v1);
|
||||
VERIFY_IS_APPROX(vzero, v1 - v1);
|
||||
VERIFY_IS_APPROX(VectorType::Zero(rows), v1 - v1);
|
||||
|
||||
VERIFY_IS_APPROX(m1, m1);
|
||||
VERIFY_IS_NOT_APPROX(m1, 2 * m1);
|
||||
VERIFY_IS_APPROX(mzero, m1 - m1);
|
||||
VERIFY_IS_APPROX(MatrixType::Zero(rows, cols), m1 - m1);
|
||||
|
||||
VERIFY_IS_APPROX(m3 = m1, m1);
|
||||
MatrixType m4;
|
||||
@@ -113,10 +85,7 @@ void integer_type_tests(const MatrixType& m) {
|
||||
|
||||
// check linear structure
|
||||
|
||||
Scalar s1;
|
||||
do {
|
||||
s1 = internal::random<Scalar>();
|
||||
} while (s1 == 0);
|
||||
Scalar s1 = internal::random<Scalar>(1, kMax);
|
||||
|
||||
VERIFY_IS_EQUAL(m1 + m1, 2 * m1);
|
||||
VERIFY_IS_EQUAL(m1 + m2 - m1, m2);
|
||||
@@ -134,10 +103,12 @@ void integer_type_tests(const MatrixType& m) {
|
||||
|
||||
// check matrix product.
|
||||
|
||||
VERIFY_IS_APPROX(identity * m1, m1);
|
||||
VERIFY_IS_APPROX(square * (m1 + m2), square * m1 + square * m2);
|
||||
VERIFY_IS_APPROX((m1 + m2).transpose() * square, m1.transpose() * square + m2.transpose() * square);
|
||||
VERIFY_IS_APPROX((m1 * m2.transpose()) * m1, m1 * (m2.transpose() * m1));
|
||||
if (!NumTraits<Scalar>::IsSigned) {
|
||||
VERIFY_IS_APPROX(SquareMatrixType::Identity(rows, rows) * m1, m1);
|
||||
VERIFY_IS_APPROX(square * (m1 + m2), square * m1 + square * m2);
|
||||
VERIFY_IS_APPROX((m1 + m2).transpose() * square, m1.transpose() * square + m2.transpose() * square);
|
||||
VERIFY_IS_APPROX((m1 * m2.transpose()) * m1, m1 * (m2.transpose() * m1));
|
||||
}
|
||||
}
|
||||
|
||||
template <int>
|
||||
|
||||
11
test/main.h
11
test/main.h
@@ -902,6 +902,17 @@ void setRandomDataInRange(DataContainer& data_container, typename DataContainer:
|
||||
|
||||
using namespace Eigen;
|
||||
|
||||
template <typename MatrixType, typename Scalar = typename MatrixType::Scalar>
|
||||
MatrixType RandomMatrix(Index rows, Index cols, Scalar min, Scalar max) {
|
||||
MatrixType M = MatrixType(rows, cols);
|
||||
for (Index i = 0; i < rows; ++i) {
|
||||
for (Index j = 0; j < cols; ++j) {
|
||||
M(i, j) = Eigen::internal::random<Scalar>(min, max);
|
||||
}
|
||||
}
|
||||
return M;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set number of repetitions for unit test from input string.
|
||||
*
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
template <typename MatrixType>
|
||||
void trmv(const MatrixType& m) {
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
typedef typename NumTraits<Scalar>::Real RealScalar;
|
||||
typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
|
||||
|
||||
Index rows = m.rows();
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
// wrapper that disables array-oriented access to real and imaginary components
|
||||
struct TestComplex : public std::complex<float> {
|
||||
TestComplex() = default;
|
||||
TestComplex(const TestComplex&) = default;
|
||||
TestComplex(std::complex<float> x) : std::complex<float>(x){};
|
||||
TestComplex(float x) : std::complex<float>(x){};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user