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
7 changes: 0 additions & 7 deletions 05_hw/CMakeLists.txt

This file was deleted.

51 changes: 51 additions & 0 deletions 05_hw/benchmark.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <stdio.h>
#include <time.h> // Для clock()
#include "stack.h"

// Бенчмарк для функции push
void benchmark_push(Stack* stack, int num_operations) {
clock_t start, end;
double cpu_time_used;

start = clock(); // Засекаем время начала
for (int i = 0; i < num_operations; i++) {
push(stack, i); // Выполняем push
}
end = clock(); // Засекаем время окончания

cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("Push benchmark: %d operations took %f seconds\n", num_operations, cpu_time_used);
}

// Бенчмарк для функции pop
void benchmark_pop(Stack* stack, int num_operations) {
clock_t start, end;
double cpu_time_used;

// Предварительно заполняем стек
for (int i = 0; i < num_operations; i++) {
push(stack, i);
}

start = clock(); // Засекаем время начала
for (int i = 0; i < num_operations; i++) {
pop(stack); // Выполняем pop
}
end = clock(); // Засекаем время окончания

cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("Pop benchmark: %d operations took %f seconds\n", num_operations, cpu_time_used);
}

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

int num_operations = 1000000; // Количество операций

benchmark_push(&stack, num_operations);
benchmark_pop(&stack, num_operations);

destroyStack(&stack);
return 0;
}
4 changes: 1 addition & 3 deletions 05_hw/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,4 @@ int main() {
}

return 0;
}


}
20 changes: 15 additions & 5 deletions 05_hw/stack.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#include "stack.h"

Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
Expand All @@ -17,10 +22,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 +36,13 @@ void push(Stack* stack, int data) {
}

void pop(Stack* stack) {
if (stack->top == NULL) {
fprintf(stderr, "Stack is empty, cannot pop\n");
return;
}
Node* temp = stack->top;
stack->top = stack->top->next;
free(temp);
}

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

bool isEmpty(Stack* stack) {
free(stack->top);
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 @@ -37,4 +37,4 @@ bool isEmpty(Stack* stack);

#ifdef __cplusplus
}
#endif
#endif
108 changes: 108 additions & 0 deletions 05_hw/tests_stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <stdlib.h>
#include "unity.h"
#include "stack.h"

void setUp(void) {
// Установка начального состояния перед каждым тестом
}

void tearDown(void) {
// Очистка после каждого теста
}

void test_createNode(void) {
Node* node = createNode(10);
TEST_ASSERT_NOT_NULL(node);
TEST_ASSERT_EQUAL(10, node->data);
TEST_ASSERT_NULL(node->next);
free(node);
}

void test_initStack(void) {
Stack stack;
initStack(&stack);
TEST_ASSERT_NULL(stack.top);
}

void test_push(void) {
Stack stack;
initStack(&stack);
push(&stack, 10);
TEST_ASSERT_NOT_NULL(stack.top);
TEST_ASSERT_EQUAL(10, stack.top->data);
}

void test_pop(void) {
Stack stack;
initStack(&stack);
push(&stack, 10);
pop(&stack);
TEST_ASSERT_NULL(stack.top);
}

void test_pop_empty_stack(void) {
Stack stack;
initStack(&stack);
pop(&stack); // Попытка извлечения из пустого стека
// Ожидаем, что программа не упадет
}

void test_searchByValue(void) {
Stack stack;
initStack(&stack);
push(&stack, 10);
push(&stack, 20);
Node* node = searchByValue(&stack, 10);
TEST_ASSERT_NOT_NULL(node);
TEST_ASSERT_EQUAL(10, node->data);
}

void test_searchByIndex(void) {
Stack stack;
initStack(&stack);
push(&stack, 10);
push(&stack, 20);
Node* node = searchByIndex(&stack, 1);
TEST_ASSERT_NOT_NULL(node);
TEST_ASSERT_EQUAL(10, node->data);
}

void test_getTop(void) {
Stack stack;
initStack(&stack);
push(&stack, 10);
Node* node = getTop(&stack);
TEST_ASSERT_NOT_NULL(node);
TEST_ASSERT_EQUAL(10, node->data);
}

void test_traverseStack(void) {
Stack stack;
initStack(&stack);
push(&stack, 10);
push(&stack, 20);
traverseStack(&stack); // Проверяем, что вывод корректный
}

void test_isEmpty(void) {
Stack stack;
initStack(&stack);
TEST_ASSERT_TRUE(isEmpty(&stack));
push(&stack, 10);
TEST_ASSERT_FALSE(isEmpty(&stack));
}

int main(void) {
UNITY_BEGIN();
RUN_TEST(test_createNode);
RUN_TEST(test_initStack);
RUN_TEST(test_push);
RUN_TEST(test_pop);
RUN_TEST(test_pop_empty_stack);
RUN_TEST(test_searchByValue);
RUN_TEST(test_searchByIndex);
RUN_TEST(test_getTop);
RUN_TEST(test_traverseStack);
RUN_TEST(test_isEmpty);
return UNITY_END();
}
Loading