qttset

#sudo apt-get install qt4-dev-tools qt4-demos qt4-doc qt4-qmake
#./assistant-qt4


//add qtestlib to project.pro
CONFIG  += qtestlib


#ls /usr/lib/qt4/examples/qtestlib
#cp /usr/lib/qt4/examples/qtestlib ~/qttest
#cd ~/qttest/tutorial1
#ls
testqstring.cpp  tutorial1.pro
// compile example
#qmake
#make
#./tutorial1 
********* Start testing of TestQString *********
Config: Using QTest library 4.8.1, Qt 4.8.1
PASS   : TestQString::initTestCase()
PASS   : TestQString::toUpper()
PASS   : TestQString::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of TestQString *********
---------------------------
In tutorial1/testqstring.cpp
//! [0]
#include <QtTest/QtTest>
class TestQString: public QObject
{
    Q_OBJECT
private slots:
    void toUpper();
};
//! [0]


//! [1]
void TestQString::toUpper()
{
    QString str = "Hello";
    QCOMPARE(str.toUpper(), QString("HELLO"));
}
//! [1]


//! [2]
QTEST_MAIN(TestQString)
#include "testqstring.moc"
//! [2]
-------------------------------------
In tutorial2/testqstring.cpp
#include <QtTest/QtTest>


//! [0]
class TestQString: public QObject
{
    Q_OBJECT


private slots:
    void toUpper_data();
    void toUpper();
};
//! [0]


//! [1]
void TestQString::toUpper_data()
{
    QTest::addColumn<QString>("string");
    QTest::addColumn<QString>("result");


    QTest::newRow("all lower") << "hello" << "HELLO";
    QTest::newRow("mixed")     << "Hello" << "HELLO";
    QTest::newRow("all upper") << "HELLO" << "HELLO";
}
//! [1]


//! [2]
void TestQString::toUpper()
{
    QFETCH(QString, string);
    QFETCH(QString, result);


    QCOMPARE(string.toUpper(), result);
}
//! [2]


//! [3]
QTEST_MAIN(TestQString)
#include "testqstring.moc"
//! [3]
------------------------
In tutorial3/testgui.cpp
//! [0]
#include <QtGui>
#include <QtTest/QtTest>


class TestGui: public QObject
{
    Q_OBJECT


private slots:
    void testGui();


};
//! [0]


//! [1]
void TestGui::testGui()
{
    QLineEdit lineEdit;


    QTest::keyClicks(&lineEdit, "hello world");


    QCOMPARE(lineEdit.text(), QString("hello world"));
}
//! [1]


//! [2]
QTEST_MAIN(TestGui)
#include "testgui.moc"
//! [2]
----------------------------
In tutorial4/testgui.cpp
#include <QtGui>
#include <QtTest/QtTest>


//! [0]
class TestGui: public QObject
{
    Q_OBJECT


private slots:
    void testGui_data();
    void testGui();
};
//! [0]


//! [1]
void TestGui::testGui_data()
{
    QTest::addColumn<QTestEventList>("events");
    QTest::addColumn<QString>("expected");


    QTestEventList list1;
    list1.addKeyClick('a');
    QTest::newRow("char") << list1 << "a";


    QTestEventList list2;
    list2.addKeyClick('a');
    list2.addKeyClick(Qt::Key_Backspace);
    QTest::newRow("there and back again") << list2 << "";
}
//! [1]


//! [2]
void TestGui::testGui()
{
    QFETCH(QTestEventList, events);
    QFETCH(QString, expected);


    QLineEdit lineEdit;


    events.simulate(&lineEdit);


    QCOMPARE(lineEdit.text(), expected);
}
//! [2]


//! [3]
QTEST_MAIN(TestGui)
#include "testgui.moc"
//! [3]
---------------------------------------
#include <QtGui>
#include <qtest.h>


class TestBenchmark : public QObject
{
    Q_OBJECT
private slots:
    void simple();
    void multiple_data();
    void multiple();
    void series_data();
    void series();
};


