Skip to content

Commit

Permalink
Testing: Improve c++ tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
jschueller committed Oct 29, 2024
1 parent a65ff40 commit 6908b62
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
14 changes: 8 additions & 6 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ macro(NLOPT_add_cpp_test test_name)
target_link_libraries (${test_name} ${nlopt_lib})
add_dependencies (tests ${test_name})
target_include_directories (${test_name} PRIVATE ${NLOPT_PRIVATE_INCLUDE_DIRS})
add_test (NAME check_${test_name} COMMAND ${test_name})
if (CMAKE_HOST_SYSTEM_NAME MATCHES Windows)
set_tests_properties (check_${test_name}
PROPERTIES ENVIRONMENT "PATH=${PROJECT_BINARY_DIR}\\${CMAKE_BUILD_TYPE};$ENV{PATH}") # to load dll
endif ()
foreach(arg IN ITEMS ${ARGN})
add_test (NAME check_${test_name}_${arg} COMMAND ${test_name} ${arg})
if (CMAKE_HOST_SYSTEM_NAME MATCHES Windows)
set_tests_properties (check_${test_name}_${arg}
PROPERTIES ENVIRONMENT "PATH=${PROJECT_BINARY_DIR}\\${CMAKE_BUILD_TYPE};$ENV{PATH}") # to load dll
endif ()
endforeach()
endmacro()

NLOPT_add_cpp_test(t_tutorial)
NLOPT_add_cpp_test(t_tutorial 24 25 31 40)
NLOPT_add_cpp_test(cpp_functor)

# have to add timer.c and mt19937ar.c as symbols are declared extern
Expand Down
13 changes: 5 additions & 8 deletions test/t_tutorial.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ double myvconstraint(const std::vector<double> &x, std::vector<double> &grad, vo
}


int main() {

nlopt::opt opt("LD_MMA", 2);
std::vector<double> lb(2);
lb[0] = -HUGE_VAL; lb[1] = 0;
int main(int argc, char *argv[]) {
nlopt::opt opt(argc < 2 ? nlopt::LD_MMA : (nlopt::algorithm)atoi(argv[1]), 2);
const std::vector<double> lb = {-HUGE_VAL, 1e-6};
opt.set_lower_bounds(lb);
opt.set_min_objective(myvfunc, NULL);
my_constraint_data data[2] = { {2,0}, {-1,1} };
Expand All @@ -55,9 +53,8 @@ int main() {
opt.set_param("rho_init", 0.5);
opt.set_initial_step(0.1);

std::vector<double> x(2);
x[0] = 1.234; x[1] = 5.678;
double minf;
std::vector<double> x = {1.234, 5.678};
double minf = 0.0;

try{
opt.optimize(x, minf);
Expand Down

0 comments on commit 6908b62

Please sign in to comment.