version / print-version (push) Successful in 2s
Users need to see at a glance what fills a disk and to remove the largest entries without digging through a file manager. The treemap makes sizes comparable, the scan runs off the UI thread, and removing an entry updates the tree in place instead of rescanning.
85 lines
2.7 KiB
C++
85 lines
2.7 KiB
C++
#include "core/treemap-layout.h"
|
|
|
|
#include <QRectF>
|
|
#include <QtTest>
|
|
|
|
class TreemapLayoutTest : public QObject {
|
|
Q_OBJECT
|
|
|
|
private slots:
|
|
void emptyInputYieldsNoTiles();
|
|
void tilesStayInsideBounds();
|
|
void tileAreasAreProportional();
|
|
void zeroSizesGetNoTile();
|
|
void singleItemFillsTheBounds();
|
|
};
|
|
|
|
void TreemapLayoutTest::emptyInputYieldsNoTiles()
|
|
{
|
|
QVERIFY(squarify({}, QRectF(0, 0, 400, 300)).isEmpty());
|
|
QVERIFY(squarify({0, 0}, QRectF(0, 0, 400, 300)).isEmpty());
|
|
QVERIFY(squarify({10, 20}, QRectF(0, 0, 0, 300)).isEmpty());
|
|
}
|
|
|
|
void TreemapLayoutTest::tilesStayInsideBounds()
|
|
{
|
|
const QRectF bounds(10, 20, 800, 500);
|
|
const QVector<qreal> sizes{50, 25, 15, 5, 3, 1, 1};
|
|
const QVector<TreemapTile> tiles = squarify(sizes, bounds);
|
|
QCOMPARE(tiles.size(), sizes.size());
|
|
|
|
const QRectF allowed = bounds.adjusted(-0.01, -0.01, 0.01, 0.01);
|
|
for (const TreemapTile &tile : tiles) {
|
|
QVERIFY(allowed.contains(tile.rect));
|
|
QVERIFY(tile.rect.width() > 0.0);
|
|
QVERIFY(tile.rect.height() > 0.0);
|
|
}
|
|
|
|
for (int i = 0; i < tiles.size(); ++i) {
|
|
for (int j = i + 1; j < tiles.size(); ++j) {
|
|
const QRectF overlap = tiles.at(i).rect.intersected(tiles.at(j).rect);
|
|
QVERIFY(qFuzzyIsNull(overlap.width() * overlap.height()));
|
|
}
|
|
}
|
|
}
|
|
|
|
void TreemapLayoutTest::tileAreasAreProportional()
|
|
{
|
|
const QRectF bounds(0, 0, 800, 600);
|
|
const QVector<qreal> sizes{50, 25, 15, 10};
|
|
const QVector<TreemapTile> tiles = squarify(sizes, bounds);
|
|
QCOMPARE(tiles.size(), sizes.size());
|
|
|
|
const qreal total = 100.0;
|
|
for (const TreemapTile &tile : tiles) {
|
|
const qreal expected = bounds.width() * bounds.height() * sizes.at(tile.index) / total;
|
|
const qreal actual = tile.rect.width() * tile.rect.height();
|
|
QVERIFY2(qAbs(actual - expected) / expected < 0.01,
|
|
qPrintable(QStringLiteral("tile %1: expected %2, got %3")
|
|
.arg(tile.index)
|
|
.arg(expected)
|
|
.arg(actual)));
|
|
}
|
|
}
|
|
|
|
void TreemapLayoutTest::zeroSizesGetNoTile()
|
|
{
|
|
const QVector<TreemapTile> tiles = squarify({10, 0, 30}, QRectF(0, 0, 200, 100));
|
|
QCOMPARE(tiles.size(), 2);
|
|
for (const TreemapTile &tile : tiles) {
|
|
QVERIFY(tile.index != 1);
|
|
}
|
|
}
|
|
|
|
void TreemapLayoutTest::singleItemFillsTheBounds()
|
|
{
|
|
const QRectF bounds(0, 0, 320, 200);
|
|
const QVector<TreemapTile> tiles = squarify({42}, bounds);
|
|
QCOMPARE(tiles.size(), 1);
|
|
QVERIFY(qAbs(tiles.at(0).rect.width() - bounds.width()) < 1e-6);
|
|
QVERIFY(qAbs(tiles.at(0).rect.height() - bounds.height()) < 1e-6);
|
|
}
|
|
|
|
QTEST_GUILESS_MAIN(TreemapLayoutTest)
|
|
#include "treemap-layout.test.moc"
|