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
10 changes: 10 additions & 0 deletions 05_hw/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,13 @@ add_library(stack stack.c)

add_executable(main main.c)
target_link_libraries(main stack)

add_executable(tests tests.c)
target_link_libraries(tests stack)

add_executable(benchmarks benchmarks.c)
target_link_libraries(benchmarks stack)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage -g")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")

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

// Функция замера времени для функций с двумя параметрами
double measureTime(void (*func)(Stack*, int), Stack* stack, int data, int iterations) {
clock_t start, end;
start = clock();
for (int i = 0; i < iterations; i++) {
func(stack, data); // Передаем данные в функцию
}
end = clock();
return ((double)(end - start)) / CLOCKS_PER_SEC;
}

// Обертка для pop, чтобы соответствовать ожидаемой сигнатуре
void popWrapper(Stack* stack, int data) {
pop(stack); // Просто вызываем pop без второго аргумента
}

// Бенчмарк для push с разными данными
void benchmarkPush(int iterations) {
Stack stack;
initStack(&stack);

// Пример с разными наборами данных
for (int data = 1; data <= 5; data++) { // Пробуем вставлять данные от 1 до 5
double timeTaken = measureTime(push, &stack, data, iterations);
printf("Push Time for data %d with %d iterations: %f seconds\n", data, iterations, timeTaken);
}
}

// Бенчмарк для pop с разными объемами стека
void benchmarkPop(int iterations) {
Stack stack;
initStack(&stack);

// Наполняем стек
for (int data = 1; data <= 5; data++) {
for (int i = 0; i < iterations * data; i++) {
push(&stack, i); // Наполняем стек с данными
}

double timeTaken = measureTime(popWrapper, &stack, 0, iterations);
printf("Pop Time for stack with %d elements and %d iterations: %f seconds\n", iterations * data, iterations, timeTaken);
}
}

int main() {
int iterations = 100000; // Количество операций

benchmarkPush(iterations);
benchmarkPop(iterations);

return 0;
}
12 changes: 9 additions & 3 deletions 05_hw/stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,27 @@ 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);
free(tmp);
}
stack->top = NULL;
}


void push(Stack* stack, int data) {
Node* newNode = createNode(data);
newNode->next = stack->top;
stack->top = newNode;
}

void pop(Stack* stack) {
if (stack->top == NULL) {
return;
}
Node* temp = stack->top;
stack->top = stack->top->next;
free(temp);
}

Node* searchByValue(Stack* stack, int value) {
Expand All @@ -40,6 +46,7 @@ Node* searchByValue(Stack* stack, int value) {
if (current->data == value) {
return current;
}
current = current->next;
}
return NULL;
}
Expand Down Expand Up @@ -72,7 +79,6 @@ void traverseStack(Stack* stack) {
}

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

110 changes: 110 additions & 0 deletions 05_hw/tests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include <stdio.h>
#include "stack.h"
#include <assert.h>

void testPush() {
Stack stack;
initStack(&stack);
push(&stack, 1);
push(&stack, 2);
push(&stack, 3);
assert(getTop(&stack)->data == 3);
printf("testPush passed.\n");
}

void testPop() {
Stack stack;
initStack(&stack);
push(&stack, 1);
push(&stack, 2);
pop(&stack);
assert(getTop(&stack)->data == 1);
pop(&stack);
assert(isEmpty(&stack));
printf("testPop passed.\n");
}

void testSearchByValue() {
Stack stack;
initStack(&stack);
push(&stack, 1);
push(&stack, 2);
push(&stack, 3);
assert(searchByValue(&stack, 2) != NULL);
assert(searchByValue(&stack, 4) == NULL);
printf("testSearchByValue passed.\n");
}

void testSearchByIndex() {
Stack stack;
initStack(&stack);
push(&stack, 1);
push(&stack, 2);
push(&stack, 3);
assert(searchByIndex(&stack, 0)->data == 3);
assert(searchByIndex(&stack, 1)->data == 2);
assert(searchByIndex(&stack, 2)->data == 1);
assert(searchByIndex(&stack, 3) == NULL);
printf("testSearchByIndex passed.\n");
}

void testDestroyStack() {
Stack stack;
initStack(&stack);
push(&stack, 1);
push(&stack, 2);
destroyStack(&stack);
assert(isEmpty(&stack));
printf("testDestroyStack passed.\n");
}

void testIsEmpty() {
Stack stack;
initStack(&stack);
assert(isEmpty(&stack));
push(&stack, 1);
assert(!isEmpty(&stack));
pop(&stack);
assert(isEmpty(&stack));
printf("testIsEmpty passed.\n");
}

void testGetTop() {
Stack stack;
initStack(&stack);
assert(getTop(&stack) == NULL);
push(&stack, 42);
assert(getTop(&stack)->data == 42);
push(&stack, 99);
assert(getTop(&stack)->data == 99);
printf("testGetTop passed.\n");
}

void testTraverseStack() {
Stack stack;
initStack(&stack);
printf("Expected output: Stack elements: \nActual output: ");
traverseStack(&stack);

push(&stack, 5);
push(&stack, 10);
push(&stack, 15);

printf("Expected output: Stack elements: 15 10 5\nActual output: ");
traverseStack(&stack);
printf("testTraverseStack passed.\n");
}


int main() {
testPush();
testPop();
testSearchByValue();
testSearchByIndex();
testDestroyStack();
testIsEmpty();
testGetTop();
testTraverseStack();
printf("All tests passed!\n");
return 0;
}