diff options
author | Qt by Nokia <qt-info@nokia.com> | 2011-04-27 12:05:43 +0200 |
---|---|---|
committer | axis <qt-info@nokia.com> | 2011-04-27 12:05:43 +0200 |
commit | 38be0d13830efd2d98281c645c3a60afe05ffece (patch) | |
tree | 6ea73f3ec77f7d153333779883e8120f82820abe /examples/qtestlib | |
download | qtbase-38be0d13830efd2d98281c645c3a60afe05ffece.tar.gz |
Initial import from the monolithic Qt.
This is the beginning of revision history for this module. If you
want to look at revision history older than this, please refer to the
Qt Git wiki for how to use Git history grafting. At the time of
writing, this wiki is located here:
http://qt.gitorious.org/qt/pages/GitIntroductionWithQt
If you have already performed the grafting and you don't see any
history beyond this commit, try running "git log" with the "--follow"
argument.
Branched from the monolithic repo, Qt master branch, at commit
896db169ea224deb96c59ce8af800d019de63f12
Diffstat (limited to 'examples/qtestlib')
-rw-r--r-- | examples/qtestlib/README | 38 | ||||
-rw-r--r-- | examples/qtestlib/qtestlib.pro | 10 | ||||
-rw-r--r-- | examples/qtestlib/tutorial1/testqstring.cpp | 64 | ||||
-rw-r--r-- | examples/qtestlib/tutorial1/tutorial1.pro | 13 | ||||
-rw-r--r-- | examples/qtestlib/tutorial2/testqstring.cpp | 80 | ||||
-rw-r--r-- | examples/qtestlib/tutorial2/tutorial2.pro | 13 | ||||
-rw-r--r-- | examples/qtestlib/tutorial3/testgui.cpp | 70 | ||||
-rw-r--r-- | examples/qtestlib/tutorial3/tutorial3.pro | 13 | ||||
-rw-r--r-- | examples/qtestlib/tutorial4/testgui.cpp | 90 | ||||
-rw-r--r-- | examples/qtestlib/tutorial4/tutorial4.pro | 13 | ||||
-rw-r--r-- | examples/qtestlib/tutorial5/benchmarking.cpp | 134 | ||||
-rw-r--r-- | examples/qtestlib/tutorial5/containers.cpp | 268 | ||||
-rw-r--r-- | examples/qtestlib/tutorial5/tutorial5.pro | 13 |
13 files changed, 819 insertions, 0 deletions
diff --git a/examples/qtestlib/README b/examples/qtestlib/README new file mode 100644 index 0000000000..e89bf5d726 --- /dev/null +++ b/examples/qtestlib/README @@ -0,0 +1,38 @@ +The QTestLib framework is a tool for unit testing Qt based applications +and libraries. QTestLib provides all the functionality commonly found +in unit testing frameworks as well as extensions for testing graphical +user interfaces. + + +The examples in this directory should be run from the command line so +that output printed to the console can be examined. + +Documentation for these examples can be found via the "Tutorial and Examples" +link in the main Qt documentation. + +Finding the Qt Examples and Demos launcher +========================================== + +On Windows: + +The launcher can be accessed via the Windows Start menu. Select the menu +entry entitled "Qt Examples and Demos" entry in the submenu containing +the Qt tools. + +On Mac OS X: + +For the binary distribution, the qtdemo executable is installed in the +/Developer/Applications/Qt directory. For the source distribution, it is +installed alongside the other Qt tools on the path specified when Qt is +configured. + +On Unix/Linux: + +The qtdemo executable is installed alongside the other Qt tools on the path +specified when Qt is configured. + +On all platforms: + +The source code for the launcher can be found in the demos/qtdemo directory +in the Qt package. This example is built at the same time as the Qt libraries, +tools, examples, and demonstrations. diff --git a/examples/qtestlib/qtestlib.pro b/examples/qtestlib/qtestlib.pro new file mode 100644 index 0000000000..a7357363eb --- /dev/null +++ b/examples/qtestlib/qtestlib.pro @@ -0,0 +1,10 @@ +TEMPLATE = subdirs +SUBDIRS = tutorial1 tutorial2 tutorial3 tutorial4 tutorial5 + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtestlib +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS qtestlib.pro README +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtestlib +INSTALLS += target sources + +symbian: include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) diff --git a/examples/qtestlib/tutorial1/testqstring.cpp b/examples/qtestlib/tutorial1/testqstring.cpp new file mode 100644 index 0000000000..79f2bfd782 --- /dev/null +++ b/examples/qtestlib/tutorial1/testqstring.cpp @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [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] + diff --git a/examples/qtestlib/tutorial1/tutorial1.pro b/examples/qtestlib/tutorial1/tutorial1.pro new file mode 100644 index 0000000000..2dd7aef66c --- /dev/null +++ b/examples/qtestlib/tutorial1/tutorial1.pro @@ -0,0 +1,13 @@ +SOURCES = testqstring.cpp +CONFIG += qtestlib + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtestlib/tutorial1 +sources.files = $$SOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtestlib/tutorial1 +INSTALLS += target sources + +symbian { + TARGET.UID3 = 0xA000C60B + include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) +} diff --git a/examples/qtestlib/tutorial2/testqstring.cpp b/examples/qtestlib/tutorial2/testqstring.cpp new file mode 100644 index 0000000000..cd0e3f186b --- /dev/null +++ b/examples/qtestlib/tutorial2/testqstring.cpp @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#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] + diff --git a/examples/qtestlib/tutorial2/tutorial2.pro b/examples/qtestlib/tutorial2/tutorial2.pro new file mode 100644 index 0000000000..ed60608c1a --- /dev/null +++ b/examples/qtestlib/tutorial2/tutorial2.pro @@ -0,0 +1,13 @@ +SOURCES = testqstring.cpp +CONFIG += qtestlib + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtestlib/tutorial2 +sources.files = $$SOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtestlib/tutorial2 +INSTALLS += target sources + +symbian { + TARGET.UID3 = 0xA000C60C + include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) +} diff --git a/examples/qtestlib/tutorial3/testgui.cpp b/examples/qtestlib/tutorial3/testgui.cpp new file mode 100644 index 0000000000..74b25a8e02 --- /dev/null +++ b/examples/qtestlib/tutorial3/testgui.cpp @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [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] + diff --git a/examples/qtestlib/tutorial3/tutorial3.pro b/examples/qtestlib/tutorial3/tutorial3.pro new file mode 100644 index 0000000000..91024b9470 --- /dev/null +++ b/examples/qtestlib/tutorial3/tutorial3.pro @@ -0,0 +1,13 @@ +SOURCES = testgui.cpp +CONFIG += qtestlib + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtestlib/tutorial3 +sources.files = $$SOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtestlib/tutorial3 +INSTALLS += target sources + +symbian { + TARGET.UID3 = 0xA000C60D + include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) +} diff --git a/examples/qtestlib/tutorial4/testgui.cpp b/examples/qtestlib/tutorial4/testgui.cpp new file mode 100644 index 0000000000..a1fbbb7d71 --- /dev/null +++ b/examples/qtestlib/tutorial4/testgui.cpp @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#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] + diff --git a/examples/qtestlib/tutorial4/tutorial4.pro b/examples/qtestlib/tutorial4/tutorial4.pro new file mode 100644 index 0000000000..6d189a255a --- /dev/null +++ b/examples/qtestlib/tutorial4/tutorial4.pro @@ -0,0 +1,13 @@ +SOURCES = testgui.cpp +CONFIG += qtestlib + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtestlib/tutorial4 +sources.files = $$SOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtestlib/tutorial4 +INSTALLS += target sources + +symbian { + TARGET.UID3 = 0xA000C60E + include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) +} diff --git a/examples/qtestlib/tutorial5/benchmarking.cpp b/examples/qtestlib/tutorial5/benchmarking.cpp new file mode 100644 index 0000000000..604eb20a50 --- /dev/null +++ b/examples/qtestlib/tutorial5/benchmarking.cpp @@ -0,0 +1,134 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#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" diff --git a/examples/qtestlib/tutorial5/containers.cpp b/examples/qtestlib/tutorial5/containers.cpp new file mode 100644 index 0000000000..b96ba31cc3 --- /dev/null +++ b/examples/qtestlib/tutorial5/containers.cpp @@ -0,0 +1,268 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +// This file contains benchmarks for comparing QVector against std::vector + +#include <QtCore> +#include <QVector> +#include <vector> + +#include <qtest.h> + +template <typename T> // T is the item type +class UseCases { +public: + virtual ~UseCases() {} + + // Use case: Insert \a size items into the vector. + virtual void insert(int size) = 0; + + // Use case: Lookup \a size items from the vector. + virtual void lookup(int size) = 0; +}; + +template <typename T> +T * f(T *ts) // dummy function to prevent code from being optimized away by the compiler +{ + return ts; +} + +// This subclass implements the use cases using QVector as efficiently as possible. +template <typename T> +class UseCases_QVector : public UseCases<T> +{ + void insert(int size) + { + QVector<T> v; + T t; + QBENCHMARK { + for (int i = 0; i < size; ++i) + v.append(t); + } + } + + void lookup(int size) + { + QVector<T> v; + + T t; + for (int i = 0; i < size; ++i) + v.append(t); + + T *ts = new T[size]; + QBENCHMARK { + for (int i = 0; i < size; ++i) + ts[i] = v.value(i); + } + f<T>(ts); + delete[] ts; + } +}; + +// This subclass implements the use cases using std::vector as efficiently as possible. +template <typename T> +class UseCases_stdvector : public UseCases<T> +{ + void insert(int size) + { + std::vector<T> v; + T t; + QBENCHMARK { + for (int i = 0; i < size; ++i) + v.push_back(t); + } + } + + void lookup(int size) + { + std::vector<T> v; + + T t; + for (int i = 0; i < size; ++i) + v.push_back(t); + + T *ts = new T[size]; + QBENCHMARK { + for (int i = 0; i < size; ++i) + ts[i] = v[i]; + } + f<T>(ts); + delete[] ts; + } +}; + +struct Large { // A "large" item type + int x[1000]; +}; + +// Symbian devices typically have limited memory +#ifdef Q_OS_SYMBIAN +# define LARGE_MAX_SIZE 2000 +#else +# define LARGE_MAX_SIZE 20000 +#endif + +class tst_vector_vs_std : public QObject +{ + Q_OBJECT +public: + tst_vector_vs_std() + { + useCases_QVector_int = new UseCases_QVector<int>; + useCases_stdvector_int = new UseCases_stdvector<int>; + + useCases_QVector_Large = new UseCases_QVector<Large>; + useCases_stdvector_Large = new UseCases_stdvector<Large>; + } + +private: + UseCases<int> *useCases_QVector_int; + UseCases<int> *useCases_stdvector_int; + UseCases<Large> *useCases_QVector_Large; + UseCases<Large> *useCases_stdvector_Large; + +private slots: + void insert_int_data(); + void insert_int(); + void insert_Large_data(); + void insert_Large(); + void lookup_int_data(); + void lookup_int(); + void lookup_Large_data(); + void lookup_Large(); +}; + +void tst_vector_vs_std::insert_int_data() +{ + QTest::addColumn<bool>("useStd"); + QTest::addColumn<int>("size"); + + for (int size = 10; size < 20000; size += 100) { + const QByteArray sizeString = QByteArray::number(size); + QTest::newRow(("std::vector-int--" + sizeString).constData()) << true << size; + QTest::newRow(("QVector-int--" + sizeString).constData()) << false << size; + } +} + +void tst_vector_vs_std::insert_int() +{ + QFETCH(bool, useStd); + QFETCH(int, size); + + if (useStd) + useCases_stdvector_int->insert(size); + else + useCases_QVector_int->insert(size); +} + +void tst_vector_vs_std::insert_Large_data() +{ + QTest::addColumn<bool>("useStd"); + QTest::addColumn<int>("size"); + + for (int size = 10; size < LARGE_MAX_SIZE; size += 100) { + const QByteArray sizeString = QByteArray::number(size); + QTest::newRow(("std::vector-Large--" + sizeString).constData()) << true << size; + QTest::newRow(("QVector-Large--" + sizeString).constData()) << false << size; + } +} + +void tst_vector_vs_std::insert_Large() +{ + QFETCH(bool, useStd); + QFETCH(int, size); + + if (useStd) + useCases_stdvector_Large->insert(size); + else + useCases_QVector_Large->insert(size); +} + +//! [1] +void tst_vector_vs_std::lookup_int_data() +{ + QTest::addColumn<bool>("useStd"); + QTest::addColumn<int>("size"); + + for (int size = 10; size < 20000; size += 100) { + const QByteArray sizeString = QByteArray::number(size); + QTest::newRow(("std::vector-int--" + sizeString).constData()) << true << size; + QTest::newRow(("QVector-int--" + sizeString).constData()) << false << size; + } +} +//! [1] + +//! [2] +void tst_vector_vs_std::lookup_int() +{ + QFETCH(bool, useStd); + QFETCH(int, size); + + if (useStd) + useCases_stdvector_int->lookup(size); // Create a std::vector and run the benchmark. + else + useCases_QVector_int->lookup(size); // Create a QVector and run the benchmark. +} +//! [2] + +void tst_vector_vs_std::lookup_Large_data() +{ + QTest::addColumn<bool>("useStd"); + QTest::addColumn<int>("size"); + + for (int size = 10; size < LARGE_MAX_SIZE; size += 100) { + const QByteArray sizeString = QByteArray::number(size); + QTest::newRow(("std::vector-Large--" + sizeString).constData()) << true << size; + QTest::newRow(("QVector-Large--" + sizeString).constData()) << false << size; + } +} + +void tst_vector_vs_std::lookup_Large() +{ + QFETCH(bool, useStd); + QFETCH(int, size); + + if (useStd) + useCases_stdvector_Large->lookup(size); + else + useCases_QVector_Large->lookup(size); +} + +QTEST_MAIN(tst_vector_vs_std) +#include "main.moc" diff --git a/examples/qtestlib/tutorial5/tutorial5.pro b/examples/qtestlib/tutorial5/tutorial5.pro new file mode 100644 index 0000000000..47d2e86e08 --- /dev/null +++ b/examples/qtestlib/tutorial5/tutorial5.pro @@ -0,0 +1,13 @@ +SOURCES = benchmarking.cpp +CONFIG += qtestlib + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtestlib/tutorial5 +sources.files = $$SOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtestlib/tutorial5 +INSTALLS += target sources + +symbian { + TARGET.UID3 = 0xA000C60F + include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) +} |