Skip to content

Commit

Permalink
Tests for DynamicArray, DoubleLinkedList, MaxHeap
Browse files Browse the repository at this point in the history
  • Loading branch information
Mazako committed Mar 25, 2023
1 parent ec515e3 commit e17bf89
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 13 deletions.
1 change: 0 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ void printMenuItems() {
std::cout << "Wybierz opcje:" << std::endl;
}


void startDynamicArrayClient() {
auto *dynamicArrayClient = new DynamicArrayClient();
dynamicArrayClient->startMainLoop();
Expand Down
12 changes: 12 additions & 0 deletions src/double_linked_list/DoubleLinkedList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ void DoubleLinkedList::unshift(int data) {
head->previous = newNode;
head = newNode;
}
size++;
}

//COMPLEXITY: O(N)
Expand Down Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions src/double_linked_list/DoubleLinkedList.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class DoubleLinkedList {

int shift();

int get(int index);

int removeAt(int index);

void addAt(int index, int data);
Expand Down
1 change: 0 additions & 1 deletion src/dynamic_array/client/DynamicArrayClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ void DynamicArrayClient::printMenu() {
}

void DynamicArrayClient::add() {
std::cout << "XD";
}

void DynamicArrayClient::addAtIndex() {
Expand Down
25 changes: 16 additions & 9 deletions src/heap/MaxHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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];

Expand All @@ -99,3 +102,7 @@ MaxHeap::MaxHeap(int *array, int n) {
}
}

int MaxHeap::getSize() {
return this->size;
}

4 changes: 3 additions & 1 deletion src/heap/MaxHeap.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ class MaxHeap {

~MaxHeap();

inline int getMax();
int getMax();

void insert(int data);

int removeMax();

int getSize();

void printHeap();

};
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
86 changes: 86 additions & 0 deletions tests/DoubleLinkedListTest.cpp
Original file line number Diff line number Diff line change
@@ -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");
}
65 changes: 65 additions & 0 deletions tests/MaxHeapTest.cpp
Original file line number Diff line number Diff line change
@@ -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);
}


0 comments on commit e17bf89

Please sign in to comment.