diff --git a/include/linkedlist.h b/include/linkedlist.h index c497993..25ac62c 100644 --- a/include/linkedlist.h +++ b/include/linkedlist.h @@ -231,7 +231,7 @@ LinkedListErr LinkedList::set(size_t index, const T& val) { template LinkedListErr LinkedList::insert(size_t index, const T& value) { - if (this->is_empty()) { + if (this->is_empty() && index == 0) { return this->append(value); } @@ -425,5 +425,9 @@ size_t LinkedList::len() { return this->size; } +template +bool LinkedList::is_empty() { + return this->len() < 1; +} #endif // ! diff --git a/test/test_linkedlist.cpp b/test/test_linkedlist.cpp index db16829..ba7a6fe 100644 --- a/test/test_linkedlist.cpp +++ b/test/test_linkedlist.cpp @@ -247,3 +247,13 @@ TEST_CASE("Find 1 value not present", "[linkedlist]") { auto result = list.find(80); REQUIRE(result.error() == LinkedListErr::LINKEDLIST_NOT_FOUND); } + +TEST_CASE("Insert at empty list", "[linkedlist]") { + LinkedList list = {}; + + REQUIRE(list.len() == 0); + + list.insert(0, 8); + REQUIRE(list.len() == 1); + REQUIRE(list.get(0) == 8); +}