Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
🎨 Use libhal_unit_test for unit tests
Browse files Browse the repository at this point in the history
Simplifies and standardizes the process for building libhal package unit
tests. Also utilizes clang-tidy in the unit test process
  • Loading branch information
Khalil Estell authored and kammce committed Sep 27, 2023
1 parent dcbabdf commit 4da8b69
Show file tree
Hide file tree
Showing 17 changed files with 84 additions and 149 deletions.
55 changes: 55 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 6 additions & 11 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(
Expand Down
96 changes: 0 additions & 96 deletions tests/CMakeLists.txt

This file was deleted.

4 changes: 1 addition & 3 deletions tests/adc.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<read_t> driver_read() override
Expand Down
1 change: 1 addition & 0 deletions tests/can.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class test_can : public hal::can
hal::callback<handler> 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
Expand Down
4 changes: 1 addition & 3 deletions tests/dac.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<write_t> driver_write(float p_value) override
Expand Down
13 changes: 6 additions & 7 deletions tests/error.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 3 additions & 1 deletion tests/i2c.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ class test_i2c : public hal::i2c
hal::byte m_address{};
std::span<const hal::byte> m_data_out{};
std::span<hal::byte> m_data_in{};
bool m_return_error_status{ false };
std::function<hal::timeout_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
Expand Down
4 changes: 1 addition & 3 deletions tests/input_pin.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions tests/motor.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<power_t> driver_power(float p_power) override
result<power_t> driver_power(float power) override
{
m_power = p_power;
m_power = power;
if (m_return_error_status) {
return hal::new_error();
}
Expand Down
4 changes: 1 addition & 3 deletions tests/output_pin.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions tests/pwm.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<frequency_t> driver_frequency(hertz p_frequency) override
Expand Down
4 changes: 1 addition & 3 deletions tests/serial.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions tests/servo.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<position_t> driver_position(hal::degrees p_position) override
Expand Down
5 changes: 1 addition & 4 deletions tests/spi.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ class test_spi : public hal::spi
std::span<hal::byte> 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
Expand Down
4 changes: 1 addition & 3 deletions tests/steady_clock.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4da8b69

Please sign in to comment.