Skip to content

Commit c2ed5bf

Browse files
committed
core/stacklist: add tests
1 parent 6024c37 commit c2ed5bf

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

src/core/stacklist.hpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <iterator>
88
#include <vector>
99

10+
#include <qlist.h>
1011
#include <qtypes.h>
1112

1213
template <class T, size_t N>
@@ -40,13 +41,24 @@ class StackList {
4041
[[nodiscard]] bool operator==(const StackList<T, N>& other) const {
4142
if (other.size != this->size) return false;
4243

43-
for (size_t i = 0; i < this->size; ++i) {
44+
for (size_t i = 0; i != this->size; ++i) {
4445
if (this->operator[](i) != other[i]) return false;
4546
}
4647

4748
return true;
4849
}
4950

51+
[[nodiscard]] QList<T> toList() const {
52+
QList<T> list;
53+
list.reserve(this->size);
54+
55+
for (const auto& entry: *this) {
56+
list.push_back(entry);
57+
}
58+
59+
return list;
60+
}
61+
5062
template <typename Self, typename ListPtr, typename IT>
5163
struct BaseIterator {
5264
using iterator_category = std::bidirectional_iterator_tag;
@@ -65,6 +77,7 @@ class StackList {
6577
++this->i;
6678
return *static_cast<Self*>(this);
6779
}
80+
6881
Self& operator--() {
6982
--this->i;
7083
return *static_cast<Self*>(this);
@@ -75,6 +88,7 @@ class StackList {
7588
this->operator++();
7689
return v;
7790
}
91+
7892
Self operator--(int) {
7993
auto v = *this;
8094
this->operator--();

src/core/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ endfunction()
77
qs_test(transformwatcher transformwatcher.cpp)
88
qs_test(ringbuffer ringbuf.cpp)
99
qs_test(scriptmodel scriptmodel.cpp)
10+
qs_test(stacklist stacklist.cpp)

src/core/test/stacklist.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include "stacklist.hpp"
2+
#include <cstddef>
3+
4+
#include <qlist.h>
5+
#include <qtest.h>
6+
#include <qtestcase.h>
7+
8+
#include "../stacklist.hpp"
9+
10+
void TestStackList::push() {
11+
StackList<int, 2> list;
12+
13+
list.push(1);
14+
list.push(2);
15+
16+
QCOMPARE_EQ(list.toList(), QList({1, 2}));
17+
QCOMPARE_EQ(list.length(), 2);
18+
}
19+
20+
void TestStackList::pushAndGrow() {
21+
StackList<int, 2> list;
22+
23+
list.push(1);
24+
list.push(2);
25+
list.push(3);
26+
list.push(4);
27+
28+
QCOMPARE_EQ(list.toList(), QList({1, 2, 3, 4}));
29+
QCOMPARE_EQ(list.length(), 4);
30+
}
31+
32+
void TestStackList::copy() {
33+
StackList<int, 2> list;
34+
35+
list.push(1);
36+
list.push(2);
37+
list.push(3);
38+
list.push(4);
39+
40+
QCOMPARE_EQ(list.toList(), QList({1, 2, 3, 4}));
41+
QCOMPARE_EQ(list.length(), 4);
42+
43+
auto list2 = list;
44+
45+
QCOMPARE_EQ(list2.toList(), QList({1, 2, 3, 4}));
46+
QCOMPARE_EQ(list2.length(), 4);
47+
QCOMPARE_EQ(list2, list);
48+
}
49+
50+
void TestStackList::viewVla() {
51+
StackList<int, 2> list;
52+
53+
list.push(1);
54+
list.push(2);
55+
56+
QCOMPARE_EQ(list.toList(), QList({1, 2}));
57+
QCOMPARE_EQ(list.length(), 2);
58+
59+
STACKLIST_VLA_VIEW(int, list, listView);
60+
61+
QList<int> ql;
62+
63+
for (size_t i = 0; i != list.length(); ++i) {
64+
ql.push_back(listView[i]); // NOLINT
65+
}
66+
67+
QCOMPARE_EQ(ql, list.toList());
68+
}
69+
70+
void TestStackList::viewVlaGrown() {
71+
StackList<int, 2> list;
72+
73+
list.push(1);
74+
list.push(2);
75+
list.push(3);
76+
list.push(4);
77+
78+
QCOMPARE_EQ(list.toList(), QList({1, 2, 3, 4}));
79+
QCOMPARE_EQ(list.length(), 4);
80+
81+
STACKLIST_VLA_VIEW(int, list, listView);
82+
83+
QList<int> ql;
84+
85+
for (size_t i = 0; i != list.length(); ++i) {
86+
ql.push_back(listView[i]); // NOLINT
87+
}
88+
89+
QCOMPARE_EQ(ql, list.toList());
90+
}
91+
92+
QTEST_MAIN(TestStackList);

src/core/test/stacklist.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <qobject.h>
4+
#include <qtmetamacros.h>
5+
6+
class TestStackList: public QObject {
7+
Q_OBJECT;
8+
9+
private slots:
10+
static void push();
11+
static void pushAndGrow();
12+
static void copy();
13+
static void viewVla();
14+
static void viewVlaGrown();
15+
};

0 commit comments

Comments
 (0)