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

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

add_executable(test_stack test_stack.c)
target_link_libraries(test_stack 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_push_pop benchmark_push_pop.c)
target_link_libraries(benchmark_push_pop stack)
50 changes: 50 additions & 0 deletions 05_hw/benchmark_push_pop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "stack.h"

// ������� ��� ���������� ���������� push
void benchmark_push(Stack* stack, size_t test_size) {
clock_t start = clock();
for (size_t i = 0; i < test_size; i++) {
push(stack, (int)i);
}
clock_t end = clock();
printf("Push %zu: %.6f seconds\n", test_size, (double)(end - start) / CLOCKS_PER_SEC);
}

// ������� ��� ���������� ���������� pop
void benchmark_pop(Stack* stack, size_t test_size) {
clock_t start = clock();
for (size_t i = 0; i < test_size; i++) {
pop(stack);
}
clock_t end = clock();
printf("Pop %zu: %.6f seconds\n", test_size, (double)(end - start) / CLOCKS_PER_SEC);
}

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

// ������ � ������� �������� ������
size_t test_sizes[] = { 1000000, 10000000 };

for (int i = 0; i < 3; i++) {
size_t test_size = test_sizes[i];

// Benchmark push
benchmark_push(&stack, test_size);

// Refill stack for pop benchmark
for (size_t j = 0; j < test_size; j++) {
push(&stack, (int)j);
}

// Benchmark pop
benchmark_pop(&stack, test_size);
}

destroyStack(&stack);
return 0;
}
4 changes: 2 additions & 2 deletions 05_hw/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,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
18 changes: 11 additions & 7 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 All @@ -57,7 +63,7 @@ Node* searchByIndex(Stack* stack, int index) {
return NULL;
}

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

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

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

}
4 changes: 2 additions & 2 deletions 05_hw/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ Node* searchByValue(Stack* stack, int value);

Node* searchByIndex(Stack* stack, int index);

Node* getTop(Stack* stack);
Node* getTop(const Stack* stack);

void traverseStack(Stack* stack);

bool isEmpty(Stack* stack);
bool isEmpty(const Stack* stack);

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

void test_push() {
Stack stack;
initStack(&stack);

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

// ���������, ��� ������� ������� ����� - ��� 30
const Node* top = getTop(&stack);
assert(top != NULL && top->data == 30);

destroyStack(&stack);
printf("test_push: SUCCESS\n");
}

void test_pop() {
Stack stack;
initStack(&stack);

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

pop(&stack);
const Node* top = getTop(&stack);
assert(top != NULL && top->data == 20);

pop(&stack);
top = getTop(&stack);
assert(top != NULL && top->data == 10);

pop(&stack);
assert(isEmpty(&stack));

destroyStack(&stack);
printf("test_pop: SUCCESS\n");
}

void test_isEmpty() {
Stack stack;
initStack(&stack);

assert(isEmpty(&stack));

push(&stack, 10);
assert(!isEmpty(&stack));

pop(&stack);
assert(isEmpty(&stack));

destroyStack(&stack);
printf("test_isEmpty: SUCCESS\n");
}

void test_searchByValue() {
Stack stack;
initStack(&stack);

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

const Node* result = searchByValue(&stack, 20);
assert(result != NULL && result->data == 20);

result = searchByValue(&stack, 40);
assert(result == NULL);

destroyStack(&stack);
printf("test_searchByValue: SUCCESS\n");
}

void test_searchByIndex() {
Stack stack;
initStack(&stack);

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

const Node* result = searchByIndex(&stack, 0);
assert(result != NULL && result->data == 30);

result = searchByIndex(&stack, 2);
assert(result != NULL && result->data == 10);

result = searchByIndex(&stack, 3);
assert(result == NULL);

destroyStack(&stack);
printf("test_searchByIndex: SUCCESS\n");
}

void test_getTop() {
Stack stack;
initStack(&stack);

push(&stack, 10);
push(&stack, 20);

const Node* top = getTop(&stack);
assert(top != NULL && top->data == 20);

pop(&stack);
top = getTop(&stack);
assert(top != NULL && top->data == 10);

pop(&stack);
top = getTop(&stack);
assert(top == NULL);

destroyStack(&stack);
printf("test_getTop: SUCCESS\n");
}

void test_destroyStack() {
Stack stack;
initStack(&stack);

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

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

printf("test_destroyStack: SUCCESS\n");
}

void test_createNode() {
Node* node = createNode(42);
assert(node != NULL && node->data == 42 && node->next == NULL);
free(node);
printf("test_createNode: SUCCESS\n");
}

void test_initStack() {
Stack stack;
initStack(&stack);
assert(stack.top == NULL);
printf("test_initStack: SUCCESS\n");
}

void test_traverseStack() {
Stack stack;
initStack(&stack);

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

printf("Expected output: Stack elements: 30 20 10\n");
printf("Actual output: ");
traverseStack(&stack);

destroyStack(&stack);
printf("test_traverseStack: SUCCESS\n");
}

int main() {
printf("Running tests...\n");

test_createNode();
test_initStack();
test_destroyStack();
test_push();
test_pop();
test_searchByValue();
test_searchByIndex();
test_getTop();
test_traverseStack();
test_isEmpty();

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

return 0;
}