Skip to content

Commit 16a67b2

Browse files
committed
Lab1 example with Unit testing example and CI Workflows
1 parent 04155d9 commit 16a67b2

12 files changed

Lines changed: 4055 additions & 2 deletions

File tree

.github/workflows/build.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Build and Test
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
build-test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
- name: Install dependencies
15+
run: sudo apt-get update && sudo apt-get install -y make gcc gcov
16+
- name: Build
17+
run: |
18+
cd C/Checksum
19+
make all
20+
- name: Run unit tests
21+
run: |
22+
cd Lab1
23+
./Lab1

.github/workflows/coverage.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Coverage
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
coverage:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3
13+
- name: Install dependencies
14+
run: sudo apt-get update && sudo apt-get install -y make gcc gcov
15+
- name: Run coverage and capture percentage
16+
id: runcov
17+
run: |
18+
cd Lab1
19+
make coverage
20+
PCT=$(grep 'Lines executed' coverage_checksum.txt | head -1 | sed -E 's/.*:([0-9.]+)%.*$/\1/')
21+
echo "coverage=$PCT" >> $GITHUB_OUTPUT
22+
- name: Update README badge
23+
if: success()
24+
run: |
25+
cd Lab1
26+
PCT=$(grep 'Lines executed' coverage_checksum.txt | head -1 | sed -E 's/.*:([0-9.]+)%.*$/\1/')
27+
cd ../../
28+
sed -i 's/badge\/coverage-[0-9]\+\.?[0-9]*%25/badge\/coverage-'"$PCT"'%25/' README.md
29+
git config user.name "github-actions[bot]"
30+
git config user.email "github-actions[bot]@users.noreply.github.com"
31+
git add README.md
32+
git commit -m "Update coverage badge to $PCT%" || echo "No changes to commit"
33+
git push

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,8 @@ dkms.conf
5353

5454
# debug information files
5555
*.dwo
56+
57+
# Coverage temporary files
58+
*.gcda
59+
*.gcno
60+
Checksum

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2026 TechEvents-FOSS
3+
Copyright (c) 2026 BHARATH G
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

Lab1/Makefile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
PROJECT = Lab1
2+
3+
SRC = checksum.c test_checksum.c
4+
TEST_SRC = unity.c
5+
INCLUDE += -I.
6+
7+
CC = gcc
8+
CFLAGS += -Wall -Wextra -Werror
9+
COVERAGE_FLAGS += -fprofile-arcs -ftest-coverage
10+
11+
12+
.PHONY: all clean
13+
14+
all: test_checksum
15+
16+
test_checksum: $(SRC) $(TEST_SRC)
17+
$(CC) $(CFLAGS) $(INCLUDE) -o $(PROJECT) $^
18+
19+
clean:
20+
rm -f $(PROJECT) *.gcda *.gcno *.gcov coverage_checksum.txt *.out
21+
22+
run: test_checksum
23+
./$(PROJECT)
24+
25+
coverage: $(SRC) $(TEST_SRC)
26+
$(CC) $(COVERAGE_FLAGS) $(CFLAGS) $(INCLUDE) -o $(PROJECT) $^
27+
./$(PROJECT)
28+
if [ -f Lab1-checksum.gcda ]; then mv Lab1-checksum.gcda checksum.gcda; fi
29+
if [ -f Lab1-checksum.gcno ]; then mv Lab1-checksum.gcno checksum.gcno; fi
30+
gcov -a checksum.c > coverage_checksum.txt

Lab1/checksum.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "checksum.h"
2+
3+
uint8_t calculate_checksum(uint8_t *buffer, uint8_t buffer_length)
4+
{
5+
uint16_t result_checksum = 0;
6+
uint8_t loop_index = 0;
7+
8+
for (loop_index = 0; loop_index < buffer_length; loop_index ++)
9+
{
10+
result_checksum += buffer[loop_index ];
11+
}
12+
return (uint8_t)(result_checksum % 256);
13+
}

Lab1/checksum.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @file checksum.h
3+
* @brief Header file contains the declarations for checksum module
4+
*
5+
*/
6+
7+
#ifndef CHECKSUM_H
8+
#define CHECKSUM_H
9+
10+
#include <stdint.h>
11+
12+
/**
13+
* @brief Function to calculate the checksum of a given buffer of data.
14+
* The checksum is computed by summing all the bytes in the buffer and taking the result modulo 256.
15+
*
16+
* @param buffer Buffer of data for which the checksum is to be calculated.
17+
* @param length Length of the buffer.
18+
* @return uint8_t The calculated checksum result.
19+
*/
20+
uint8_t calculate_checksum(uint8_t *buffer, uint8_t length);
21+
22+
#endif

Lab1/test_checksum.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "checksum.h"
2+
#include "unity.h"
3+
4+
void setUp(void)
5+
{
6+
/* This function is called before each test case.
7+
You can use it to set up any common test data or state. */
8+
}
9+
10+
void tearDown(void)
11+
{
12+
/* This function is called after each test case.
13+
You can use it to clean up any resources allocated in setUp or during the test. */
14+
}
15+
16+
void test_calculate_checksum_with_valid_data(void) {
17+
uint8_t data[] = {0x01, 0x02, 0x03, 0x04};
18+
uint8_t expected_checksum = 10; // (0x01 + 0x02 + 0x03 + 0x04) % 256 = 10
19+
uint8_t actual_checksum = calculate_checksum(data, sizeof(data));
20+
TEST_ASSERT_EQUAL_UINT8(expected_checksum, actual_checksum);
21+
}
22+
23+
void test_calculate_checksum_with_empty_data(void) {
24+
uint8_t data[] = {};
25+
uint8_t expected_checksum = 0;
26+
uint8_t actual_checksum = calculate_checksum(data, sizeof(data));
27+
TEST_ASSERT_EQUAL_UINT8(expected_checksum, actual_checksum);
28+
}
29+
30+
void test_calculate_checksum_with_max_values(void) {
31+
uint8_t data[] = {0xFF, 0xFF, 0xFF, 0xFF};
32+
uint8_t expected_checksum = 252; // (0xFF + 0xFF + 0xFF + 0xFF) % 256 = 252
33+
uint8_t actual_checksum = calculate_checksum(data, sizeof(data));
34+
TEST_ASSERT_EQUAL_UINT8(expected_checksum, actual_checksum);
35+
}
36+
37+
int main(void) {
38+
UNITY_BEGIN();
39+
RUN_TEST(test_calculate_checksum_with_valid_data);
40+
RUN_TEST(test_calculate_checksum_with_empty_data);
41+
RUN_TEST(test_calculate_checksum_with_max_values);
42+
return UNITY_END();
43+
}
44+

0 commit comments

Comments
 (0)