diff --git a/sources/test/CMakeLists.txt b/sources/test/CMakeLists.txt index f28898a..0ed3817 100644 --- a/sources/test/CMakeLists.txt +++ b/sources/test/CMakeLists.txt @@ -11,47 +11,53 @@ include_directories( ${Kf5_INCLUDE} ) +## library +set(AWTESTLIBRARY_HEADERS awtestlibrary.h) +set(AWTESTLIBRARY_SOURCES awtestlibrary.cpp) +add_library(${SUBPROJECT}-awtest STATIC ${AWTESTLIBRARY_SOURCES} ${AWTESTLIBRARY_HEADERS}) +target_link_libraries(${SUBPROJECT}-awtest ${Qt_LIBRARIES} ${Qt5Test_LIBRARIES}) +set(LIBRARY_TEST_SET ${SUBPROJECT}-awtest ${PROJECT_LIBRARY} ${Qt_LIBRARIES} ${Qt5Test_LIBRARIES}) ## extensions # abstractextitem set(ABSTRACTEXTITEM_HEADERS testabstractextitem.h) set(ABSTRACTEXTITEM_SOURCES testabstractextitem.cpp) add_executable(${SUBPROJECT}-abstractextitem ${ABSTRACTEXTITEM_HEADERS} ${ABSTRACTEXTITEM_SOURCES}) -target_link_libraries(${SUBPROJECT}-abstractextitem ${PROJECT_LIBRARY} ${Qt_LIBRARIES} ${Qt5Test_LIBRARIES}) +target_link_libraries(${SUBPROJECT}-abstractextitem ${LIBRARY_TEST_SET}) add_test(NAME "AbstractExtItem" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-abstractextitem) # extquotes set(EXTQUOTES_HEADERS testextquotes.h) set(EXTQUOTES_SOURCES testextquotes.cpp) add_executable(${SUBPROJECT}-extquotes ${EXTQUOTES_HEADERS} ${EXTQUOTES_SOURCES}) -target_link_libraries(${SUBPROJECT}-extquotes ${PROJECT_LIBRARY} ${Qt_LIBRARIES} ${Qt5Test_LIBRARIES}) +target_link_libraries(${SUBPROJECT}-extquotes ${LIBRARY_TEST_SET}) add_test(NAME "ExtQuotes" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-extquotes) # extscript set(EXTSCRIPT_HEADERS testextscript.h) set(EXTSCRIPT_SOURCES testextscript.cpp) add_executable(${SUBPROJECT}-extscript ${EXTSCRIPT_HEADERS} ${EXTSCRIPT_SOURCES}) -target_link_libraries(${SUBPROJECT}-extscript ${PROJECT_LIBRARY} ${Qt_LIBRARIES} ${Qt5Test_LIBRARIES}) +target_link_libraries(${SUBPROJECT}-extscript ${LIBRARY_TEST_SET}) add_test(NAME "ExtScript" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-extscript) # extupgrade set(EXTUPGRADE_HEADERS testextupgrade.h) set(EXTUPGRADE_SOURCES testextupgrade.cpp) add_executable(${SUBPROJECT}-extupgrade ${EXTUPGRADE_HEADERS} ${EXTUPGRADE_SOURCES}) -target_link_libraries(${SUBPROJECT}-extupgrade ${PROJECT_LIBRARY} ${Qt_LIBRARIES} ${Qt5Test_LIBRARIES}) +target_link_libraries(${SUBPROJECT}-extupgrade ${LIBRARY_TEST_SET}) add_test(NAME "ExtUpgrade" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-extupgrade) # extweather set(EXTWEATHER_HEADERS testextweather.h) set(EXTWEATHER_SOURCES testextweather.cpp) add_executable(${SUBPROJECT}-extweather ${EXTWEATHER_HEADERS} ${EXTWEATHER_SOURCES}) -target_link_libraries(${SUBPROJECT}-extweather ${PROJECT_LIBRARY} ${Qt_LIBRARIES} ${Qt5Test_LIBRARIES}) +target_link_libraries(${SUBPROJECT}-extweather ${LIBRARY_TEST_SET}) add_test(NAME "ExtWeather" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-extweather) ## formatters # float formatter set(FLOATFORMATTER_HEADERS testfloatformatter.h) set(FLOATFORMATTER_SOURCES testfloatformatter.cpp) add_executable(${SUBPROJECT}-floatformatter ${FLOATFORMATTER_HEADERS} ${FLOATFORMATTER_SOURCES}) -target_link_libraries(${SUBPROJECT}-floatformatter ${PROJECT_LIBRARY} ${Qt_LIBRARIES} ${Qt5Test_LIBRARIES}) -add_test(NAME "FloatFormatter" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-floatformatter) +target_link_libraries(${SUBPROJECT}-floatformatter ${LIBRARY_TEST_SET}) +add_test(NAME "Float4Formatter" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-floatformatter) # no formatter set(NOFORMATTER_HEADERS testnoformatter.h) set(NOFORMATTER_SOURCES testnoformatter.cpp) add_executable(${SUBPROJECT}-noformatter ${NOFORMATTER_HEADERS} ${NOFORMATTER_SOURCES}) -target_link_libraries(${SUBPROJECT}-noformatter ${PROJECT_LIBRARY} ${Qt_LIBRARIES} ${Qt5Test_LIBRARIES}) +target_link_libraries(${SUBPROJECT}-noformatter ${LIBRARY_TEST_SET}) add_test(NAME "NoFormatter" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-noformatter) diff --git a/sources/test/awtestlibrary.cpp b/sources/test/awtestlibrary.cpp new file mode 100644 index 0000000..f1eb1db --- /dev/null +++ b/sources/test/awtestlibrary.cpp @@ -0,0 +1,61 @@ +/*************************************************************************** + * This file is part of awesome-widgets * + * * + * awesome-widgets is free software: you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * awesome-widgets is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with awesome-widgets. If not, see http://www.gnu.org/licenses/ * + ***************************************************************************/ + + +#include "awtestlibrary.h" + + +char AWTestLibrary::randomChar() +{ + return 'A' + (rand() % static_cast('Z' - 'A')); +} + + +double AWTestLibrary::randomDouble() +{ + return static_cast(rand()) / static_cast(RAND_MAX); +} + + +int AWTestLibrary::randomInt(const int max) +{ + return rand() % max; +} + + +QString AWTestLibrary::randomString(const int max) +{ + QString output; + + int count = 1 + randomInt(max); + for (int i = 0; i < count; i++) + output += QChar(randomChar()); + + return output; +} + + +QStringList AWTestLibrary::randomStringList(const int max) +{ + QStringList output; + + int count = 1 + randomInt(max); + for (int i = 0; i < count; i++) + output.append(randomString()); + + return output; +} diff --git a/sources/test/awtestlibrary.h b/sources/test/awtestlibrary.h new file mode 100644 index 0000000..a4d1096 --- /dev/null +++ b/sources/test/awtestlibrary.h @@ -0,0 +1,35 @@ +/*************************************************************************** + * This file is part of awesome-widgets * + * * + * awesome-widgets is free software: you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * awesome-widgets is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with awesome-widgets. If not, see http://www.gnu.org/licenses/ * + ***************************************************************************/ + + +#ifndef AWTESTLIBRARY_H +#define AWTESTLIBRARY_H + +#include + + +namespace AWTestLibrary +{ + char randomChar(); + double randomDouble(); + int randomInt(const int max = 100); + QString randomString(const int max = 100); + QStringList randomStringList(const int max = 100); +}; + + +#endif /* AWTESTLIBRARY_H */ diff --git a/sources/test/testabstractextitem.cpp b/sources/test/testabstractextitem.cpp index dcb48d5..ffb9068 100644 --- a/sources/test/testabstractextitem.cpp +++ b/sources/test/testabstractextitem.cpp @@ -20,6 +20,7 @@ #include +#include "awtestlibrary.h" #include "extupgrade.h" @@ -114,13 +115,10 @@ void TestAbstractExtItem::generateFilename() writeFileName = QString("%1/awesomewidgets/tmp/") .arg(QStandardPaths::writableLocation( QStandardPaths::GenericDataLocation)); - int diff = 'Z' - 'A'; - int count = rand() % 20 + 1; - for (int i = 0; i < count; i++) { - char c = 'A' + (rand() % diff); - fileName += QChar(c); - writeFileName += QChar(c); - } + + QString name = AWTestLibrary::randomString(20); + fileName += name; + writeFileName += name; } diff --git a/sources/test/testextscript.cpp b/sources/test/testextscript.cpp index e5c8619..4ecc050 100644 --- a/sources/test/testextscript.cpp +++ b/sources/test/testextscript.cpp @@ -20,12 +20,13 @@ #include +#include "awtestlibrary.h" #include "extscript.h" void TestExtScript::initTestCase() { - generateRandomString(); + randomString = AWTestLibrary::randomString(); extScript = new ExtScript(nullptr); extScript->setInterval(1); @@ -102,17 +103,4 @@ void TestExtScript::test_copy() } -void TestExtScript::generateRandomString() -{ - randomString.clear(); - - int diff = 'Z' - 'A'; - int count = rand() % 100 + 1; - for (int i = 0; i < count; i++) { - char c = 'A' + (rand() % diff); - randomString += QChar(c); - } -} - - QTEST_MAIN(TestExtScript); diff --git a/sources/test/testextscript.h b/sources/test/testextscript.h index 16cbb2d..01bed73 100644 --- a/sources/test/testextscript.h +++ b/sources/test/testextscript.h @@ -40,7 +40,6 @@ private slots: void test_copy(); private: - void generateRandomString(); ExtScript *extScript = nullptr; QString randomString; }; diff --git a/sources/test/testextupgrade.cpp b/sources/test/testextupgrade.cpp index 3e4f44c..9b149a0 100644 --- a/sources/test/testextupgrade.cpp +++ b/sources/test/testextupgrade.cpp @@ -20,12 +20,14 @@ #include +#include "awtestlibrary.h" #include "extupgrade.h" void TestExtUpgrade::initTestCase() { - generateRandomStrings(); + randomStrings = AWTestLibrary::randomStringList(); + cmd = QString("echo -e '%1'").arg(randomStrings.join(QString("\n"))); extUpgrade = new ExtUpgrade(nullptr); extUpgrade->setInterval(1); @@ -68,7 +70,7 @@ void TestExtUpgrade::test_run() void TestExtUpgrade::test_null() { - int null = rand() % randomStrings.count(); + int null = AWTestLibrary::randomInt(randomStrings.count()); extUpgrade->setNull(null); QSignalSpy spy(extUpgrade, SIGNAL(dataReceived(const QVariantHash &))); extUpgrade->run(); @@ -85,9 +87,9 @@ void TestExtUpgrade::test_null() void TestExtUpgrade::test_filter() { QSet filters; - int count = rand() % randomStrings.count(); + int count = AWTestLibrary::randomInt(randomStrings.count()); for (int i = 0; i < count; i++) { - int index = rand() % randomStrings.count(); + int index = AWTestLibrary::randomInt(randomStrings.count()); filters << randomStrings.at(index); } @@ -120,29 +122,4 @@ void TestExtUpgrade::test_copy() } -QString TestExtUpgrade::generateRandomString() const -{ - QString string; - int diff = 'Z' - 'A'; - int count = rand() % 100 + 1; - for (int i = 0; i < count; i++) { - char c = 'A' + (rand() % diff); - string += QChar(c); - } - - return string; -} - - -void TestExtUpgrade::generateRandomStrings() -{ - randomStrings.clear(); - - int count = rand() % 100 + 1; - for (int i = 0; i < count; i++) - randomStrings.append(generateRandomString()); - cmd = QString("echo -e '%1'").arg(randomStrings.join(QString("\n"))); -} - - QTEST_MAIN(TestExtUpgrade); diff --git a/sources/test/testextupgrade.h b/sources/test/testextupgrade.h index e20b3a5..99f8a0b 100644 --- a/sources/test/testextupgrade.h +++ b/sources/test/testextupgrade.h @@ -41,8 +41,6 @@ private slots: void test_copy(); private: - QString generateRandomString() const; - void generateRandomStrings(); ExtUpgrade *extUpgrade = nullptr; QString cmd; QStringList randomStrings; diff --git a/sources/test/testfloatformatter.cpp b/sources/test/testfloatformatter.cpp index c035880..711e6c6 100644 --- a/sources/test/testfloatformatter.cpp +++ b/sources/test/testfloatformatter.cpp @@ -20,6 +20,7 @@ #include +#include "awtestlibrary.h" #include "awfloatformatter.h" @@ -43,12 +44,12 @@ void TestAWFloatFormatter::test_values() void TestAWFloatFormatter::test_count() { // assign - int count = 10 + rand() % 200; + int count = 10 + AWTestLibrary::randomInt(); formatter->setCount(count); QCOMPARE(formatter->count(), count); // test - float value = getValue(); + double value = AWTestLibrary::randomDouble(); QString output = formatter->convert(value); QCOMPARE(output.count(), count); @@ -60,13 +61,13 @@ void TestAWFloatFormatter::test_count() void TestAWFloatFormatter::test_fillChar() { // assign - char c = 'A' + (rand() % static_cast('Z' - 'A')); + char c = AWTestLibrary::randomChar(); formatter->setFillChar(QChar(c)); QCOMPARE(formatter->fillChar(), QChar(c)); formatter->setCount(101); // test - int value = rand() % 100; + int value = AWTestLibrary::randomInt(); QString output = formatter->convert(value); QVERIFY(output.startsWith(QChar(c))); @@ -86,7 +87,7 @@ void TestAWFloatFormatter::test_format() QCOMPARE(formatter->format(), 'e'); // test - float value = getValue(); + double value = AWTestLibrary::randomDouble(); QString output = formatter->convert(value); QVERIFY(output.contains('e')); @@ -98,12 +99,12 @@ void TestAWFloatFormatter::test_format() void TestAWFloatFormatter::test_precision() { // assign - int precision = 1 + rand() % 5; + int precision = 1 + AWTestLibrary::randomInt(5); formatter->setPrecision(precision); QCOMPARE(formatter->precision(), precision); // test - float value = getValue(); + double value = AWTestLibrary::randomDouble(); QString output = formatter->convert(value); output.remove(QString("0.")); QCOMPARE(output.count(), precision); @@ -118,13 +119,14 @@ void TestAWFloatFormatter::test_multiplier() formatter->setPrecision(6); // assign - double multiplier = getValue(); + double multiplier = AWTestLibrary::randomDouble(); formatter->setMultiplier(multiplier); QCOMPARE(formatter->multiplier(), multiplier); // test - double value = getValue(); - QCOMPARE(formatter->convert(value), QString::number(value * multiplier, 'f', 6)); + double value = AWTestLibrary::randomDouble(); + QCOMPARE(formatter->convert(value), + QString::number(value * multiplier, 'f', 6)); // reset formatter->setMultiplier(1.0); @@ -134,13 +136,14 @@ void TestAWFloatFormatter::test_multiplier() void TestAWFloatFormatter::test_summand() { // assign - double summand = getValue(); + double summand = AWTestLibrary::randomDouble(); formatter->setSummand(summand); QCOMPARE(formatter->summand(), summand); // test - double value = getValue(); - QCOMPARE(formatter->convert(value), QString::number(value + summand, 'f', 6)); + double value = AWTestLibrary::randomDouble(); + QCOMPARE(formatter->convert(value), + QString::number(value + summand, 'f', 6)); // reset formatter->setSummand(1.0); @@ -166,18 +169,12 @@ void TestAWFloatFormatter::test_copy() void TestAWFloatFormatter::doRandom() { - formatter->setCount(rand() % 100); - formatter->setFillChar(QChar('A' + (rand() % static_cast('Z' - 'A')))); - formatter->setFormat('A' + (rand() % static_cast('Z' - 'A'))); - formatter->setMultiplier(getValue()); - formatter->setPrecision(rand() % 100); - formatter->setSummand(getValue()); -} - - -float TestAWFloatFormatter::getValue() const -{ - return static_cast(rand()) / static_cast(RAND_MAX); + formatter->setCount(AWTestLibrary::randomInt()); + formatter->setFillChar(QChar(AWTestLibrary::randomChar())); + formatter->setFormat(AWTestLibrary::randomChar()); + formatter->setMultiplier(AWTestLibrary::randomDouble()); + formatter->setPrecision(AWTestLibrary::randomInt()); + formatter->setSummand(AWTestLibrary::randomDouble()); } diff --git a/sources/test/testfloatformatter.h b/sources/test/testfloatformatter.h index ea88f53..8ee99f7 100644 --- a/sources/test/testfloatformatter.h +++ b/sources/test/testfloatformatter.h @@ -45,7 +45,6 @@ private slots: private: void doRandom(); - float getValue() const; AWFloatFormatter *formatter = nullptr; }; diff --git a/sources/test/testnoformatter.cpp b/sources/test/testnoformatter.cpp index b4db641..dc5a937 100644 --- a/sources/test/testnoformatter.cpp +++ b/sources/test/testnoformatter.cpp @@ -20,6 +20,7 @@ #include +#include "awtestlibrary.h" #include "awnoformatter.h" @@ -43,12 +44,12 @@ void TestAWNoFormatter::test_values() void TestAWNoFormatter::test_conversion() { // integer - int randomInt = rand(); + int randomInt = AWTestLibrary::randomInt(); QCOMPARE(formatter->convert(randomInt), QString::number(randomInt)); // float QWARN("Float conversion isn't tested here due to possible rounding errors"); // string - QString randomString = generateRandomString(); + QString randomString = AWTestLibrary::randomString(); QCOMPARE(formatter->convert(randomString), randomString); } @@ -63,19 +64,4 @@ void TestAWNoFormatter::test_copy() } -QString TestAWNoFormatter::generateRandomString() -{ - QString string; - - int diff = 'Z' - 'A'; - int count = rand() % 100 + 1; - for (int i = 0; i < count; i++) { - char c = 'A' + (rand() % diff); - string += QChar(c); - } - - return string; -} - - QTEST_MAIN(TestAWNoFormatter); diff --git a/sources/test/testnoformatter.h b/sources/test/testnoformatter.h index 26c01aa..1b43309 100644 --- a/sources/test/testnoformatter.h +++ b/sources/test/testnoformatter.h @@ -39,7 +39,6 @@ private slots: void test_copy(); private: - QString generateRandomString(); AWNoFormatter *formatter = nullptr; };