Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarfovich committed Sep 12, 2024
1 parent 8aa96d5 commit 89b6056
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
22 changes: 21 additions & 1 deletion HW3/cc_lib/include/cc/forward_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ class ForwardList

public: // methods
iterator begin() const;
const_iterator cbegin() const;
iterator end() const;
const_iterator cend() const;
iterator insert_after(iterator position, const value_type& value);
bool empty() const noexcept;

Expand Down Expand Up @@ -115,9 +117,15 @@ auto ForwardList<T, Allocator>::insert_after(iterator position, const value_type
auto* newNode = nodeAllocator.allocate(1);
using NodeAllocatorTraits = std::allocator_traits<decltype(nodeAllocator)>;
NodeAllocatorTraits::construct(nodeAllocator, newNode, value, next.getNode());

if(!firstNode){
firstNode = newNode;
} else{
position.getNode()->nextNode = newNode;
}
++size;

return next;
return {this, newNode};
}

template<typename T, typename Allocator>
Expand All @@ -126,12 +134,24 @@ auto ForwardList<T, Allocator>::begin() const -> iterator
return { this, firstNode };
}

template<typename T, typename Allocator>
auto ForwardList<T, Allocator>::cbegin() const -> const_iterator
{
return this->begin();
}

template<typename T, typename Allocator>
auto ForwardList<T, Allocator>::end() const -> iterator
{
return { this };
}

template<typename T, typename Allocator>
auto ForwardList<T, Allocator>::cend() const -> const_iterator
{
return this->end();
}

template<typename T, typename Allocator>
bool ForwardList<T, Allocator>::empty() const noexcept
{
Expand Down
30 changes: 29 additions & 1 deletion HW3/tests/forward_list_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,37 @@

#include <gtest/gtest.h>

#include <forward_list>
TEST(ForwardListTest, ConstructingEmpty)
{
cc::ForwardList<int> list;
EXPECT_TRUE(list.empty());
}

TEST(ForwardListTest, InsertionAfterInBeginning)
{
cc::ForwardList<int> list;
list.insert_after(list.begin(), 1);
EXPECT_EQ(*list.begin(), 1);

list.insert_after(list.begin(), 2);
EXPECT_EQ(*std::next(list.begin()), 2);

list.insert_after(list.begin(), 3);
EXPECT_EQ(*std::next(list.begin()), 3);
}

TEST(ForwardListTest, ConsequtiveInsertion)
{
const int inserts = 4;

cc::ForwardList<int> list;
auto iter = list.begin();
for (int i = 0; i < inserts; ++i){
iter = list.insert_after(iter, i);
}

iter = list.begin();
for (int i = 0; iter != list.end(); ++i, ++iter) {
EXPECT_EQ(*iter, i);
}
}

0 comments on commit 89b6056

Please sign in to comment.