add test library for random values generation

This commit is contained in:
Evgenii Alekseev 2016-05-29 03:33:41 +03:00
parent 7b154af1b3
commit cae9e0d2e3
12 changed files with 148 additions and 105 deletions

View File

@ -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)

View File

@ -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<int>('Z' - 'A'));
}
double AWTestLibrary::randomDouble()
{
return static_cast<double>(rand()) / static_cast<double>(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;
}

View File

@ -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 <QStringList>
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 */

View File

@ -20,6 +20,7 @@
#include <QtTest>
#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;
}

View File

@ -20,12 +20,13 @@
#include <QtTest>
#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);

View File

@ -40,7 +40,6 @@ private slots:
void test_copy();
private:
void generateRandomString();
ExtScript *extScript = nullptr;
QString randomString;
};

View File

@ -20,12 +20,14 @@
#include <QtTest>
#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<QString> 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);

View File

@ -41,8 +41,6 @@ private slots:
void test_copy();
private:
QString generateRandomString() const;
void generateRandomStrings();
ExtUpgrade *extUpgrade = nullptr;
QString cmd;
QStringList randomStrings;

View File

@ -20,6 +20,7 @@
#include <QtTest>
#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<int>('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<int>('Z' - 'A'))));
formatter->setFormat('A' + (rand() % static_cast<int>('Z' - 'A')));
formatter->setMultiplier(getValue());
formatter->setPrecision(rand() % 100);
formatter->setSummand(getValue());
}
float TestAWFloatFormatter::getValue() const
{
return static_cast<float>(rand()) / static_cast<float>(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());
}

View File

@ -45,7 +45,6 @@ private slots:
private:
void doRandom();
float getValue() const;
AWFloatFormatter *formatter = nullptr;
};

View File

@ -20,6 +20,7 @@
#include <QtTest>
#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);

View File

@ -39,7 +39,6 @@ private slots:
void test_copy();
private:
QString generateRandomString();
AWNoFormatter *formatter = nullptr;
};