• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

rocs/RocsCore

  • sources
  • kde-4.12
  • kdeedu
  • rocs
  • RocsCore
  • DataStructures
  • Graph
  • Tests
TestGraphStructureAlgorithms.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Rocs.
3  Copyright 2012 Andreas Cord-Landwehr <cola@uni-paderborn.de>
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License as
7  published by the Free Software Foundation; either version 2 of
8  the License, or (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "TestGraphStructureAlgorithms.h"
20 #include "DataStructure.h"
21 #include "../GraphStructure.h"
22 #include "Data.h"
23 #include "Pointer.h"
24 #include "KrossBackend.h"
25 #include "QtScriptBackend.h"
26 #include <qtest_kde.h>
27 
28 #include <kross/core/action.h>
29 #include <kross/core/manager.h>
30 #include <Document.h>
31 #include <DataStructureBackendManager.h>
32 #include <DocumentManager.h>
33 
34 #include <cmath>
35 
36 TestGraphStructureAlgorithms::TestGraphStructureAlgorithms()
37 {
38  QVERIFY(DataStructureBackendManager::self().backends().count() > 0);
39 }
40 
41 void TestGraphStructureAlgorithms::init()
42 {
43  DataStructureBackendManager::self().setBackend("Graph");
44  DocumentManager::self().newDocument();
45 }
46 
47 
48 void TestGraphStructureAlgorithms::cleanupTestCase()
49 {
50  qDebug() << "Remove previous test case.";
51  DocumentManager::self().removeDocument(DocumentManager::self().activeDocument());
52 }
53 
54 void TestGraphStructureAlgorithms::testDistances()
55 {
56  Document *document = DocumentManager::self().activeDocument();
57  DataList dataList;
58 
59  document->pointerType(0)->setDirection(PointerType::Bidirectional);
60 
61  DataStructurePtr ds = document->addDataStructure("Dijkstra");
62  boost::shared_ptr<Rocs::GraphStructure> graph = boost::static_pointer_cast<Rocs::GraphStructure>(ds);
63 
64  // need to set engine because GraphStructure::distances calls QScriptEngine::newArray
65  ds->setEngine(new QScriptEngine(this));
66 
67  // create 3 nodes connected as follows:
68  // x-x x
69  int nodes = 3;
70  dataList.clear();
71  for (int i = 0; i < nodes; ++i) {
72  dataList.append(graph->createData(QString(i), 0));
73  }
74  dataList[0]->createPointer(dataList[1]);
75 
76  // test distances from 0 to all others
77  QScriptValue distances = graph->distances(dataList[0].get());
78  QVERIFY2(distances.property(0).equals(QScriptValue(0)), "ERROR: distance is wrong");
79  QVERIFY2(distances.property(1).equals(QScriptValue(1)), "ERROR: distance is wrong");
80  QVERIFY2(distances.property(2).equals(QScriptValue(INFINITY)), "ERROR: distance is wrong");
81 }
82 
83 void TestGraphStructureAlgorithms::testDijkstraBidirectional()
84 {
85  Document *document = DocumentManager::self().activeDocument();
86  DataList dataList;
87 
88  document->pointerType(0)->setDirection(PointerType::Bidirectional);
89 
90  DataStructurePtr ds = document->addDataStructure("Dijkstra");
91  boost::shared_ptr<Rocs::GraphStructure> graph = boost::static_pointer_cast<Rocs::GraphStructure>(ds);
92 
93  // create line of length 9 and test values
94  int nodes = 10;
95  dataList.clear();
96 
97  for (int i = 0; i < nodes; ++i) {
98  dataList.append(graph->createData(QString(i), 0));
99  }
100  for (int i = 0; i < nodes-1; ++i) {
101  dataList[i]->createPointer(dataList[i+1]);
102  }
103 
104  // test distances from 0 to all others
105  QMap<DataPtr, PointerList> paths = graph->dijkstraShortestPaths(dataList.at(0));
106  for (int i = 0; i < nodes; ++i) {
107  QVERIFY2(paths[dataList[i]].length() == i, "ERROR: distance is wrong");
108  }
109  // test distances from n to all others
110  paths = graph->dijkstraShortestPaths(dataList.at(nodes-1));
111  for (int i = 0; i < nodes; ++i) {
112  QVERIFY2(paths[dataList[nodes-i-1]].length() == i, "ERROR: distance is wrong");
113  }
114 }
115 
116 void TestGraphStructureAlgorithms::testDijkstraUnidirectional()
117 {
118  Document *document = DocumentManager::self().activeDocument();
119  DataList dataList;
120 
121  document->pointerType(0)->setDirection(PointerType::Unidirectional);
122 
123  DataStructurePtr ds = document->addDataStructure("Dijkstra");
124  boost::shared_ptr<Rocs::GraphStructure> graph = boost::static_pointer_cast<Rocs::GraphStructure>(ds);
125 
126  // create circle of 10 nodes
127  int nodes = 10;
128  dataList.clear();
129 
130  for (int i = 0; i < nodes; ++i) {
131  dataList.append(graph->createData(QString(i), 0));
132  }
133  for (int i = 0; i < nodes-1; ++i) {
134  dataList[i]->createPointer(dataList[i+1]);
135  }
136  dataList[nodes-1]->createPointer(dataList[0]);
137 
138  // test distances from 0 to all others
139  QMap<DataPtr, PointerList> paths = graph->dijkstraShortestPaths(dataList.at(0));
140  for (int i = 0; i < nodes; ++i) {
141  QVERIFY2(paths[dataList[i]].length() == i, "ERROR: distance is wrong");
142  }
143 }
144 
145 QTEST_KDEMAIN_CORE(TestGraphStructureAlgorithms)
KrossBackend.h
Rocs::GraphStructure::createPointer
PointerPtr createPointer(DataPtr from, DataPtr to, int pointerType)
Internal method to create new graph edge.
Definition: GraphStructure.cpp:423
Rocs::GraphStructure
Definition: GraphStructure.h:29
PointerType::Bidirectional
Definition: PointerType.h:48
DocumentManager.h
TestGraphStructureAlgorithms::TestGraphStructureAlgorithms
TestGraphStructureAlgorithms()
Definition: TestGraphStructureAlgorithms.cpp:36
DocumentManager::self
static DocumentManager & self()
Definition: DocumentManager.cpp:57
TestGraphStructureAlgorithms.h
DataStructurePtr
boost::shared_ptr< DataStructure > DataStructurePtr
Definition: CoreTypes.h:38
GmlParser::document
Document * document
Definition: GmlGrammar.cpp:40
DocumentManager::activeDocument
Document * activeDocument() const
Returns the currently active document, or 0 if there document list is empty.
Definition: DocumentManager.cpp:96
DataList
QList< boost::shared_ptr< Data > > DataList
Definition: CoreTypes.h:30
QtScriptBackend.h
Data.h
DataStructureBackendManager::self
static DataStructureBackendManager & self()
Returns self reference to backend manager.
Definition: DataStructureBackendManager.cpp:233
Document.h
PointerType::Unidirectional
Definition: PointerType.h:47
DocumentManager::newDocument
Document * newDocument()
Creates and loads a new graph document.
Definition: DocumentManager.cpp:215
DataStructure::setEngine
virtual void setEngine(QScriptEngine *engine)
Definition: DataStructure.cpp:515
DataStructure.h
Document::addDataStructure
DataStructurePtr addDataStructure(const QString &name=QString())
Add data structure to graph document with name name.
Definition: Document.cpp:333
Document
Definition: Document.h:41
Document::pointerType
PointerTypePtr pointerType(int pointerType) const
Definition: Document.cpp:207
DataStructureBackendManager::setBackend
void setBackend(const QString &pluginIdentifier)
Change the active backend.
Definition: DataStructureBackendManager.cpp:240
Pointer.h
DataStructureBackendManager.h
TestGraphStructureAlgorithms
Definition: TestGraphStructureAlgorithms.h:29
DocumentManager::removeDocument
void removeDocument(Document *document)
Remove document from document list.
Definition: DocumentManager.cpp:173
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:42:26 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

rocs/RocsCore

Skip menu "rocs/RocsCore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal