From f38eda1f6486a9d495bbc911e4723ee79297db0f Mon Sep 17 00:00:00 2001 From: Corentin Le Molgat Date: Mon, 7 Oct 2024 09:53:14 +0200 Subject: [PATCH] cmake: Add MathOpt tests to build (#4402) --- cmake/cpp.cmake | 84 +++++ ortools/math_opt/CMakeLists.txt | 1 + .../constraints/indicator/CMakeLists.txt | 18 + .../constraints/quadratic/CMakeLists.txt | 18 + .../second_order_cone/CMakeLists.txt | 18 + .../math_opt/constraints/sos/CMakeLists.txt | 18 + .../math_opt/constraints/util/CMakeLists.txt | 18 + ortools/math_opt/solver_tests/CMakeLists.txt | 348 ++++++++++++++++++ ortools/math_opt/solvers/CMakeLists.txt | 25 ++ 9 files changed, 548 insertions(+) create mode 100644 ortools/math_opt/solver_tests/CMakeLists.txt diff --git a/cmake/cpp.cmake b/cmake/cpp.cmake index d586dbcdea1..c13259a39b0 100644 --- a/cmake/cpp.cmake +++ b/cmake/cpp.cmake @@ -216,6 +216,90 @@ function(ortools_cxx_test) message(STATUS "Configuring test ${TEST_NAME} ...DONE") endfunction() +################### +## C++ Library ## +################### +# ortools_cxx_library() +# CMake function to generate and build C++ library. +# Parameters: +# NAME: CMake target name +# SOURCES: List of source files +# [TYPE]: SHARED or STATIC +# [COMPILE_DEFINITIONS]: List of private compile definitions +# [COMPILE_OPTIONS]: List of private compile options +# [LINK_LIBRARIES]: List of **public** libraries to use when linking +# note: ortools::ortools is always linked to the target +# [LINK_OPTIONS]: List of private link options +# e.g.: +# ortools_cxx_library( +# NAME +# foo_bar_library +# SOURCES +# bar_library.cc +# ${PROJECT_SOURCE_DIR}/ortools/foo/bar_library.cc +# TYPE +# SHARED +# LINK_LIBRARIES +# GTest::gmock +# GTest::gtest_main +# TESTING +# ) +function(ortools_cxx_library) + set(options "TESTING") + set(oneValueArgs "NAME;TYPE") + set(multiValueArgs + "SOURCES;COMPILE_DEFINITIONS;COMPILE_OPTIONS;LINK_LIBRARIES;LINK_OPTIONS") + cmake_parse_arguments(LIBRARY + "${options}" + "${oneValueArgs}" + "${multiValueArgs}" + ${ARGN} + ) + if(LIBRARY_TESTING AND NOT BUILD_TESTING) + return() + endif() + + if(NOT LIBRARY_NAME) + message(FATAL_ERROR "no NAME provided") + endif() + if(NOT LIBRARY_SOURCES) + message(FATAL_ERROR "no SOURCES provided") + endif() + message(STATUS "Configuring library ${LIBRARY_NAME} ...") + + add_library(${LIBRARY_NAME} ${LIBRARY_TYPE} "") + target_sources(${LIBRARY_NAME} PRIVATE ${LIBRARY_SOURCES}) + target_include_directories(${LIBRARY_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + target_compile_definitions(${LIBRARY_NAME} PRIVATE ${LIBRARY_COMPILE_DEFINITIONS}) + target_compile_features(${LIBRARY_NAME} PRIVATE cxx_std_17) + target_compile_options(${LIBRARY_NAME} PRIVATE ${LIBRARY_COMPILE_OPTIONS}) + target_link_libraries(${LIBRARY_NAME} PUBLIC + ${PROJECT_NAMESPACE}::ortools + ${LIBRARY_LINK_LIBRARIES} + ) + target_link_options(${LIBRARY_NAME} PRIVATE ${LIBRARY_LINK_OPTIONS}) + + include(GNUInstallDirs) + if(APPLE) + set_target_properties(${LIBRARY_NAME} PROPERTIES + INSTALL_RPATH "@loader_path/../${CMAKE_INSTALL_LIBDIR};@loader_path") + elseif(UNIX) + cmake_path(RELATIVE_PATH CMAKE_INSTALL_FULL_LIBDIR + BASE_DIRECTORY ${CMAKE_INSTALL_FULL_BINDIR} + OUTPUT_VARIABLE libdir_relative_path) + set_target_properties(${LIBRARY_NAME} PROPERTIES + INSTALL_RPATH "$ORIGIN/${libdir_relative_path}:$ORIGIN") + endif() + add_library(${PROJECT_NAMESPACE}::${LIBRARY_NAME} ALIAS ${LIBRARY_NAME}) + + if(BUILD_TESTING) + add_test( + NAME cxx_${LIBRARY_NAME} + COMMAND ${LIBRARY_NAME}) + endif() + message(STATUS "Configuring library ${LIBRARY_NAME} ...DONE") +endfunction() + ################## ## PROTO FILE ## ################## diff --git a/ortools/math_opt/CMakeLists.txt b/ortools/math_opt/CMakeLists.txt index 13615e7acc5..83398885258 100644 --- a/ortools/math_opt/CMakeLists.txt +++ b/ortools/math_opt/CMakeLists.txt @@ -20,6 +20,7 @@ add_subdirectory(constraints) add_subdirectory(cpp) add_subdirectory(io) add_subdirectory(labs) +add_subdirectory(solver_tests) add_subdirectory(solvers) add_subdirectory(storage) add_subdirectory(validators) diff --git a/ortools/math_opt/constraints/indicator/CMakeLists.txt b/ortools/math_opt/constraints/indicator/CMakeLists.txt index af47b4cb6ab..5e286eee832 100644 --- a/ortools/math_opt/constraints/indicator/CMakeLists.txt +++ b/ortools/math_opt/constraints/indicator/CMakeLists.txt @@ -26,3 +26,21 @@ target_link_libraries(${NAME} PRIVATE absl::strings ${PROJECT_NAMESPACE}::math_opt_proto) #install(TARGETS ${NAME} EXPORT ${PROJECT_NAME}Targets) + +if(BUILD_TESTING) + file(GLOB _TEST_SRCS "*_test.cc") + foreach(_FULL_FILE_NAME IN LISTS _TEST_SRCS) + get_filename_component(_NAME ${_FULL_FILE_NAME} NAME_WE) + get_filename_component(_FILE_NAME ${_FULL_FILE_NAME} NAME) + ortools_cxx_test( + NAME + math_opt_constraints_indicator_${_NAME} + SOURCES + ${_FILE_NAME} + LINK_LIBRARIES + #benchmark::benchmark + GTest::gmock + GTest::gtest_main + ) + endforeach() +endif() diff --git a/ortools/math_opt/constraints/quadratic/CMakeLists.txt b/ortools/math_opt/constraints/quadratic/CMakeLists.txt index 0240bc5296c..a58437c85cb 100644 --- a/ortools/math_opt/constraints/quadratic/CMakeLists.txt +++ b/ortools/math_opt/constraints/quadratic/CMakeLists.txt @@ -25,3 +25,21 @@ target_include_directories(${NAME} PUBLIC target_link_libraries(${NAME} PRIVATE absl::strings ${PROJECT_NAMESPACE}::math_opt_proto) + +if(BUILD_TESTING) + file(GLOB _TEST_SRCS "*_test.cc") + foreach(_FULL_FILE_NAME IN LISTS _TEST_SRCS) + get_filename_component(_NAME ${_FULL_FILE_NAME} NAME_WE) + get_filename_component(_FILE_NAME ${_FULL_FILE_NAME} NAME) + ortools_cxx_test( + NAME + math_opt_constraints_quadratic_${_NAME} + SOURCES + ${_FILE_NAME} + LINK_LIBRARIES + #benchmark::benchmark + GTest::gmock + GTest::gtest_main + ) + endforeach() +endif() diff --git a/ortools/math_opt/constraints/second_order_cone/CMakeLists.txt b/ortools/math_opt/constraints/second_order_cone/CMakeLists.txt index c282fe1a7ef..53c8cd9ede8 100644 --- a/ortools/math_opt/constraints/second_order_cone/CMakeLists.txt +++ b/ortools/math_opt/constraints/second_order_cone/CMakeLists.txt @@ -25,3 +25,21 @@ target_include_directories(${NAME} PUBLIC target_link_libraries(${NAME} PRIVATE absl::strings ${PROJECT_NAMESPACE}::math_opt_proto) + +if(BUILD_TESTING) + file(GLOB _TEST_SRCS "*_test.cc") + foreach(_FULL_FILE_NAME IN LISTS _TEST_SRCS) + get_filename_component(_NAME ${_FULL_FILE_NAME} NAME_WE) + get_filename_component(_FILE_NAME ${_FULL_FILE_NAME} NAME) + ortools_cxx_test( + NAME + math_opt_constraints_second_order_cone_${_NAME} + SOURCES + ${_FILE_NAME} + LINK_LIBRARIES + #benchmark::benchmark + GTest::gmock + GTest::gtest_main + ) + endforeach() +endif() diff --git a/ortools/math_opt/constraints/sos/CMakeLists.txt b/ortools/math_opt/constraints/sos/CMakeLists.txt index c0b0ff207a3..8d54916edf2 100644 --- a/ortools/math_opt/constraints/sos/CMakeLists.txt +++ b/ortools/math_opt/constraints/sos/CMakeLists.txt @@ -25,3 +25,21 @@ target_include_directories(${NAME} PUBLIC target_link_libraries(${NAME} PRIVATE absl::strings ${PROJECT_NAMESPACE}::math_opt_proto) + +if(BUILD_TESTING) + file(GLOB _TEST_SRCS "*_test.cc") + foreach(_FULL_FILE_NAME IN LISTS _TEST_SRCS) + get_filename_component(_NAME ${_FULL_FILE_NAME} NAME_WE) + get_filename_component(_FILE_NAME ${_FULL_FILE_NAME} NAME) + ortools_cxx_test( + NAME + math_opt_constraints_sos_${_NAME} + SOURCES + ${_FILE_NAME} + LINK_LIBRARIES + #benchmark::benchmark + GTest::gmock + GTest::gtest_main + ) + endforeach() +endif() diff --git a/ortools/math_opt/constraints/util/CMakeLists.txt b/ortools/math_opt/constraints/util/CMakeLists.txt index 151755f2841..1c7aa149dcd 100644 --- a/ortools/math_opt/constraints/util/CMakeLists.txt +++ b/ortools/math_opt/constraints/util/CMakeLists.txt @@ -25,3 +25,21 @@ target_include_directories(${NAME} PUBLIC target_link_libraries(${NAME} PRIVATE absl::strings ${PROJECT_NAMESPACE}::math_opt_proto) + +if(BUILD_TESTING) + file(GLOB _TEST_SRCS "*_test.cc") + foreach(_FULL_FILE_NAME IN LISTS _TEST_SRCS) + get_filename_component(_NAME ${_FULL_FILE_NAME} NAME_WE) + get_filename_component(_FILE_NAME ${_FULL_FILE_NAME} NAME) + ortools_cxx_test( + NAME + math_opt_constraints_util_${_NAME} + SOURCES + ${_FILE_NAME} + LINK_LIBRARIES + #benchmark::benchmark + GTest::gmock + GTest::gtest_main + ) + endforeach() +endif() diff --git a/ortools/math_opt/solver_tests/CMakeLists.txt b/ortools/math_opt/solver_tests/CMakeLists.txt new file mode 100644 index 00000000000..9a82f7865e2 --- /dev/null +++ b/ortools/math_opt/solver_tests/CMakeLists.txt @@ -0,0 +1,348 @@ +# Copyright 2010-2024 Google LLC +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set(_PREFIX math_opt) + +ortools_cxx_library( + NAME + ${_PREFIX}_base_solver_test + SOURCES + "base_solver_test.cc" + "base_solver_test.h" + TYPE + SHARED + LINK_LIBRARIES + GTest::gmock + absl::log + TESTING +) + +ortools_cxx_library( + NAME + ${_PREFIX}_callback_tests + SOURCES + "callback_tests.cc" + "callback_tests.h" + TYPE + SHARED + LINK_LIBRARIES + GTest::gmock + absl::log + absl::status + absl::strings + ortools::math_opt_base_solver_test + ortools::math_opt_test_models + TESTING +) + + +ortools_cxx_library( + NAME + ${_PREFIX}_status_tests + SOURCES + "status_tests.cc" + "status_tests.h" + TYPE + SHARED + LINK_LIBRARIES + GTest::gmock + absl::log + ortools::math_opt_test_models + TESTING +) + +ortools_cxx_library( + NAME + ${_PREFIX}_lp_tests + SOURCES + "lp_tests.cc" + "lp_tests.h" + TYPE + SHARED + LINK_LIBRARIES + GTest::gmock + absl::log + ortools::math_opt_base_solver_test + TESTING +) + +ortools_cxx_library( + NAME + ${_PREFIX}_lp_incomplete_solve_test + SOURCES + "lp_incomplete_solve_tests.cc" + "lp_incomplete_solve_tests.h" + TYPE + SHARED + LINK_LIBRARIES + GTest::gmock + absl::log + ortools::math_opt_test_models + TESTING +) + +ortools_cxx_library( + NAME + ${_PREFIX}_invalid_input_tests + SOURCES + "invalid_input_tests.cc" + "invalid_input_tests.h" + TYPE + SHARED + LINK_LIBRARIES + GTest::gmock + absl::log + ortools::math_opt_base_solver_test + TESTING +) + +ortools_cxx_test( + NAME + ${_PREFIX}_unregistered_solver_test + SOURCES + "unregistered_solver_test.cc" + LINK_LIBRARIES + GTest::gmock + GTest::gmock_main +) + +ortools_cxx_library( + NAME + ${_PREFIX}_mip_tests + SOURCES + "mip_tests.cc" + "mip_tests.h" + TYPE + SHARED + LINK_LIBRARIES + GTest::gmock + absl::log + absl::status + ortools::math_opt_base_solver_test + TESTING +) + +ortools_cxx_library( + NAME + ${_PREFIX}_ip_model_solve_parameters_tests + SOURCES + "ip_model_solve_parameters_tests.cc" + "ip_model_solve_parameters_tests.h" + TYPE + SHARED + LINK_LIBRARIES + GTest::gmock + absl::log + ortools::math_opt_base_solver_test + TESTING +) + +ortools_cxx_library( + NAME + ${_PREFIX}_ip_multiple_solutions_tests + SOURCES + "ip_multiple_solutions_tests.cc" + "ip_multiple_solutions_tests.h" + TYPE + SHARED + LINK_LIBRARIES + GTest::gmock + absl::strings + TESTING +) + +ortools_cxx_library( + NAME + ${_PREFIX}_lp_model_solve_parameters_tests + SOURCES + "lp_model_solve_parameters_tests.cc" + "lp_model_solve_parameters_tests.h" + TYPE + SHARED + LINK_LIBRARIES + GTest::gmock + absl::log + ortools::math_opt_base_solver_test + TESTING +) + +ortools_cxx_library( + NAME + ${_PREFIX}_lp_parameter_tests + SOURCES + "lp_parameter_tests.cc" + "lp_parameter_tests.h" + TYPE + SHARED + LINK_LIBRARIES + GTest::gmock + absl::log + absl::status + absl::strings + TESTING +) + +ortools_cxx_library( + NAME + ${_PREFIX}_lp_initial_basis_tests + SOURCES + "lp_initial_basis_tests.cc" + "lp_initial_basis_tests.h" + TYPE + SHARED + LINK_LIBRARIES + absl::log + ortools::math_opt_base_solver_test + TESTING +) + +ortools_cxx_library( + NAME + ${_PREFIX}_ip_parameter_tests + SOURCES + "ip_parameter_tests.cc" + "ip_parameter_tests.h" + TYPE + SHARED + LINK_LIBRARIES + GTest::gmock + absl::log + ortools::math_opt_test_models + TESTING +) + +ortools_cxx_library( + NAME + ${_PREFIX}_multi_objective_tests + SOURCES + "multi_objective_tests.cc" + "multi_objective_tests.h" + TYPE + SHARED + LINK_LIBRARIES + GTest::gmock + absl::log + TESTING +) + +ortools_cxx_library( + NAME + ${_PREFIX}_qp_tests + SOURCES + "qp_tests.cc" + "qp_tests.h" + TYPE + SHARED + LINK_LIBRARIES + GTest::gmock + absl::log + TESTING +) + +ortools_cxx_library( + NAME + ${_PREFIX}_qc_tests + SOURCES + "qc_tests.cc" + "qc_tests.h" + TYPE + SHARED + LINK_LIBRARIES + GTest::gmock + absl::log + TESTING +) + +ortools_cxx_library( + NAME + ${_PREFIX}_second_order_cone_tests + SOURCES + "second_order_cone_tests.cc" + "second_order_cone_tests.h" + TYPE + SHARED + LINK_LIBRARIES + absl::log + GTest::gmock + TESTING +) + +ortools_cxx_library( + NAME + ${_PREFIX}_logical_constraint_tests + SOURCES + "logical_constraint_tests.cc" + "logical_constraint_tests.h" + TYPE + SHARED + LINK_LIBRARIES + absl::log + GTest::gmock + TESTING +) + +ortools_cxx_library( + NAME + ${_PREFIX}_test_models + SOURCES + "test_models.cc" + "test_models.h" + TYPE + SHARED + LINK_LIBRARIES + absl::log + absl::strings + TESTING +) + +ortools_cxx_test( + NAME + ${_PREFIX}_test_models_test + SOURCES + "test_models_test.cc" + LINK_LIBRARIES + GTest::gmock + GTest::gmock_main + ortools::math_opt_test_models +) + +ortools_cxx_library( + NAME + ${_PREFIX}_generic_tests + SOURCES + "generic_tests.cc" + "generic_tests.h" + TYPE + SHARED + LINK_LIBRARIES + absl::log + ortools::math_opt_test_models + TESTING +) + +ortools_cxx_library( + NAME + ${_PREFIX}_infeasible_subsystem_tests + SOURCES + "infeasible_subsystem_tests.cc" + "infeasible_subsystem_tests.h" + TYPE + SHARED + LINK_LIBRARIES + absl::log + absl::status + absl::strings + absl::time + GTest::gmock + TESTING +) diff --git a/ortools/math_opt/solvers/CMakeLists.txt b/ortools/math_opt/solvers/CMakeLists.txt index a5eac44747a..24f5f6cc8c6 100644 --- a/ortools/math_opt/solvers/CMakeLists.txt +++ b/ortools/math_opt/solvers/CMakeLists.txt @@ -52,3 +52,28 @@ target_link_libraries(${NAME} PRIVATE $<$:Eigen3::Eigen> $<$:libscip> ${PROJECT_NAMESPACE}::math_opt_proto) + +ortools_cxx_test( + NAME + math_opt_solvers_gscip_solver_test + SOURCES + "gscip_solver_test.cc" + LINK_LIBRARIES + GTest::gmock + GTest::gmock_main + ortools::math_opt_callback_tests + ortools::math_opt_generic_tests + ortools::math_opt_infeasible_subsystem_tests + ortools::math_opt_invalid_input_tests + ortools::math_opt_ip_model_solve_parameters_tests + ortools::math_opt_ip_multiple_solutions_tests + ortools::math_opt_ip_parameter_tests + ortools::math_opt_logical_constraint_tests + ortools::math_opt_mip_tests + ortools::math_opt_multi_objective_tests + ortools::math_opt_qc_tests + ortools::math_opt_qp_tests + ortools::math_opt_second_order_cone_tests + ortools::math_opt_status_tests +) +