Skip to content

Commit

Permalink
unit tests and debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
ahigerd committed Jun 29, 2022
1 parent 91639c2 commit 3fd9566
Show file tree
Hide file tree
Showing 9 changed files with 285 additions and 75 deletions.
20 changes: 20 additions & 0 deletions src/platform/qt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,23 @@ if(DISTBUILD AND NOT APPLE)
add_custom_command(TARGET ${BINARY_NAME}-qt POST_BUILD COMMAND "${STRIP}" "$<TARGET_FILE:${BINARY_NAME}-qt>")
endif()
endif()

if(BUILD_SUITE)
enable_testing()
find_package(${QT}Test)
if(${QT}Test_FOUND)
set(TEST_SRC
test/main.cpp
test/library.cpp
test/spanset.cpp
utils.cpp
library/LibraryModel.cpp
)
add_executable(test-platform-qt WIN32 ${TEST_SRC})
target_link_libraries(test-platform-qt ${PLATFORM_LIBRARY} ${BINARY_NAME} ${QT_LIBRARIES} ${QT}::Test)
set_target_properties(test-platform-qt PROPERTIES COMPILE_DEFINITIONS "${FEATURE_DEFINES};${FUNCTION_DEFINES};${OS_DEFINES};${QT_DEFINES}" COMPILE_OPTIONS "${FEATURE_FLAGS}")
add_test(platform-qt test-platform-qt)
else()
message("${QT}Test not found")
endif()
endif()
71 changes: 5 additions & 66 deletions src/platform/qt/library/LibraryModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "LibraryModel.h"

#include "utils.h"
#include "../utils.h"

#include <QApplication>
#include <QDir>
Expand All @@ -16,69 +16,8 @@

using namespace QGBA;

namespace QGBA {

struct SpanSet {
struct Span {
int left;
int right;

bool operator<(const Span& other) const { return left < other.left; }
bool operator>(const Span& other) const { return left > other.left; }
};

void add(int pos) {
bool found = false;
for (Span& span : spans) {
if (pos == span.left - 1) {
span.left = pos;
found = true;
} else if (pos == span.right + 1) {
span.right = pos;
found = true;
}
}
if (!found) {
spans << Span{ pos, pos };
return;
}
}

void merge() {
int numSpans = spans.size();
if (!numSpans) {
return;
}
sort();
QVector<Span> merged({ spans[0] });
int lastRight = merged[0].right;
for (int i = 1; i < numSpans; i++) {
int right = spans[i].right;
if (spans[i].left - 1 <= lastRight) {
merged.back().right = right;
} else {
merged << spans[i];
}
lastRight = right;
}
}

void sort(bool reverse = false) {
if (reverse) {
std::sort(spans.begin(), spans.end(), std::greater<Span>());
} else {
std::sort(spans.begin(), spans.end());
}
}

QVector<Span> spans;
};

}

