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 benchmark.c)
target_link_libraries(benchmark stack)
50 changes: 50 additions & 0 deletions 05_hw/benchmark.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;
}
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;
}

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

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

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

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

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

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

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

pop(&stack);
assert(getTop(&stack)->data == 25);

pop(&stack);
assert(getTop(&stack)->data == 15);

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

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

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

assert(isEmpty(&stack)); // ���� ���� ��� �������������

push(&stack, 42);
assert(!isEmpty(&stack)); // ���� ������ �� ����

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

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

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

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

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

found = searchByValue(&stack, 99);
assert(found == NULL);

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

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

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

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

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

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

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

push(&stack, 8);
push(&stack, 18);

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

pop(&stack);
assert(getTop(&stack)->data == 8);

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

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

push(&stack, 1);
push(&stack, 2);

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

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

// ���� ��� �������� ����
void test_createNode() {
Node* node = createNode(100);
assert(node != NULL && node->data == 100 && node->next == NULL);

free(node);
printf("test_createNode: PASSED\n");
}

// ���� ��� ������������� �����
void test_initStack() {
Stack stack;
initStack(&stack);
assert(stack.top == NULL);

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

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

push(&stack, 3);
push(&stack, 6);
push(&stack, 9);

printf("Expected output: 9 6 3\n");
printf("Actual output: ");
traverseStack(&stack);

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

// ������� ������� ��� ������� ���� ������
int main() {
printf("Starting test suite...\n");

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

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