diff --git a/main.cpp b/main.cpp index 71476c6..9fd1c93 100644 --- a/main.cpp +++ b/main.cpp @@ -55,7 +55,6 @@ void printMenuItems() { std::cout << "Wybierz opcje:" << std::endl; } - void startDynamicArrayClient() { auto *dynamicArrayClient = new DynamicArrayClient(); dynamicArrayClient->startMainLoop(); diff --git a/src/double_linked_list/DoubleLinkedList.cpp b/src/double_linked_list/DoubleLinkedList.cpp index 857b1e9..fc9c9e0 100644 --- a/src/double_linked_list/DoubleLinkedList.cpp +++ b/src/double_linked_list/DoubleLinkedList.cpp @@ -104,6 +104,7 @@ void DoubleLinkedList::unshift(int data) { head->previous = newNode; head = newNode; } + size++; } //COMPLEXITY: O(N) @@ -157,3 +158,14 @@ int DoubleLinkedList::removeAt(int index) { } return data; } + +int DoubleLinkedList::get(int index) { + auto *nodePtr = this->head; + for (int i = 0; i < index; i++) { + nodePtr = nodePtr->next; + if (nodePtr == nullptr) { + throw std::invalid_argument("Array index out of bound"); + } + } + return nodePtr->data; +} diff --git a/src/double_linked_list/DoubleLinkedList.h b/src/double_linked_list/DoubleLinkedList.h index eb4a4a3..462657a 100644 --- a/src/double_linked_list/DoubleLinkedList.h +++ b/src/double_linked_list/DoubleLinkedList.h @@ -17,6 +17,8 @@ class DoubleLinkedList { int shift(); + int get(int index); + int removeAt(int index); void addAt(int index, int data); diff --git a/src/dynamic_array/client/DynamicArrayClient.cpp b/src/dynamic_array/client/DynamicArrayClient.cpp index 4090814..3f9eca8 100644 --- a/src/dynamic_array/client/DynamicArrayClient.cpp +++ b/src/dynamic_array/client/DynamicArrayClient.cpp @@ -59,7 +59,6 @@ void DynamicArrayClient::printMenu() { } void DynamicArrayClient::add() { - std::cout << "XD"; } void DynamicArrayClient::addAtIndex() { diff --git a/src/heap/MaxHeap.cpp b/src/heap/MaxHeap.cpp index 40eb5da..81d7fed 100644 --- a/src/heap/MaxHeap.cpp +++ b/src/heap/MaxHeap.cpp @@ -23,17 +23,20 @@ MaxHeap::~MaxHeap() { } void MaxHeap::reallocate(int newSize) { - if (newSize == 0) { - this->size--; - return; + int *tmpArray = nullptr; + + if (newSize > 0) { + tmpArray = new int[newSize]; + for (int i = 0; i < newSize; i++) { + tmpArray[i] = array[i]; + } } - int *tmpArray = new int[newSize]; - for (int i = 0; i < size; i++) { - tmpArray[i] = array[i]; + + if (this->array != nullptr) { + delete[] this->array; } - int *arrayPtr = array; + this->array = tmpArray; - delete arrayPtr; this->size = newSize; } @@ -81,7 +84,7 @@ void MaxHeap::heapify(int i) { int MaxHeap::removeMax() { if (size == 0) { - new std::logic_error("Stack is empty"); + throw std::logic_error("Heap is empty"); } int data = array[0]; @@ -99,3 +102,7 @@ MaxHeap::MaxHeap(int *array, int n) { } } +int MaxHeap::getSize() { + return this->size; +} + diff --git a/src/heap/MaxHeap.h b/src/heap/MaxHeap.h index 4544448..d4d20ea 100644 --- a/src/heap/MaxHeap.h +++ b/src/heap/MaxHeap.h @@ -26,12 +26,14 @@ class MaxHeap { ~MaxHeap(); - inline int getMax(); + int getMax(); void insert(int data); int removeMax(); + int getSize(); + void printHeap(); }; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1080da3..2250be9 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,6 +1,6 @@ project(tests) add_subdirectory(googletest-1.13.0) -add_executable(test_run DynamicArrayTests.cpp) +add_executable(test_run DynamicArrayTests.cpp DoubleLinkedListTest.cpp MaxHeapTest.cpp) target_link_libraries(test_run SDIZO_PROJ_1) target_link_libraries(test_run gtest gtest_main) \ No newline at end of file diff --git a/tests/DoubleLinkedListTest.cpp b/tests/DoubleLinkedListTest.cpp new file mode 100644 index 0000000..a1f8375 --- /dev/null +++ b/tests/DoubleLinkedListTest.cpp @@ -0,0 +1,86 @@ +#include "gtest/gtest.h" +#include "double_linked_list/DoubleLinkedList.h" + + +class DoubleLinkedListTest : public ::testing::Test { + +}; + +TEST_F(DoubleLinkedListTest, PushAndPop) { + DoubleLinkedList list; + list.push(1); + list.push(2); + list.push(3); + + ASSERT_EQ(list.pop(), 3); + ASSERT_EQ(list.pop(), 2); + ASSERT_EQ(list.pop(), 1); +} + +TEST_F(DoubleLinkedListTest, UnshiftAndShift) { + DoubleLinkedList list; + list.unshift(1); + list.unshift(2); + list.unshift(3); + + ASSERT_EQ(list.shift(), 3); + ASSERT_EQ(list.shift(), 2); + ASSERT_EQ(list.shift(), 1); +} + +TEST_F(DoubleLinkedListTest, AddAtAndRemoveAt) { + DoubleLinkedList list; + list.addAt(0, 1); + list.addAt(1, 3); + list.addAt(1, 2); + + ASSERT_EQ(list.removeAt(1), 2); + ASSERT_EQ(list.removeAt(1), 3); + ASSERT_EQ(list.removeAt(0), 1); +} + +TEST_F(DoubleLinkedListTest, Get) { + DoubleLinkedList list; + list.push(1); + list.push(2); + list.push(3); + + ASSERT_EQ(list.get(0), 1); + ASSERT_EQ(list.get(1), 2); + ASSERT_EQ(list.get(2), 3); +} + +TEST_F(DoubleLinkedListTest, ListEmptyException) { + DoubleLinkedList list; + + ASSERT_THROW(list.pop(), std::logic_error); + ASSERT_THROW(list.shift(), std::invalid_argument); +} + +TEST_F(DoubleLinkedListTest, IndexOutOfBoundsException) { + DoubleLinkedList list; + list.push(1); + list.push(2); + list.push(3); + + ASSERT_THROW(list.get(3), std::invalid_argument); + ASSERT_THROW(list.addAt(4, 4), std::invalid_argument); + ASSERT_THROW(list.removeAt(3), std::invalid_argument); +} + +TEST_F(DoubleLinkedListTest, PrintListAndPrintReversed) { + DoubleLinkedList list; + list.push(1); + list.push(2); + list.push(3); + + testing::internal::CaptureStdout(); + list.printList(); + std::string output = testing::internal::GetCapturedStdout(); + ASSERT_EQ(output, "1 2 3 \n"); + + testing::internal::CaptureStdout(); + list.printReversed(); + output = testing::internal::GetCapturedStdout(); + ASSERT_EQ(output, "3 2 1 \n"); +} diff --git a/tests/MaxHeapTest.cpp b/tests/MaxHeapTest.cpp new file mode 100644 index 0000000..4e1f9a9 --- /dev/null +++ b/tests/MaxHeapTest.cpp @@ -0,0 +1,65 @@ +#include "gtest/gtest.h" +#include "heap/MaxHeap.h" + +class MaxHeapTest : public ::testing::Test { + +}; + +TEST_F(MaxHeapTest, EmptyHeap) { + MaxHeap heap; + ASSERT_THROW(heap.removeMax(), std::logic_error); + ASSERT_EQ(heap.getSize(), 0); +} + +TEST_F(MaxHeapTest, InsertAndRemoveMax) { + MaxHeap heap; + heap.insert(3); + ASSERT_EQ(heap.getMax(), 3); + heap.insert(5); + ASSERT_EQ(heap.getMax(), 5); + heap.insert(1); + ASSERT_EQ(heap.getMax(), 5); + ASSERT_EQ(heap.removeMax(), 5); + ASSERT_EQ(heap.getMax(), 3); +} + +TEST_F(MaxHeapTest, HeapifyArray) { + int n = 7; + int *inputArray = new int[n]{1, 2, 3, 4, 5, 6, 7}; + MaxHeap heap(inputArray, n); + + ASSERT_EQ(heap.getMax(), 7); + ASSERT_EQ(heap.removeMax(), 7); + ASSERT_EQ(heap.getMax(), 6); +} + +TEST_F(MaxHeapTest, HeapifyArrayAndInsert) { + int n = 7; + int *inputArray = new int[n]{1, 2, 3, 4, 5, 6, 7}; + MaxHeap heap(inputArray, n); + ASSERT_EQ(heap.getMax(), 7); + ASSERT_EQ(heap.removeMax(), 7); + ASSERT_EQ(heap.getMax(), 6); + + heap.insert(8); + ASSERT_EQ(heap.getMax(), 8); + ASSERT_EQ(heap.removeMax(), 8); + ASSERT_EQ(heap.getMax(), 6); +} + +TEST_F(MaxHeapTest, ReallocationAndSize) { + MaxHeap heap; + heap.insert(1); + heap.insert(2); + heap.insert(3); + heap.insert(4); + heap.insert(5); + ASSERT_EQ(heap.getSize(), 5); + ASSERT_EQ(heap.getMax(), 5); + ASSERT_EQ(heap.removeMax(), 5); + ASSERT_EQ(heap.getSize(), 4); + ASSERT_EQ(heap.removeMax(), 4); + ASSERT_EQ(heap.getSize(), 3); +} + +