tst_qbarset.cpp
520 lines
| 12.9 KiB
| text/x-c
|
CppLexer
|
r1076 | /**************************************************************************** | ||
** | ||||
** Copyright (C) 2012 Digia Plc | ||||
** All rights reserved. | ||||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
#include <QtTest/QtTest> | ||||
#include <qbarset.h> | ||||
|
r1594 | #include <qbarseries.h> | ||
|
r1335 | #include <qchartview.h> | ||
|
r2241 | #include "tst_definitions.h" | ||
|
r1076 | |||
QTCOMMERCIALCHART_USE_NAMESPACE | ||||
class tst_QBarSet : public QObject | ||||
{ | ||||
Q_OBJECT | ||||
public slots: | ||||
void initTestCase(); | ||||
void cleanupTestCase(); | ||||
void init(); | ||||
void cleanup(); | ||||
private slots: | ||||
void qbarset_data(); | ||||
void qbarset(); | ||||
|
r1429 | void label_data(); | ||
void label(); | ||||
|
r1076 | void append_data(); | ||
void append(); | ||||
void appendOperator_data(); | ||||
void appendOperator(); | ||||
void insert_data(); | ||||
void insert(); | ||||
void remove_data(); | ||||
void remove(); | ||||
void replace_data(); | ||||
void replace(); | ||||
void at_data(); | ||||
void at(); | ||||
void atOperator_data(); | ||||
void atOperator(); | ||||
void count_data(); | ||||
void count(); | ||||
void sum_data(); | ||||
void sum(); | ||||
|
r1335 | void customize(); | ||
|
r1158 | |||
|
r1076 | private: | ||
QBarSet* m_barset; | ||||
}; | ||||
void tst_QBarSet::initTestCase() | ||||
{ | ||||
} | ||||
void tst_QBarSet::cleanupTestCase() | ||||
{ | ||||
} | ||||
void tst_QBarSet::init() | ||||
{ | ||||
|
r1429 | m_barset = new QBarSet(QString("label")); | ||
|
r1076 | } | ||
void tst_QBarSet::cleanup() | ||||
{ | ||||
delete m_barset; | ||||
m_barset = 0; | ||||
} | ||||
void tst_QBarSet::qbarset_data() | ||||
{ | ||||
} | ||||
void tst_QBarSet::qbarset() | ||||
{ | ||||
|
r1429 | QBarSet barset(QString("label")); | ||
QCOMPARE(barset.label(), QString("label")); | ||||
|
r1076 | QCOMPARE(barset.count(), 0); | ||
|
r2242 | QVERIFY(qFuzzyCompare(barset.sum(), 0)); | ||
|
r1076 | } | ||
|
r1429 | void tst_QBarSet::label_data() | ||
|
r1076 | { | ||
|
r1429 | QTest::addColumn<QString> ("label"); | ||
|
r1100 | QTest::addColumn<QString> ("result"); | ||
|
r1429 | QTest::newRow("label0") << QString("label0") << QString("label0"); | ||
QTest::newRow("label1") << QString("label1") << QString("label1"); | ||||
|
r1076 | } | ||
|
r1429 | void tst_QBarSet::label() | ||
|
r1076 | { | ||
|
r1429 | QFETCH(QString, label); | ||
|
r1100 | QFETCH(QString, result); | ||
|
r1076 | |||
|
r1429 | QSignalSpy labelSpy(m_barset,SIGNAL(labelChanged())); | ||
m_barset->setLabel(label); | ||||
QCOMPARE(m_barset->label(), result); | ||||
QVERIFY(labelSpy.count() == 1); | ||||
|
r1076 | } | ||
void tst_QBarSet::append_data() | ||||
{ | ||||
QTest::addColumn<int> ("count"); | ||||
|
r1100 | QTest::newRow("0") << 0; | ||
QTest::newRow("5") << 5; | ||||
QTest::newRow("100") << 100; | ||||
QTest::newRow("1000") << 1000; | ||||
|
r1076 | } | ||
void tst_QBarSet::append() | ||||
{ | ||||
QFETCH(int, count); | ||||
QCOMPARE(m_barset->count(), 0); | ||||
|
r2242 | QVERIFY(qFuzzyCompare(m_barset->sum(), 0)); | ||
|
r1076 | |||
|
r1465 | QSignalSpy valueSpy(m_barset, SIGNAL(valuesAdded(int,int))); | ||
|
r1353 | |||
|
r1076 | qreal sum(0.0); | ||
qreal value(0.0); | ||||
for (int i=0; i<count; i++) { | ||||
m_barset->append(value); | ||||
|
r1580 | QCOMPARE(m_barset->at(i), value); | ||
|
r1076 | sum += value; | ||
value += 1.0; | ||||
} | ||||
QCOMPARE(m_barset->count(), count); | ||||
QVERIFY(qFuzzyCompare(m_barset->sum(), sum)); | ||||
|
r1353 | |||
|
r1465 | QCOMPARE(valueSpy.count(), count); | ||
|
r1076 | } | ||
void tst_QBarSet::appendOperator_data() | ||||
{ | ||||
append_data(); | ||||
} | ||||
void tst_QBarSet::appendOperator() | ||||
{ | ||||
QFETCH(int, count); | ||||
QCOMPARE(m_barset->count(), 0); | ||||
|
r2242 | QVERIFY(qFuzzyCompare(m_barset->sum(), 0)); | ||
|
r1076 | |||
|
r1353 | QSignalSpy valueSpy(m_barset,SIGNAL(valuesAdded(int,int))); | ||
|
r1076 | qreal sum(0.0); | ||
qreal value(0.0); | ||||
for (int i=0; i<count; i++) { | ||||
*m_barset << value; | ||||
|
r1580 | QCOMPARE(m_barset->at(i), value); | ||
|
r1076 | sum += value; | ||
value += 1.0; | ||||
} | ||||
QCOMPARE(m_barset->count(), count); | ||||
QVERIFY(qFuzzyCompare(m_barset->sum(), sum)); | ||||
|
r1465 | QCOMPARE(valueSpy.count(), count); | ||
|
r1076 | } | ||
void tst_QBarSet::insert_data() | ||||
{ | ||||
} | ||||
void tst_QBarSet::insert() | ||||
{ | ||||
QCOMPARE(m_barset->count(), 0); | ||||
|
r2242 | QVERIFY(qFuzzyCompare(m_barset->sum(), 0)); | ||
|
r1353 | QSignalSpy valueSpy(m_barset,SIGNAL(valuesAdded(int,int))); | ||
|
r1076 | |||
m_barset->insert(0, 1.0); // 1.0 | ||||
|
r1580 | QCOMPARE(m_barset->at(0), 1.0); | ||
|
r1076 | QCOMPARE(m_barset->count(), 1); | ||
|
r1680 | QVERIFY(qFuzzyCompare(m_barset->sum(), (qreal)1.0)); | ||
|
r1076 | |||
m_barset->insert(0, 2.0); // 2.0 1.0 | ||||
|
r1580 | QCOMPARE(m_barset->at(0), 2.0); | ||
QCOMPARE(m_barset->at(1), 1.0); | ||||
|
r1076 | QCOMPARE(m_barset->count(), 2); | ||
|
r1680 | QVERIFY(qFuzzyCompare(m_barset->sum(), (qreal)3.0)); | ||
|
r1076 | |||
m_barset->insert(1, 3.0); // 2.0 3.0 1.0 | ||||
|
r1580 | QCOMPARE(m_barset->at(1), 3.0); | ||
QCOMPARE(m_barset->at(0), 2.0); | ||||
QCOMPARE(m_barset->at(2), 1.0); | ||||
|
r1076 | QCOMPARE(m_barset->count(), 3); | ||
|
r1680 | QVERIFY(qFuzzyCompare(m_barset->sum(), (qreal)6.0)); | ||
|
r1465 | QCOMPARE(valueSpy.count(), 3); | ||
|
r1076 | } | ||
void tst_QBarSet::remove_data() | ||||
{ | ||||
} | ||||
void tst_QBarSet::remove() | ||||
{ | ||||
QCOMPARE(m_barset->count(), 0); | ||||
|
r2242 | QVERIFY(qFuzzyCompare(m_barset->sum(), 0)); | ||
|
r1076 | |||
|
r1353 | QSignalSpy valueSpy(m_barset,SIGNAL(valuesRemoved(int,int))); | ||
|
r1076 | m_barset->append(1.0); | ||
m_barset->append(2.0); | ||||
m_barset->append(3.0); | ||||
m_barset->append(4.0); | ||||
QCOMPARE(m_barset->count(), 4); | ||||
QCOMPARE(m_barset->sum(), 10.0); | ||||
|
r1512 | // Remove middle | ||
|
r1076 | m_barset->remove(2); // 1.0 2.0 4.0 | ||
|
r1580 | QCOMPARE(m_barset->at(0), 1.0); | ||
QCOMPARE(m_barset->at(1), 2.0); | ||||
QCOMPARE(m_barset->at(2), 4.0); | ||||
|
r1076 | QCOMPARE(m_barset->count(), 3); | ||
QCOMPARE(m_barset->sum(), 7.0); | ||||
|
r1514 | QCOMPARE(valueSpy.count(), 1); | ||
QList<QVariant> valueSpyArg = valueSpy.takeFirst(); | ||||
// Verify index of removed signal | ||||
QVERIFY(valueSpyArg.at(0).type() == QVariant::Int); | ||||
QVERIFY(valueSpyArg.at(0).toInt() == 2); | ||||
// Verify count of removed signal | ||||
QVERIFY(valueSpyArg.at(1).type() == QVariant::Int); | ||||
QVERIFY(valueSpyArg.at(1).toInt() == 1); | ||||
|
r1076 | |||
|
r1512 | // Remove first | ||
|
r1076 | m_barset->remove(0); // 2.0 4.0 | ||
|
r1580 | QCOMPARE(m_barset->at(0), 2.0); | ||
QCOMPARE(m_barset->at(1), 4.0); | ||||
|
r1076 | QCOMPARE(m_barset->count(), 2); | ||
QCOMPARE(m_barset->sum(), 6.0); | ||||
|
r1353 | |||
|
r1514 | QCOMPARE(valueSpy.count(), 1); | ||
valueSpyArg = valueSpy.takeFirst(); | ||||
// Verify index of removed signal | ||||
QVERIFY(valueSpyArg.at(0).type() == QVariant::Int); | ||||
QVERIFY(valueSpyArg.at(0).toInt() == 0); | ||||
// Verify count of removed signal | ||||
QVERIFY(valueSpyArg.at(1).type() == QVariant::Int); | ||||
QVERIFY(valueSpyArg.at(1).toInt() == 1); | ||||
|
r1512 | // Illegal indexes | ||
m_barset->remove(4); | ||||
QCOMPARE(m_barset->count(), 2); | ||||
QCOMPARE(m_barset->sum(), 6.0); | ||||
m_barset->remove(-1); | ||||
QCOMPARE(m_barset->count(), 2); | ||||
QCOMPARE(m_barset->sum(), 6.0); | ||||
|
r1514 | // nothing removed, no signals should be emitted | ||
QCOMPARE(valueSpy.count(), 0); | ||||
// Remove more items than list has | ||||
m_barset->remove(0,312); | ||||
QCOMPARE(m_barset->count(), 0); | ||||
|
r2242 | QVERIFY(qFuzzyCompare(m_barset->sum(), 0)); | ||
|
r1514 | |||
QCOMPARE(valueSpy.count(), 1); | ||||
valueSpyArg = valueSpy.takeFirst(); | ||||
// Verify index of removed signal | ||||
QVERIFY(valueSpyArg.at(0).type() == QVariant::Int); | ||||
QVERIFY(valueSpyArg.at(0).toInt() == 0); | ||||
// Verify count of removed signal (expect 2 values removed, because list had only 2 items) | ||||
QVERIFY(valueSpyArg.at(1).type() == QVariant::Int); | ||||
QVERIFY(valueSpyArg.at(1).toInt() == 2); | ||||
|
r1076 | } | ||
void tst_QBarSet::replace_data() | ||||
{ | ||||
} | ||||
void tst_QBarSet::replace() | ||||
{ | ||||
QCOMPARE(m_barset->count(), 0); | ||||
|
r2242 | QVERIFY(qFuzzyCompare(m_barset->sum(), 0)); | ||
|
r1353 | QSignalSpy valueSpy(m_barset,SIGNAL(valueChanged(int))); | ||
|
r1076 | |||
m_barset->append(1.0); | ||||
m_barset->append(2.0); | ||||
m_barset->append(3.0); | ||||
m_barset->append(4.0); | ||||
QCOMPARE(m_barset->count(), 4); | ||||
QCOMPARE(m_barset->sum(), 10.0); | ||||
|
r1512 | // Replace first | ||
|
r1076 | m_barset->replace(0, 5.0); // 5.0 2.0 3.0 4.0 | ||
QCOMPARE(m_barset->count(), 4); | ||||
QCOMPARE(m_barset->sum(), 14.0); | ||||
|
r1580 | QCOMPARE(m_barset->at(0), 5.0); | ||
|
r1076 | |||
|
r1512 | // Replace last | ||
|
r1076 | m_barset->replace(3, 6.0); | ||
QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0 | ||||
QCOMPARE(m_barset->sum(), 16.0); | ||||
|
r1580 | QCOMPARE(m_barset->at(0), 5.0); | ||
QCOMPARE(m_barset->at(1), 2.0); | ||||
QCOMPARE(m_barset->at(2), 3.0); | ||||
QCOMPARE(m_barset->at(3), 6.0); | ||||
|
r1353 | |||
|
r1512 | // Illegal indexes | ||
m_barset->replace(4, 6.0); | ||||
QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0 | ||||
QCOMPARE(m_barset->sum(), 16.0); | ||||
m_barset->replace(-1, 6.0); | ||||
QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0 | ||||
QCOMPARE(m_barset->sum(), 16.0); | ||||
|
r1580 | m_barset->replace(4, 1.0); | ||
|
r1512 | QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0 | ||
QCOMPARE(m_barset->sum(), 16.0); | ||||
|
r1580 | m_barset->replace(-1, 1.0); | ||
|
r1512 | QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0 | ||
QCOMPARE(m_barset->sum(), 16.0); | ||||
|
r1353 | QVERIFY(valueSpy.count() == 2); | ||
|
r1076 | } | ||
void tst_QBarSet::at_data() | ||||
{ | ||||
} | ||||
void tst_QBarSet::at() | ||||
{ | ||||
QCOMPARE(m_barset->count(), 0); | ||||
|
r2242 | QVERIFY(qFuzzyCompare(m_barset->sum(), 0)); | ||
|
r1076 | |||
m_barset->append(1.0); | ||||
m_barset->append(2.0); | ||||
m_barset->append(3.0); | ||||
m_barset->append(4.0); | ||||
|
r1580 | QCOMPARE(m_barset->at(0), 1.0); | ||
QCOMPARE(m_barset->at(1), 2.0); | ||||
QCOMPARE(m_barset->at(2), 3.0); | ||||
QCOMPARE(m_barset->at(3), 4.0); | ||||
|
r1076 | } | ||
void tst_QBarSet::atOperator_data() | ||||
{ | ||||
} | ||||
void tst_QBarSet::atOperator() | ||||
{ | ||||
QCOMPARE(m_barset->count(), 0); | ||||
|
r2242 | QVERIFY(qFuzzyCompare(m_barset->sum(), 0)); | ||
|
r1076 | |||
m_barset->append(1.0); | ||||
m_barset->append(2.0); | ||||
m_barset->append(3.0); | ||||
m_barset->append(4.0); | ||||
|
r1580 | QCOMPARE(m_barset->operator [](0), 1.0); | ||
QCOMPARE(m_barset->operator [](1), 2.0); | ||||
QCOMPARE(m_barset->operator [](2), 3.0); | ||||
QCOMPARE(m_barset->operator [](3), 4.0); | ||||
|
r1076 | } | ||
void tst_QBarSet::count_data() | ||||
{ | ||||
} | ||||
void tst_QBarSet::count() | ||||
{ | ||||
QCOMPARE(m_barset->count(), 0); | ||||
|
r2242 | QVERIFY(qFuzzyCompare(m_barset->sum(), 0)); | ||
|
r1076 | |||
m_barset->append(1.0); | ||||
QCOMPARE(m_barset->count(),1); | ||||
m_barset->append(2.0); | ||||
QCOMPARE(m_barset->count(),2); | ||||
m_barset->append(3.0); | ||||
QCOMPARE(m_barset->count(),3); | ||||
m_barset->append(4.0); | ||||
QCOMPARE(m_barset->count(),4); | ||||
} | ||||
void tst_QBarSet::sum_data() | ||||
{ | ||||
} | ||||
void tst_QBarSet::sum() | ||||
{ | ||||
QCOMPARE(m_barset->count(), 0); | ||||
|
r2242 | QVERIFY(qFuzzyCompare(m_barset->sum(), 0)); | ||
|
r1076 | |||
m_barset->append(1.0); | ||||
|
r1680 | QVERIFY(qFuzzyCompare(m_barset->sum(), (qreal)1.0)); | ||
|
r1076 | m_barset->append(2.0); | ||
|
r1680 | QVERIFY(qFuzzyCompare(m_barset->sum(), (qreal)3.0)); | ||
|
r1076 | m_barset->append(3.0); | ||
|
r1680 | QVERIFY(qFuzzyCompare(m_barset->sum(), (qreal)6.0)); | ||
|
r1076 | m_barset->append(4.0); | ||
|
r1680 | QVERIFY(qFuzzyCompare(m_barset->sum(), (qreal)10.0)); | ||
|
r1076 | } | ||
|
r1335 | void tst_QBarSet::customize() | ||
{ | ||||
// Create sets | ||||
QBarSet *set1 = new QBarSet("set1"); | ||||
QBarSet *set2 = new QBarSet("set2"); | ||||
// Append set1 to series | ||||
|
r1594 | QBarSeries *series = new QBarSeries(); | ||
|
r1339 | bool success = series->append(set1); | ||
QVERIFY(success); | ||||
|
r1335 | |||
// Add series to the chart | ||||
QChartView view(new QChart()); | ||||
view.resize(200, 200); | ||||
view.chart()->addSeries(series); | ||||
view.show(); | ||||
QTest::qWaitForWindowShown(&view); | ||||
// Test adding data to the sets | ||||
*set1 << 1 << 2 << 1 << 3; | ||||
*set2 << 2 << 1 << 3 << 1; | ||||
|
r1789 | // Remove sets from series | ||
|
r1820 | series->take(set1); | ||
series->take(set2); | ||||
|
r1789 | |||
|
r1335 | // Test pen | ||
QVERIFY(set1->pen() != QPen()); | ||||
QVERIFY(set2->pen() == QPen()); | ||||
QPen pen(QColor(128,128,128,128)); | ||||
set1->setPen(pen); | ||||
|
r1789 | |||
// Add sets back to series | ||||
series->append(set1); | ||||
series->append(set2); | ||||
QVERIFY(set1->pen() == pen); // Should be customized | ||||
QVERIFY(set2->pen() != QPen()); // Should be decorated by theme | ||||
// Remove sets from series | ||||
|
r1820 | series->take(set1); | ||
series->take(set2); | ||||
|
r1335 | |||
// Test brush | ||||
|
r1789 | set2->setBrush(QBrush()); | ||
|
r1335 | QVERIFY(set1->brush() != QBrush()); | ||
QVERIFY(set2->brush() == QBrush()); | ||||
QBrush brush(QColor(128,128,128,128)); | ||||
set1->setBrush(brush); | ||||
|
r1789 | |||
// Add sets back to series | ||||
series->append(set1); | ||||
series->append(set2); | ||||
QVERIFY(set1->brush() == brush); // Should be customized | ||||
QVERIFY(set2->brush() != QBrush()); // Should be decorated by theme | ||||
// Remove sets from series | ||||
|
r1820 | series->take(set1); | ||
series->take(set2); | ||||
|
r1335 | |||
// Test label brush | ||||
|
r1789 | set2->setLabelBrush(QBrush()); | ||
|
r1335 | QVERIFY(set1->labelBrush() != QBrush()); | ||
QVERIFY(set2->labelBrush() == QBrush()); | ||||
set1->setLabelBrush(brush); | ||||
|
r1789 | |||
series->append(set1); | ||||
series->append(set2); | ||||
QVERIFY(set1->labelBrush() == brush); // Should be customized | ||||
QVERIFY(set2->labelBrush() != QBrush()); // Should be decorated by theme | ||||
|
r1335 | |||
// Test label font | ||||
// Note: QFont empty constructor creates font with application's default font, so the font may or may not be the | ||||
// same for the set added to the series (depending on the QChart's theme configuration) | ||||
QVERIFY(set1->labelFont() != QFont() || set1->labelFont() == QFont()); | ||||
QVERIFY(set2->labelFont() == QFont()); | ||||
|
r1158 | QFont font; | ||
font.setBold(true); | ||||
font.setItalic(true); | ||||
|
r1335 | set1->setLabelFont(font); | ||
QVERIFY(set1->labelFont() == font); | ||||
QVERIFY(set2->labelFont() == QFont()); | ||||
|
r1158 | |||
|
r1335 | // Test adding data to the sets | ||
*set1 << 1 << 2 << 1 << 3; | ||||
*set2 << 2 << 1 << 3 << 1; | ||||
|
r1789 | |||
|
r1158 | } | ||
|
r1789 | |||
|
r1076 | QTEST_MAIN(tst_QBarSet) | ||
#include "tst_qbarset.moc" | ||||