LibraryModel::LibraryModel(LibraryController* parent)
LibraryModel::LibraryModel(QObject* parent)
: QAbstractItemModel(parent)
, m_controller(parent)
, m_treeMode(false)
, m_showFilename(false)
{
Expand Down Expand Up @@ -288,7 +227,7 @@ QModelIndex LibraryModel::index(int row, int column, const QModelIndex& parent)
if (!parent.isValid()) {
return createIndex(row, column, quintptr(-1));
}
if (!m_treeMode || parent.internalId() != quintptr(-1)) {
if (!m_treeMode || parent.internalId() != quintptr(-1) || parent.column() != 0) {
return QModelIndex();
}
return createIndex(row, column, parent.row());
Expand All @@ -302,7 +241,7 @@ QModelIndex LibraryModel::parent(const QModelIndex& child) const {
}

int LibraryModel::columnCount(const QModelIndex& parent) const {
if (!parent.isValid() || !parent.parent().isValid()) {
if (!parent.isValid() || (parent.column() == 0 && !parent.parent().isValid())) {
return MAX_COLUMN + 1;
}
return 0;
Expand All @@ -311,7 +250,7 @@ int LibraryModel::columnCount(const QModelIndex& parent) const {
int LibraryModel::rowCount(const QModelIndex& parent) const {
if (parent.isValid()) {
if (m_treeMode) {
if (parent.row() < 0 || parent.row() >= m_pathOrder.size()) {
if (parent.row() < 0 || parent.row() >= m_pathOrder.size() || parent.column() != 0) {
return 0;
}
return m_pathIndex[m_pathOrder[parent.row()]].size();
Expand Down
16 changes: 7 additions & 9 deletions src/platform/qt/library/LibraryModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class QTreeView;

namespace QGBA {

class LibraryModel final : public QAbstractItemModel, public AbstractGameList {
class LibraryModel final : public QAbstractItemModel {
Q_OBJECT

public:
Expand All @@ -33,18 +33,18 @@ Q_OBJECT
FullPathRole = Qt::UserRole + 1,
};

explicit LibraryModel(LibraryController* parent = nullptr);
explicit LibraryModel(QObject* parent = nullptr);

bool treeMode() const;
void setTreeMode(bool tree);

bool showFilename() const;
void setShowFilename(bool show) override;
void setShowFilename(bool show);

void resetEntries(const QList<LibraryEntry>& items) override;
void addEntries(const QList<LibraryEntry>& items) override;
void updateEntries(const QList<LibraryEntry>& items) override;
void removeEntries(const QList<QString>& items) override;
void resetEntries(const QList<LibraryEntry>& items);
void addEntries(const QList<LibraryEntry>& items);
void updateEntries(const QList<LibraryEntry>& items);
void removeEntries(const QList<QString>& items);

QModelIndex index(const QString& game) const;
QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
Expand All @@ -65,8 +65,6 @@ Q_OBJECT
void addEntriesTree(const QList<LibraryEntry>& items);
void addEntryInternal(const LibraryEntry& item);

LibraryController* m_controller;

bool m_treeMode;
bool m_showFilename;

Expand Down
90 changes: 90 additions & 0 deletions src/platform/qt/test/library.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* Copyright (c) 2013-2022 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "suite.h"
#include "platform/qt/library/LibraryModel.h"

#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
#include <QAbstractItemModelTester>
#endif

#include <QtDebug>
#include <QSignalSpy>

using namespace QGBA;

class LibraryModelTest : public QObject {
Q_OBJECT

private:
LibraryModel* model = nullptr;

LibraryEntry makeGBA(const QString& base, const QString& name, uint32_t crc) {
LibraryEntry entry;
entry.base = base;
entry.filename = name + ".gba";
entry.fullpath = base + "/" + entry.filename;
entry.title = name;
entry.internalTitle = name.toUpper().toUtf8();
entry.internalCode = entry.internalTitle.replace(" ", "").left(4);
entry.platform = mPLATFORM_GBA;
entry.filesize = entry.fullpath.size() * 4;
entry.crc32 = crc;
return entry;
}

LibraryEntry makeGB(const QString& base, const QString& name, uint32_t crc) {
LibraryEntry entry = makeGBA(base, name, crc);
entry.filename = name + ".gb";
entry.fullpath = base + "/" + entry.filename;
entry.platform = mPLATFORM_GB;
entry.filesize /= 4;;
return entry;
}

void addTestGames1() {
model->addEntries({
makeGBA("/gba", "Test Game", 0x12345678),
makeGBA("/gba", "Another", 0x23456789),
makeGB("/gb", "Old Game", 0x87654321),
});
}

private slots:
void init() {
model = new LibraryModel();
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
new QAbstractItemModelTester(model, QAbstractItemModelTester::FailureReportingMode::QtTest, model);
#endif
}

void cleanup() {
delete model;
model = nullptr;
}

void initList() {
addTestGames1();
QCOMPARE(model->rowCount(), 3);
}

void initTree() {
QSignalSpy resetSpy(model, SIGNAL(modelReset()));
model->setTreeMode(true);
QVERIFY(resetSpy.count());
addTestGames1();
int gbRow = 0, gbaRow = 1;
if (model->index(0, 0).data() == "gba") {
gbaRow = 0;
gbRow = 1;
}
QCOMPARE(model->rowCount(), 2);
QCOMPARE(model->rowCount(model->index(gbRow, 0)), 1);
QCOMPARE(model->rowCount(model->index(gbaRow, 0)), 2);
}
};

M_REGISTER_QT_TEST(LibraryModelTest)
#include "library.moc"
19 changes: 19 additions & 0 deletions src/platform/qt/test/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* Copyright (c) 2013-2022 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "suite.h"

QList<TestRegistrar*> registeredTests;

int main(int argc, char** argv) {
int err = 0;
for (TestRegistrar* test : registeredTests) {
int result = test->main(argc, argv);
if (result && !err) {
err = result;
}
}
return err;
}
62 changes: 62 additions & 0 deletions src/platform/qt/test/spanset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* Copyright (c) 2013-2022 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "suite.h"
#include "platform/qt/utils.h"

#include <QtDebug>

using namespace QGBA;

class SpanSetTest : public QObject {
Q_OBJECT

private:
void debugSpans(const SpanSet& spanSet) {
QStringList debug;
for (auto span : spanSet.spans) {
debug << QStringLiteral("[%1, %2]").arg(span.left).arg(span.right);
}
qDebug() << QStringLiteral("SpanSet{%1}").arg(debug.join(", "));
}

private slots:
void oneSpan() {
SpanSet spanSet;
spanSet.add(1);
spanSet.add(2);
spanSet.add(3);
QCOMPARE(spanSet.spans.size(), 1);
spanSet.merge();
QCOMPARE(spanSet.spans.size(), 1);
}

void twoSpans() {
SpanSet spanSet;
spanSet.add(1);
spanSet.add(2);
spanSet.add(4);
QCOMPARE(spanSet.spans.size(), 2);
spanSet.merge();
QCOMPARE(spanSet.spans.size(), 2);
}

void mergeSpans() {
SpanSet spanSet;
spanSet.add(1);
spanSet.add(3);
spanSet.add(2);
spanSet.add(5);
spanSet.add(4);
spanSet.add(7);
spanSet.add(8);
QCOMPARE(spanSet.spans.size(), 4);
spanSet.merge();
QCOMPARE(spanSet.spans.size(), 2);
}
};

M_REGISTER_QT_TEST(SpanSetTest)
#include "spanset.moc"
23 changes: 23 additions & 0 deletions src/platform/qt/test/suite.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Copyright (c) 2013-2022 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#pragma once

#define QT_NO_OPENGL

#include <QtTest>
#include <QList>

struct TestRegistrar {
virtual int main(int argc, char** argv) = 0;
};

extern QList<TestRegistrar*> registeredTests;

#define M_REGISTER_QT_TEST(Class) \
static struct Class ## Registrar : TestRegistrar { \
QTEST_MAIN(Class) \
Class ## Registrar() { registeredTests << this; } \
} registrar_ ## Class;
Loading

0 comments on commit 3fd9566

Please sign in to comment.