From 6440546b74e7a4343cebbafe85f55a07fd1528a9 Mon Sep 17 00:00:00 2001 From: Hari Limaye Date: Mon, 16 Aug 2021 10:04:43 +0100 Subject: [PATCH 1/3] Style: Remove drivers unit test directory from codeignore Unit tests for drivers are currently not checked for style compliance. We should be checking coding style in unit tests, so we remove them from the .codecheckignore file. --- .codecheckignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.codecheckignore b/.codecheckignore index 62e12b56f4e..3b8d06abad4 100644 --- a/.codecheckignore +++ b/.codecheckignore @@ -35,4 +35,3 @@ ^UNITTESTS ^storage/blockdevice/tests/UNITTESTS ^storage/kvstore/tests/UNITTESTS -^drivers/tests/UNITTESTS From 9491403f43344619ef82bb20d5516799d437c659 Mon Sep 17 00:00:00 2001 From: Hari Limaye Date: Tue, 10 Aug 2021 17:50:47 +0100 Subject: [PATCH 2/3] Unit tests: Add stubs for analogin_api.c functions Currently there are no stub implementations of the analogin_api.c functions. As the AnalogIn class makes use of these functions, these stub definitions are required in order to build AnalogIn.cpp and generate .gcno files to be used to generate code coverage metrics. --- hal/tests/UNITTESTS/doubles/CMakeLists.txt | 2 + .../UNITTESTS/doubles/analogin_api_stub.c | 49 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 hal/tests/UNITTESTS/doubles/analogin_api_stub.c diff --git a/hal/tests/UNITTESTS/doubles/CMakeLists.txt b/hal/tests/UNITTESTS/doubles/CMakeLists.txt index 896617bc249..dd0f3faf079 100644 --- a/hal/tests/UNITTESTS/doubles/CMakeLists.txt +++ b/hal/tests/UNITTESTS/doubles/CMakeLists.txt @@ -17,6 +17,7 @@ target_compile_definitions(mbed-stubs-hal DEVICE_PWMOUT DEVICE_WATCHDOG MBED_WDOG_ASSERT=1 + DEVICE_ANALOGIN ) target_sources(mbed-stubs-hal @@ -24,6 +25,7 @@ target_sources(mbed-stubs-hal pwmout_api_stub.c us_ticker_stub.cpp watchdog_api_stub.c + analogin_api_stub.c ) target_link_libraries(mbed-stubs-hal diff --git a/hal/tests/UNITTESTS/doubles/analogin_api_stub.c b/hal/tests/UNITTESTS/doubles/analogin_api_stub.c new file mode 100644 index 00000000000..1ae5ba1bf10 --- /dev/null +++ b/hal/tests/UNITTESTS/doubles/analogin_api_stub.c @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2021 Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * 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. + */ +#include "hal/analogin_api.h" +#include + +#if DEVICE_ANALOGIN + +void analogin_init_direct(analogin_t *obj, const PinMap *pinmap) +{ +} + +void analogin_init(analogin_t *obj, PinName pin) +{ +} + +void analogin_free(analogin_t *obj) +{ +} + +float analogin_read(analogin_t *obj) +{ + return 0; +} + +uint16_t analogin_read_u16(analogin_t *obj) +{ + return 0; +} + +const PinMap *analogin_pinmap(void) +{ + return NULL; +} + +#endif // DEVICE_ANALOGIN From c6312a3c70410ba507c5c41291b43b11ae904dc7 Mon Sep 17 00:00:00 2001 From: Hari Limaye Date: Tue, 10 Aug 2021 13:02:28 +0100 Subject: [PATCH 3/3] Unit tests: Add boilerplate code for AnalogIn.cpp In pursuit of increasing unit test coverage of Mbed OS, we add the boilerplate code for unit testing of AnalogIn.cpp and an empty test source file. This serves two purposes - it allows us to report on currently untested sources in code coverage data, and it makes it a little easier for developers to write unit tests for these sources. The 'empty' test file contains a main function that simply returns. This is required to allow CMake's add_executable() to be used to pull in the source file for the SUT, which ensures that this source file is built and therefore instrumented to generate coverage data. The alternative that was explored was to instead use Google Test's TEST() macro and prefix the test name with 'DISABLED_' to skip it, but that resulted in the test being reported as skipped, which was deemed undesireable for these 'empty' tests. --- .../tests/UNITTESTS/AnalogIn/CMakeLists.txt | 31 +++++++++++++++++++ .../UNITTESTS/AnalogIn/test_analogin.cpp | 31 +++++++++++++++++++ drivers/tests/UNITTESTS/CMakeLists.txt | 1 + 3 files changed, 63 insertions(+) create mode 100644 drivers/tests/UNITTESTS/AnalogIn/CMakeLists.txt create mode 100644 drivers/tests/UNITTESTS/AnalogIn/test_analogin.cpp diff --git a/drivers/tests/UNITTESTS/AnalogIn/CMakeLists.txt b/drivers/tests/UNITTESTS/AnalogIn/CMakeLists.txt new file mode 100644 index 00000000000..c0ac369d90d --- /dev/null +++ b/drivers/tests/UNITTESTS/AnalogIn/CMakeLists.txt @@ -0,0 +1,31 @@ +# Copyright (c) 2021 ARM Limited. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +include(GoogleTest) + +set(TEST_NAME analogin-unittest) + +add_executable(${TEST_NAME}) + +target_compile_definitions(${TEST_NAME} + PRIVATE + DEVICE_ANALOGIN + MBED_CONF_TARGET_DEFAULT_ADC_VREF=3.3f +) + +target_sources(${TEST_NAME} + PRIVATE + ${mbed-os_SOURCE_DIR}/drivers/source/AnalogIn.cpp + test_analogin.cpp +) + +target_link_libraries(${TEST_NAME} + PRIVATE + mbed-headers-platform + mbed-headers-hal + mbed-headers-drivers + mbed-stubs-hal + mbed-stubs-platform +) + +gtest_discover_tests(${TEST_NAME} PROPERTIES LABELS "drivers") diff --git a/drivers/tests/UNITTESTS/AnalogIn/test_analogin.cpp b/drivers/tests/UNITTESTS/AnalogIn/test_analogin.cpp new file mode 100644 index 00000000000..d089b5a4c12 --- /dev/null +++ b/drivers/tests/UNITTESTS/AnalogIn/test_analogin.cpp @@ -0,0 +1,31 @@ +/* Copyright (c) 2021 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + * + * 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. + */ + +/* This empty test file is present in order to generate coverage data for the + * corresponding source file. This file allows an executable CMake target to be + * created, which builds the corresponding source file and produces a .gcno + * file to be used by a coverage tool. + * + * Replace this main function with Google Test tests, as described in the + * Mbed OS documentation: + * + * https://os.mbed.com/docs/mbed-os/latest/debug-test/unit-testing.html + * + */ +int main() +{ + return 0; +} diff --git a/drivers/tests/UNITTESTS/CMakeLists.txt b/drivers/tests/UNITTESTS/CMakeLists.txt index f4170014883..2e4ce437c1d 100644 --- a/drivers/tests/UNITTESTS/CMakeLists.txt +++ b/drivers/tests/UNITTESTS/CMakeLists.txt @@ -1,5 +1,6 @@ # Copyright (c) 2021 ARM Limited. All rights reserved. # SPDX-License-Identifier: Apache-2.0 add_subdirectory(doubles) +add_subdirectory(AnalogIn) add_subdirectory(PwmOut) add_subdirectory(Watchdog)