From 4d6e1232358d5fe4f2360185a2f16a2bd5f292a0 Mon Sep 17 00:00:00 2001 From: Giulio Romualdi Date: Mon, 24 May 2021 16:29:44 +0200 Subject: [PATCH] Write a test for the YarpParametersHandler bindings --- .../python/ParametersHandler/CMakeLists.txt | 20 +- .../python/ParametersHandler/tests/config.ini | 10 + .../tests/test_parameters_handler_yarp.py | 207 ++++++++++++++++++ 3 files changed, 236 insertions(+), 1 deletion(-) create mode 100644 bindings/python/ParametersHandler/tests/config.ini create mode 100644 bindings/python/ParametersHandler/tests/test_parameters_handler_yarp.py diff --git a/bindings/python/ParametersHandler/CMakeLists.txt b/bindings/python/ParametersHandler/CMakeLists.txt index 8542e74a5d..b8460ecd26 100644 --- a/bindings/python/ParametersHandler/CMakeLists.txt +++ b/bindings/python/ParametersHandler/CMakeLists.txt @@ -2,10 +2,28 @@ # This software may be modified and distributed under the terms of the # GNU Lesser General Public License v2.1 or any later version. +set(H_PREFIX include/BipedalLocomotion/bindings/ParametersHandler) + add_bipedal_locomotion_python_module( NAME ParametersHandlerBindings SOURCES src/ParametersHandler.cpp src/Module.cpp - HEADERS include/BipedalLocomotion/bindings/ParametersHandler/ParametersHandler.h include/BipedalLocomotion/bindings/ParametersHandler/Module.h + HEADERS ${H_PREFIX}/ParametersHandler.h ${H_PREFIX}/Module.h LINK_LIBRARIES BipedalLocomotion::ParametersHandler TESTS tests/test_parameters_handler_std.py ) + + +if(FRAMEWORK_COMPILE_YarpImplementation) + + add_bipedal_locomotion_python_module( + NAME ParametersHandlerYarpImplementationBindings + SOURCES src/YarpParametersHandler.cpp src/YarpModule.cpp + HEADERS ${H_PREFIX}/YarpParametersHandler.h ${H_PREFIX}/YarpModule.h + LINK_LIBRARIES BipedalLocomotion::ParametersHandlerYarpImplementation + TESTS tests/test_parameters_handler_yarp.py + ) + + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tests/config.ini ${PROJECT_BINARY_DIR}/config.ini COPYONLY) + + +endif() diff --git a/bindings/python/ParametersHandler/tests/config.ini b/bindings/python/ParametersHandler/tests/config.ini new file mode 100644 index 0000000000..d67de163ca --- /dev/null +++ b/bindings/python/ParametersHandler/tests/config.ini @@ -0,0 +1,10 @@ +answer_to_the_ultimate_question_of_life 42 +pi 3.14 +John Smith +"Fibonacci Numbers" (1, 1, 2, 3, 5, 8, 13, 21) + +[CARTOONS] +"Donald's nephews" ("Huey", "Dewey", "Louie") +Fibonacci_Numbers (1, 1, 2, 3, 5, 8, 13, 21) +John Doe + diff --git a/bindings/python/ParametersHandler/tests/test_parameters_handler_yarp.py b/bindings/python/ParametersHandler/tests/test_parameters_handler_yarp.py new file mode 100644 index 0000000000..4d523a7529 --- /dev/null +++ b/bindings/python/ParametersHandler/tests/test_parameters_handler_yarp.py @@ -0,0 +1,207 @@ +import pytest +pytestmark = pytest.mark.parameters_handler_yarp + +import bipedal_locomotion_framework.bindings.parameters_handler as blf +import numpy as np + +def test_bool(): + + handler = blf.YarpParametersHandler() + + handler.set_parameter_bool(name="my_bool", value=True) + + assert handler.get_parameter_bool(name="my_bool") is True + + with pytest.raises(ValueError): + handler.get_parameter_int(name="my_bool") + + with pytest.raises(ValueError): + handler.get_parameter_float(name="my_bool") + + with pytest.raises(ValueError): + handler.get_parameter_string(name="my_bool") + + +def test_int(): + + handler = blf.YarpParametersHandler() + + handler.set_parameter_int(name="my_int", value=42) + + assert handler.get_parameter_int(name="my_int") == 42 + + with pytest.raises(ValueError): + handler.get_parameter_bool(name="my_int") + + with pytest.raises(ValueError): + handler.get_parameter_float(name="my_int") + + with pytest.raises(ValueError): + handler.get_parameter_string(name="my_int") + + +def test_float(): + + handler = blf.YarpParametersHandler() + + handler.set_parameter_float(name="my_float", value=3.1415) + + assert handler.get_parameter_float(name="my_float") == pytest.approx(3.1415) + + with pytest.raises(ValueError): + handler.get_parameter_bool(name="my_float") + + with pytest.raises(ValueError): + handler.get_parameter_int(name="my_float") + + with pytest.raises(ValueError): + handler.get_parameter_string(name="my_float") + + +def test_string(): + + handler = blf.YarpParametersHandler() + + handler.set_parameter_string(name="my_string", value="foo") + + assert handler.get_parameter_string(name="my_string") == "foo" + + with pytest.raises(ValueError): + handler.get_parameter_bool(name="my_string") + + with pytest.raises(ValueError): + handler.get_parameter_int(name="my_string") + + with pytest.raises(ValueError): + handler.get_parameter_float(name="my_string") + + +def test_vector_bool(): + + handler = blf.YarpParametersHandler() + + handler.set_parameter_vector_bool(name="my_vector_bool",value= [True, False, True]) + + assert handler.get_parameter_vector_bool(name="my_vector_bool") == [True, False, True] + + with pytest.raises(ValueError): + handler.get_parameter_vector_int(name="my_vector_bool") + + with pytest.raises(ValueError): + handler.get_parameter_vector_float(name="my_vector_bool") + + with pytest.raises(ValueError): + handler.get_parameter_vector_string(name="my_vector_bool") + + +def test_vector_int(): + + handler = blf.YarpParametersHandler() + + handler.set_parameter_vector_int(name="my_vector_int", value=[-1, 2, 10]) + + assert handler.get_parameter_vector_int(name="my_vector_int") == [-1, 2, 10] + + with pytest.raises(ValueError): + handler.get_parameter_vector_bool(name="my_vector_int") + + with pytest.raises(ValueError): + handler.get_parameter_vector_float(name="my_vector_int") + + with pytest.raises(ValueError): + handler.get_parameter_vector_string(name="my_vector_int") + + +def test_vector_float(): + + handler = blf.YarpParametersHandler() + + handler.set_parameter_vector_float(name="my_vector_float", + value=[-3.14, 2.7182, 42.0]) + + assert handler.get_parameter_vector_float(name="my_vector_float") == \ + pytest.approx([-3.14, 2.7182, 42.0]) + + with pytest.raises(ValueError): + handler.get_parameter_vector_bool(name="my_vector_float") + + with pytest.raises(ValueError): + handler.get_parameter_vector_int(name="my_vector_float") + + with pytest.raises(ValueError): + handler.get_parameter_vector_string(name="my_vector_float") + + +def test_vector_string(): + + handler = blf.YarpParametersHandler() + + handler.set_parameter_vector_string(name="my_vector_string", + value=["foo", "bar", "bipedal", "locomotion"]) + + assert handler.get_parameter_vector_string(name="my_vector_string") == \ + ["foo", "bar", "bipedal", "locomotion"] + + with pytest.raises(ValueError): + handler.get_parameter_vector_bool(name="my_vector_string") + + with pytest.raises(ValueError): + handler.get_parameter_vector_int(name="my_vector_string") + + with pytest.raises(ValueError): + handler.get_parameter_vector_float(name="my_vector_string") + + +def test_vector_mixed(): + + handler = blf.YarpParametersHandler() + + # 1. Mixed vector: store as more general type float + handler.set_parameter_vector_float(name="to_float", value=[42.0, 1, -3.14, False]) + + assert handler.get_parameter_vector_float(name="to_float") == \ + pytest.approx([42.0, 1.0, -3.14, 0.0]) + + # 2. Mixed vector: store as more general type int + handler.set_parameter_vector_float(name="to_int", value=[42, 1, -3, False]) + + assert handler.get_parameter_vector_float(name="to_int") == \ + pytest.approx([42, 1, -3, 0]) + + # 3. Mixed vector: store as less general type int + with pytest.raises(TypeError): + handler.set_parameter_vector_int(name="to_int_fail", + value=[42.0, 1, -3.14, False]) + + +def test_clear(): + + handler = blf.YarpParametersHandler() + + handler.set_parameter_bool(name="my_bool1", value=False) + handler.set_parameter_bool(name="my_bool2", value=True) + handler.set_parameter_float(name="my_float", value=-42.42) + handler.set_parameter_vector_string(name="my_vector_string", value=["bar", "foo"]) + + handler.clear() + + with pytest.raises(ValueError): + _ = handler.get_parameter_bool(name="my_bool1") + + with pytest.raises(ValueError): + _ = handler.get_parameter_bool(name="my_bool2") + + with pytest.raises(ValueError): + _ = handler.get_parameter_float(name="my_float") + + with pytest.raises(ValueError): + _ = handler.get_parameter_vector_string(name="my_float") + + +def test_load_from_file(): + + handler = blf.YarpParametersHandler() + assert handler.set_from_file('config.ini') == True + + assert handler.get_parameter_int("answer_to_the_ultimate_question_of_life") == 42 + assert handler.get_group("CARTOONS").get_parameter_string("John") == "Doe"