Skip to content

Commit

Permalink
Merge pull request #2276 from traversaro:reducetestboilerplate
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 707613522
Change-Id: Ic945c6b057b77f3462dbdb758139af12133f6a8f
  • Loading branch information
copybara-github committed Dec 18, 2024
2 parents edec8c5 + 756a8d7 commit 526637f
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 135 deletions.
30 changes: 24 additions & 6 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,22 @@ set(MUJOCO_TEST_WORKING_DIR ${CMAKE_CURRENT_SOURCE_DIR})

include(GoogleTest)

# This macro is used to add a C++ test to MuJoCo CMake build system
#
# The CMake targets linked by default with target_link_libraries are:
# mujoco fixture gmock
#
# The macro supports the following parameters:
# * PROPERTIES: multiple value parameter, its value is set as test properties
# via the set_tests_properties(<test> PROPERTIES <argument>) function
# * ADDITIONAL_LINK_LIBRARIES: multiple value parameter, additional libraries linked
# to the test via target_link_libraries
# * MAIN_TARGET: single value parameter, used to specify the target linked to the tests
# that define the main entry poiny. If not indicated, gest_main is used
macro(mujoco_test name)
set(options)
set(oneValueArgs)
set(multiValueArgs PROPERTIES)
set(oneValueArgs MAIN_TARGET)
set(multiValueArgs PROPERTIES ADDITIONAL_LINK_LIBRARIES)
cmake_parse_arguments(
_ARGS
"${options}"
Expand All @@ -30,7 +42,15 @@ macro(mujoco_test name)
)

