diff options
Diffstat (limited to 'host/tests')
| -rw-r--r-- | host/tests/CMakeLists.txt | 10 | ||||
| -rw-r--r-- | host/tests/eeprom_c_test.c | 155 | ||||
| -rw-r--r-- | host/tests/error_c_test.cpp | 142 | ||||
| -rw-r--r-- | host/tests/ranges_c_test.c | 236 | ||||
| -rw-r--r-- | host/tests/sensors_c_test.c | 411 | ||||
| -rw-r--r-- | host/tests/subdev_spec_c_test.c | 130 | 
6 files changed, 1084 insertions, 0 deletions
diff --git a/host/tests/CMakeLists.txt b/host/tests/CMakeLists.txt index 1fb1a1951..009509286 100644 --- a/host/tests/CMakeLists.txt +++ b/host/tests/CMakeLists.txt @@ -53,6 +53,16 @@ ADD_DEFINITIONS(-DBOOST_TEST_DYN_LINK -DBOOST_TEST_MAIN)  SET(UHD_TEST_TARGET_DEPS uhd)  SET(UHD_TEST_LIBRARY_DIRS ${Boost_LIBRARY_DIRS}) +IF(ENABLE_C_API) +    LIST(APPEND test_sources +        eeprom_c_test.c +        error_c_test.cpp +        ranges_c_test.c +        sensors_c_test.c +        subdev_spec_c_test.c +    ) +ENDIF(ENABLE_C_API) +  #for each source: build an executable, register it as a test  FOREACH(test_source ${test_sources})      GET_FILENAME_COMPONENT(test_name ${test_source} NAME_WE) diff --git a/host/tests/eeprom_c_test.c b/host/tests/eeprom_c_test.c new file mode 100644 index 000000000..0f03295e8 --- /dev/null +++ b/host/tests/eeprom_c_test.c @@ -0,0 +1,155 @@ +// +// Copyright 2015 Ettus Research LLC +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program.  If not, see <http://www.gnu.org/licenses/>. +// + +#include <uhd.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#define UHD_TEST_EXECUTE_OR_GOTO(label, ...) \ +    if(__VA_ARGS__){ \ +        fprintf(stderr, "Error occurred at %s:%d\n", __FILE__, (__LINE__-1)); \ +        return_code = EXIT_FAILURE; \ +        goto label; \ +    } + +#define BUFFER_SIZE 1024 + +int main(){ + +    // Variables +    int return_code; +    uhd_mboard_eeprom_handle mb_eeprom; +    uhd_dboard_eeprom_handle db_eeprom; +    int db_revision; +    char str_buffer[BUFFER_SIZE]; + +    return_code = EXIT_SUCCESS; + +    /* +     * Motherboard EEPROM test +     */ + +    // Create EEPROM handle +    UHD_TEST_EXECUTE_OR_GOTO(end_of_test, +        uhd_mboard_eeprom_make(&mb_eeprom) +    ) + +    // Set a value, retrieve it, and make sure it matches +    UHD_TEST_EXECUTE_OR_GOTO(free_mboard_eeprom, +        uhd_mboard_eeprom_set_value( +            mb_eeprom, +            "serial", +            "F12345" +        ) +    ) +    UHD_TEST_EXECUTE_OR_GOTO(free_mboard_eeprom, +        uhd_mboard_eeprom_get_value( +            mb_eeprom, +            "serial", +            str_buffer, +            BUFFER_SIZE +        ) +    ) +    if(strcmp(str_buffer, "F12345")){ +        return_code = EXIT_FAILURE; +        fprintf(stderr, "%s:%d: Mismatched EEPROM value: \"%s\" vs. \"F12345\"\n", +                        __FILE__, __LINE__, +                        str_buffer); +        goto free_mboard_eeprom; +    } + +    /* +     * Daughterboard EEPROM test +     */ + +    // Create EEPROM handle +    UHD_TEST_EXECUTE_OR_GOTO(free_mboard_eeprom, +        uhd_dboard_eeprom_make(&db_eeprom) +    ) + +    // Set the ID, retrieve it, and make sure it matches +    UHD_TEST_EXECUTE_OR_GOTO(free_dboard_eeprom, +        uhd_dboard_eeprom_set_id(db_eeprom, "0x0067") +    ) +    UHD_TEST_EXECUTE_OR_GOTO(free_dboard_eeprom, +        uhd_dboard_eeprom_get_id( +            db_eeprom, +            str_buffer, +            BUFFER_SIZE +        ) +    ) +    if(strcmp(str_buffer, "0x0067")){ +        return_code = EXIT_FAILURE; +        fprintf(stderr, "%s:%d: Mismatched daughterboard ID: \"%s\" vs. \"0x0067\"\n", +                        __FILE__, __LINE__, +                        str_buffer); +        goto free_dboard_eeprom; +    } + +    // Set the serial, retrieve it, and make sure it matches +    UHD_TEST_EXECUTE_OR_GOTO(free_dboard_eeprom, +        uhd_dboard_eeprom_set_serial(db_eeprom, "F12345") +    ) +    UHD_TEST_EXECUTE_OR_GOTO(free_dboard_eeprom, +        uhd_dboard_eeprom_get_serial( +            db_eeprom, +            str_buffer, +            BUFFER_SIZE +        ) +    ) +    if(strcmp(str_buffer, "F12345")){ +        return_code = EXIT_FAILURE; +        fprintf(stderr, "%s:%d: Mismatched daughterboard serial: \"%s\" vs. \"F12345\"\n", +                        __FILE__, __LINE__, +                        str_buffer); +        goto free_dboard_eeprom; +    } + +    // Set the revision, retrieve it, and make sure it matches +    UHD_TEST_EXECUTE_OR_GOTO(free_dboard_eeprom, +        uhd_dboard_eeprom_set_revision(db_eeprom, 4) +    ) +    UHD_TEST_EXECUTE_OR_GOTO(free_dboard_eeprom, +        uhd_dboard_eeprom_get_revision(db_eeprom, &db_revision) +    ) +    if(db_revision != 4){ +        return_code = EXIT_FAILURE; +        fprintf(stderr, "%s:%d: Mismatched daughterboard revision: \"%d\" vs. 4\n", +                        __FILE__, __LINE__, db_revision); +        goto free_dboard_eeprom; +    } + +    free_dboard_eeprom: +        if(return_code){ +            uhd_dboard_eeprom_last_error(db_eeprom, str_buffer, BUFFER_SIZE); +            fprintf(stderr, "db_eeprom error: %s\n", str_buffer); +        } + +    free_mboard_eeprom: +        if(return_code){ +            uhd_mboard_eeprom_last_error(mb_eeprom, str_buffer, BUFFER_SIZE); +            fprintf(stderr, "mb_eeprom error: %s\n", str_buffer); +        } + +    end_of_test: +        if(!return_code){ +            printf("\nNo errors detected\n"); +        } +        return return_code; +} diff --git a/host/tests/error_c_test.cpp b/host/tests/error_c_test.cpp new file mode 100644 index 000000000..bb9454678 --- /dev/null +++ b/host/tests/error_c_test.cpp @@ -0,0 +1,142 @@ +// +// Copyright 2015 Ettus Research LLC +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program.  If not, see <http://www.gnu.org/licenses/>. +// + +#include <uhd/error.h> +#include <uhd/types/dict.hpp> + +#include <boost/test/unit_test.hpp> +#include <boost/algorithm/string.hpp> +#include <boost/assign.hpp> +#include <boost/format.hpp> + +/* + * Test our conversions from exceptions to C-level UHD error codes. + * We use inline functions separate from the Boost test cases themselves + * to test our C++ macro, which returns the error code. + */ + +typedef struct { +    std::string last_error; +} dummy_handle_t; + +template <typename error_type> +UHD_INLINE uhd_error throw_uhd_exception(dummy_handle_t *handle, const uhd::exception* except){ +    UHD_SAFE_C_SAVE_ERROR(handle, +        throw *dynamic_cast<const error_type*>(except); +    ) +} + +UHD_INLINE uhd_error throw_boost_exception(dummy_handle_t *handle){ +    UHD_SAFE_C_SAVE_ERROR(handle, +        std::runtime_error except("This is a std::runtime_error, thrown by Boost."); +        BOOST_THROW_EXCEPTION(except); +    ) +} + +UHD_INLINE uhd_error throw_std_exception(dummy_handle_t *handle){ +    UHD_SAFE_C_SAVE_ERROR(handle, +        throw std::runtime_error("This is a std::runtime_error."); +    ) +} + +UHD_INLINE uhd_error throw_unknown_exception(dummy_handle_t *handle){ +    UHD_SAFE_C_SAVE_ERROR(handle, +        throw 1; +    ) +} + +// There are enough non-standard names that we can't just use a conversion function +static const uhd::dict<std::string, std::string> pretty_exception_names = +    boost::assign::map_list_of +        ("assertion_error",       "AssertionError") +        ("lookup_error",          "LookupError") +        ("index_error",           "LookupError: IndexError") +        ("key_error",             "LookupError: KeyError") +        ("type_error",            "TypeError") +        ("value_error",           "ValueError") +        ("runtime_error",         "RuntimeError") +        ("not_implemented_error", "RuntimeError: NotImplementedError") +        ("usb_error",             "RuntimeError: USBError 1") +        ("environment_error",     "EnvironmentError") +        ("io_error",              "EnvironmentError: IOError") +        ("os_error",              "EnvironmentError: OSError") +        ("system_error",          "SystemError") +    ; + +#define UHD_TEST_CHECK_ERROR_CODE(cpp_exception_type, c_error_code) \ +    expected_msg = str(boost::format("This is a uhd::%s.") % BOOST_STRINGIZE(cpp_exception_type)); \ +    uhd::cpp_exception_type cpp_exception_type ## _foo(expected_msg); \ +    error_code = throw_uhd_exception<uhd::cpp_exception_type>(&handle, &cpp_exception_type ## _foo); \ +    BOOST_CHECK_EQUAL(error_code, c_error_code); \ +    BOOST_CHECK_EQUAL(handle.last_error, \ +                      str(boost::format("%s: %s") \ +                          % pretty_exception_names.get(BOOST_STRINGIZE(cpp_exception_type)) \ +                          % expected_msg)); + +// uhd::usb_error has a different constructor +#define UHD_TEST_CHECK_USB_ERROR_CODE() \ +    expected_msg = "This is a uhd::usb_error."; \ +    uhd::usb_error usb_error_foo(1, expected_msg); \ +    error_code = throw_uhd_exception<uhd::usb_error>(&handle, &usb_error_foo); \ +    BOOST_CHECK_EQUAL(error_code, UHD_ERROR_USB); \ +    BOOST_CHECK_EQUAL(handle.last_error, \ +                      str(boost::format("%s: %s") \ +                          % pretty_exception_names.get("usb_error") \ +                          % expected_msg)); + +BOOST_AUTO_TEST_CASE(test_uhd_exception){ +    dummy_handle_t handle; +    std::string expected_msg; +    uhd_error error_code; + +    UHD_TEST_CHECK_ERROR_CODE(assertion_error,       UHD_ERROR_ASSERTION); +    UHD_TEST_CHECK_ERROR_CODE(lookup_error,          UHD_ERROR_LOOKUP); +    UHD_TEST_CHECK_ERROR_CODE(index_error,           UHD_ERROR_INDEX); +    UHD_TEST_CHECK_ERROR_CODE(key_error,             UHD_ERROR_KEY); +    UHD_TEST_CHECK_ERROR_CODE(type_error,            UHD_ERROR_TYPE); +    UHD_TEST_CHECK_ERROR_CODE(value_error,           UHD_ERROR_VALUE); +    UHD_TEST_CHECK_ERROR_CODE(runtime_error,         UHD_ERROR_RUNTIME); +    UHD_TEST_CHECK_ERROR_CODE(not_implemented_error, UHD_ERROR_NOT_IMPLEMENTED); +    UHD_TEST_CHECK_ERROR_CODE(io_error,              UHD_ERROR_IO); +    UHD_TEST_CHECK_ERROR_CODE(os_error,              UHD_ERROR_OS); +    UHD_TEST_CHECK_ERROR_CODE(system_error,          UHD_ERROR_SYSTEM); +    UHD_TEST_CHECK_USB_ERROR_CODE(); +} + +BOOST_AUTO_TEST_CASE(test_boost_exception){ +    dummy_handle_t handle; +    uhd_error error_code = throw_boost_exception(&handle); + +    // Boost error message cannot be determined here, so just check code +    BOOST_CHECK_EQUAL(error_code, UHD_ERROR_BOOSTEXCEPT); +} + +BOOST_AUTO_TEST_CASE(test_std_exception){ +    dummy_handle_t handle; +    uhd_error error_code = throw_std_exception(&handle); + +    BOOST_CHECK_EQUAL(error_code, UHD_ERROR_STDEXCEPT); +    BOOST_CHECK_EQUAL(handle.last_error, "This is a std::runtime_error."); +} + +BOOST_AUTO_TEST_CASE(test_unknown_exception){ +    dummy_handle_t handle; +    uhd_error error_code = throw_unknown_exception(&handle); + +    BOOST_CHECK_EQUAL(error_code, UHD_ERROR_UNKNOWN); +    BOOST_CHECK_EQUAL(handle.last_error, "Unrecognized exception caught."); +} diff --git a/host/tests/ranges_c_test.c b/host/tests/ranges_c_test.c new file mode 100644 index 000000000..da9c86a48 --- /dev/null +++ b/host/tests/ranges_c_test.c @@ -0,0 +1,236 @@ +// +// Copyright 2015 Ettus Research LLC +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program.  If not, see <http://www.gnu.org/licenses/>. +// + +#include <uhd.h> + +#include <stdio.h> +#include <stdlib.h> +#include <math.h> + +#define UHD_TEST_EXECUTE_OR_GOTO(label, ...) \ +    if(__VA_ARGS__){ \ +        fprintf(stderr, "Error occurred at %s:%d\n", __FILE__, (__LINE__-1)); \ +        return_code = EXIT_FAILURE; \ +        goto label; \ +    } + +#define UHD_TEST_CHECK_CLOSE(lhs, rhs) (fabs(lhs-rhs) < 0.001) + +#define BUFFER_SIZE 1024 + +static UHD_INLINE int test_range_values( +    const uhd_range_t *range, +    double start_input, double stop_input, double step_input +){ +    if(!UHD_TEST_CHECK_CLOSE(range->start, start_input)){ +        fprintf(stderr, "%s:%d: Starts did not match: %f vs. %f\n", +                        __FILE__, __LINE__, +                        range->start, start_input); +        return 1; +    } +    if(!UHD_TEST_CHECK_CLOSE(range->stop, stop_input)){ +        fprintf(stderr, "%s:%d: Stops did not match: %f vs. %f\n", +                        __FILE__, __LINE__, +                        range->stop, stop_input); +        return 1; +    } +    if(!UHD_TEST_CHECK_CLOSE(range->step, step_input)){ +        fprintf(stderr, "%s:%d: Steps did not match: %f vs. %f\n", +                        __FILE__, __LINE__, +                        range->step, step_input); +        return 1; +    } + +    return 0; +} + +static UHD_INLINE int test_meta_range_values( +    uhd_meta_range_handle meta_range, +    double start_input, double stop_input, double step_input, +    double start_test, double stop_test, double step_test +){ +    // Add range +    uhd_range_t range; +    range.start = start_input; +    range.stop = stop_input; +    range.step = step_input; +    if(uhd_meta_range_push_back(meta_range, &range)){ +        fprintf(stderr, "%s:%d: Failed to push back range.\n", +                __FILE__, __LINE__); +        return 1; +    } + +    // Test bounds +    uhd_meta_range_start(meta_range, &range.start); +    if(!UHD_TEST_CHECK_CLOSE(range.start, start_test)){ +        fprintf(stderr, "%s:%d: Starts did not match: %f vs. %f\n", +                        __FILE__, __LINE__, +                        range.start, start_test); +        return 1; +    } +    uhd_meta_range_stop(meta_range, &range.stop); +    if(!UHD_TEST_CHECK_CLOSE(range.stop, stop_test)){ +        fprintf(stderr, "%s:%d: Stops did not match: %f vs. %f\n", +                        __FILE__, __LINE__, +                        range.stop, stop_test); +        return 1; +    } +    uhd_meta_range_step(meta_range, &range.step); +    if(!UHD_TEST_CHECK_CLOSE(range.step, step_test)){ +        fprintf(stderr, "%s:%d: Steps did not match: %f vs. %f\n", +                        __FILE__, __LINE__, +                        range.step, step_test); +        return 1; +    } + +    return 0; +} + +static UHD_INLINE int test_meta_range_clip( +    uhd_meta_range_handle meta_range, +    double clip_value, double test_value, +    bool clip_step +){ +    double clip_result; + +    uhd_meta_range_clip(meta_range, clip_value, clip_step, &clip_result); +    if(!UHD_TEST_CHECK_CLOSE(test_value, clip_result)){ +        fprintf(stderr, "%s:%d: Values did not match: %f vs. %f\n", +                        __FILE__, __LINE__, +                        test_value, clip_result); +        return 1; +    } + +    return 0; +} + +int main(){ + +    // Variables +    int return_code; +    uhd_range_t range; +    uhd_meta_range_handle meta_range1, meta_range2; +    char str_buffer[BUFFER_SIZE]; +    size_t size; + +    return_code = EXIT_SUCCESS; + +    // Create meta range 1 +    UHD_TEST_EXECUTE_OR_GOTO(end_of_test, +        uhd_meta_range_make(&meta_range1) +    ) + +    // Test bounds +    UHD_TEST_EXECUTE_OR_GOTO(free_meta_range1, +        test_meta_range_values(meta_range1, -1.0, +1.0, 0.1, +                                            -1.0, +1.0, 0.1) +    ) +    UHD_TEST_EXECUTE_OR_GOTO(free_meta_range1, +        test_meta_range_values(meta_range1, 40.0, 60.0, 1.0, +                                            -1.0, 60.0, 0.1) +    ) +    uhd_meta_range_at(meta_range1, 0, &range); +    UHD_TEST_EXECUTE_OR_GOTO(free_meta_range1, +        test_range_values(&range, -1.0, +1.0, 0.1) +    ) + +    // Check meta range size +    UHD_TEST_EXECUTE_OR_GOTO(free_meta_range1, +        uhd_meta_range_size(meta_range1, &size) +    ) +    if(size != 2){ +        fprintf(stderr, "%s:%d: Invalid size: %lu vs. 2", +                        __FILE__, __LINE__, +                        size); +        goto free_meta_range1; +    } + +    // Test clipping (with steps) +    UHD_TEST_EXECUTE_OR_GOTO(free_meta_range1, +        test_meta_range_clip(meta_range1, -30.0, -1.0, false) +    ) +    UHD_TEST_EXECUTE_OR_GOTO(free_meta_range1, +        test_meta_range_clip(meta_range1, 70.0, 60.0, false) +    ) +    UHD_TEST_EXECUTE_OR_GOTO(free_meta_range1, +        test_meta_range_clip(meta_range1, 20.0, 1.0, false) +    ) +    UHD_TEST_EXECUTE_OR_GOTO(free_meta_range1, +        test_meta_range_clip(meta_range1, 50.0, 50.0, false) +    ) +    UHD_TEST_EXECUTE_OR_GOTO(free_meta_range1, +        test_meta_range_clip(meta_range1, 50.9, 50.9, false) +    ) +    UHD_TEST_EXECUTE_OR_GOTO(free_meta_range1, +        test_meta_range_clip(meta_range1, 50.9, 51.0, true) +    ) + +    // Create meta range 2 +    UHD_TEST_EXECUTE_OR_GOTO(free_meta_range1, +        uhd_meta_range_make(&meta_range2) +    ) +    range.step = 0.0; +    range.start = range.stop = 1.; +    UHD_TEST_EXECUTE_OR_GOTO(free_meta_range2, +        uhd_meta_range_push_back(meta_range2, &range) +    ) +    range.start = range.stop = 2.; +    UHD_TEST_EXECUTE_OR_GOTO(free_meta_range2, +        uhd_meta_range_push_back(meta_range2, &range) +    ) +    range.start = range.stop = 3.; +    UHD_TEST_EXECUTE_OR_GOTO(free_meta_range2, +        uhd_meta_range_push_back(meta_range2, &range) +    ) + +    // Test clipping (without steps) +    UHD_TEST_EXECUTE_OR_GOTO(free_meta_range2, +        test_meta_range_clip(meta_range2, 2., 2., true) +    ) +    UHD_TEST_EXECUTE_OR_GOTO(free_meta_range2, +        test_meta_range_clip(meta_range2, 0., 1., true) +    ) +    UHD_TEST_EXECUTE_OR_GOTO(free_meta_range2, +        test_meta_range_clip(meta_range2, 1.2, 1., true) +    ) +    UHD_TEST_EXECUTE_OR_GOTO(free_meta_range2, +        test_meta_range_clip(meta_range2, 3.1, 3., true) +    ) +    UHD_TEST_EXECUTE_OR_GOTO(free_meta_range2, +        test_meta_range_clip(meta_range2, 4., 3., true) +    ) + +    free_meta_range2: +        if(return_code){ +            uhd_meta_range_last_error(meta_range2, str_buffer, BUFFER_SIZE); +            fprintf(stderr, "meta_range2 error: %s\n", str_buffer); +        } +        uhd_meta_range_free(&meta_range1); + +    free_meta_range1: +        if(return_code){ +            uhd_meta_range_last_error(meta_range1, str_buffer, BUFFER_SIZE); +            fprintf(stderr, "meta_range1 error: %s\n", str_buffer); +        } +        uhd_meta_range_free(&meta_range1); + +    end_of_test: +        if(!return_code){ +            printf("\nNo errors detected.\n"); +        } +        return return_code; +} diff --git a/host/tests/sensors_c_test.c b/host/tests/sensors_c_test.c new file mode 100644 index 000000000..8babe905a --- /dev/null +++ b/host/tests/sensors_c_test.c @@ -0,0 +1,411 @@ +// +// Copyright 2015 Ettus Research LLC +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program.  If not, see <http://www.gnu.org/licenses/>. +// + +#include <uhd.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <math.h> + +#define UHD_TEST_EXECUTE_OR_GOTO(label, ...) \ +    if(__VA_ARGS__){ \ +        fprintf(stderr, "Error occurred at %s:%d\n", __FILE__, (__LINE__-1)); \ +        return_code = EXIT_FAILURE; \ +        goto label; \ +    } + +#define UHD_TEST_CHECK_CLOSE(lhs, rhs) (fabs(lhs-rhs) < 0.001) + +#define BUFFER_SIZE 1024 + +int main(){ + +    // Variables +    int return_code; +    uhd_sensor_value_handle boolean_sensor, integer_sensor, realnum_sensor, string_sensor; +    uhd_sensor_value_data_type_t sensor_type; +    bool bool_out; +    int int_out; +    double realnum_out; +    char str_buffer[BUFFER_SIZE]; + +    return_code = EXIT_SUCCESS; + +    /* +     * Test a sensor made from a boolean +     */ + +    // Create the sensor +    UHD_TEST_EXECUTE_OR_GOTO(end_of_test, +        uhd_sensor_value_make_from_bool( +            &boolean_sensor, +            "Bool sensor", false, +            "True", "False" +        ) +    ) + +    // Check the name +    UHD_TEST_EXECUTE_OR_GOTO(free_boolean_sensor, +        uhd_sensor_value_name( +            boolean_sensor, +            str_buffer, BUFFER_SIZE +        ) +    ) +    if(strcmp(str_buffer, "Bool sensor")){ +        fprintf(stderr, "%s:%d: Boolean sensor name invalid: \"%s\" vs. \"false\"\n", +                        __FILE__, __LINE__, str_buffer); +        return_code = EXIT_FAILURE; +        goto free_boolean_sensor; +    } + +    // Check the value +    UHD_TEST_EXECUTE_OR_GOTO(free_boolean_sensor, +        uhd_sensor_value_value( +            boolean_sensor, +            str_buffer, BUFFER_SIZE +        ) +    ) +    if(strcmp(str_buffer, "false")){ +        fprintf(stderr, "%s:%d: Boolean sensor value invalid: \"%s\" vs. \"false\"\n", +                        __FILE__, __LINE__, str_buffer); +        return_code = EXIT_FAILURE; +        goto free_boolean_sensor; +    } + +    // Check the unit +    UHD_TEST_EXECUTE_OR_GOTO(free_boolean_sensor, +        uhd_sensor_value_unit( +            boolean_sensor, +            str_buffer, BUFFER_SIZE +        ) +    ) +    if(strcmp(str_buffer, "False")){ +        fprintf(stderr, "%s:%d: Boolean sensor unit invalid: \"%s\" vs. \"False\"\n", +                        __FILE__, __LINE__, str_buffer); +        return_code = EXIT_FAILURE; +        goto free_boolean_sensor; +    } + +    // Check the type +    UHD_TEST_EXECUTE_OR_GOTO(free_boolean_sensor, +        uhd_sensor_value_data_type( +            boolean_sensor, +            &sensor_type +        ) +    ) +    if(sensor_type != UHD_SENSOR_VALUE_BOOLEAN){ +        fprintf(stderr, "%s:%d: Wrong sensor type detected: %d vs. %d\n", +                        __FILE__, __LINE__, +                        sensor_type, UHD_SENSOR_VALUE_BOOLEAN); +        return_code = EXIT_FAILURE; +        goto free_boolean_sensor; +    } + +    // Check the casted value +    UHD_TEST_EXECUTE_OR_GOTO(free_boolean_sensor, +        uhd_sensor_value_to_bool( +            boolean_sensor, +            &bool_out +        ) +    ) +    if(bool_out){ +        fprintf(stderr, "%s:%d: Boolean sensor value invalid: true vs. false\n", +                        __FILE__, __LINE__); +        return_code = EXIT_FAILURE; +        goto free_boolean_sensor; +    } + +    /* +     * Test a sensor made from a integer +     */ + +    // Create the sensor +    UHD_TEST_EXECUTE_OR_GOTO(free_boolean_sensor, +        uhd_sensor_value_make_from_int( +            &integer_sensor, +            "Int sensor", 50, +            "Int type", "%d" +        ) +    ) + +    // Check the name +    UHD_TEST_EXECUTE_OR_GOTO(free_integer_sensor, +        uhd_sensor_value_name( +            integer_sensor, +            str_buffer, BUFFER_SIZE +        ) +    ) +    if(strcmp(str_buffer, "Int sensor")){ +        fprintf(stderr, "%s:%d: Integer sensor name invalid: \"%s\" vs. \"Int sensor\"\n", +                        __FILE__, __LINE__, str_buffer); +        return_code = EXIT_FAILURE; +        goto free_integer_sensor; +    } + +    // Check the value +    UHD_TEST_EXECUTE_OR_GOTO(free_integer_sensor, +        uhd_sensor_value_value( +            integer_sensor, +            str_buffer, BUFFER_SIZE +        ) +    ) +    if(strcmp(str_buffer, "50")){ +        fprintf(stderr, "%s:%d: Integer sensor value invalid: \"%s\" vs. \"50\"\n", +                        __FILE__, __LINE__, str_buffer); +        return_code = EXIT_FAILURE; +        goto free_integer_sensor; +    } + +    // Check the unit +    UHD_TEST_EXECUTE_OR_GOTO(free_integer_sensor, +        uhd_sensor_value_unit( +            integer_sensor, +            str_buffer, BUFFER_SIZE +        ) +    ) +    if(strcmp(str_buffer, "Int type")){ +        fprintf(stderr, "%s:%d: Integer sensor unit invalid: \"%s\" vs. \"Int type\"\n", +                        __FILE__, __LINE__, str_buffer); +        return_code = EXIT_FAILURE; +        goto free_integer_sensor; +    } + +    // Check the type +    UHD_TEST_EXECUTE_OR_GOTO(free_integer_sensor, +        uhd_sensor_value_data_type( +            integer_sensor, +            &sensor_type +        ) +    ) +    if(sensor_type != UHD_SENSOR_VALUE_INTEGER){ +        fprintf(stderr, "%s:%d: Wrong sensor type detected: %d vs. %d\n", +                        __FILE__, __LINE__, +                        sensor_type, UHD_SENSOR_VALUE_INTEGER); +        return_code = EXIT_FAILURE; +        goto free_integer_sensor; +    } + +    // Check the casted value +    UHD_TEST_EXECUTE_OR_GOTO(free_integer_sensor, +        uhd_sensor_value_to_int( +            integer_sensor, +            &int_out +        ) +    ) +    if(int_out != 50){ +        fprintf(stderr, "%s:%d: Integer sensor value invalid: %d vs. 50\n", +                        __FILE__, __LINE__, +                        int_out); +        return_code = EXIT_FAILURE; +        goto free_integer_sensor; +    } + +    /* +     * Test a sensor made from a real number +     */ + +    // Create the sensor +    UHD_TEST_EXECUTE_OR_GOTO(free_integer_sensor, +        uhd_sensor_value_make_from_realnum( +            &realnum_sensor, +            "Realnum sensor", 50.0, +            "Realnum type", "%d" +        ) +    ) + +    // Check the name +    UHD_TEST_EXECUTE_OR_GOTO(free_realnum_sensor, +        uhd_sensor_value_name( +            realnum_sensor, +            str_buffer, BUFFER_SIZE +        ) +    ) +    if(strcmp(str_buffer, "Realnum sensor")){ +        fprintf(stderr, "%s:%d: Realnum sensor name invalid: \"%s\" vs. \"Realnum sensor\"\n", +                        __FILE__, __LINE__, str_buffer); +        return_code = EXIT_FAILURE; +        goto free_realnum_sensor; +    } + +    // Check the value +    UHD_TEST_EXECUTE_OR_GOTO(free_realnum_sensor, +        uhd_sensor_value_value( +            realnum_sensor, +            str_buffer, BUFFER_SIZE +        ) +    ) +    if(strcmp(str_buffer, "50")){ +        fprintf(stderr, "%s:%d: Realnum sensor value invalid: \"%s\" vs. \"50\"\n", +                        __FILE__, __LINE__, str_buffer); +        return_code = EXIT_FAILURE; +        goto free_realnum_sensor; +    } + +    // Check the unit +    UHD_TEST_EXECUTE_OR_GOTO(free_realnum_sensor, +        uhd_sensor_value_unit( +            realnum_sensor, +            str_buffer, BUFFER_SIZE +        ) +    ) +    if(strcmp(str_buffer, "Realnum type")){ +        fprintf(stderr, "%s:%d: Realnum sensor unit invalid: \"%s\" vs. \"Realnum type\"\n", +                        __FILE__, __LINE__, str_buffer); +        return_code = EXIT_FAILURE; +        goto free_realnum_sensor; +    } + +    // Check the type +    UHD_TEST_EXECUTE_OR_GOTO(free_realnum_sensor, +        uhd_sensor_value_data_type( +            realnum_sensor, +            &sensor_type +        ) +    ) +    if(sensor_type != UHD_SENSOR_VALUE_REALNUM){ +        fprintf(stderr, "%s:%d: Wrong sensor type detected: %d vs. %d\n", +                        __FILE__, __LINE__, +                        sensor_type, UHD_SENSOR_VALUE_REALNUM); +        return_code = EXIT_FAILURE; +        goto free_realnum_sensor; +    } + +    // Check the casted value +    UHD_TEST_EXECUTE_OR_GOTO(free_realnum_sensor, +        uhd_sensor_value_to_realnum( +            realnum_sensor, +            &realnum_out +        ) +    ) +    if(realnum_out != 50.0){ +        fprintf(stderr, "%s:%d: Realnum sensor value invalid: %2.1f vs. 50.0\n", +                        __FILE__, __LINE__, +                        realnum_out); +        return_code = EXIT_FAILURE; +        goto free_realnum_sensor; +    } + +    /* +     * Test a sensor made from a string +     */ + +    // Create the sensor +    UHD_TEST_EXECUTE_OR_GOTO(free_realnum_sensor, +        uhd_sensor_value_make_from_string( +            &string_sensor, +            "String sensor", +            "String value", +            "String unit" +        ) +    ) + +    // Check the name +    UHD_TEST_EXECUTE_OR_GOTO(free_string_sensor, +        uhd_sensor_value_name( +            string_sensor, +            str_buffer, BUFFER_SIZE +        ) +    ) +    if(strcmp(str_buffer, "String sensor")){ +        fprintf(stderr, "%s:%d: String sensor name invalid: \"%s\" vs. \"String sensor\"\n", +                        __FILE__, __LINE__, str_buffer); +        return_code = EXIT_FAILURE; +        goto free_string_sensor; +    } + +    // Check the value +    UHD_TEST_EXECUTE_OR_GOTO(free_string_sensor, +        uhd_sensor_value_value( +            string_sensor, +            str_buffer, BUFFER_SIZE +        ) +    ) +    if(strcmp(str_buffer, "String value")){ +        fprintf(stderr, "%s:%d: String sensor value invalid: \"%s\" vs. \"String value\"\n", +                        __FILE__, __LINE__, str_buffer); +        return_code = EXIT_FAILURE; +        goto free_string_sensor; +    } + +    // Check the unit +    UHD_TEST_EXECUTE_OR_GOTO(free_string_sensor, +        uhd_sensor_value_unit( +            string_sensor, +            str_buffer, BUFFER_SIZE +        ) +    ) +    if(strcmp(str_buffer, "String unit")){ +        fprintf(stderr, "%s:%d: String sensor unit invalid: \"%s\" vs. \"String unit\"\n", +                        __FILE__, __LINE__, str_buffer); +        return_code = EXIT_FAILURE; +        goto free_string_sensor; +    } + +    // Check the type +    UHD_TEST_EXECUTE_OR_GOTO(free_string_sensor, +        uhd_sensor_value_data_type( +            string_sensor, +            &sensor_type +        ) +    ) +    if(sensor_type != UHD_SENSOR_VALUE_STRING){ +        fprintf(stderr, "%s:%d: Wrong sensor type detected: %d vs. %d\n", +                        __FILE__, __LINE__, +                        sensor_type, UHD_SENSOR_VALUE_STRING); +        return_code = EXIT_FAILURE; +        goto free_string_sensor; +    } + +    /* +     * Cleanup +     */ + +    free_string_sensor: +        if(return_code){ +            uhd_sensor_value_last_error(string_sensor, str_buffer, BUFFER_SIZE); +            fprintf(stderr, "string_sensor error: %s\n", str_buffer); +        } +        uhd_sensor_value_free(&string_sensor); + +    free_realnum_sensor: +        if(return_code){ +            uhd_sensor_value_last_error(realnum_sensor, str_buffer, BUFFER_SIZE); +            fprintf(stderr, "realnum_sensor error: %s\n", str_buffer); +        } +        uhd_sensor_value_free(&realnum_sensor); + +    free_integer_sensor: +        if(return_code){ +            uhd_sensor_value_last_error(integer_sensor, str_buffer, BUFFER_SIZE); +            fprintf(stderr, "integer_sensor error: %s\n", str_buffer); +        } +        uhd_sensor_value_free(&integer_sensor); + +    free_boolean_sensor: +        if(return_code){ +            uhd_sensor_value_last_error(boolean_sensor, str_buffer, BUFFER_SIZE); +            fprintf(stderr, "boolean_sensor error: %s\n", str_buffer); +        } +        uhd_sensor_value_free(&boolean_sensor); + +    end_of_test: +        if(!return_code){ +            printf("\nNo errors detected.\n"); +        } +        return return_code; +} diff --git a/host/tests/subdev_spec_c_test.c b/host/tests/subdev_spec_c_test.c new file mode 100644 index 000000000..7663ba357 --- /dev/null +++ b/host/tests/subdev_spec_c_test.c @@ -0,0 +1,130 @@ +// +// Copyright 2015 Ettus Research LLC +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program.  If not, see <http://www.gnu.org/licenses/>. +// + +#include <uhd.h> + +#include <stdio.h> +#include <stdlib.h> + +#define UHD_TEST_EXECUTE_OR_GOTO(label, ...) \ +    if(__VA_ARGS__){ \ +        fprintf(stderr, "Error occurred at %s:%d\n", __FILE__, (__LINE__-1)); \ +        return_code = EXIT_FAILURE; \ +        goto label; \ +    } + +#define BUFFER_SIZE 1024 + +int main(){ + +    // Variables +    int return_code; +    uhd_subdev_spec_pair_t subdev_spec_pair1, subdev_spec_pair2; +    uhd_subdev_spec_handle subdev_spec1, subdev_spec2; +    size_t size1, size2, i; +    bool pairs_equal; +    char str_buffer[BUFFER_SIZE]; + +    printf("Testing subdevice specification...\n"); +    return_code = EXIT_SUCCESS; + +    // Create subdev spec +    UHD_TEST_EXECUTE_OR_GOTO(end_of_test, +        uhd_subdev_spec_make(&subdev_spec1, "A:AB B:AB") +    ) + +    // Convert to and from args string +    UHD_TEST_EXECUTE_OR_GOTO(free_subdev_spec1, +        uhd_subdev_spec_to_pp_string(subdev_spec1, str_buffer, BUFFER_SIZE) +    ) +    printf("Pretty Print:\n%s", str_buffer); + +    UHD_TEST_EXECUTE_OR_GOTO(free_subdev_spec1, +        uhd_subdev_spec_to_string(subdev_spec1, str_buffer, BUFFER_SIZE) +    ) +    printf("Markup String: %s\n", str_buffer); + +    // Make a second subdev spec from the first's markup string +    UHD_TEST_EXECUTE_OR_GOTO(free_subdev_spec2, +        uhd_subdev_spec_make(&subdev_spec2, str_buffer) +    ) + +    // Make sure both subdev specs are equal +    UHD_TEST_EXECUTE_OR_GOTO(free_subdev_spec2, +        uhd_subdev_spec_size(subdev_spec1, &size1) +    ) +    UHD_TEST_EXECUTE_OR_GOTO(free_subdev_spec2, +        uhd_subdev_spec_size(subdev_spec2, &size2) +    ) +    if(size1 != size2){ +        printf("%s:%d: Sizes do not match. %lu vs. %lu\n", __FILE__, __LINE__, size1, size2); +        return_code = EXIT_FAILURE; +        goto free_subdev_spec2; +    } +    for(i = 0; i < size1; i++){ +        UHD_TEST_EXECUTE_OR_GOTO(free_subdev_spec_pair1, +            uhd_subdev_spec_at(subdev_spec1, i, &subdev_spec_pair1) +        ) +        UHD_TEST_EXECUTE_OR_GOTO(free_subdev_spec_pair2, +            uhd_subdev_spec_at(subdev_spec2, i, &subdev_spec_pair2) +        ) +        UHD_TEST_EXECUTE_OR_GOTO(free_subdev_spec_pair2, +            uhd_subdev_spec_pairs_equal(&subdev_spec_pair1, &subdev_spec_pair2, &pairs_equal) +        ) +        if(!pairs_equal){ +            printf("%s:%d: Subdev spec pairs are not equal.\n" +                   "    db_name: %s vs. %s\n" +                   "    sd_name: %s vs. %s\n", +                   __FILE__, __LINE__, +                   subdev_spec_pair1.db_name, subdev_spec_pair2.db_name, +                   subdev_spec_pair1.sd_name, subdev_spec_pair2.sd_name +                  ); +            return_code = EXIT_FAILURE; +            goto free_subdev_spec_pair2; +        } +        uhd_subdev_spec_pair_free(&subdev_spec_pair1); +        uhd_subdev_spec_pair_free(&subdev_spec_pair2); +    } + +    // Cleanup (and error report, if needed) + +    free_subdev_spec_pair2: +        uhd_subdev_spec_pair_free(&subdev_spec_pair2); + +    free_subdev_spec_pair1: +        uhd_subdev_spec_pair_free(&subdev_spec_pair1); + +    free_subdev_spec2: +        if(return_code){ +            uhd_subdev_spec_last_error(subdev_spec2, str_buffer, BUFFER_SIZE); +            fprintf(stderr, "subdev_spec2 error: %s\n", str_buffer); +        } +        uhd_subdev_spec_free(&subdev_spec2); + +    free_subdev_spec1: +        if(return_code){ +            uhd_subdev_spec_last_error(subdev_spec1, str_buffer, BUFFER_SIZE); +            fprintf(stderr, "subdev_spec1 error: %s\n", str_buffer); +        } +        uhd_subdev_spec_free(&subdev_spec1); + +    end_of_test: +        if(!return_code){ +            printf("\nNo errors detected.\n"); +        } +        return return_code; +}  | 
