Skip to content
Snippets Groups Projects
Commit d8f6ff3e authored by Stauder, Lucas's avatar Stauder, Lucas
Browse files

WiSe 2024/25

parents
Branches master
No related tags found
No related merge requests found
# CLion
/.idea
/*-build-*
# Visual Studio
/.vs
/out
# vscode
/.vscode
/build
cmake_minimum_required(VERSION 3.26)
project(Hausuebung2)
set(CMAKE_CXX_STANDARD 14)
add_executable(Hausuebung2
src/main.cpp
src/LinkedList.cpp
src/LinkedList.hpp
src/ListItem.cpp
src/ListItem.hpp
)
# Fetch Catch2
Include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.15.2
)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
enable_testing()
add_executable(test_listitem
src/ListItem.cpp
src/ListItem.hpp
src/ListItem.test-public.cpp
)
add_executable(test_linkedlist
src/LinkedList.cpp
src/LinkedList.hpp
src/ListItem.cpp
src/ListItem.hpp
src/LinkedList.test-public.cpp
)
add_executable(test_private
src/ListItem.cpp
src/ListItem.hpp
src/LinkedList.cpp
src/LinkedList.hpp
src/ListItem.test-private.cpp
src/LinkedList.test-private.cpp
)
target_link_libraries(test_listitem PRIVATE GTest::gtest_main)
target_link_libraries(test_linkedlist PRIVATE GTest::gtest_main)
target_link_libraries(test_private PRIVATE GTest::gtest_main)
target_include_directories(test_listitem PRIVATE src/)
target_include_directories(test_linkedlist PRIVATE src/)
target_include_directories(test_private PRIVATE src/)
include(GoogleTest)
gtest_discover_tests(test_listitem)
gtest_discover_tests(test_linkedlist)
gtest_discover_tests(test_private)
#include "LinkedList.hpp"
#include <stdexcept>
// This _DUMMY_STR_LINKED_LIST is only a placeholder that is used
// for the dummy functions that return a const reference to a string.
// It should be removed for the final solution.
static const std::string _DUMMY_STR_LINKED_LIST{""};
std::string LinkedList::remove(size_t position) { return _DUMMY_STR_LINKED_LIST; }
std::string LinkedList::pop_front() { return _DUMMY_STR_LINKED_LIST; }
std::string LinkedList::pop_back() { return _DUMMY_STR_LINKED_LIST; }
const std::string& LinkedList::get(size_t position) const { return _DUMMY_STR_LINKED_LIST; }
const std::string& LinkedList::back() const { return _DUMMY_STR_LINKED_LIST; }
const std::string& LinkedList::front() const { return _DUMMY_STR_LINKED_LIST; }
void LinkedList::insert(const std::string element, size_t position) {}
void LinkedList::push_front(const std::string element) {}
void LinkedList::push_back(const std::string element) {}
void LinkedList::prepend(LinkedList list) {}
void LinkedList::append(LinkedList list) {}
void LinkedList::insert_sorted(const std::string element) {}
#ifndef PROGAUT_HAUSUEBUNG2_LINKEDLIST_HPP
#define PROGAUT_HAUSUEBUNG2_LINKEDLIST_HPP
#include <cstddef>
#include <string>
#include "ListItem.hpp"
class LinkedList {
public:
LinkedList() {}
~LinkedList() {}
LinkedList(const LinkedList& other) {}
LinkedList& operator=(const LinkedList& other) { return *this; }
bool empty() const { return false; }
size_t size() const { return 0; }
const std::string& front() const;
const std::string& back() const;
const std::string& get(size_t position) const;
void push_front(const std::string element);
void push_back(const std::string element);
void insert(const std::string element, size_t position);
std::string pop_front();
std::string pop_back();
std::string remove(size_t position);
void prepend(LinkedList list);
void append(LinkedList list);
void insert_sorted(const std::string element);
};
#endif // PROGAUT_HAUSUEBUNG2_LINKEDLIST_HPP
\ No newline at end of file
#include "LinkedList.hpp"
#include <gtest/gtest.h>
// You can add more tests here
TEST(LinkedListTest, PrivateTestsAvailable) {
FAIL();
}
\ No newline at end of file
#include "LinkedList.hpp"
#include <gtest/gtest.h>
TEST(LinkedListTest, ConstructorAndFrontOperations) {
LinkedList list{};
list.push_front("a");
EXPECT_EQ(list.front(), "a");
EXPECT_EQ(list.pop_front(), "a");
}
TEST(LinkedListTest, ConstructorAndMultipleFrontOperations) {
LinkedList list{};
list.push_front("a");
list.push_front("b");
EXPECT_EQ(list.front(), "b");
EXPECT_EQ(list.pop_front(), "b");
EXPECT_EQ(list.pop_front(), "a");
}
TEST(LinkedListTest, Size) {
LinkedList list{};
EXPECT_EQ(list.size(), 0);
list.push_front("a");
EXPECT_EQ(list.size(), 1);
list.push_front("b");
EXPECT_EQ(list.size(), 2);
EXPECT_EQ(list.pop_front(), "b");
EXPECT_EQ(list.size(), 1);
EXPECT_EQ(list.pop_front(), "a");
EXPECT_EQ(list.size(), 0);
}
TEST(LinkedListTest, Empty) {
LinkedList list{};
EXPECT_TRUE(list.empty());
list.push_front("a");
EXPECT_FALSE(list.empty());
list.push_front("b");
EXPECT_FALSE(list.empty());
list.pop_front();
EXPECT_FALSE(list.empty());
list.pop_front();
EXPECT_TRUE(list.empty());
}
TEST(LinkedListTest, BackOperations) {
LinkedList list{};
list.push_front("a");
list.push_back("b");
EXPECT_EQ(list.front(), "a");
EXPECT_EQ(list.back(), "b");
EXPECT_EQ(list.pop_back(), "b");
EXPECT_EQ(list.pop_back(), "a");
EXPECT_TRUE(list.empty());
}
TEST(LinkedListTest, IndexedOperations) {
LinkedList list{};
list.push_front("b");
list.push_front("a");
EXPECT_EQ(list.get(0), "a");
EXPECT_EQ(list.get(1), "b");
list.insert("c", 1);
EXPECT_EQ(list.size(), 3);
EXPECT_EQ(list.get(0), "a");
EXPECT_EQ(list.get(1), "c");
EXPECT_EQ(list.get(2), "b");
list.remove(0);
EXPECT_EQ(list.size(), 2);
EXPECT_EQ(list.front(), "c");
}
TEST(LinkedListTest, CopyConstructor) {
LinkedList list{};
list.push_front("c");
list.push_front("b");
list.push_front("a");
LinkedList list2{list};
list.pop_front();
EXPECT_EQ(list2.pop_front(), "a");
EXPECT_EQ(list2.pop_front(), "b");
EXPECT_EQ(list2.pop_front(), "c");
EXPECT_TRUE(list2.empty());
EXPECT_EQ(list.size(), 2);
EXPECT_EQ(list.pop_front(), "b");
EXPECT_EQ(list.pop_front(), "c");
EXPECT_TRUE(list.empty());
}
TEST(LinkedListTest, CopyAssignment) {
LinkedList list{};
list.push_front("c");
list.push_front("b");
list.push_front("a");
LinkedList list2{};
list2.push_front("d");
list2 = list;
list.pop_front();
EXPECT_EQ(list2.pop_front(), "a");
EXPECT_EQ(list2.pop_front(), "b");
EXPECT_EQ(list2.pop_front(), "c");
EXPECT_TRUE(list2.empty());
EXPECT_EQ(list.size(), 2);
EXPECT_EQ(list.pop_front(), "b");
EXPECT_EQ(list.pop_front(), "c");
EXPECT_TRUE(list.empty());
}
TEST(LinkedListTest, Append) {
LinkedList list{};
list.push_front("c");
list.push_front("b");
list.push_front("a");
LinkedList list2;
list2.push_front("e");
list2.push_front("d");
list.append(list2);
EXPECT_EQ(list.pop_front(), "a");
EXPECT_EQ(list.pop_front(), "b");
EXPECT_EQ(list.pop_front(), "c");
EXPECT_EQ(list.pop_front(), "d");
EXPECT_EQ(list.pop_front(), "e");
EXPECT_TRUE(list.empty());
}
TEST(LinkedListTest, InsertSorted) {
LinkedList list{};
list.push_front("g");
list.insert_sorted("a");
list.insert_sorted("i");
list.insert_sorted("h");
EXPECT_EQ(list.size(), 4);
EXPECT_EQ(list.pop_front(), "a");
EXPECT_EQ(list.pop_front(), "g");
EXPECT_EQ(list.pop_front(), "h");
EXPECT_EQ(list.pop_front(), "i");
EXPECT_TRUE(list.empty());
}
#include "ListItem.hpp"
#ifndef PROGAUT_HAUSUEBUNG2_LISTITEM_HPP
#define PROGAUT_HAUSUEBUNG2_LISTITEM_HPP
#include <string>
// This _DUMMY_STR_LIST_ITEM is only a placeholder that is used
// for the dummy functions that return a const reference to a string.
// It should be removed for the final solution.
static const std::string _DUMMY_STR_LIST_ITEM{""};
class ListItem {
public:
ListItem(const std::string content) {}
~ListItem() {}
ListItem(const ListItem&) = delete;
ListItem& operator=(const ListItem&) = delete;
ListItem(ListItem&&) = delete;
ListItem& operator=(ListItem&&) = delete;
ListItem* getNext() const { return nullptr; }
ListItem* getPrevious() const { return nullptr; }
const std::string& getContent() const { return _DUMMY_STR_LIST_ITEM; }
void insertBefore(ListItem* item) {}
void insertAfter(ListItem* item) {}
void remove() {}
};
#endif // PROGAUT_HAUSUEBUNG2_LISTITEM_HPP
\ No newline at end of file
#include "ListItem.hpp"
#include <gtest/gtest.h>
// You can add more tests here
TEST(ListItemTest, PrivateTestsAvailable) {
FAIL();
}
\ No newline at end of file
#include "ListItem.hpp"
#include <gtest/gtest.h>
TEST(ListItemTest, ConstructorAndGetter) {
auto* a = new ListItem{"3"};
EXPECT_EQ(a->getPrevious(), nullptr);
EXPECT_EQ(a->getContent(), "3");
delete a;
}
TEST(ListItemTest, InsertBefore) {
ListItem a{"A"};
ListItem b{"B"};
a.insertBefore(&b);
EXPECT_EQ(a.getPrevious(), nullptr);
EXPECT_EQ(a.getNext(), &b);
EXPECT_EQ(b.getPrevious(), &a);
EXPECT_EQ(b.getNext(), nullptr);
EXPECT_EQ(a.getContent(), "A");
EXPECT_EQ(b.getContent(), "B");
}
TEST(ListItemTest, InsertBefore3AlwaysFront) {
ListItem a{"A"};
ListItem b{"B"};
ListItem c{"C"};
b.insertBefore(&c);
a.insertBefore(&b);
EXPECT_EQ(a.getPrevious(), nullptr);
EXPECT_EQ(a.getNext(), &b);
EXPECT_EQ(b.getPrevious(), &a);
EXPECT_EQ(b.getNext(), &c);
EXPECT_EQ(c.getPrevious(), &b);
EXPECT_EQ(c.getNext(), nullptr);
EXPECT_EQ(a.getContent(), "A");
EXPECT_EQ(b.getContent(), "B");
EXPECT_EQ(c.getContent(), "C");
}
TEST(ListItemTest, InsertBefore3IntoMiddle) {
ListItem a{"A"};
ListItem b{"B"};
ListItem c{"C"};
b.insertBefore(&c);
a.insertBefore(&c);
EXPECT_EQ(b.getPrevious(), nullptr);
EXPECT_EQ(b.getNext(), &a);
EXPECT_EQ(a.getPrevious(), &b);
EXPECT_EQ(a.getNext(), &c);
EXPECT_EQ(c.getPrevious(), &a);
EXPECT_EQ(c.getNext(), nullptr);
EXPECT_EQ(a.getContent(), "A");
EXPECT_EQ(b.getContent(), "B");
EXPECT_EQ(c.getContent(), "C");
}
TEST(ListItemTest, Remove) {
ListItem a{"A"};
ListItem b{"B"};
ListItem c{"C"};
b.insertBefore(&c);
a.insertBefore(&b);
b.remove();
EXPECT_EQ(a.getPrevious(), nullptr);
EXPECT_EQ(a.getNext(), &c);
EXPECT_EQ(c.getPrevious(), &a);
EXPECT_EQ(c.getNext(), nullptr);
EXPECT_EQ(b.getPrevious(), nullptr);
EXPECT_EQ(b.getNext(), nullptr);
EXPECT_EQ(a.getContent(), "A");
EXPECT_EQ(b.getContent(), "B");
EXPECT_EQ(c.getContent(), "C");
}
TEST(ListItemTest, Destructor) {
ListItem a{"A"};
ListItem* b = new ListItem{"B"};
ListItem c{"C"};
b->insertBefore(&c);
a.insertBefore(b);
delete b;
EXPECT_EQ(a.getPrevious(), nullptr);
EXPECT_EQ(a.getNext(), &c);
EXPECT_EQ(c.getPrevious(), &a);
EXPECT_EQ(c.getNext(), nullptr);
EXPECT_EQ(a.getContent(), "A");
EXPECT_EQ(c.getContent(), "C");
}
#include <iostream>
#include "LinkedList.hpp"
int main() {
ListItem* a = new ListItem{"a"};
ListItem* b = new ListItem{"b"};
ListItem* c = new ListItem{"c"};
b->insertAfter(a); // a <-> b
c->insertAfter(b); // a <-> b <-> c
a->remove(); // b <-> c
a->insertAfter(b); // b <-> a <-> c
ListItem* p = b; // get pointer to first element of list
// traverse list and print content
while (true) {
std::cout << p->getContent();
p = p->getNext();
if (p == nullptr) {
break;
}
std::cout << " <-> ";
}
delete c;
delete b;
delete a;
std::cout << "\n\n\n";
LinkedList list{};
list.push_back("a");
list.push_back("b");
list.push_back("c");
list.pop_front();
list.insert("a", 1);
while (!list.empty()) {
std::cout << list.pop_front() << " ";
}
return 0;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment