forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mosek_solver_moseklm_license_file_test.cc
59 lines (46 loc) · 1.82 KB
/
mosek_solver_moseklm_license_file_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <cstdlib>
#include <stdexcept>
#include <gtest/gtest.h>
#include "drake/solvers/mathematical_program.h"
#include "drake/solvers/mosek_solver.h"
namespace drake {
namespace solvers {
namespace {
// These tests is deliberately not in mosek_solver_test.cc to avoid causing
// license issues during tests in that file.
GTEST_TEST(MoseklmLicenseFileTest, MoseklmLicenseFileSet) {
const char* moseklm_license_file = std::getenv("MOSEKLM_LICENSE_FILE");
ASSERT_STRNE(nullptr, moseklm_license_file);
MathematicalProgram program;
// Add a variable to avoid the "Solve" function terminating without calling
// the external MOSEK solver.
const auto x = program.NewContinuousVariables<1>();
MosekSolver solver;
EXPECT_NO_THROW(solver.Solve(program));
}
GTEST_TEST(MoseklmLicenseFileTest, MoseklmLicenseFileUnset) {
const char* moseklm_license_file = std::getenv("MOSEKLM_LICENSE_FILE");
ASSERT_STRNE(nullptr, moseklm_license_file);
const int unsetenv_result = ::unsetenv("MOSEKLM_LICENSE_FILE");
ASSERT_EQ(0, unsetenv_result);
MathematicalProgram program;
// Add a variable to avoid the "Solve" function terminating without calling
// the external MOSEK solver.
const auto x = program.NewContinuousVariables<1>();
MosekSolver solver;
try {
solver.Solve(program);
ADD_FAILURE() << "Expected exception of type std::runtime_error.";
} catch (const std::runtime_error& err) {
EXPECT_EQ(err.what(), std::string("Could not locate MOSEK license file "
"because MOSEKLM_LICENSE_FILE environment variable was not set."));
} catch (...) {
ADD_FAILURE() << "Expected exception of type std::runtime_error.";
}
const int setenv_result =
::setenv("MOSEKLM_LICENSE_FILE", moseklm_license_file, 1);
ASSERT_EQ(0, setenv_result);
}
} // namespace
} // namespace solvers
} // namespace drake