Skip to content

Commit d4f5a88

Browse files
committed
Added Lab1 & Lab2 files and disabled bot commits
1 parent 8173e38 commit d4f5a88

12 files changed

Lines changed: 4013 additions & 83 deletions

File tree

.github/workflows/build.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ jobs:
2121
run: |
2222
cd Lab1
2323
./Lab1
24+
- name: Run unit tests
25+
run: |
26+
cd Lab2
27+
./Lab2
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# name: Tests & Coverage
2+
3+
# on:
4+
# push:
5+
# branches:
6+
# - main
7+
8+
# permissions:
9+
# contents: write
10+
11+
# jobs:
12+
# test-and-coverage:
13+
# runs-on: ubuntu-latest
14+
# steps:
15+
# - uses: actions/checkout@v3
16+
# - name: Install dependencies
17+
# run: sudo apt-get update && sudo apt-get install -y make gcc build-essential gcovr
18+
19+
# - name: Build and run unit tests
20+
# id: runtest
21+
# run: |
22+
# cd Lab1
23+
# make test_checksum
24+
# OUTPUT=$(./Lab1 2>&1)
25+
# echo "$OUTPUT"
26+
# # Parse Unity output: "X Tests Y Failures Z Ignored"
27+
# TESTS=$(echo "$OUTPUT" | grep -oP '\d+(?=\s+Tests)' | head -1 || echo "0")
28+
# FAILURES=$(echo "$OUTPUT" | grep -oP '\d+(?=\s+Failures)' | head -1 || echo "0")
29+
# PASSED=$((TESTS - FAILURES))
30+
# echo "tests=$TESTS" >> $GITHUB_OUTPUT
31+
# echo "passed=$PASSED" >> $GITHUB_OUTPUT
32+
# echo "failed=$FAILURES" >> $GITHUB_OUTPUT
33+
34+
# - name: Run coverage analysis
35+
# id: runcov
36+
# run: |
37+
# cd Lab1
38+
# make coverage
39+
# PCT=$(grep 'Lines executed' coverage_checksum.txt | head -1 | sed -E 's/.*:([0-9.]+)%.*$/\1/')
40+
# echo "coverage=$PCT" >> $GITHUB_OUTPUT
41+
42+
# - name: Update README badges
43+
# if: success()
44+
# run: |
45+
# TESTS=${{ steps.runtest.outputs.tests }}
46+
# PASSED=${{ steps.runtest.outputs.passed }}
47+
# FAILED=${{ steps.runtest.outputs.failed }}
48+
# PCT=${{ steps.runcov.outputs.coverage }}
49+
50+
# # Determine test badge color
51+
# if [ "$FAILED" -eq 0 ] && [ "$PASSED" -gt 0 ]; then TEST_COLOR="brightgreen"; else TEST_COLOR="red"; fi
52+
53+
# # Determine coverage badge color based on percentage
54+
# if (( $(echo "$PCT >= 80" | bc -l) )); then COVERAGE_COLOR="brightgreen"; elif (( $(echo "$PCT >= 60" | bc -l) )); then COVERAGE_COLOR="yellowgreen"; elif (( $(echo "$PCT >= 40" | bc -l) )); then COVERAGE_COLOR="orange"; else COVERAGE_COLOR="red"; fi
55+
56+
# # Update both badges in README
57+
# sed -i "s|badge/tests-[^-]*-[a-z]*|badge/tests-${PASSED}/${TESTS}-${TEST_COLOR}|" README.md
58+
# sed -i "s|badge/coverage-[^-]*-[a-z]*|badge/coverage-${PCT}%25-${COVERAGE_COLOR}|" README.md
59+
60+
# # Check if README actually changed
61+
# if git diff --quiet README.md; then
62+
# echo "Badges unchanged, skipping commit and push"
63+
# else
64+
# echo "Badges updated, committing changes"
65+
# git config user.name "github-actions[bot]"
66+
# git config user.email "github-actions[bot]@users.noreply.github.com"
67+
# git add README.md
68+
# git commit -m "Update test (${PASSED}/${TESTS}) and coverage (${PCT}%) badges"
69+
# git push
70+
# fi

.github/workflows/test-and-coverage.yml

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

Lab1/Makefile

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,3 @@ clean:
2121

2222
run: test_checksum
2323
./$(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

Lab2/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

Lab2/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+
}

Lab2/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

Lab2/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)