Skip to content

Commit

Permalink
update(LinkedList): added more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Rohith-Raju <[email protected]>
  • Loading branch information
Rohith-Raju committed Apr 26, 2024
1 parent e137c46 commit 264d268
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 15 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Build
on:
push:
jobs:
build-project:
name: Build Project
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/[email protected]
with:
submodules: true

- name: Configure Project
uses: threeal/[email protected]
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
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.27)

project(CppTemplation)
project(LinkedList)

set(CMAKE_CXX_STANDARD 17)

Expand All @@ -11,4 +11,4 @@ include_directories(include)
add_subdirectory(testing)

# Sources
add_executable(CppTemplation main.cpp)
add_executable(LinkedList main.cpp)
52 changes: 47 additions & 5 deletions include/LinkedList.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ template <class T> class LinkedList {
// Move constructor
LinkedList(LinkedList &&other);

// Destructor
~LinkedList();

/* Iterators */
Expand Down Expand Up @@ -199,6 +200,11 @@ template <typename T> LinkedList<T> &LinkedList<T>::clean() {
return *this;
}

/*
* reverse() reverses all the elements where head = prev_tail
* and tail = prev_head
*/

template <typename T> void LinkedList<T>::reverse() {
Node *first_ptr = nullptr;
Node *second_ptr = head;
Expand All @@ -212,6 +218,9 @@ template <typename T> void LinkedList<T>::reverse() {
head = first_ptr;
}

/*
* deep_clean() drills into the entire list and removes all the elements
*/
template <typename T> void LinkedList<T>::deep_clean(Node *&list) {
if (list != tail) {
deep_clean(list->next);
Expand All @@ -222,17 +231,28 @@ template <typename T> void LinkedList<T>::deep_clean(Node *&list) {
return;
}

/*
* insert_first() inserts element only when there are no elements in the list
*/
template <typename T> void LinkedList<T>::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 <typename T> bool LinkedList<T>::is_empty() { return head == nullptr; }

/*
* push_back() append new element to the end of the list
*/
template <typename T> void LinkedList<T>::push_back(T data) {
Node *node = new Node(data);
if (is_empty()) {
Expand All @@ -243,9 +263,14 @@ template <typename T> void LinkedList<T>::push_back(T data) {
tail->next = node;
tail = node;
}

/*
* size() returns the size of the list
*/
template <typename T> size_t LinkedList<T>::size() { return size_counter; }

/*
* at() returns the node containing the data at a given position
*/
template <typename T>
typename LinkedList<T>::Node *LinkedList<T>::at(size_t pos) {
if (pos >= size())
Expand All @@ -263,6 +288,9 @@ typename LinkedList<T>::Node *LinkedList<T>::operator[](size_t pos) {
return at(pos);
}

/*
* delete_at() remove element in the given pos
*/
template <typename T> bool LinkedList<T>::delete_at(size_t pos) {
if (pos >= size()) {
throw std::out_of_range("Cannot delete. Index out of bounds");
Expand Down Expand Up @@ -298,13 +326,27 @@ template <typename T> bool LinkedList<T>::delete_at(size_t pos) {
}
}

/*
* get_head_val() return the value of head
*/
template <typename T> T LinkedList<T>::get_head_val() { return head->data; }

/*
* get_tail_val() return the value of tail
*/
template <typename T> T LinkedList<T>::get_tail_val() { return tail->data; }

/*
* sort() sorts all the elements in the list (default accending)
*/
template <typename T> LinkedList<T> &LinkedList<T>::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 <typename T>
template <class function>
LinkedList<T> &LinkedList<T>::sort(function &&func) {
Expand Down
6 changes: 0 additions & 6 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
//
// Created by Rohith on 1/10/24.
//
#include "LinkedList.h"
#include "iostream"

int main() {
LinkedList<int> list = {1, 2, 3, 4, 5};
for (auto it = list.rbegin(); it != list.rend(); it++) {
std::cout << *it;
}
}
8 changes: 7 additions & 1 deletion testing/TestLinkedList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion testing/lib
Submodule lib updated 32 files
+0 −43 .github/workflows/gtest-ci.yml
+4 −0 MODULE.bazel
+8 −9 docs/primer.md
+2 −2 docs/reference/mocking.md
+4 −2 docs/reference/testing.md
+33 −0 fake_fuchsia_sdk.bzl
+4 −3 googlemock/CMakeLists.txt
+6 −6 googlemock/include/gmock/gmock-actions.h
+74 −74 googlemock/include/gmock/gmock-matchers.h
+3 −2 googlemock/include/gmock/gmock-more-actions.h
+8 −6 googlemock/include/gmock/internal/gmock-internal-utils.h
+4 −4 googlemock/include/gmock/internal/gmock-port.h
+3 −2 googlemock/src/gmock-internal-utils.cc
+26 −0 googlemock/test/gmock-more-actions_test.cc
+1 −1 googletest/include/gtest/gtest-assertion-result.h
+4 −4 googletest/include/gtest/gtest-param-test.h
+65 −61 googletest/include/gtest/gtest-typed-test.h
+19 −3 googletest/include/gtest/gtest.h
+7 −1 googletest/include/gtest/internal/gtest-filepath.h
+24 −62 googletest/include/gtest/internal/gtest-internal.h
+76 −74 googletest/include/gtest/internal/gtest-param-util.h
+2 −0 googletest/include/gtest/internal/gtest-port-arch.h
+34 −13 googletest/include/gtest/internal/gtest-port.h
+10 −10 googletest/src/gtest-death-test.cc
+28 −16 googletest/src/gtest-internal-inl.h
+34 −19 googletest/src/gtest-port.cc
+119 −98 googletest/src/gtest.cc
+1 −0 googletest/test/googletest-color-test.py
+15 −0 googletest/test/googletest-json-output-unittest.py
+3 −0 googletest/test/gtest_json_test_utils.py
+10 −22 googletest/test/gtest_unittest.cc
+6 −0 googletest_deps.bzl

0 comments on commit 264d268

Please sign in to comment.