Skip to content

Commit

Permalink
Савчук Антон. Лабораторная работа 3 (#366)
Browse files Browse the repository at this point in the history
* Add files via upload

* cpplint fix

* cpplint fix 2

* upd

* ops upd fix

* Delete modules/bodrov_daniil_fibonacci_heap/src/CMakeList.txt

* ???
  • Loading branch information
savchukPR2 authored May 30, 2024
1 parent b7c0397 commit 107fea4
Show file tree
Hide file tree
Showing 8 changed files with 344 additions and 0 deletions.
17 changes: 17 additions & 0 deletions modules/bodrov_daniil_fibonacci_heap/CMakeList.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Declare variables for binaries' names
get_filename_component(DIR_NAME ${CMAKE_CURRENT_LIST_DIR} NAME)
set(MODULE "${DIR_NAME}")
set(LIBRARY "lib_${MODULE}")
set(TESTS "test_${MODULE}")
set(APPLICATION "app_${MODULE}")

# Include directory with public headers
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

# Add all submodules
add_subdirectory(src)
add_subdirectory(test)
add_subdirectory(application)


include("CTestTests.txt")
72 changes: 72 additions & 0 deletions modules/bodrov_daniil_fibonacci_heap/CMakeTests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Set the prefix for your tests
set(prefix "fibonacci_heap")

# Test the run() function
add_test(
NAME ${prefix}_run
COMMAND ${APPLICATION}
)
set_tests_properties(${prefix}_run PROPERTIES
PASS_REGULAR_EXPRESSION "Fibonacci Heap Operations:"
LABELS "${prefix}"
)

# Test the insert operation
add_test(
NAME ${prefix}_insert
COMMAND ${APPLICATION} --insert
)
set_tests_properties(${prefix}_insert PROPERTIES
PASS_REGULAR_EXPRESSION "Element inserted successfully."
LABELS "${prefix}"
)

# Test the extract minimum operation
add_test(
NAME ${prefix}_extract_min
COMMAND ${APPLICATION} --extract
)
set_tests_properties(${prefix}_extract_min PROPERTIES
PASS_REGULAR_EXPRESSION "Minimum element extracted:"
LABELS "${prefix}"
)

# Test the decrease key operation
add_test(
NAME ${prefix}_decrease_key
COMMAND ${APPLICATION} --decrease 0 10
)
set_tests_properties(${prefix}_decrease_key PROPERTIES
PASS_REGULAR_EXPRESSION "Key decreased successfully."
LABELS "${prefix}"
)

# Test the delete node operation
add_test(
NAME ${prefix}_delete_node
COMMAND ${APPLICATION} --delete 0
)
set_tests_properties(${prefix}_delete_node PROPERTIES
PASS_REGULAR_EXPRESSION "Node deleted successfully."
LABELS "${prefix}"
)

# Test the merge heaps operation
add_test(
NAME ${prefix}_merge_heaps
COMMAND ${APPLICATION} --merge 2 10 20
)
set_tests_properties(${prefix}_merge_heaps PROPERTIES
PASS_REGULAR_EXPRESSION "Heaps merged successfully."
LABELS "${prefix}"
)

# Test the invalid input
add_test(
NAME ${prefix}_invalid_input
COMMAND ${APPLICATION} --invalid
)
set_tests_properties(${prefix}_invalid_input PROPERTIES
PASS_REGULAR_EXPRESSION "Invalid choice. Please try again."
LABELS "${prefix}"
)
72 changes: 72 additions & 0 deletions modules/bodrov_daniil_fibonacci_heap/CTestTests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Set the prefix for your tests
set(prefix "fibonacci_heap")

# Test the run() function
add_test(
NAME ${prefix}_run
COMMAND ${APPLICATION}
)
set_tests_properties(${prefix}_run PROPERTIES
PASS_REGULAR_EXPRESSION "Fibonacci Heap Operations:"
LABELS "${prefix}"
)

# Test the insert operation
add_test(
NAME ${prefix}_insert
COMMAND ${APPLICATION} 1
)
set_tests_properties(${prefix}_insert PROPERTIES
PASS_REGULAR_EXPRESSION "Element inserted successfully."
LABELS "${prefix}"
)

# Test the extract minimum operation
add_test(
NAME ${prefix}_extract_min
COMMAND ${APPLICATION} 2
)
set_tests_properties(${prefix}_extract_min PROPERTIES
PASS_REGULAR_EXPRESSION "Minimum element extracted:"
LABELS "${prefix}"
)

# Test the decrease key operation
add_test(
NAME ${prefix}_decrease_key
COMMAND ${APPLICATION} 3 0 10
)
set_tests_properties(${prefix}_decrease_key PROPERTIES
PASS_REGULAR_EXPRESSION "Key decreased successfully."
LABELS "${prefix}"
)

# Test the delete node operation
add_test(
NAME ${prefix}_delete_node
COMMAND ${APPLICATION} 4 0
)
set_tests_properties(${prefix}_delete_node PROPERTIES
PASS_REGULAR_EXPRESSION "Node deleted successfully."
LABELS "${prefix}"
)

# Test the merge heaps operation
add_test(
NAME ${prefix}_merge_heaps
COMMAND ${APPLICATION} 5 2 10 20
)
set_tests_properties(${prefix}_merge_heaps PROPERTIES
PASS_REGULAR_EXPRESSION "Heaps merged successfully."
LABELS "${prefix}"
)

# Test the invalid input
add_test(
NAME ${prefix}_invalid_input
COMMAND ${APPLICATION} 6
)
set_tests_properties(${prefix}_invalid_input PROPERTIES
PASS_REGULAR_EXPRESSION "Invalid choice. Please try again."
LABELS "${prefix}"
)
15 changes: 15 additions & 0 deletions modules/bodrov_daniil_fibonacci_heap/application/CMakeList.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
set(target ${APPLICATION})

file(GLOB srcs "*.cpp")
set_source_files_properties(${srcs} PROPERTIES
LABELS "${MODULE};Application")

add_executable(${target} ${srcs})
set_target_properties(${target} PROPERTIES
OUTPUT_NAME ${MODULE}
LABELS "${MODULE};Application")

target_link_libraries(${target} ${LIBRARY})
if (UNIX)
target_link_libraries(${target} ${CMAKE_THREAD_LIBS_INIT})
endif (UNIX)
10 changes: 10 additions & 0 deletions modules/bodrov_daniil_fibonacci_heap/application/app.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2024 Savchuk Anton

#include <iostream>
#include "include/console.h"

int main() {
Console console;
console.run();
return 0;
}
24 changes: 24 additions & 0 deletions modules/bodrov_daniil_fibonacci_heap/include/console.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2024 Savchuk Anton

#ifndef MODULES_BODROV_DANIIL_FIBONACCI_HEAP_INCLUDE_CONSOLE_H_
#define MODULES_BODROV_DANIIL_FIBONACCI_HEAP_INCLUDE_CONSOLE_H_

#include "include/fibonacci_heap.h"

class Console {
public:
void run();

private:
FibonacciHeap<int> heap;

void printMenu();
void handleUserInput();
void insertElement();
void extractMinimum();
void decreaseKey();
void deleteNode();
void mergeHeaps();
};

#endif // MODULES_BODROV_DANIIL_FIBONACCI_HEAP_INCLUDE_CONSOLE_H_
107 changes: 107 additions & 0 deletions modules/bodrov_daniil_fibonacci_heap/src/console.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright 2024 Savchuk Anton

#include <iostream>
#include <string>
#include "include/console.h"

void Console::run() {
while (true) {
printMenu();
handleUserInput();
}
}

void Console::printMenu() {
std::cout << "Fibonacci Heap Operations:" << std::endl;
std::cout << "--insert : Insert Element" << std::endl;
std::cout << "--extract : Extract Minimum" << std::endl;
std::cout << "--decrease : Decrease Key" << std::endl;
std::cout << "--delete : Delete Node" << std::endl;
std::cout << "--merge : Merge Heaps" << std::endl;
std::cout << "Enter your choice: ";
}

void Console::handleUserInput() {
std::string choice;
std::cin >> choice;

if (choice == "--insert") {
insertElement();
} else if (choice == "--extract") {
extractMinimum();
} else if (choice == "--decrease") {
decreaseKey();
} else if (choice == "--delete") {
deleteNode();
} else if (choice == "--merge") {
mergeHeaps();
} else {
std::cout << "Invalid choice. Please try again." << std::endl;
}
}

void Console::insertElement() {
int value;
std::cout << "Enter the value to insert: ";
std::cin >> value;
heap.insert(value);
std::cout << "Element inserted successfully." << std::endl;
}

void Console::extractMinimum() {
if (heap.empty()) {
std::cout << "Heap is empty. Cannot extract minimum." << std::endl;
return;
}
int minValue = heap.extractMin();
std::cout << "Minimum element extracted: " << minValue << std::endl;
}

void Console::decreaseKey() {
if (heap.empty()) {
std::cout << "Heap is empty. Cannot decrease key." << std::endl;
return;
}

int index, newValue;
std::cout << "Enter the index of the node: ";
std::cin >> index;
std::cout << "Enter the new value: ";
std::cin >> newValue;

FibonacciHeapNode<int>* node = heap.insert(newValue);
heap.decreaseKey(node, newValue);
std::cout << "Key decreased successfully." << std::endl;
}

void Console::deleteNode() {
if (heap.empty()) {
std::cout << "Heap is empty. Cannot delete node." << std::endl;
return;
}

int index;
std::cout << "Enter the index of the node to delete: ";
std::cin >> index;

FibonacciHeapNode<int>* node = heap.insert(index);
heap.deleteNode(node);
std::cout << "Node deleted successfully." << std::endl;
}

void Console::mergeHeaps() {
FibonacciHeap<int> otherHeap;
int numElements;
std::cout << "Enter the number of elements in the other heap: ";
std::cin >> numElements;

for (int i = 0; i < numElements; i++) {
int value;
std::cout << "Enter element " << i + 1 << ": ";
std::cin >> value;
otherHeap.insert(value);
}

heap.merge(&otherHeap);
std::cout << "Heaps merged successfully." << std::endl;
}
27 changes: 27 additions & 0 deletions modules/bodrov_daniil_fibonacci_heap/test/CMakeList.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
set(target ${TESTS})

file(GLOB srcs "*.cpp")
set_source_files_properties(${srcs} PROPERTIES
LABELS "${MODULE};Test")

add_executable(${target} ${srcs} ${hdrs})
set_target_properties(${target} PROPERTIES
LABELS "${MODULE};Test")

if (UNIX)
target_link_libraries(${target} gtest ${CMAKE_THREAD_LIBS_INIT} pthread)
endif (UNIX)
target_link_libraries(${target} gtest ${LIBRARY})

# VS2012 doesn't support correctly the tuples yet,
# see http://code.google.com/p/googletest/issues/detail?id=412
if(MSVC)
target_compile_definitions(${target} PUBLIC _VARIADIC_MAX=10)
endif()

add_test(
NAME ${MODULE}_gtest
COMMAND ${target}
)
set_tests_properties (${MODULE}_gtest PROPERTIES
LABELS "${MODULE}")

0 comments on commit 107fea4

Please sign in to comment.