From fba74228485b8059e72ff1d1478ca7ef8cb20c11 Mon Sep 17 00:00:00 2001 From: Alvin Zhang <41vin2h4n9@gmail.com> Date: Sat, 2 Nov 2024 17:28:20 -0700 Subject: [PATCH] feat: add test for LinearProblem --- test/LinearProblem_test.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 test/LinearProblem_test.cpp diff --git a/test/LinearProblem_test.cpp b/test/LinearProblem_test.cpp new file mode 100644 index 0000000..5034409 --- /dev/null +++ b/test/LinearProblem_test.cpp @@ -0,0 +1,35 @@ +#include + +#include +#include + +using namespace suboptimal; +using namespace Eigen; + +TEST_CASE("LinearProblem - Problem initialization") { + auto problem = LinearProblem::maximizationProblem(Vector2d{{3, 2}}); + problem.addLessThanConstraint(Vector2d{{2, 1}}, 50); + problem.addGreaterThanConstraint(Vector2d{{1, 3}}, 15); + problem.addEqualityConstraint(Vector2d{{5, 6}}, 60); + + CHECK(problem.numConstraints() == 3); + CHECK(problem.numDecisionVars() == 2); + CHECK(problem.numSlackVars() == 2); + CHECK(problem.numArtificialVars() == 2); + + const Matrix expected_mat{{2, 1, 1, 0, 0, 0}, // + {5, 6, 0, 0, 1, 0}, // + {1, 3, 0, -1, 0, 1}}; // + const Vector3d expected_rhs{{50, 60, 15}}; + + MatrixXd mat; + VectorXd rhs; + problem.buildConstraints(mat, rhs); + + REQUIRE(mat.rows() == expected_mat.rows()); + REQUIRE(mat.cols() == expected_mat.cols()); + REQUIRE(rhs.size() == expected_rhs.size()); + + CHECK(mat == expected_mat); + CHECK(rhs == expected_rhs); +}