//! [0]
void TestBenchmark::simple()
{
    QString str1 = QLatin1String("This is a test string");
    QString str2 = QLatin1String("This is a test string");


    QCOMPARE(str1.localeAwareCompare(str2), 0);


    QBENCHMARK {
        str1.localeAwareCompare(str2);
    }
}
//! [0]


//! [1]
void TestBenchmark::multiple_data()
{
    QTest::addColumn<bool>("useLocaleCompare");
    QTest::newRow("locale aware compare") << true;
    QTest::newRow("standard compare") << false;
}
//! [1]


//! [2]
void TestBenchmark::multiple()
{
    QFETCH(bool, useLocaleCompare);
    QString str1 = QLatin1String("This is a test string");
    QString str2 = QLatin1String("This is a test string");
    
    int result;
    if (useLocaleCompare) {
        QBENCHMARK {
            result = str1.localeAwareCompare(str2);
        }
    } else {
        QBENCHMARK {
            result = (str1 == str2);
        }
    }
}
//! [2]


//! [3]
void TestBenchmark::series_data()
{
    QTest::addColumn<bool>("useLocaleCompare");
    QTest::addColumn<int>("stringSize");
    
    for (int i = 1; i < 10000; i += 2000) {
        QByteArray size = QByteArray::number(i);
        QTest::newRow(("locale aware compare--" + size).constData()) << true << i;
        QTest::newRow(("standard compare--" + size).constData()) << false << i;
    }
}
//! [4]


//! [5]
void TestBenchmark::series()
{
    QFETCH(bool, useLocaleCompare);
    QFETCH(int, stringSize);


    QString str1 = QString().fill('A', stringSize);
    QString str2 = QString().fill('A', stringSize);
    int result;
    if (useLocaleCompare) {
        QBENCHMARK {
            result = str1.localeAwareCompare(str2);
        }
    } else {
        QBENCHMARK {
            result = (str1 == str2);
        }
    }
}
//! [5]


QTEST_MAIN(TestBenchmark)
#include "benchmarking.moc"
---------------------------------------
./tutorial4 --help
 Usage: ./tutorial4 [options] [testfunction[:testdata]]...
    By default, all testfunctions will be run.


 options:
 -functions : Returns a list of current testfunctions
 -datatags  : Returns a list of current data tags.
              A global data tag is preceded by ' __global__ '.
 -xunitxml  : Outputs results as XML XUnit document
 -xml       : Outputs results as XML document
 -lightxml  : Outputs results as stream of XML tags
 -flush     : Flushes the results
 -o filename: Writes all output into a file
 -silent    : Only outputs warnings and failures
 -v1        : Print enter messages for each testfunction
 -v2        : Also print out each QVERIFY/QCOMPARE/QTEST
 -vs        : Print every signal emitted
 -random    : Run testcases within each test in random order
 -seed n    : Positive integer to be used as seed for -random. If not specified,
              the current time will be used as seed.
 -eventdelay ms    : Set default delay for mouse and keyboard simulation to ms milliseconds
 -keydelay ms      : Set default delay for keyboard simulation to ms milliseconds
 -mousedelay ms    : Set default delay for mouse simulation to ms milliseconds
 -keyevent-verbose : Turn on verbose messages for keyboard simulation
 -maxwarnings n    : Sets the maximum amount of messages to output.
                     0 means unlimited, default: 2000
 -nocrashhandler   : Disables the crash handler


 Benchmark related options:
 -callgrind      : Use callgrind to time benchmarks
 -tickcounter    : Use CPU tick counters to time benchmarks
 -eventcounter   : Counts events received during benchmarks
 -minimumvalue n : Sets the minimum acceptable measurement value
 -iterations  n  : Sets the number of accumulation iterations.
 -median  n      : Sets the number of median iterations.
 -vb             : Print out verbose benchmarking information.


 -help      : This help


--------------------------------
valgrind
gdb
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值