add_executable(${name} ${name}.cc)
target_link_libraries(${name} gtest_main mujoco)
target_link_libraries(${name} mujoco fixture gmock)
if(_ARGS_MAIN_TARGET)
target_link_libraries(${name} ${_ARGS_MAIN_TARGET})
else()
target_link_libraries(${name} gtest_main)
endif()
if(_ARGS_ADDITIONAL_LINK_LIBRARIES)
target_link_libraries(${name} ${_ARGS_ADDITIONAL_LINK_LIBRARIES})
endif()
target_include_directories(${name} PRIVATE ${MUJOCO_TEST_INCLUDE})
set_target_properties(${name} PROPERTIES BUILD_RPATH ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
# gtest_discover_tests is recommended over gtest_add_tests, but has some issues in Windows.
Expand All @@ -48,6 +68,7 @@ macro(mujoco_test name)
if(_ARGS_PROPERTIES)
set_tests_properties(${testList} PROPERTIES ${_ARGS_PROPERTIES})
endif()

endmacro()

add_library(fixture STATIC fixture.h fixture.cc)
Expand All @@ -67,13 +88,10 @@ target_link_libraries(
target_include_directories(fixture PRIVATE ${mujoco_SOURCE_DIR}/include gmock)

mujoco_test(fixture_test)
target_link_libraries(fixture_test fixture gmock)

mujoco_test(header_test)
target_link_libraries(header_test fixture gmock)

mujoco_test(pipeline_test)
target_link_libraries(pipeline_test fixture gmock)

add_subdirectory(benchmark)
add_subdirectory(engine)
Expand Down
83 changes: 21 additions & 62 deletions test/benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,86 +12,45 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Macro for benchmarks that don't use GL (same as mujoco_api_test, but uses
# benchmark::benchmark_main).

macro(mujoco_benchmark_test name)
add_executable(${name} ${name}.cc)
target_link_libraries(
${name}
benchmark::benchmark_main
mujoco
absl::core_headers
)
target_include_directories(${name} PRIVATE ${MUJOCO_TEST_INCLUDE})
# TODO(fraromano) Check RPATH settings
set_target_properties(${name} PROPERTIES BUILD_RPATH ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
# gtest_discover_tests is recommended over gtest_add_tests, but has some issues in Windows.
gtest_add_tests(
TARGET ${name}
SOURCES ${name}.cc
WORKING_DIRECTORY ${MUJOCO_TEST_WORKING_DIR}
TEST_LIST testList
)
if(WIN32)
set_tests_properties(
${testList} PROPERTIES ENVIRONMENT "PATH=$<TARGET_FILE_DIR:mujoco>;$ENV{PATH}"
)
endif()
endmacro()

mujoco_benchmark_test(ccd_benchmark_test)
target_link_libraries(
mujoco_test(
ccd_benchmark_test
fixture
gmock
benchmark::benchmark
MAIN_TARGET benchmark::benchmark_main
ADDITIONAL_LINK_LIBRARIES benchmark::benchmark absl::core_headers
)

mujoco_benchmark_test(step_benchmark_test)
target_link_libraries(
mujoco_test(
step_benchmark_test
fixture
gmock
benchmark::benchmark
MAIN_TARGET benchmark::benchmark_main
ADDITIONAL_LINK_LIBRARIES benchmark::benchmark absl::core_headers
)

mujoco_benchmark_test(thread_performance_test)
target_link_libraries(
mujoco_test(
thread_performance_test
fixture
gmock
benchmark::benchmark
MAIN_TARGET benchmark::benchmark_main
ADDITIONAL_LINK_LIBRARIES benchmark::benchmark absl::core_headers
)

mujoco_benchmark_test(parse_benchmark_test)
target_link_libraries(
mujoco_test(
parse_benchmark_test
fixture
gmock
benchmark::benchmark
MAIN_TARGET benchmark::benchmark_main
ADDITIONAL_LINK_LIBRARIES benchmark::benchmark absl::core_headers
)

mujoco_benchmark_test(engine_util_spatial_benchmark_test)
target_link_libraries(
mujoco_test(
engine_util_spatial_benchmark_test
fixture
gmock
benchmark::benchmark
MAIN_TARGET benchmark::benchmark_main
ADDITIONAL_LINK_LIBRARIES benchmark::benchmark absl::core_headers
)

mujoco_benchmark_test(engine_core_smooth_benchmark_test)
target_link_libraries(
mujoco_test(
engine_core_smooth_benchmark_test
fixture
gmock
benchmark::benchmark
MAIN_TARGET benchmark::benchmark_main
ADDITIONAL_LINK_LIBRARIES benchmark::benchmark absl::core_headers
)

mujoco_benchmark_test(engine_util_sparse_benchmark_test)
target_link_libraries(
mujoco_test(
engine_util_sparse_benchmark_test
fixture
gmock
benchmark::benchmark
MAIN_TARGET benchmark::benchmark_main
ADDITIONAL_LINK_LIBRARIES benchmark::benchmark absl::core_headers
)
43 changes: 5 additions & 38 deletions test/engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,96 +13,63 @@
# limitations under the License.

mujoco_test(engine_collision_box_test)
target_link_libraries(engine_collision_box_test fixture gmock)

mujoco_test(engine_collision_convex_test)
target_link_libraries(engine_collision_convex_test fixture gmock)

mujoco_test(engine_collision_driver_test)
target_link_libraries(engine_collision_driver_test fixture gmock)

mujoco_test(engine_collision_gjk_test)
target_link_libraries(engine_collision_gjk_test fixture gmock ccd)
mujoco_test(engine_collision_gjk_test ADDITIONAL_LINK_LIBRARIES ccd)

mujoco_test(engine_core_constraint_test)
target_link_libraries(engine_core_constraint_test fixture gmock)

mujoco_test(engine_core_smooth_test)
target_link_libraries(engine_core_smooth_test fixture gmock absl::span)
mujoco_test(engine_core_smooth_test ADDITIONAL_LINK_LIBRARIES absl::span)

mujoco_test(engine_derivative_test)
target_link_libraries(engine_derivative_test fixture gmock)

mujoco_test(engine_forward_test)
target_link_libraries(engine_forward_test fixture gmock)

mujoco_test(engine_inverse_test)
target_link_libraries(engine_inverse_test fixture gmock)

mujoco_test(engine_island_test)
target_link_libraries(engine_island_test fixture gmock)

mujoco_test(engine_io_test)
target_link_libraries(
engine_io_test
fixture
gmock
absl::str_format
)
mujoco_test(engine_io_test ADDITIONAL_LINK_LIBRARIES absl::str_format)

mujoco_test(
engine_plugin_test
ADDITIONAL_LINK_LIBRARIES
absl::str_format
PROPERTIES
ENVIRONMENT
"MUJOCO_PLUGIN_DIR=$<TARGET_FILE_DIR:elasticity>"
)
target_link_libraries(
engine_plugin_test
fixture
gmock
absl::str_format
)

mujoco_test(engine_passive_test)
target_link_libraries(engine_passive_test fixture gmock)

mujoco_test(engine_print_test)
target_link_libraries(engine_print_test fixture gmock)

mujoco_test(engine_ray_test)
target_link_libraries(engine_ray_test fixture gmock)

mujoco_test(engine_sensor_test)
target_link_libraries(engine_sensor_test fixture gmock)

mujoco_test(engine_solver_test)
target_link_libraries(engine_solver_test fixture gmock)

mujoco_test(engine_sort_test)
target_link_libraries(engine_sort_test fixture gmock)

mujoco_test(engine_support_test)
target_link_libraries(engine_support_test fixture gmock)

mujoco_test(engine_thread_test)
target_link_libraries(engine_thread_test fixture gmock)

mujoco_test(engine_util_blas_test)
target_link_libraries(engine_util_blas_test fixture gmock)

mujoco_test(engine_util_errmem_test)
target_link_libraries(engine_util_errmem_test fixture gmock)

mujoco_test(engine_util_solve_test)
target_link_libraries(engine_util_solve_test fixture gmock)

mujoco_test(engine_util_spatial_test)
target_link_libraries(engine_util_spatial_test fixture gmock)

mujoco_test(
engine_vis_state_test
PROPERTIES
ENVIRONMENT
"MUJOCO_PLUGIN_DIR=$<TARGET_FILE_DIR:elasticity>"
)
target_link_libraries(engine_vis_state_test fixture gmock)
5 changes: 4 additions & 1 deletion test/plugin/actuator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@

mujoco_test(
pid_test
ADDITIONAL_LINK_LIBRARIES
absl::cleanup
absl::strings
PROPERTIES
ENVIRONMENT
"MUJOCO_PLUGIN_DIR=$<TARGET_FILE_DIR:actuator>"
)
target_link_libraries(pid_test fixture gmock absl::cleanup absl::strings)
target_link_libraries(pid_test fixture gmock )
1 change: 0 additions & 1 deletion test/plugin/elasticity/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ mujoco_test(
ENVIRONMENT
"MUJOCO_PLUGIN_DIR=$<TARGET_FILE_DIR:elasticity>"
)
target_link_libraries(elasticity_test fixture gmock)
1 change: 0 additions & 1 deletion test/plugin/sensor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ mujoco_test(
ENVIRONMENT
"MUJOCO_PLUGIN_DIR=$<TARGET_FILE_DIR:sensor>"
)
target_link_libraries(sensor_test fixture gmock)
2 changes: 0 additions & 2 deletions test/thread/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@
# limitations under the License.

mujoco_test(thread_pool_test)
target_link_libraries(thread_pool_test fixture gmock)

mujoco_test(thread_queue_test)
target_link_libraries(thread_queue_test fixture gmock)
19 changes: 3 additions & 16 deletions test/user/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.

mujoco_test(user_model_test)
target_link_libraries(user_model_test fixture gmock absl::str_format)
mujoco_test(user_model_test ADDITIONAL_LINK_LIBRARIES absl::str_format)

mujoco_test(user_objects_test)
target_link_libraries(user_objects_test fixture gmock)

mujoco_test(
user_api_test
PROPERTIES
ENVIRONMENT
"MUJOCO_PLUGIN_DIR=$<TARGET_FILE_DIR:elasticity>"
)
target_link_libraries(user_api_test fixture gmock)

mujoco_test(user_flex_test)
target_link_libraries(user_flex_test fixture gmock)

mujoco_test(user_mesh_test)
target_link_libraries(
user_mesh_test
fixture
gmock
absl::str_format
)

mujoco_test(user_mesh_test ADDITIONAL_LINK_LIBRARIES absl::str_format)

mujoco_test(user_composite_test)
target_link_libraries(user_composite_test fixture gmock)

mujoco_test(user_resource_test)
target_link_libraries(user_resource_test fixture gmock)

mujoco_test(user_vfs_test)
target_link_libraries(user_vfs_test fixture gmock)
10 changes: 2 additions & 8 deletions test/xml/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,14 @@
# limitations under the License.

mujoco_test(xml_api_test)
target_link_libraries(xml_api_test fixture gmock)

mujoco_test(xml_native_reader_test)
target_link_libraries(xml_native_reader_test fixture gmock)

mujoco_test(
xml_native_writer_test
ADDITIONAL_LINK_LIBRARIES
absl::flat_hash_set
PROPERTIES
ENVIRONMENT
"MUJOCO_PLUGIN_DIR=$<TARGET_FILE_DIR:elasticity>"
)
target_link_libraries(
xml_native_writer_test
fixture
gmock
absl::flat_hash_set
)

0 comments on commit 526637f

Please sign in to comment.