Skip to content

Commit 9a89922

Browse files
committed
Added Ceedling and Mocking based example as Lab4
1 parent 4f886a2 commit 9a89922

14 files changed

Lines changed: 341 additions & 15 deletions

File tree

.github/workflows/test_coverage.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ jobs:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- uses: actions/checkout@v3
14-
- name: Install dependencies
15-
run: sudo apt-get update && sudo apt-get install -y make gcc gcovr build-essential
16-
- name: Install Renode
17-
run: ./Lab3/install_tools.sh
14+
- name: Setup Environment
15+
run: ./install_tools.sh
1816
- name: Run renode based code coverage analysis
1917
run: make coverage -C Lab3
18+
- name: Lab4 code coverage analysis
19+
run: make coverage_summary -C Lab4

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,6 @@ dkms.conf
6161
# Lab1 build artifacts and reports
6262
Lab1/Lab1
6363
Lab1/coverage_checksum.txt
64-
Lab1/*.gcov
64+
Lab1/*.gcov
65+
66+
*build*

Lab3/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ coverage: run
4141
msg = sprintf("\nTotal Coverage: %.2f%% (%d/%d lines hit)\n", percent, hit, found); \
4242
print msg; \
4343
if (ENVIRON["GITHUB_STEP_SUMMARY"] != "") { \
44-
printf "### 📊 Coverage Report\n- **Status:** %s\n- **Percentage:** %.2f%%\n- **Details:** %d out of %d lines executed\n", (percent == 100 ? "✅ Pass" : "⚠️ Partial"), percent, hit, found >> ENVIRON["GITHUB_STEP_SUMMARY"]; \
44+
printf "### 📊 Coverage Report\n\n---\n\n## Lab3 - Renode Coverage\n- **Status:** %s\n- **Percentage:** %.2f%%\n- **Details:** %d out of %d lines executed\n", (percent == 100 ? "✅ Pass" : "⚠️ Partial"), percent, hit, found >> ENVIRON["GITHUB_STEP_SUMMARY"]; \
4545
} \
4646
}'
4747

Lab3/install_tools.sh

Lines changed: 0 additions & 9 deletions
This file was deleted.

Lab4/Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
coverage:
2+
ceedling gcov:all
3+
4+
mock_test:
5+
ceedling test:all
6+
7+
coverage_summary: coverage
8+
@python3 -m gcovr build/gcov/out --json-summary-pretty -o build/coverage.json
9+
@python3 scripts/coverage_summary.py
10+
11+
clean:
12+
rm -rf build

Lab4/README.MD

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Mocking Hardware APIs using CMock
2+
* This Lab demonstrates how embedded firmware can be tested without hardware by mocking ADC dependencies.
3+
4+
## Learning Outcomes
5+
1. Understand hardware abstraction.
6+
2. Generate mocks using CMock.
7+
3. Validate business logic independent of hardware.
8+
4. Measure code coverage.
9+
5. Prepare firmware for CI/CD integration.
10+
11+
Execution Environment:
12+
Host PC (Linux/Windows/macOS)
13+
14+
Tools:
15+
- Unity
16+
- CMock
17+
- GCC
18+
- gcov

Lab4/project.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
:project:
2+
:build_root: build
3+
:test_file_prefix: test_
4+
5+
:paths:
6+
:source:
7+
- src
8+
:include:
9+
- src
10+
:test:
11+
- test
12+
13+
:cmock:
14+
:mock_prefix: Mock
15+
16+
:plugins:
17+
:enabled:
18+
- gcov
19+
20+
:gcov:
21+
:reports:
22+
- HtmlDetailed

Lab4/scripts/coverage_summary.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import json
2+
import os
3+
4+
with open("build/coverage.json") as f:
5+
d = json.load(f)
6+
7+
p = d["line_percent"]
8+
h = d["line_covered"]
9+
t = d["line_total"]
10+
11+
status = "✅ Pass" if p == 100 else "⚠️ Partial"
12+
13+
print("\n📊 Coverage Report")
14+
print(f"Status: {status}")
15+
print(f"Percentage: {p:.2f}%")
16+
print(f"Details: {h} out of {t} lines executed\n")
17+
18+
summary = os.getenv("GITHUB_STEP_SUMMARY")
19+
if summary:
20+
with open(summary, "a") as f:
21+
f.write(
22+
f"\n---\n\n"
23+
f"## Lab4 - Unit Test Coverage\n"
24+
f"- **Status:** {status}\n"
25+
f"- **Percentage:** {p:.2f}%\n"
26+
f"- **Details:** {h} out of {t} lines executed\n"
27+
)

Lab4/src/adc.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "adc.h"
2+
3+
/**
4+
* @brief Read ADC value and convert to millivolts.
5+
*
6+
* In real firmware this function would read
7+
* from ADC hardware registers and convert to mv.
8+
*/
9+
uint16_t adc_read_battery_mv(void)
10+
{
11+
return 3700;
12+
}

Lab4/src/adc.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef ADC_H
2+
#define ADC_H
3+
4+
#include <stdint.h>
5+
6+
/**
7+
* @brief Read battery voltage from ADC.
8+
*
9+
* @return Battery voltage in millivolts.
10+
*/
11+
uint16_t adc_read_battery_mv(void);
12+
13+
#endif /* ADC_H */

0 commit comments

Comments
 (0)