From 7b154af1b3abafb500c1e111e861d213ff2eac1f Mon Sep 17 00:00:00 2001 From: arcan1s Date: Sun, 29 May 2016 02:45:57 +0300 Subject: [PATCH] add test cases for float and no formatters --- sources/test/CMakeLists.txt | 14 +++ sources/test/testfloatformatter.cpp | 184 ++++++++++++++++++++++++++++ sources/test/testfloatformatter.h | 53 ++++++++ sources/test/testnoformatter.cpp | 81 ++++++++++++ sources/test/testnoformatter.h | 47 +++++++ 5 files changed, 379 insertions(+) create mode 100644 sources/test/testfloatformatter.cpp create mode 100644 sources/test/testfloatformatter.h create mode 100644 sources/test/testnoformatter.cpp create mode 100644 sources/test/testnoformatter.h diff --git a/sources/test/CMakeLists.txt b/sources/test/CMakeLists.txt index 2542dc0..f28898a 100644 --- a/sources/test/CMakeLists.txt +++ b/sources/test/CMakeLists.txt @@ -11,6 +11,7 @@ include_directories( ${Kf5_INCLUDE} ) +## extensions # abstractextitem set(ABSTRACTEXTITEM_HEADERS testabstractextitem.h) set(ABSTRACTEXTITEM_SOURCES testabstractextitem.cpp) @@ -41,3 +42,16 @@ set(EXTWEATHER_SOURCES testextweather.cpp) add_executable(${SUBPROJECT}-extweather ${EXTWEATHER_HEADERS} ${EXTWEATHER_SOURCES}) target_link_libraries(${SUBPROJECT}-extweather ${PROJECT_LIBRARY} ${Qt_LIBRARIES} ${Qt5Test_LIBRARIES}) 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) +# 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}) +add_test(NAME "NoFormatter" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-noformatter) diff --git a/sources/test/testfloatformatter.cpp b/sources/test/testfloatformatter.cpp new file mode 100644 index 0000000..c035880 --- /dev/null +++ b/sources/test/testfloatformatter.cpp @@ -0,0 +1,184 @@ +/*************************************************************************** + * 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 "testfloatformatter.h" + +#include + +#include "awfloatformatter.h" + + +void TestAWFloatFormatter::initTestCase() +{ + formatter = new AWFloatFormatter(nullptr); +} + + +void TestAWFloatFormatter::cleanupTestCase() +{ + delete formatter; +} + + +void TestAWFloatFormatter::test_values() +{ +} + + +void TestAWFloatFormatter::test_count() +{ + // assign + int count = 10 + rand() % 200; + formatter->setCount(count); + QCOMPARE(formatter->count(), count); + + // test + float value = getValue(); + QString output = formatter->convert(value); + QCOMPARE(output.count(), count); + + // reset + formatter->setCount(0); +} + + +void TestAWFloatFormatter::test_fillChar() +{ + // assign + char c = 'A' + (rand() % static_cast('Z' - 'A')); + formatter->setFillChar(QChar(c)); + QCOMPARE(formatter->fillChar(), QChar(c)); + formatter->setCount(101); + + // test + int value = rand() % 100; + QString output = formatter->convert(value); + QVERIFY(output.startsWith(QChar(c))); + + // reset + formatter->setFillChar(QChar()); + formatter->setCount(0); +} + + +void TestAWFloatFormatter::test_format() +{ + // assign + QWARN("Lets assing 'z' formatter, it should cause a warning"); + formatter->setFormat('z'); + QCOMPARE(formatter->format(), 'f'); + formatter->setFormat('e'); + QCOMPARE(formatter->format(), 'e'); + + // test + float value = getValue(); + QString output = formatter->convert(value); + QVERIFY(output.contains('e')); + + // reset + formatter->setFormat('f'); +} + + +void TestAWFloatFormatter::test_precision() +{ + // assign + int precision = 1 + rand() % 5; + formatter->setPrecision(precision); + QCOMPARE(formatter->precision(), precision); + + // test + float value = getValue(); + QString output = formatter->convert(value); + output.remove(QString("0.")); + QCOMPARE(output.count(), precision); + + // reset + formatter->setPrecision(-1); +} + + +void TestAWFloatFormatter::test_multiplier() +{ + formatter->setPrecision(6); + + // assign + double multiplier = getValue(); + formatter->setMultiplier(multiplier); + QCOMPARE(formatter->multiplier(), multiplier); + + // test + double value = getValue(); + QCOMPARE(formatter->convert(value), QString::number(value * multiplier, 'f', 6)); + + // reset + formatter->setMultiplier(1.0); +} + + +void TestAWFloatFormatter::test_summand() +{ + // assign + double summand = getValue(); + formatter->setSummand(summand); + QCOMPARE(formatter->summand(), summand); + + // test + double value = getValue(); + QCOMPARE(formatter->convert(value), QString::number(value + summand, 'f', 6)); + + // reset + formatter->setSummand(1.0); +} + + +void TestAWFloatFormatter::test_copy() +{ + doRandom(); + AWFloatFormatter *newFormatter = formatter->copy(QString("/dev/null"), 1); + + QCOMPARE(newFormatter->count(), formatter->count()); + QCOMPARE(newFormatter->fillChar(), formatter->fillChar()); + QCOMPARE(newFormatter->format(), formatter->format()); + QCOMPARE(newFormatter->multiplier(), formatter->multiplier()); + QCOMPARE(newFormatter->precision(), formatter->precision()); + QCOMPARE(newFormatter->summand(), formatter->summand()); + QCOMPARE(newFormatter->number(), 1); + + delete newFormatter; +} + + +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); +} + + +QTEST_MAIN(TestAWFloatFormatter); diff --git a/sources/test/testfloatformatter.h b/sources/test/testfloatformatter.h new file mode 100644 index 0000000..ea88f53 --- /dev/null +++ b/sources/test/testfloatformatter.h @@ -0,0 +1,53 @@ +/*************************************************************************** + * 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 TESTFLOATFORMATTER_H +#define TESTFLOATFORMATTER_H + +#include +#include + + +class AWFloatFormatter; + +class TestAWFloatFormatter : public QObject +{ + Q_OBJECT + +private slots: + // initialization + void initTestCase(); + void cleanupTestCase(); + // test + void test_values(); + void test_count(); + void test_fillChar(); + void test_format(); + void test_precision(); + void test_multiplier(); + void test_summand(); + void test_copy(); + +private: + void doRandom(); + float getValue() const; + AWFloatFormatter *formatter = nullptr; +}; + + +#endif /* TESTFLOATFORMATTER_H */ diff --git a/sources/test/testnoformatter.cpp b/sources/test/testnoformatter.cpp new file mode 100644 index 0000000..b4db641 --- /dev/null +++ b/sources/test/testnoformatter.cpp @@ -0,0 +1,81 @@ +/*************************************************************************** + * 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 "testnoformatter.h" + +#include + +#include "awnoformatter.h" + + +void TestAWNoFormatter::initTestCase() +{ + formatter = new AWNoFormatter(nullptr); +} + + +void TestAWNoFormatter::cleanupTestCase() +{ + delete formatter; +} + + +void TestAWNoFormatter::test_values() +{ +} + + +void TestAWNoFormatter::test_conversion() +{ + // integer + int randomInt = rand(); + QCOMPARE(formatter->convert(randomInt), QString::number(randomInt)); + // float + QWARN("Float conversion isn't tested here due to possible rounding errors"); + // string + QString randomString = generateRandomString(); + QCOMPARE(formatter->convert(randomString), randomString); +} + + +void TestAWNoFormatter::test_copy() +{ + AWNoFormatter *newFormatter = formatter->copy(QString("/dev/null"), 1); + + QCOMPARE(newFormatter->number(), 1); + + delete newFormatter; +} + + +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 new file mode 100644 index 0000000..26c01aa --- /dev/null +++ b/sources/test/testnoformatter.h @@ -0,0 +1,47 @@ +/*************************************************************************** + * 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 TESTNOFORMATTER_H +#define TESTNOFORMATTER_H + +#include +#include + + +class AWNoFormatter; + +class TestAWNoFormatter : public QObject +{ + Q_OBJECT + +private slots: + // initialization + void initTestCase(); + void cleanupTestCase(); + // test + void test_values(); + void test_conversion(); + void test_copy(); + +private: + QString generateRandomString(); + AWNoFormatter *formatter = nullptr; +}; + + +#endif /* TESTNOFORMATTER_H */