diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 000000000..fe1be5fa6 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,55 @@ +# Copyright 2023 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. + +cmake_minimum_required(VERSION 3.15) + +# Because libhal is a header only library, this cmake file only builds and +# executes the unit tests +project(libhal LANGUAGES CXX) + +libhal_unit_test(SOURCES + tests/helpers.cpp + tests/can.test.cpp + tests/pwm.test.cpp + tests/timer.test.cpp + tests/i2c.test.cpp + tests/spi.test.cpp + tests/adc.test.cpp + tests/dac.test.cpp + tests/input_pin.test.cpp + tests/interrupt_pin.test.cpp + tests/output_pin.test.cpp + tests/serial.test.cpp + tests/steady_clock.test.cpp + tests/motor.test.cpp + tests/timeout.test.cpp + tests/error.test.cpp + tests/accelerometer.test.cpp + tests/distance_sensor.test.cpp + tests/gyroscope.test.cpp + tests/magnetometer.test.cpp + tests/rotation_sensor.test.cpp + tests/temperature_sensor.test.cpp + tests/servo.test.cpp + tests/g_force.test.cpp + tests/lengths.test.cpp + tests/main.test.cpp + + PACKAGES + boost-leaf + tl-function-ref + + LINK_LIBRARIES + boost::leaf + tl::function-ref) diff --git a/conanfile.py b/conanfile.py index c53aec98d..67abdb89e 100644 --- a/conanfile.py +++ b/conanfile.py @@ -34,7 +34,7 @@ class libhal_conan(ConanFile): "peripherals and devices using modern C++") topics = ("peripherals", "hardware", "abstraction", "devices", "hal") settings = "compiler", "build_type", "os", "arch" - exports_sources = "include/*", "tests/*", "LICENSE" + exports_sources = "include/*", "tests/*", "CMakeLists.txt", "LICENSE" generators = "CMakeToolchain", "CMakeDeps" no_copy_source = True @@ -60,25 +60,20 @@ def validate(self): def build_requirements(self): self.tool_requires("cmake/3.27.1") + self.tool_requires("libhal-cmake-util/1.2.0") + self.test_requires("boost-ext-ut/1.1.9") def requirements(self): self.requires("tl-function-ref/1.0.0") self.requires("boost-leaf/1.81.0") - self.test_requires("boost-ext-ut/1.1.9") def layout(self): cmake_layout(self) def build(self): - if not self.conf.get("tools.build:skip_test", default=False): - cmake = CMake(self) - if self.settings.os == "Windows": - cmake.configure(build_script_folder="tests") - else: - cmake.configure(build_script_folder="tests", - variables={"ENABLE_ASAN": True}) - cmake.build() - self.run(os.path.join(self.cpp.build.bindir, "unit_test")) + cmake = CMake(self) + cmake.configure() + cmake.build() def package(self): copy(self, "LICENSE", dst=os.path.join( diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt deleted file mode 100644 index 889c34ea8..000000000 --- a/tests/CMakeLists.txt +++ /dev/null @@ -1,96 +0,0 @@ -# Copyright 2023 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. - -cmake_minimum_required(VERSION 3.15) - -project(unit_test VERSION 0.0.1 LANGUAGES CXX) - -if(DEFINED ENABLE_ASAN) - set(ASAN_FLAG "-fsanitize=address" CACHE INTERNAL "Use AddressSanatizer") -endif(DEFINED ENABLE_ASAN) - -if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") - # require at least gcc 11 - if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 11.0) - message(FATAL_ERROR "GCC version must be at least 11!") - endif() -elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") - # require at least clang 14 - if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 14) - message(FATAL_ERROR "Clang version must be at least 14!") - endif() -elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang") - # require at least clang 14 - if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 14) - message(FATAL_ERROR "Clang version must be at least 14!") - endif() -else() - message(WARNING "You are using an unsupported compiler! Compilation has only been tested with Clang and GCC, detected ${CMAKE_CXX_COMPILER_ID}.") -endif() - -find_package(ut REQUIRED CONFIG) -find_package(boost-leaf REQUIRED CONFIG) -find_package(tl-function-ref REQUIRED CONFIG) - -add_executable(${PROJECT_NAME} - helpers.cpp - can.test.cpp - pwm.test.cpp - timer.test.cpp - i2c.test.cpp - spi.test.cpp - adc.test.cpp - dac.test.cpp - input_pin.test.cpp - interrupt_pin.test.cpp - output_pin.test.cpp - serial.test.cpp - steady_clock.test.cpp - motor.test.cpp - timeout.test.cpp - error.test.cpp - accelerometer.test.cpp - distance_sensor.test.cpp - gyroscope.test.cpp - magnetometer.test.cpp - rotation_sensor.test.cpp - temperature_sensor.test.cpp - servo.test.cpp - g_force.test.cpp - lengths.test.cpp - main.test.cpp) - -target_include_directories(${PROJECT_NAME} PUBLIC . ../include) -target_compile_options(${PROJECT_NAME} PRIVATE - --coverage - -fprofile-arcs - -ftest-coverage - ${ASAN_FLAG} - -Werror - -Wall - -Wextra - -Wshadow - -Wnon-virtual-dtor - -Wno-gnu-statement-expression - -pedantic) -target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20) -target_link_options(${PROJECT_NAME} PRIVATE - --coverage - -fprofile-arcs - -ftest-coverage - ${ASAN_FLAG}) -target_link_libraries(${PROJECT_NAME} PRIVATE - boost-ext-ut::ut - tl::function-ref - boost::leaf) diff --git a/tests/adc.test.cpp b/tests/adc.test.cpp index fe57593d6..04462f985 100644 --- a/tests/adc.test.cpp +++ b/tests/adc.test.cpp @@ -26,9 +26,7 @@ class test_adc : public hal::adc constexpr static float m_returned_position{ 0.5f }; bool m_return_error_status{ false }; - ~test_adc() - { - } + ~test_adc() override = default; private: result driver_read() override diff --git a/tests/can.test.cpp b/tests/can.test.cpp index 5d1e6de79..9d954fca8 100644 --- a/tests/can.test.cpp +++ b/tests/can.test.cpp @@ -36,6 +36,7 @@ class test_can : public hal::can hal::callback m_handler = [](const message_t&) {}; bool m_return_error_status{ false }; bool m_bus_on_called{ false }; + ~test_can() override = default; private: status driver_configure(const settings& p_settings) override diff --git a/tests/dac.test.cpp b/tests/dac.test.cpp index c3bc99e5f..7350f9e34 100644 --- a/tests/dac.test.cpp +++ b/tests/dac.test.cpp @@ -25,9 +25,7 @@ class test_dac : public hal::dac float m_passed_value{}; bool m_return_error_status{ false }; - ~test_dac() - { - } + ~test_dac() override = default; private: result driver_write(float p_value) override diff --git a/tests/error.test.cpp b/tests/error.test.cpp index ba651adad..38177e629 100644 --- a/tests/error.test.cpp +++ b/tests/error.test.cpp @@ -43,17 +43,16 @@ void error_test() "[success] hal::attempt calls handler"_test = []() { // Setup - constexpr int expected = 123456789; + static const int expected = 123456789; int value_to_be_change = 0; // Exercise // Should call the `on_error_callback` defined in the tweaks file - auto result = - attempt([expected]() -> status { return new_error(expected); }, - [&value_to_be_change](int p_handler_value) -> status { - value_to_be_change = p_handler_value; - return {}; - }); + auto result = attempt([]() -> status { return new_error(expected); }, + [&value_to_be_change](int p_handler_value) -> status { + value_to_be_change = p_handler_value; + return {}; + }); // Verify expect(that % value_to_be_change == expected); diff --git a/tests/helpers.cpp b/tests/helpers.cpp index 2a2676ec1..2a0798de2 100644 --- a/tests/helpers.cpp +++ b/tests/helpers.cpp @@ -2,6 +2,6 @@ bool compare_floats(float p_first, float p_second, float p_error_margin) { - float difference = abs(p_first - p_second); + float difference = std::abs(p_first - p_second); return difference < p_error_margin; } \ No newline at end of file diff --git a/tests/i2c.test.cpp b/tests/i2c.test.cpp index e70812882..ab15e881a 100644 --- a/tests/i2c.test.cpp +++ b/tests/i2c.test.cpp @@ -35,10 +35,12 @@ class test_i2c : public hal::i2c hal::byte m_address{}; std::span m_data_out{}; std::span m_data_in{}; + bool m_return_error_status{ false }; std::function m_timeout = []() -> hal::status { return hal::success(); }; - bool m_return_error_status{ false }; + + ~test_i2c() override = default; private: status driver_configure(const settings& p_settings) override diff --git a/tests/input_pin.test.cpp b/tests/input_pin.test.cpp index e45d39cc7..b739af18e 100644 --- a/tests/input_pin.test.cpp +++ b/tests/input_pin.test.cpp @@ -27,9 +27,7 @@ class test_input_pin : public hal::input_pin settings m_settings{}; bool m_return_error_status{ false }; - ~test_input_pin() - { - } + ~test_input_pin() override = default; private: status driver_configure(const settings& p_settings) override diff --git a/tests/motor.test.cpp b/tests/motor.test.cpp index c352dbb4e..a8da81510 100644 --- a/tests/motor.test.cpp +++ b/tests/motor.test.cpp @@ -26,14 +26,12 @@ class test_motor : public hal::motor float m_power{}; bool m_return_error_status{ false }; - ~test_motor() - { - } + ~test_motor() override = default; private: - result driver_power(float p_power) override + result driver_power(float power) override { - m_power = p_power; + m_power = power; if (m_return_error_status) { return hal::new_error(); } diff --git a/tests/output_pin.test.cpp b/tests/output_pin.test.cpp index d10305251..a5e544442 100644 --- a/tests/output_pin.test.cpp +++ b/tests/output_pin.test.cpp @@ -29,9 +29,7 @@ class test_output_pin : public hal::output_pin bool m_driver_level{}; bool m_return_error_status{ false }; - ~test_output_pin() - { - } + ~test_output_pin() override = default; private: status driver_configure(const settings& p_settings) override diff --git a/tests/pwm.test.cpp b/tests/pwm.test.cpp index 88d040282..fd9888ed6 100644 --- a/tests/pwm.test.cpp +++ b/tests/pwm.test.cpp @@ -28,9 +28,7 @@ class test_pwm : public hal::pwm float m_duty_cycle{}; bool m_return_error_status{ false }; - ~test_pwm() - { - } + ~test_pwm() override = default; private: result driver_frequency(hertz p_frequency) override diff --git a/tests/serial.test.cpp b/tests/serial.test.cpp index fc1abc1b3..ab842c2e8 100644 --- a/tests/serial.test.cpp +++ b/tests/serial.test.cpp @@ -31,9 +31,7 @@ class test_serial : public hal::serial bool m_flush_called{ false }; bool m_return_error_status{ false }; - ~test_serial() - { - } + ~test_serial() override = default; private: status driver_configure(const settings& p_settings) override diff --git a/tests/servo.test.cpp b/tests/servo.test.cpp index 75224b383..b6fdd64b8 100644 --- a/tests/servo.test.cpp +++ b/tests/servo.test.cpp @@ -27,9 +27,7 @@ class test_servo : public hal::servo public: hal::degrees m_position = 0.0f; - ~test_servo() - { - } + ~test_servo() override = default; private: result driver_position(hal::degrees p_position) override diff --git a/tests/spi.test.cpp b/tests/spi.test.cpp index c65d868ca..f4b51eb3e 100644 --- a/tests/spi.test.cpp +++ b/tests/spi.test.cpp @@ -31,10 +31,7 @@ class test_spi : public hal::spi std::span m_data_in{}; hal::byte m_filler{}; bool m_return_error_status{ false }; - - ~test_spi() - { - } + ~test_spi() override = default; private: status driver_configure(const settings& p_settings) override diff --git a/tests/steady_clock.test.cpp b/tests/steady_clock.test.cpp index 1e4bf8991..63e6e4c5a 100644 --- a/tests/steady_clock.test.cpp +++ b/tests/steady_clock.test.cpp @@ -24,9 +24,7 @@ class test_steady_clock : public hal::steady_clock constexpr static hertz m_frequency{ 1.0_Hz }; constexpr static std::uint64_t m_uptime{ 100 }; - ~test_steady_clock() - { - } + ~test_steady_clock() override = default; private: frequency_t driver_frequency() override