Integrate Refactor solver sources and dual MSVC CMake builds
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
@@ -109,22 +110,60 @@ TEST(DenseMath, RowMajorMatrixMatchesKnownGemvGemm) {
|
||||
std::invalid_argument);
|
||||
}
|
||||
|
||||
TEST(DenseMath, MatrixProductsRejectNonfiniteOperandsAndPreserveValidReuse) {
|
||||
Matrix left{2U, 2U, 1.0};
|
||||
Matrix right{2U, 1U, 2.0};
|
||||
Vector rhs{2U, 2.0};
|
||||
rhs[1U] = std::numeric_limits<double>::infinity();
|
||||
EXPECT_THROW(static_cast<void>(left.Multiply(rhs)), std::invalid_argument);
|
||||
rhs[1U] = 2.0;
|
||||
left(0U, 1U) = std::numeric_limits<double>::quiet_NaN();
|
||||
EXPECT_THROW(static_cast<void>(left.Multiply(rhs)), std::invalid_argument);
|
||||
EXPECT_THROW(static_cast<void>(left.Multiply(right)), std::invalid_argument);
|
||||
left(0U, 1U) = 1.0;
|
||||
right(1U, 0U) = std::numeric_limits<double>::infinity();
|
||||
EXPECT_THROW(static_cast<void>(left.Multiply(right)), std::invalid_argument);
|
||||
right(1U, 0U) = 2.0;
|
||||
EXPECT_DOUBLE_EQ(left.Multiply(rhs)[0U], 4.0);
|
||||
EXPECT_DOUBLE_EQ(left.Multiply(right)(1U, 0U), 4.0);
|
||||
|
||||
TEST(DenseMath, MatrixContiguousScaleAndRobustNorm) {
|
||||
Matrix matrix{2U, 3U};
|
||||
const double entries[]{1.0, -2.0, 3.0, -4.0, 5.0, -6.0};
|
||||
for (std::size_t i = 0; i < 6U; ++i) matrix.Data()[i] = entries[i];
|
||||
const Matrix& immutable = matrix;
|
||||
EXPECT_EQ(immutable.Data() + 3, &immutable(1U, 0U));
|
||||
EXPECT_NEAR(matrix.Norm(), std::sqrt(91.0), 2.0e-15);
|
||||
matrix.Scale(-2.0);
|
||||
for (std::size_t i = 0; i < 6U; ++i)
|
||||
EXPECT_DOUBLE_EQ(matrix.Data()[i], -2.0 * entries[i]);
|
||||
EXPECT_DOUBLE_EQ(immutable(1U, 2U), 12.0);
|
||||
Matrix huge{1U, 2U, 1.0e308};
|
||||
EXPECT_NEAR(huge.Norm() / 1.0e308, std::sqrt(2.0), 4.0e-16);
|
||||
Matrix tiny{2U, 1U, 1.0e-300};
|
||||
EXPECT_NEAR(tiny.Norm() / 1.0e-300, std::sqrt(2.0), 4.0e-16);
|
||||
Matrix empty{0U, (std::numeric_limits<std::size_t>::max)()};
|
||||
EXPECT_DOUBLE_EQ(empty.Norm(), 0.0);
|
||||
EXPECT_NO_THROW(empty.Scale(3.0));
|
||||
}
|
||||
|
||||
TEST(DenseMath, MatrixProductKeepsShapeAndFiniteValidationOrder) {
|
||||
const double nan = (std::numeric_limits<double>::quiet_NaN)();
|
||||
Matrix matrix{1U, 2U, nan};
|
||||
try {
|
||||
static_cast<void>(matrix.Multiply(Vector{1U}));
|
||||
FAIL() << "Shape mismatch was accepted";
|
||||
} catch (const std::invalid_argument& failure) {
|
||||
EXPECT_STREQ(failure.what(),
|
||||
"Matrix-vector multiplication has incompatible dimensions.");
|
||||
}
|
||||
EXPECT_THROW(static_cast<void>(matrix.Multiply(Vector{2U})),
|
||||
std::invalid_argument);
|
||||
EXPECT_THROW(static_cast<void>(matrix.Multiply(Matrix{2U, 0U})),
|
||||
std::invalid_argument);
|
||||
EXPECT_THROW(static_cast<void>(Matrix{0U, 2U}.Multiply(Vector{2U, nan})),
|
||||
std::invalid_argument);
|
||||
const Matrix finite{2U, 2U, 3.0};
|
||||
const auto square = finite.Multiply(finite);
|
||||
EXPECT_DOUBLE_EQ(square(0U, 0U), 18.0);
|
||||
EXPECT_DOUBLE_EQ(finite(0U, 0U), 3.0);
|
||||
}
|
||||
|
||||
TEST(DenseMath, MatrixSelfAssignmentPreservesShapeAndStorage) {
|
||||
Matrix matrix{2U, 1U, 7.0};
|
||||
const Matrix* same = &matrix;
|
||||
matrix = *same;
|
||||
EXPECT_EQ(matrix.Rows(), 2U);
|
||||
EXPECT_EQ(matrix.Columns(), 1U);
|
||||
EXPECT_DOUBLE_EQ(matrix(1U, 0U), 7.0);
|
||||
Matrix* mutable_same = &matrix;
|
||||
matrix = std::move(*mutable_same);
|
||||
EXPECT_EQ(matrix.Rows(), 2U);
|
||||
EXPECT_DOUBLE_EQ(matrix(1U, 0U), 7.0);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user