Implemented Route class and unit tests for it
authorSami Rämö <sami.ramo@ixonos.com>
Mon, 12 Jul 2010 06:49:20 +0000 (09:49 +0300)
committerSami Rämö <sami.ramo@ixonos.com>
Mon, 12 Jul 2010 06:49:20 +0000 (09:49 +0300)
 - Also added qDebug() prints to RouteSegment class

src/routing/route.cpp
src/routing/route.h
src/routing/routesegment.cpp
tests/routing/route/route.pro
tests/routing/route/testroute.cpp
tests/routing/routesegment/routesegment.pro

index 01a9ddc..a11dfc6 100644 (file)
@@ -19,6 +19,8 @@
     USA.
 */
 
+#include <QDebug>
+
 #include "routingcommon.h"
 
 #include "route.h"
@@ -27,64 +29,96 @@ Route::Route()
     : m_totalDistance(ROUTING_VALUE_UNDEFINED),
       m_totalTime(ROUTING_VALUE_UNDEFINED)
 {
+    qDebug() << __PRETTY_FUNCTION__;
 }
 
-QString Route::endPointName() const
+Route::~Route()
 {
-    return m_endPointName;
+    qDebug() << __PRETTY_FUNCTION__;
+
+    qDeleteAll(m_segments);
 }
 
-const QList<QPointF>& Route::geometryPoints() const
+void Route::appendGeometryPoint(QPointF geometryPoint)
 {
-    /// @todo implement
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_geometryPoints.append(geometryPoint);
 }
 
-const QList<RouteSegment>& Route::segments() const
+void Route::appendSegment(RouteSegment *segment)
 {
-    /// @todo implement
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_segments.append(segment);
 }
 
-void Route::setEndPointName(QString endPoint)
+QString Route::endPointName() const
 {
-    m_endPointName = endPoint;
+    qDebug() << __PRETTY_FUNCTION__;
+
+    return m_endPointName;
 }
 
-void Route::setGeometryPoints(QList<QPointF> geometryPoints)
+const QList<QPointF>& Route::geometryPoints() const
 {
-    /// @todo implement
+    qDebug() << __PRETTY_FUNCTION__;
+
+    return m_geometryPoints;
 }
 
-void Route::setSegments(QList<RouteSegment> segments)
+const QList<RouteSegment *>& Route::segments() const
 {
-    /// @todo implement
+    qDebug() << __PRETTY_FUNCTION__;
+
+    return m_segments;
+}
+
+void Route::setEndPointName(QString endPoint)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_endPointName = endPoint;
 }
 
 void Route::setStartPointName(QString startPoint)
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     m_startPointName = startPoint;
 }
 
 void Route::setTotalDistance(int meters)
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     m_totalDistance = meters;
 }
 
 void Route::setTotalTime(int seconds)
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     m_totalTime = seconds;
 }
 
 QString Route::startPointName() const
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     return m_startPointName;
 }
 
 int Route::totalDistance() const
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     return m_totalDistance;
 }
 
 int Route::totalTime() const
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     return m_totalTime;
 }
index 7693821..1800842 100644 (file)
@@ -33,14 +33,22 @@ class Route
 {
 public:
     Route();
+    ~Route();
+
+    void appendGeometryPoint(QPointF geometryPoint);
+
+    /**
+    * @brief Append a route segment to the route
+    *
+    * Takes the ownership of the segment object. Appending must be done in order starting from the
+    * begin of the route.
+    */
+    void appendSegment(RouteSegment *segment);
 
     QString endPointName() const;
     const QList<QPointF>& geometryPoints() const;
-    const QList<RouteSegment>& segments() const;
+    const QList<RouteSegment *>& segments() const;
     void setEndPointName(QString endPoint);
-    /// @todo append segments & points, pointers?, takes ownership, no copying data
-    void setGeometryPoints(QList<QPointF> geometryPoints);
-    void setSegments(QList<RouteSegment> segments);
     void setStartPointName(QString startPoint);
     void setTotalDistance(int meters);
     void setTotalTime(int seconds);
@@ -53,7 +61,7 @@ private:
     int m_totalTime;                    // estimated route total time in seconds
 
     QList<QPointF> m_geometryPoints;    // lat/lon coordinates of the route points
-    QList<RouteSegment> m_segments;     // route segments
+    QList<RouteSegment *> m_segments;   // route segments
 
     QString m_endPointName;             // name of the route end point
     QString m_startPointName;           // name of the route starting point
index ab7d374..f0e2219 100644 (file)
@@ -19,6 +19,8 @@
     USA.
 */
 
+#include <QDebug>
+
 #include "routingcommon.h"
 
 #include "routesegment.h"
@@ -30,100 +32,139 @@ RouteSegment::RouteSegment()
       m_length(ROUTING_VALUE_UNDEFINED),
       m_turnAngle(ROUTING_VALUE_UNDEFINED)
 {
+    qDebug() << __PRETTY_FUNCTION__;
 }
 
 qreal RouteSegment::azimuth() const
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     return m_azimuth;
 }
 
 QString RouteSegment::earthDirection() const
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     return m_earthDirection;
 }
 
 QString RouteSegment::instruction() const
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     return m_instruction;
 }
 
 qreal RouteSegment::length() const
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     return m_length;
 }
 
 QString RouteSegment::lengthCaption() const
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     return m_lengthCaption;
 }
 
 int RouteSegment::positionIndex() const
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     return m_positionIndex;
 }
 
 void RouteSegment::setAzimuth(qreal azimuth)
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     m_azimuth = azimuth;
 }
 
 void RouteSegment::setEarthDirection(QString direction)
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     m_earthDirection = direction;
 }
 
 void RouteSegment::setInstruction(QString instruction)
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     m_instruction = instruction;
 }
 
 void RouteSegment::setLength(qreal meters)
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     m_length = meters;
 }
 
 void RouteSegment::setLengthCaption(QString length)
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     m_lengthCaption = length;
 }
 
 void RouteSegment::setPositionIndex(int index)
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     m_positionIndex = index;
 }
 
 void RouteSegment::setTime(int seconds)
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     m_timeSeconds = seconds;
 }
 
 void RouteSegment::setTurnAngle(qreal degrees)
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     m_turnAngle = degrees;
 }
 
 void RouteSegment::setTurnType(QString type)
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     m_turnType = type;
 }
 
 QString RouteSegment::street() const
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     /// @todo Implement parser
     return QString();
 }
 
 int RouteSegment::time() const
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     return m_timeSeconds;
 }
 
 qreal RouteSegment::turnAngle() const
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     return m_turnAngle;
 }
 
 QString RouteSegment::turnType() const
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     return m_turnType;
 }
