Skip to content

Commit b8ab517

Browse files
committed
Add unit testing lcov reports for math_extras. Update unit testing README.md
1 parent 560db31 commit b8ab517

File tree

110 files changed

+15889
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+15889
-4
lines changed

README.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
# 2023_Analysis_zephyr
22

3+
This repository "Software Verification" course at the Faculty of Mathematics in Belgrade.
4+
5+
Techniques and tools used:
6+
- Unit testing via [twister]() tool
7+
-
8+
9+
Repo organization:
10+
11+
```
12+
├── unit_testing
13+
│   ├── code_coverage
14+
│   └── test_coverage_reports
15+
└── zephyr
16+
```
17+
318
### Initializing zephyr submodule
419

520
#### Install zephyr tools
@@ -11,18 +26,22 @@ Zephyr-project's west tool is used for updating the zephyr repository, building,
1126
source ~/.bashrc
1227
```
1328

29+
#### Install additional requirements
30+
1431
`cd zephyr`
1532
`pip3 install --user -r scripts/requirements.txt`
1633

34+
### Install the zephyr SDK
35+
36+
In order to use all of the required tools for testing and building zephyr applications, SDK is needed. It can be downloaded using following commands:
37+
1738
#### Set up the environment for SDK installation
18-
TODO: create a script for setting these variables
39+
1940
`export ZEPHYR_TOOLCHAIN_VARIANT=zephyr`
2041
`export ZEPHYR_SDK_INSTALL_DIR=/opt/zephyr-sdk`
2142
`. ./zephyr-env.sh`
2243

23-
#### Install the zephyr SDK
24-
25-
In order to use all of the required tools for testing and building zephyr applications, SDK is needed. It can be downloaded using following commands:
44+
#### Download and install the SDK
2645

2746
`cd ~`
2847
`wget https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.16.0/zephyr-sdk-0.16.0_linux-x86_64.tar.xz`

unit_testing/README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Unit testing
2+
3+
Since the project is huge, the focus will be only on couple of components.
4+
5+
## Zephyr's twister tool
6+
7+
Twister is a useful tool provided by Zephyr for easier testing and coverage reports generation. Documentation regarding Twister can be found [here](https://docs.zephyrproject.org/3.1.0/develop/test/twister.html).
8+
9+
This directory contains folders containins .html coverage reports in lcov format for a couple of different test runs. In order to get output binaries and logs, twister has to be run again. These files can be large, so they are excluded from the project's repo.
10+
11+
Commands that were used for generation of those reports are given below:
12+
13+
- Execute unit tests related to math_extras and store reports at `/test_coverage/unit_run_math_extras` and `/test_coverage/unit_run_math_extras_builtin`. In order to run these tests, a `CMakeLists.txt` had to be modified before running the commands. There was also a naming bug in the `portable.c` file:
14+
15+
```
16+
// portable.c
17+
18+
...
19+
20+
#define VNAME(N) test_portable_##N
21+
// Changed to
22+
#define VNAME(N) run_portable_##N
23+
24+
...
25+
26+
ZTEST(math_extras_portable, test_portable_u32_add) {
27+
run_portable_u32_add();
28+
}
29+
30+
...
31+
32+
```
33+
34+
Twister command for running the tests:
35+
36+
`west twister --coverage -p unit_testing -T tests/unit/math_extras/ -v --report-dir ~/2023_Analysis_zephyr/twister_reports/test_coverage/unit_run_math_extras --outdir ~/2023_Analysis_zephyr/twister_reports/test_coverage/math_extras/unit_run_math_extras`
37+
38+
*NOTE*: As mentioned, `CMakeLists.txt` was modified between two runs. This was done to test math_extras functions with and without builtin support.
39+
40+
`CMakeLists.txt` **with** builtin support:
41+
42+
```
43+
# SPDX-License-Identifier: Apache-2.0
44+
45+
cmake_minimum_required(VERSION 3.20.0)
46+
47+
project(math_extras)
48+
find_package(Zephyr COMPONENTS unittest REQUIRED HINTS $ENV{ZEPHYR_BASE})
49+
target_sources(testbinary PRIVATE main.c)
50+
```
51+
52+
`CMakeLists.txt` **without** builtin support:
53+
54+
```
55+
# SPDX-License-Identifier: Apache-2.0
56+
57+
cmake_minimum_required(VERSION 3.20.0)
58+
59+
project(math_extras)
60+
find_package(Zephyr COMPONENTS unittest REQUIRED HINTS $ENV{ZEPHYR_BASE})
61+
target_sources(testbinary PRIVATE portable.c)
62+
```
63+
64+
The only difference is the target source file.
65+
66+
### Ztest framework
67+
68+
In the section above, tests were run without any modification or explanation. Zephyr project has developed a testing framework, `Ztest` that alows writing and executing various tests. Comprehensive documentation for Ztest is given [here](https://docs.zephyrproject.org/3.1.0/develop/test/ztest.html). Source code for math_extras tests is in `tests/unit/math_extras/main.c`, `tests/unit/math_extras/portable.c` and `tests/unit/math_extras/tests.inc`.
69+
70+
`main.c` and `portable.c` contain the main **test** logic. In other words, test cases are defined in those files. Helper functions that call functions under test are defined in `tests.inc` in the form of `run_<function_name>` / `run_portable_<function_name>` based on the macro `VNAME(x)` defined in both `main.c` and `portable.c`. Twister will build and execute the tests based on the source files listed in `CMakeLists.txt` (`target_sources` line).
71+
72+
Each test case is defined using `ZTEST` macro with arguments defining test suite name and test case name. These will be visible during test run.
73+
74+
For example, test case defined as:
75+
```
76+
ZTEST(math_extras, test_u32_add) {
77+
run_u32_add();
78+
}
79+
```
80+
81+
means that Twister will call `math_extras.test_u32_add` test case during execution. The body of the `ZTEST`-defined test case contains all of the test logic (setting up the test, executing helper functions, functions under test, deinitialization etc.)
82+
83+
### lcov report
84+
85+
lcov is a tool that creates graphical representation of the code coverage in .html format. Reports for math_extras functions are given below:
86+
87+
![code coverage report](./test_coverage/mat_extras/lcov_math_extras_report.png "lcov report").
88+
89+
90+
As the reports show, there is a 100% coverage of the `math_extras` functions both with builtin functions and with portable implementation.
Loading

0 commit comments

Comments
 (0)