cleverca22
1/14/2015 - 3:35 PM

conversion problem

conversion problem

######################################################################
# Automatically generated by qmake (2.01a) Wed Jan 14 11:32:13 2015
######################################################################

QT += testlib
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

# Input
HEADERS += test.h
SOURCES += test.cpp
#include <QtTest/QtTest>

class TestCase : public QObject {
Q_OBJECT
private slots:
        void convert_data();
        void convert();
};
#include "test.h"

QTEST_MAIN(TestCase)

void TestCase::convert_data() {
        QTest::addColumn<QString>("input");
        QTest::addColumn<int>("output");

        QTest::newRow("201") << "201" << 20100;
        QTest::newRow("201.6") << "201.6" << 20160;
}
int parseValue(QString input) {
        QStringList parts = input.split(".");
        Q_ASSERT(parts.length() <= 2);
        if (parts.length() == 1) {
                return parts[0].toInt() * 100;
        } else {
                int first = parts[0].toInt() * 100;
                Q_ASSERT(parts[1].length() <= 2);
                if (parts[1].length() == 1) {
                        return first + (parts[1].toInt() * 10);
                } else {
                        return first + parts[1].toInt();
                }
        }
}
void TestCase::convert() {
        QFETCH(QString,input);
        QFETCH(int,output);
        qDebug() << input.toDouble();
        int converted = parseValue(input);
        QCOMPARE(output,converted);
}
********* Start testing of TestCase *********
Config: Using QTest library 4.8.5, Qt 4.8.5
PASS   : TestCase::initTestCase()
QDEBUG : TestCase::convert(201) 201 
QDEBUG : TestCase::convert(201.6) 201.6 
PASS   : TestCase::convert()
PASS   : TestCase::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of TestCase *********