Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: C/C++ CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- name: checkout
uses: actions/checkout@v4


- name: get gcc
run: |
sudo apt-get update
sudo apt-get install -y build-essential gcc

- name: tests
run: |
gcc -fsanitize=address 05_hw/tests.c -o tests.exe
./tests.exe





73 changes: 73 additions & 0 deletions 05_hw/benchmarks.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "stack.h"
#include "stack.c"

void benchmark_push(Stack* stack, int ITEMS){
//Start the clock
clock_t start = clock();

//Push X elements
for (int i = 0; i < ITEMS; i++){
push(stack, i);
}

//Stop the clock
clock_t end = clock();

//Calculate the time
float benchmark_time = (float)(end - start) / CLOCKS_PER_SEC;

printf("Time taken to complete push benchmark (%d elements): %.5f seconds\n", ITEMS, benchmark_time);

}

void benchmark_pop(Stack* stack, int ITEMS){

//Start the clock
clock_t start = clock();


//Pop X elements
for (int i = 0; i < ITEMS; i++){
pop(stack);
}

//Stop the clock
clock_t end = clock();

//Calculate the time
float benchmark_time = (float)(end - start) / CLOCKS_PER_SEC;

printf("Time taken to complete pop benchmark (%d elements): %.5f seconds\n", ITEMS, benchmark_time);

}

int main(){

//Test with different num of elements
int NUMBER_TESTS[] = {1000, 10000, 1000000};


//Do 3 benchmarks
for (int i = 0; i < 3; i++){
//Stack init
Stack stack;
initStack(&stack);

benchmark_push(&stack, NUMBER_TESTS[i]);

for (int i = 0; i < NUMBER_TESTS[i]; i++){
push(&stack, i);
}

benchmark_pop(&stack, NUMBER_TESTS[i]);

destroyStack(&stack);
}

return 0;

}
5 changes: 3 additions & 2 deletions 05_hw/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "stack.h"

#include <stdio.h>
#include "stack.c"

int main() {
Stack stack;
Expand All @@ -18,14 +19,14 @@ int main() {
printf("After popping an element:\n");
traverseStack(&stack);

Node* searchResult = searchByValue(&stack, 20);
const Node* searchResult = searchByValue(&stack, 20);
if (searchResult != NULL) {
printf("Element with value 20 found.\n");
} else {
printf("Element with value 20 not found.\n");
}

Node* topElement = getTop(&stack);
const Node* topElement = getTop(&stack);
if (topElement != NULL) {
printf("Top element: %d\n", topElement->data);
}
Expand Down
17 changes: 17 additions & 0 deletions 05_hw/report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Running: cppcheck --enable=all --template={"file":"{file}","line":{line},"column":{column},"severity":"{severity}","message":"{message}","id":"{id}"}, c:\Users\User\Desktop\works\lecture-testing-task-main\05_hw\main.c
Checking c:\Users\User\Desktop\works\lecture-testing-task-main\05_hw\main.c ...

{"file":"c:\Users\User\Desktop\works\lecture-testing-task-main\05_hw\stack.h","line":3,"column":0,"severity":"information","message":"Include file: <stdbool.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.","id":"missingIncludeSystem"},
{"file":"c:\Users\User\Desktop\works\lecture-testing-task-main\05_hw\main.c","line":3,"column":0,"severity":"information","message":"Include file: <stdio.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.","id":"missingIncludeSystem"},
{"file":"c:\Users\User\Desktop\works\lecture-testing-task-main\05_hw\stack.c","line":1,"column":0,"severity":"information","message":"Include file: <stdio.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.","id":"missingIncludeSystem"},
{"file":"c:\Users\User\Desktop\works\lecture-testing-task-main\05_hw\stack.c","line":2,"column":0,"severity":"information","message":"Include file: <stdlib.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.","id":"missingIncludeSystem"},

{"file":"c:\Users\User\Desktop\works\lecture-testing-task-main\05_hw\main.c","line":22,"column":11,"severity":"style","message":"Variable 'searchResult' can be declared as pointer to const","id":"constVariablePointer"},
{"file":"c:\Users\User\Desktop\works\lecture-testing-task-main\05_hw\main.c","line":29,"column":11,"severity":"style","message":"Variable 'topElement' can be declared as pointer to const","id":"constVariablePointer"},
{"file":"c:\Users\User\Desktop\works\lecture-testing-task-main\05_hw\stack.c","line":71,"column":21,"severity":"style","message":"Parameter 'stack' can be declared as pointer to const","id":"constParameterPointer"},
{"file":"c:\Users\User\Desktop\works\lecture-testing-task-main\05_hw\stack.c","line":85,"column":21,"severity":"style","message":"Parameter 'stack' can be declared as pointer to const","id":"constParameterPointer"},

{"file":"c:\Users\User\Desktop\works\lecture-testing-task-main\05_hw\stack.c","line":21,"column":6,"severity":"style","message":"The function 'destroyStack' is never used.","id":"unusedFunction"},
{"file":"c:\Users\User\Desktop\works\lecture-testing-task-main\05_hw\stack.c","line":58,"column":7,"severity":"style","message":"The function 'searchByIndex' is never used.","id":"unusedFunction"},
{"file":"c:\Users\User\Desktop\works\lecture-testing-task-main\05_hw\stack.c","line":85,"column":6,"severity":"style","message":"The function 'isEmpty' is never used.","id":"unusedFunction"},
{"file":"c:\Users\User\Desktop\works\lecture-testing-task-main\05_hw\stack.c","line":6,"column":7,"severity":"style","message":"The function 'createNode' should have static linkage since it is not used outside of its translation unit.","id":"staticFunction"},
18 changes: 14 additions & 4 deletions 05_hw/stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL){
printf("malloc failure");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
Expand All @@ -17,10 +21,11 @@ void initStack(Stack* stack) {
void destroyStack(Stack* stack) {
Node* current = stack->top;
while (current != NULL) {
Node *tmp = current;
Node* tmp = current;
current = current->next;
free(tmp);
}
stack->top = NULL;
}

void push(Stack* stack, int data) {
Expand All @@ -30,8 +35,13 @@ void push(Stack* stack, int data) {
}

void pop(Stack* stack) {
if (stack->top == NULL){
printf("Attempting to pop an empty stack\n");
return;
}
Node* temp = stack->top;
stack->top = stack->top->next;
free(temp);
}

Node* searchByValue(Stack* stack, int value) {
Expand All @@ -40,6 +50,7 @@ Node* searchByValue(Stack* stack, int value) {
if (current->data == value) {
return current;
}
current = current->next;
}
return NULL;
}
Expand All @@ -57,7 +68,7 @@ Node* searchByIndex(Stack* stack, int index) {
return NULL;
}

Node* getTop(Stack* stack) {
Node* getTop(Stack* const stack) {
return stack->top;
}

Expand All @@ -71,8 +82,7 @@ void traverseStack(Stack* stack) {
printf("\n");
}

bool isEmpty(Stack* stack) {
free(stack->top);
bool isEmpty(Stack* const stack) {
return stack->top == NULL;
}

2 changes: 1 addition & 1 deletion 05_hw/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Node* createNode(int data);

void initStack(Stack* stack);

void destroyStack(Stack *stack);
void destroyStack(Stack* stack);

void push(Stack* stack, int data);

Expand Down
Loading