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(test test.c)
target_link_libraries(test 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")

add_executable(benchmark_pp benchmark_pp.c)
target_link_libraries(benchmark_pp stack)

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

// ������� ��� ��������� ������� ���������� �������� push
void measurePushPerformance(Stack* stack, size_t elementCount) {
clock_t startTime = clock();
for (size_t i = 0; i < elementCount; ++i) {
push(stack, (int)i);
}
clock_t endTime = clock();
printf("Push operation for %zu elements took: %.6f seconds\n", elementCount, (double)(endTime - startTime) / CLOCKS_PER_SEC);
}

// ������� ��� ��������� ������� ���������� �������� pop
void measurePopPerformance(Stack* stack, size_t elementCount) {
clock_t startTime = clock();
for (size_t i = 0; i < elementCount; ++i) {
pop(stack);
}
clock_t endTime = clock();
printf("Pop operation for %zu elements took: %.6f seconds\n", elementCount, (double)(endTime - startTime) / CLOCKS_PER_SEC);
}

int main() {
Stack stack;
initStack(&stack);

// ���������� ��������� ��� ������������
size_t testElementCount = 1500000;

// ��������� ������������������ �������� push
measurePushPerformance(&stack, testElementCount);

// ���������� ����� ��� ������������ pop
for (size_t i = 0; i < testElementCount; ++i) {
push(&stack, (int)i);
}

// ��������� ������������������ �������� pop
measurePopPerformance(&stack, testElementCount);

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

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

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 +45,7 @@ Node* searchByValue(Stack* stack, int value) {
if (current->data == value) {
return current;
}
current = current->next; // ��������� ���������
}
return NULL;
}
Expand Down Expand Up @@ -72,7 +78,5 @@ void traverseStack(Stack* stack) {
}

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

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

void verifyPushFunctionality() {
Stack testStack;
initStack(&testStack);

push(&testStack, 15);
push(&testStack, 25);
push(&testStack, 35);

const Node* topNode = getTop(&testStack);
assert(topNode && topNode->data == 35);

destroyStack(&testStack);
printf("verifyPushFunctionality: PASSED\n");
}

void verifyPopFunctionality() {
Stack testStack;
initStack(&testStack);

push(&testStack, 5);
push(&testStack, 15);
push(&testStack, 25);

pop(&testStack);
const Node* topNode = getTop(&testStack);
assert(topNode && topNode->data == 15);

pop(&testStack);
topNode = getTop(&testStack);
assert(topNode && topNode->data == 5);

pop(&testStack);
assert(isEmpty(&testStack));

destroyStack(&testStack);
printf("verifyPopFunctionality: PASSED\n");
}

void verifyEmptyStackBehavior() {
Stack testStack;
initStack(&testStack);

assert(isEmpty(&testStack));

push(&testStack, 100);
assert(!isEmpty(&testStack));

pop(&testStack);
assert(isEmpty(&testStack));

destroyStack(&testStack);
printf("verifyEmptyStackBehavior: PASSED\n");
}

void verifyValueSearch() {
Stack testStack;
initStack(&testStack);

push(&testStack, 10);
push(&testStack, 20);
push(&testStack, 30);

const Node* foundNode = searchByValue(&testStack, 20);
assert(foundNode && foundNode->data == 20);

foundNode = searchByValue(&testStack, 40);
assert(foundNode == NULL);

destroyStack(&testStack);
printf("verifyValueSearch: PASSED\n");
}

void verifyIndexSearch() {
Stack testStack;
initStack(&testStack);

push(&testStack, 10);
push(&testStack, 20);
push(&testStack, 30);

const Node* foundNode = searchByIndex(&testStack, 0);
assert(foundNode && foundNode->data == 30);

foundNode = searchByIndex(&testStack, 2);
assert(foundNode && foundNode->data == 10);

foundNode = searchByIndex(&testStack, 3);
assert(foundNode == NULL);

destroyStack(&testStack);
printf("verifyIndexSearch: PASSED\n");
}

void verifyTopRetrieval() {
Stack testStack;
initStack(&testStack);

push(&testStack, 10);
push(&testStack, 15);

const Node* topNode = getTop(&testStack);
assert(topNode && topNode->data == 15);

pop(&testStack);
topNode = getTop(&testStack);
assert(topNode && topNode->data == 10);

pop(&testStack);
topNode = getTop(&testStack);
assert(topNode == NULL);

destroyStack(&testStack);
printf("verifyTopRetrieval: PASSED\n");
}

void verifyStackDestruction() {
Stack testStack;
initStack(&testStack);

push(&testStack, 10);
push(&testStack, 20);
push(&testStack, 30);

destroyStack(&testStack);
assert(getTop(&testStack) == NULL);

printf("verifyStackDestruction: PASSED\n");
}

void verifyNodeCreation() {
Node* newNode = createNode(50);
assert(newNode && newNode->data == 50 && newNode->next == NULL);
free(newNode);
printf("verifyNodeCreation: PASSED\n");
}

void verifyStackInitialization() {
Stack testStack;
initStack(&testStack);
assert(testStack.top == NULL);
printf("verifyStackInitialization: PASSED\n");
}

void verifyStackTraversal() {
Stack testStack;
initStack(&testStack);

push(&testStack, 15);
push(&testStack, 25);
push(&testStack, 35);

printf("Expected output: Stack contents: 35 25 15\n");
printf("Actual output: ");
traverseStack(&testStack);

destroyStack(&testStack);
printf("verifyStackTraversal: PASSED\n");
}

int main() {
printf("Executing test suite...\n");

verifyNodeCreation();
verifyStackInitialization();
verifyStackDestruction();
verifyPushFunctionality();
verifyPopFunctionality();
verifyValueSearch();
verifyIndexSearch();
verifyTopRetrieval();
verifyStackTraversal();
verifyEmptyStackBehavior();

printf("All tests completed successfully.\n");

return 0;
}