diff --git a/src/core/stacklist.hpp b/src/core/stacklist.hpp index 7e9ee788..41dc58ee 100644 --- a/src/core/stacklist.hpp +++ b/src/core/stacklist.hpp @@ -7,6 +7,7 @@ #include #include +#include #include template @@ -40,13 +41,24 @@ public: [[nodiscard]] bool operator==(const StackList& other) const { if (other.size != this->size) return false; - for (size_t i = 0; i < this->size; ++i) { + for (size_t i = 0; i != this->size; ++i) { if (this->operator[](i) != other[i]) return false; } return true; } + [[nodiscard]] QList toList() const { + QList list; + list.reserve(this->size); + + for (const auto& entry: *this) { + list.push_back(entry); + } + + return list; + } + template struct BaseIterator { using iterator_category = std::bidirectional_iterator_tag; @@ -65,6 +77,7 @@ public: ++this->i; return *static_cast(this); } + Self& operator--() { --this->i; return *static_cast(this); @@ -75,6 +88,7 @@ public: this->operator++(); return v; } + Self operator--(int) { auto v = *this; this->operator--(); diff --git a/src/core/test/CMakeLists.txt b/src/core/test/CMakeLists.txt index d38c2868..bb49192d 100644 --- a/src/core/test/CMakeLists.txt +++ b/src/core/test/CMakeLists.txt @@ -7,3 +7,4 @@ endfunction() qs_test(transformwatcher transformwatcher.cpp) qs_test(ringbuffer ringbuf.cpp) qs_test(scriptmodel scriptmodel.cpp) +qs_test(stacklist stacklist.cpp) diff --git a/src/core/test/stacklist.cpp b/src/core/test/stacklist.cpp new file mode 100644 index 00000000..9b981729 --- /dev/null +++ b/src/core/test/stacklist.cpp @@ -0,0 +1,92 @@ +#include "stacklist.hpp" +#include + +#include +#include +#include + +#include "../stacklist.hpp" + +void TestStackList::push() { + StackList list; + + list.push(1); + list.push(2); + + QCOMPARE_EQ(list.toList(), QList({1, 2})); + QCOMPARE_EQ(list.length(), 2); +} + +void TestStackList::pushAndGrow() { + StackList list; + + list.push(1); + list.push(2); + list.push(3); + list.push(4); + + QCOMPARE_EQ(list.toList(), QList({1, 2, 3, 4})); + QCOMPARE_EQ(list.length(), 4); +} + +void TestStackList::copy() { + StackList list; + + list.push(1); + list.push(2); + list.push(3); + list.push(4); + + QCOMPARE_EQ(list.toList(), QList({1, 2, 3, 4})); + QCOMPARE_EQ(list.length(), 4); + + auto list2 = list; + + QCOMPARE_EQ(list2.toList(), QList({1, 2, 3, 4})); + QCOMPARE_EQ(list2.length(), 4); + QCOMPARE_EQ(list2, list); +} + +void TestStackList::viewVla() { + StackList list; + + list.push(1); + list.push(2); + + QCOMPARE_EQ(list.toList(), QList({1, 2})); + QCOMPARE_EQ(list.length(), 2); + + STACKLIST_VLA_VIEW(int, list, listView); + + QList ql; + + for (size_t i = 0; i != list.length(); ++i) { + ql.push_back(listView[i]); // NOLINT + } + + QCOMPARE_EQ(ql, list.toList()); +} + +void TestStackList::viewVlaGrown() { + StackList list; + + list.push(1); + list.push(2); + list.push(3); + list.push(4); + + QCOMPARE_EQ(list.toList(), QList({1, 2, 3, 4})); + QCOMPARE_EQ(list.length(), 4); + + STACKLIST_VLA_VIEW(int, list, listView); + + QList ql; + + for (size_t i = 0; i != list.length(); ++i) { + ql.push_back(listView[i]); // NOLINT + } + + QCOMPARE_EQ(ql, list.toList()); +} + +QTEST_MAIN(TestStackList); diff --git a/src/core/test/stacklist.hpp b/src/core/test/stacklist.hpp new file mode 100644 index 00000000..f582761d --- /dev/null +++ b/src/core/test/stacklist.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include +#include + +class TestStackList: public QObject { + Q_OBJECT; + +private slots: + static void push(); + static void pushAndGrow(); + static void copy(); + static void viewVla(); + static void viewVlaGrown(); +};