core/stacklist: add tests

This commit is contained in:
outfoxxed 2025-01-15 02:47:14 -08:00
parent 6024c37492
commit c2ed5bf559
Signed by: outfoxxed
GPG key ID: 4C88A185FB89301E
4 changed files with 123 additions and 1 deletions

View file

@ -7,6 +7,7 @@
#include <iterator>
#include <vector>
#include <qlist.h>
#include <qtypes.h>
template <class T, size_t N>
@ -40,13 +41,24 @@ public:
[[nodiscard]] bool operator==(const StackList<T, N>& 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<T> toList() const {
QList<T> list;
list.reserve(this->size);
for (const auto& entry: *this) {
list.push_back(entry);
}
return list;
}
template <typename Self, typename ListPtr, typename IT>
struct BaseIterator {
using iterator_category = std::bidirectional_iterator_tag;
@ -65,6 +77,7 @@ public:
++this->i;
return *static_cast<Self*>(this);
}
Self& operator--() {
--this->i;
return *static_cast<Self*>(this);
@ -75,6 +88,7 @@ public:
this->operator++();
return v;
}
Self operator--(int) {
auto v = *this;
this->operator--();

View file

@ -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)

View file

@ -0,0 +1,92 @@
#include "stacklist.hpp"
#include <cstddef>
#include <qlist.h>
#include <qtest.h>
#include <qtestcase.h>
#include "../stacklist.hpp"
void TestStackList::push() {
StackList<int, 2> list;
list.push(1);
list.push(2);
QCOMPARE_EQ(list.toList(), QList({1, 2}));
QCOMPARE_EQ(list.length(), 2);
}
void TestStackList::pushAndGrow() {
StackList<int, 2> 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<int, 2> 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<int, 2> 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<int> 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<int, 2> 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<int> ql;
for (size_t i = 0; i != list.length(); ++i) {
ql.push_back(listView[i]); // NOLINT
}
QCOMPARE_EQ(ql, list.toList());
}
QTEST_MAIN(TestStackList);

View file

@ -0,0 +1,15 @@
#pragma once
#include <qobject.h>
#include <qtmetamacros.h>
class TestStackList: public QObject {
Q_OBJECT;
private slots:
static void push();
static void pushAndGrow();
static void copy();
static void viewVla();
static void viewVlaGrown();
};