First Commit

This commit is contained in:
xiaolinquan committed 2026-11-06 01:04:35 +08:00
commit 3796ec5cd2
146 files changed
+4825

No files matched your search

@@ -0,0 +1,119 @@
#include <QtTest>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QSpinBox>
#include <QTabWidget>
#include <QLabel>
#include <QSet>
#include "roster.h"
#include "mainwindow.h"
#include <QApplication>
#include <QDir>
#include "appearance.h"
static void savePreview(QWidget &window, const QString &fileName)
{
const QString folder = QString::fromLocal8Bit(qgetenv("QT_TEACHING_SCREENSHOTS"));
if (folder.isEmpty()) return;
QDir().mkpath(folder);
window.show();
QApplication::processEvents();
QVERIFY(window.grab().save(QDir(folder).filePath(fileName)));
}
class RaffleTest : public QObject {
Q_OBJECT
private slots:
void initTestCase() { applyAppearance(*qApp); }
void parsing()
{
auto parsed = Roster::parse(QStringLiteral(" 01 张三 \r\n\n02 李四\r\n"));
QVERIFY(parsed.ok()); QCOMPARE(parsed.names.size(), 2);
QCOMPARE(parsed.names.first(), QStringLiteral("01 张三"));
QVERIFY(!Roster::parse(QStringLiteral("张三\n 张三 ")).ok());
QVERIFY(!Roster::parse(QStringLiteral(" \n ")).ok());
QVERIFY(Roster::parse(QStringLiteral("01 张三\n02 张三")).ok());
QVERIFY(!Roster::parse(QString(101, QLatin1Char('x'))).ok());
}
void drawingWithoutReplacement()
{
Roster::Session session;
const QStringList names = {QStringLiteral("A"), QStringLiteral("B"),
QStringLiteral("C"), QStringLiteral("D")};
session.setNames(names);
QRandomGenerator random(12345);
QVERIFY(session.draw(5, random).isEmpty());
QCOMPARE(session.remainingCount(), 4);
QStringList drawn = session.draw(2, random);
drawn.append(session.draw(2, random));
QSet<QString> unique;
for (const QString &name : drawn) unique.insert(name);
QCOMPARE(unique.size(), 4);
QCOMPARE(session.remainingCount(), 0);
QVERIFY(session.draw(1, random).isEmpty());
session.resetDraw();
QCOMPARE(session.remainingCount(), 4);
}
void groupingInvariants()
{
QRandomGenerator random(54321);
for (int n = 1; n <= 100; ++n) {
QStringList names;
for (int i = 0; i < n; ++i) names.append(QString::number(i));
Roster::Session session; session.setNames(names);
session.draw(1, random); // 分组仍使用全部名单。
for (bool byCount : {false, true}) {
const int value = byCount ? qMin(n, 7) : 7;
const auto groups = session.groups(value, byCount, random);
QCOMPARE(groups.size(), byCount ? value : (n + value - 1) / value);
QSet<QString> all; int total = 0, smallest = n, largest = 0;
for (const QStringList &group : groups) {
QVERIFY(!group.isEmpty());
smallest = qMin(smallest, static_cast<int>(group.size()));
largest = qMax(largest, static_cast<int>(group.size()));
total += static_cast<int>(group.size());
for (const QString &name : group) all.insert(name);
}
QCOMPARE(total, n); QCOMPARE(all.size(), n);
QVERIFY(largest - smallest <= 1);
if (!byCount) QVERIFY(largest <= value);
}
QVERIFY(session.groups(n + 1, true, random).isEmpty());
}
}
void uiWorkflow()
{
MainWindow w; w.show();
auto *names = w.findChild<QPlainTextEdit *>(QStringLiteral("namesEdit"));
auto *apply = w.findChild<QPushButton *>(QStringLiteral("applyButton"));
auto *draw = w.findChild<QPushButton *>(QStringLiteral("drawButton"));
auto *chosen = w.findChild<QPlainTextEdit *>(QStringLiteral("chosenEdit"));
QVERIFY(!draw->isEnabled());
QTest::mouseClick(apply, Qt::LeftButton);
QVERIFY(draw->isEnabled());
QTest::mouseClick(draw, Qt::LeftButton);
QVERIFY(!chosen->toPlainText().isEmpty());
savePreview(w, QStringLiteral("03_raffle.png"));
w.findChild<QTabWidget *>(QStringLiteral("resultTabs"))->setCurrentIndex(1);
QTest::mouseClick(w.findChild<QPushButton *>(QStringLiteral("groupsButton")), Qt::LeftButton);
auto *groups = w.findChild<QPlainTextEdit *>(QStringLiteral("groupsEdit"));
QVERIFY(groups->toPlainText().contains(QStringLiteral("第 3 组")));
savePreview(w, QStringLiteral("03_groups.png"));
names->setPlainText(QStringLiteral("A\nA"));
QVERIFY(!draw->isEnabled());
QVERIFY(groups->toPlainText().isEmpty());
QTest::mouseClick(apply, Qt::LeftButton);
QVERIFY(w.findChild<QLabel *>(QStringLiteral("statusLabel"))->text().contains(QStringLiteral("重复")));
names->setPlainText(QStringLiteral("A\nB"));
QTest::mouseClick(apply, Qt::LeftButton);
w.findChild<QTabWidget *>(QStringLiteral("resultTabs"))->setCurrentIndex(0);
w.findChild<QSpinBox *>(QStringLiteral("drawCountSpin"))->setValue(2);
QTest::mouseClick(draw, Qt::LeftButton);
QVERIFY(!draw->isEnabled());
QTest::mouseClick(w.findChild<QPushButton *>(QStringLiteral("resetButton")), Qt::LeftButton);
QVERIFY(draw->isEnabled());
}
};
QTEST_MAIN(RaffleTest)
#include "test_RaffleGroups.moc"
@@ -0,0 +1,11 @@
QT += widgets testlib
CONFIG += c++14 console testcase
greaterThan(QT_MAJOR_VERSION, 5): CONFIG += c++17
TEMPLATE = app
TARGET = test_RaffleGroups
INCLUDEPATH += ..
SOURCES += test_RaffleGroups.cpp ../mainwindow.cpp ../roster.cpp
HEADERS += ../mainwindow.h ../roster.h
FORMS += ../mainwindow.ui
msvc: QMAKE_CXXFLAGS += /utf-8