index d9a8d22..c626dd9 100644 (file)
@@ -16,7 +16,8 @@ TEMPLATE = app
 
 
 SOURCES += testroute.cpp \
-    ../../../src/routing/route.cpp
+    ../../../src/routing/route.cpp \
+    ../../../src/routing/routesegment.cpp
 DEFINES += SRCDIR=\\\"$$PWD/\\\"
 
 INCLUDEPATH += . \
@@ -24,4 +25,7 @@ INCLUDEPATH += . \
 
 HEADERS += \
     ../../../src/routing/route.h \
-    ../../../src/routing/routingcommon.h
+    ../../../src/routing/routingcommon.h \
+    ../../../src/routing/routesegment.h
+
+DEFINES += QT_NO_DEBUG_OUTPUT
index 5134fda..3b686d0 100644 (file)
@@ -23,6 +23,8 @@
 #include <QtCore/QString>
 #include <QtTest/QtTest>
 
+#include "routing/routesegment.h"
+
 #include "routing/route.h"
 
 class TestRoute : public QObject
@@ -33,6 +35,8 @@ public:
     TestRoute();
 
 private Q_SLOTS:
+    void geometryPoints();
+    void segments();
     void settersAndGetters();
 };
 
@@ -40,9 +44,69 @@ TestRoute::TestRoute()
 {
 }
 
+void TestRoute::geometryPoints()
+{
+    // test points
+    QPointF point1(12.3456, 54.3210);
+    QPointF point2(65.5000, 25.0000);
+    QPointF point3(-123.4567, 43.0987);
+
+    // create route with points
+    Route route;
+    route.appendGeometryPoint(point1);
+    route.appendGeometryPoint(point2);
+    route.appendGeometryPoint(point3);
+
+    // test points
+    const QList<QPointF> &points = route.geometryPoints();
+    QVERIFY(points.count() == 3);
+    QCOMPARE(points.at(0), point1);
+    QCOMPARE(points.at(1), point2);
+    QCOMPARE(points.at(2), point3);
+}
+
+void TestRoute::segments()
+{
+    // test segments
+    RouteSegment *segment1 = new RouteSegment();
+    RouteSegment *segment2 = new RouteSegment();
+    RouteSegment *segment3 = new RouteSegment();
+
+    // create route with segments
+    Route route;
+    route.appendSegment(segment1);
+    route.appendSegment(segment2);
+    route.appendSegment(segment3);
+
+    // test segments
+    const QList<RouteSegment *> &segments = route.segments();
+    QVERIFY(segments.count() == 3);
+    QCOMPARE(segments.at(0), segment1);
+    QCOMPARE(segments.at(1), segment2);
+    QCOMPARE(segments.at(2), segment3);
+}
+
 void TestRoute::settersAndGetters()
 {
-    QVERIFY(false);
+    // test data
+    QString endPoint = "Kiviharjunlenkki 1";
+    QString startPoint = "Kauppurienkatu 21";
+    int distance = 2534;
+    int time = 212;
+
+    Route route;
+
+    // setters
+    route.setEndPointName(endPoint);
+    route.setStartPointName(startPoint);
+    route.setTotalDistance(distance);
+    route.setTotalTime(time);
+
+    // compare values with getters
+    QCOMPARE(route.endPointName(), endPoint);
+    QCOMPARE(route.startPointName(), startPoint);
+    QCOMPARE(route.totalDistance(), distance);
+    QCOMPARE(route.totalTime(), time);
 }
 
 QTEST_APPLESS_MAIN(TestRoute);
index b188ebc..39ac1ab 100644 (file)
@@ -24,3 +24,5 @@ INCLUDEPATH += . \
 
 HEADERS += \
     ../../../src/routing/routesegment.h
+
+DEFINES += QT_NO_DEBUG_OUTPUT