From 264d26821677ec5eae8544d068ca77f6ec708168 Mon Sep 17 00:00:00 2001 From: Rohith-Raju Date: Mon, 18 Mar 2024 17:48:02 +0530 Subject: [PATCH] update(LinkedList): added more tests Signed-off-by: Rohith-Raju --- .github/workflows/workflow.yaml | 25 ++++++++++++++++ CMakeLists.txt | 4 +-- include/LinkedList.h | 52 +++++++++++++++++++++++++++++---- main.cpp | 6 ---- testing/TestLinkedList.cpp | 8 ++++- testing/lib | 2 +- 6 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 .github/workflows/workflow.yaml diff --git a/.github/workflows/workflow.yaml b/.github/workflows/workflow.yaml new file mode 100644 index 0000000..5054f98 --- /dev/null +++ b/.github/workflows/workflow.yaml @@ -0,0 +1,25 @@ +name: Build +on: + push: +jobs: + build-project: + name: Build Project + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4.1.1 + with: + submodules: true + + - name: Configure Project + uses: threeal/cmake-action@v1.3.0 + with: + c-compiler: gcc + cxx-compiler: g++ + cxx-flags: "--std=c++17" + + - name: Build Project + run: cmake --build build + + - name: Run-Tests + run: ./build/test/LinkedList_test \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 143c184..9b67e38 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.27) -project(CppTemplation) +project(LinkedList) set(CMAKE_CXX_STANDARD 17) @@ -11,4 +11,4 @@ include_directories(include) add_subdirectory(testing) # Sources -add_executable(CppTemplation main.cpp) +add_executable(LinkedList main.cpp) diff --git a/include/LinkedList.h b/include/LinkedList.h index 79eb307..41c6ad4 100644 --- a/include/LinkedList.h +++ b/include/LinkedList.h @@ -48,6 +48,7 @@ template class LinkedList { // Move constructor LinkedList(LinkedList &&other); + // Destructor ~LinkedList(); /* Iterators */ @@ -199,6 +200,11 @@ template LinkedList &LinkedList::clean() { return *this; } +/* + * reverse() reverses all the elements where head = prev_tail + * and tail = prev_head + */ + template void LinkedList::reverse() { Node *first_ptr = nullptr; Node *second_ptr = head; @@ -212,6 +218,9 @@ template void LinkedList::reverse() { head = first_ptr; } +/* + * deep_clean() drills into the entire list and removes all the elements + */ template void LinkedList::deep_clean(Node *&list) { if (list != tail) { deep_clean(list->next); @@ -222,17 +231,28 @@ template void LinkedList::deep_clean(Node *&list) { return; } +/* + * insert_first() inserts element only when there are no elements in the list + */ template void LinkedList::insert_first(Node *node) { - head = node; - size_counter = 1; - if (tail == nullptr) { - tail = head; + if (!is_empty()) { + head = node; + size_counter = 1; + if (tail == nullptr) { + tail = head; + } } return; } +/* + * is_empty() returns true if the list is empty + */ template bool LinkedList::is_empty() { return head == nullptr; } +/* + * push_back() append new element to the end of the list + */ template void LinkedList::push_back(T data) { Node *node = new Node(data); if (is_empty()) { @@ -243,9 +263,14 @@ template void LinkedList::push_back(T data) { tail->next = node; tail = node; } - +/* + * size() returns the size of the list + */ template size_t LinkedList::size() { return size_counter; } +/* + * at() returns the node containing the data at a given position + */ template typename LinkedList::Node *LinkedList::at(size_t pos) { if (pos >= size()) @@ -263,6 +288,9 @@ typename LinkedList::Node *LinkedList::operator[](size_t pos) { return at(pos); } +/* + * delete_at() remove element in the given pos + */ template bool LinkedList::delete_at(size_t pos) { if (pos >= size()) { throw std::out_of_range("Cannot delete. Index out of bounds"); @@ -298,13 +326,27 @@ template bool LinkedList::delete_at(size_t pos) { } } +/* + * get_head_val() return the value of head + */ template T LinkedList::get_head_val() { return head->data; } +/* + * get_tail_val() return the value of tail + */ template T LinkedList::get_tail_val() { return tail->data; } + +/* + * sort() sorts all the elements in the list (default accending) + */ template LinkedList &LinkedList::sort() { return sort([](T leftVal, T rightVal) { return leftVal < rightVal; }); } +/* + * sort() sorts all the elements in the list by passing in a anonymous function + */ + template template LinkedList &LinkedList::sort(function &&func) { diff --git a/main.cpp b/main.cpp index a9aac35..15c21fb 100644 --- a/main.cpp +++ b/main.cpp @@ -1,12 +1,6 @@ // // Created by Rohith on 1/10/24. // -#include "LinkedList.h" -#include "iostream" int main() { - LinkedList list = {1, 2, 3, 4, 5}; - for (auto it = list.rbegin(); it != list.rend(); it++) { - std::cout << *it; - } } diff --git a/testing/TestLinkedList.cpp b/testing/TestLinkedList.cpp index 6f31952..6f38ca8 100644 --- a/testing/TestLinkedList.cpp +++ b/testing/TestLinkedList.cpp @@ -82,7 +82,13 @@ TEST(LinkedList, TestDeleteAtPosition) { ASSERT_EQ(ptr->data, 3); list.delete_at(2); ASSERT_NE(ptr->data, 3); - // todo: write SIGSEGV test case +} + +TEST(LinkedList, TestDeleteAtEnd) { + LinkedList list = {1, 2, 3, 4, 5}; + ASSERT_EQ(list.get_tail_val(), 5); + list.delete_at(4); + ASSERT_EQ(list.get_tail_val(), 4); } TEST(LinkedList, TestCustomSort) { diff --git a/testing/lib b/testing/lib index 5df0241..d83fee1 160000 --- a/testing/lib +++ b/testing/lib @@ -1 +1 @@ -Subproject commit 5df0241ea4880e5a846775d3efc8b873f7b36c31 +Subproject commit d83fee138a9ae6cb7c03688a2d08d4043a39815d