forked from quickshell/quickshell
core/stacklist: add tests
This commit is contained in:
parent
6024c37492
commit
c2ed5bf559
4 changed files with 123 additions and 1 deletions
|
@ -7,6 +7,7 @@
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include <qlist.h>
|
||||||
#include <qtypes.h>
|
#include <qtypes.h>
|
||||||
|
|
||||||
template <class T, size_t N>
|
template <class T, size_t N>
|
||||||
|
@ -40,13 +41,24 @@ public:
|
||||||
[[nodiscard]] bool operator==(const StackList<T, N>& other) const {
|
[[nodiscard]] bool operator==(const StackList<T, N>& other) const {
|
||||||
if (other.size != this->size) return false;
|
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;
|
if (this->operator[](i) != other[i]) return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
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>
|
template <typename Self, typename ListPtr, typename IT>
|
||||||
struct BaseIterator {
|
struct BaseIterator {
|
||||||
using iterator_category = std::bidirectional_iterator_tag;
|
using iterator_category = std::bidirectional_iterator_tag;
|
||||||
|
@ -65,6 +77,7 @@ public:
|
||||||
++this->i;
|
++this->i;
|
||||||
return *static_cast<Self*>(this);
|
return *static_cast<Self*>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
Self& operator--() {
|
Self& operator--() {
|
||||||
--this->i;
|
--this->i;
|
||||||
return *static_cast<Self*>(this);
|
return *static_cast<Self*>(this);
|
||||||
|
@ -75,6 +88,7 @@ public:
|
||||||
this->operator++();
|
this->operator++();
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
Self operator--(int) {
|
Self operator--(int) {
|
||||||
auto v = *this;
|
auto v = *this;
|
||||||
this->operator--();
|
this->operator--();
|
||||||
|
|
|
@ -7,3 +7,4 @@ endfunction()
|
||||||
qs_test(transformwatcher transformwatcher.cpp)
|
qs_test(transformwatcher transformwatcher.cpp)
|
||||||
qs_test(ringbuffer ringbuf.cpp)
|
qs_test(ringbuffer ringbuf.cpp)
|
||||||
qs_test(scriptmodel scriptmodel.cpp)
|
qs_test(scriptmodel scriptmodel.cpp)
|
||||||
|
qs_test(stacklist stacklist.cpp)
|
||||||
|
|
92
src/core/test/stacklist.cpp
Normal file
92
src/core/test/stacklist.cpp
Normal 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);
|
15
src/core/test/stacklist.hpp
Normal file
15
src/core/test/stacklist.hpp
Normal 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();
|
||||||
|
};
|
Loading…
Add table
Add a link
Reference in a new issue