• 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
  • Tests
TestDataStructure.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Rocs.
3  Copyright 2004-2010 Tomaz Canabrava <tomaz.canabrava@gmail.com>
4  Copyright 2012 Andreas Cord-Landwehr <cola@uni-paderborn.de>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License as
8  published by the Free Software Foundation; either version 2 of
9  the License, or (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "TestDataStructure.h"
21 #include "DataStructure.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 <KDebug>
31 #include <Document.h>
32 #include <DataStructureBackendManager.h>
33 #include <DocumentManager.h>
34 
35 TestDataStructure::TestDataStructure()
36 {
37  DocumentManager::self().addDocument(new Document("test"));;
38 }
39 
40 void TestDataStructure::init()
41 {
42  DocumentManager::self().newDocument();
43 }
44 
45 void TestDataStructure::cleanup()
46 {
47  DocumentManager::self().removeDocument(DocumentManager::self().activeDocument());
48 }
49 
50 void TestDataStructure::dataAddDeleteTest()
51 {
52  DataStructurePtr ds = DataStructure::create(DocumentManager::self().activeDocument());
53  DocumentManager::self().activeDocument()->setActiveDataStructure(ds);
54  DataList dataList;
55 
56  // create 10 data elements
57  for (int i = 0; i < 10; i++) {
58  dataList.append(ds->createData(QString(i), 0));
59  }
60  QVERIFY2(ds->dataList(0).size() == 10, "ERROR: Number of data elements is not 10");
61 
62  // remove all data elements
63  foreach(DataPtr data, dataList) {
64  data->remove();
65  }
66 
67  QVERIFY2(ds->dataList(0).size() == 0, "ERROR: Not all data elements were deleted");
68 
69  DocumentManager::self().removeDocument(DocumentManager::self().activeDocument());
70 }
71 
72 void TestDataStructure::pointerAddDeleteTest()
73 {
74  // test for undirected pointers
75  DataStructurePtr ds = DataStructure::create(DocumentManager::self().activeDocument());
76  DocumentManager::self().activeDocument()->setActiveDataStructure(ds);
77  DataList dataList = DataList();
78 
79  // create 10 data elements
80  // x x x x x x x x x x
81  for (int i = 0; i < 10; i++) {
82  dataList.append(ds->createData(QString::number(i), 0));
83  }
84  QVERIFY2(ds->dataList(0).size() == 10, "ERROR: Number of data elements is not 10");
85 
86  // connect data elements to a line
87  // x-x-x-x-x-x-x-x-x-x
88  for (int i = 0; i < dataList.size() - 1; i++) {
89  ds->createPointer(dataList[i], dataList[i + 1], 0);
90  }
91  QVERIFY2(ds->pointers(0).size() == 9, "ERROR: Number of data elements is not 9");
92  QVERIFY2(dataList[0]->pointerList().size() == 1, "ERROR: data gives wrong number of pointers");
93  QVERIFY2(dataList[1]->pointerList().size() == 2, "ERROR: data gives wrong number of pointers");
94 
95  // remove first pointer from list
96  // x x-x-x-x-x-x-x-x-x
97  ds->pointers(0).first()->remove();
98  QVERIFY2(ds->pointers(0).size() == 8, "ERROR: pointer was not removed");
99  QVERIFY2(dataList[0]->pointerList().size() == 0, "ERROR: data gives wrong number of pointers");
100  QVERIFY2(dataList[1]->pointerList().size() == 1, "ERROR: data gives wrong number of pointers");
101 
102  // remove second node, should trigger deletion of second pointer
103  // x o x-x-x-x-x-x-x-x
104  dataList[1]->remove();
105  QVERIFY2(ds->pointers(0).size() == 7, "ERROR: data deletion did not remove its pointers");
106 
107  // remove fourth node, should trigger deletion of its two adjacend pointers
108  // x o x o x-x-x-x-x-x
109  dataList[3]->remove();
110  QVERIFY2(ds->pointers(0).size() == 5, "ERROR: data deletion did not remove its both pointers");
111 }
112 
113 
114 void TestDataStructure::createSimpleGraph()
115 {
116  QMap<QString, DataPtr> dataList;
117  /* Creates a simple Graph with 5 datums and connects them with pointers. */
118  DataStructurePtr ds = DataStructure::create(DocumentManager::self().activeDocument());
119  DocumentManager::self().activeDocument()->setActiveDataStructure(ds);
120 
121  ds->setProperty("name", "Graph1");
122  dataList.insert("a", ds->createData("a", 0));
123  dataList.insert("b", ds->createData("b", 0));
124  dataList.insert("c", ds->createData("c", 0));
125  dataList.insert("d", ds->createData("d", 0));
126  dataList.insert("e", ds->createData("e", 0));
127 
128  ds->createPointer(dataList["a"], dataList["b"], 0);
129  ds->createPointer(dataList["b"], dataList["c"], 0);
130  ds->createPointer(dataList["c"], dataList["d"], 0);
131  ds->createPointer(dataList["d"], dataList["e"], 0);
132  ds->createPointer(dataList["e"], dataList["a"], 0);
133 
134  QVERIFY2(ds->dataList(0).size() == 5, "ERROR: Number of data is not 5 ");
135  QVERIFY2(ds->pointers(0).size() == 5, "ERROR: Number of pointers is not 5 ");
136 
137  foreach(DataPtr n, ds->dataList(0)) {
138  QVERIFY2(n->outPointerList().size() == 1, "ERROR: Number of out pointers is not 1");
139  QVERIFY2(n->inPointerList().size() == 1, "ERROR: Number of in pointers is not 1");
140  QVERIFY2(n->adjacentDataList().size() == 2, "ERROR: Number of Adjacent Nodes is not 2");
141  QVERIFY2(n->pointerList().size() == 2, "ERROR: Number of adjacent pointers is not 2");
142  }
143  dataList.clear();
144 }
145 
146 
147 void TestDataStructure::dataTypesTest()
148 {
149  DataStructurePtr ds = DocumentManager::self().activeDocument()->addDataStructure();
150 
151  DataList dataListDefault, dataList1, dataList2;
152  QVERIFY2(ds->document()->dataTypeList().size() == 1, "ERROR: no default data type created");
153 
154  // register two further data types
155  int type1 = ds->document()->registerDataType("type1");
156  int type2 = ds->document()->registerDataType("type2");
157  QVERIFY2(ds->document()->dataTypeList().size() == 3, "ERROR: data types were not created");
158 
159  // create data elements
160  for (int i = 0; i < 3; i++) {
161  dataListDefault.append(ds->createData(QString::number(i), 0));
162  dataList1.append(ds->createData(QString::number(i), type1));
163  dataList2.append(ds->createData(QString::number(i), type2));
164  }
165  QVERIFY2(ds->dataList(0).size() == 3,
166  "list contains " + ds->dataList(0).size()
167  );
168  QVERIFY2(ds->dataList(type1).size() == 3,
169  "list contains " + ds->dataList(0).size()
170  );
171  QVERIFY2(ds->dataList(type2).size() == 3,
172  "list contains " + ds->dataList(0).size()
173  );
174  QVERIFY2(dataListDefault.at(0)->dataType() == 0, "ERROR: not correct autoset of type");
175  QVERIFY2(dataList1.at(0)->dataType() == type1, "ERROR: not correct autoset of type");
176  QVERIFY2(dataList2.at(0)->dataType() == type2, "ERROR: not correct autoset of type");
177 
178  // add pointers
179  ds->createPointer(dataListDefault[0], dataList1[0], 0);
180  ds->createPointer(dataList1[0], dataList2[0], 0);
181  ds->createPointer(dataList2[0], dataListDefault[0], 0);
182  QVERIFY2(ds->pointers(0).size() == 3, "ERROR: pointers were not correctly created");
183 
184  // remove data type
185  ds->document()->removeDataType(type2);
186  QVERIFY2(!ds->document()->dataTypeList().contains(type2),"ERROR: data type was not unregistered");
187  QVERIFY2(ds->pointers(0).size() == 1, "ERROR: pointers were not correctly deleted");
188 }
189 
190 
191 void TestDataStructure::pointerTypesTest()
192 {
193  DataStructurePtr ds = DataStructure::create(DocumentManager::self().activeDocument());
194  DocumentManager::self().activeDocument()->setActiveDataStructure(ds);
195 
196  DataList dataList;
197  QVERIFY2(ds->document()->pointerTypeList().size() == 1, "ERROR: no default pointer type created");
198 
199  // create data elements
200  for (int i = 0; i < 10; i++) {
201  dataList.append(ds->createData(QString::number(i), 0));
202  }
203 
204  // register two further data types
205  int type1 = ds->document()->registerPointerType("type1");
206  QVERIFY2(ds->document()->pointerTypeList().size() == 2, "ERROR: pointer types were not created");
207 
208  // connect data elements to a line
209  for (int i = 0; i < 4; i++) {
210  ds->createPointer(dataList[i], dataList[i + 1], 0);
211  }
212  for (int i = 0; i < 9; i++) {
213  ds->createPointer(dataList[i], dataList[i + 1], type1);
214  }
215  QVERIFY2(ds->pointers(0).size() == 4, "ERROR: wrong number of pointers");
216  QVERIFY2(ds->pointers(type1).size() == 9, "ERROR: wrong number of pointers");
217  QVERIFY(dataList[0]->adjacentDataList().size() == 1);
218  QVERIFY(dataList[1]->adjacentDataList().size() == 2);
219  QVERIFY(dataList[5]->adjacentDataList().size() == 2);
220 
221  // remove first node
222  dataList[0]->remove();
223  QVERIFY2(ds->pointers(0).size() == 3, "ERROR: wrong number of pointers");
224  QVERIFY2(ds->pointers(type1).size() == 8, "ERROR: wrong number of pointers");
225 
226  ds->document()->removePointerType(type1);
227  QVERIFY(dataList[2]->adjacentDataList().size() == 2);
228  QVERIFY(dataList[6]->adjacentDataList().size() == 0);
229 }
230 
231 void TestDataStructure::pointerDirectionChange()
232 {
233  QMap<int, DataPtr> dataList;
234  DataStructurePtr ds = DataStructure::create(DocumentManager::self().activeDocument());
235  DocumentManager::self().activeDocument()->setActiveDataStructure(ds);
236 
237  // add two nodes 0 and 1, set type of default pointer type to unidirectional
238  ds->document()->pointerType(0)->setDirection(PointerType::Unidirectional);
239  dataList.insert(0, ds->createData("0", 0));
240  dataList.insert(1, ds->createData("1", 0));
241 
242  // add pointer from 0 to 1 and test pointer list properties
243  dataList[0]->createPointer(dataList[1]);
244  QVERIFY(dataList[0]->outPointerList().length() == 1);
245  QVERIFY(dataList[0]->inPointerList().length() == 0);
246  QVERIFY(dataList[0]->adjacentDataList().length() == 1);
247  QVERIFY(dataList[1]->outPointerList().length() == 0);
248  QVERIFY(dataList[1]->inPointerList().length() == 1);
249  QVERIFY(dataList[1]->adjacentDataList().length() == 1);
250 
251  ds->document()->pointerType(0)->setDirection(PointerType::Bidirectional);
252  QVERIFY(dataList[0]->outPointerList().length() == 1);
253  QVERIFY(dataList[0]->inPointerList().length() == 1);
254  QVERIFY(dataList[0]->adjacentDataList().length() == 1);
255  QVERIFY(dataList[1]->outPointerList().length() == 1);
256  QVERIFY(dataList[1]->inPointerList().length() == 1);
257  QVERIFY(dataList[1]->adjacentDataList().length() == 1);
258 
259  ds->document()->pointerType(0)->setDirection(PointerType::Unidirectional);
260  QVERIFY(dataList[0]->outPointerList().length() == 1);
261  QVERIFY(dataList[0]->inPointerList().length() == 0);
262  QVERIFY(dataList[0]->adjacentDataList().length() == 1);
263  QVERIFY(dataList[1]->outPointerList().length() == 0);
264  QVERIFY(dataList[1]->inPointerList().length() == 1);
265  QVERIFY(dataList[1]->adjacentDataList().length() == 1);
266 
267  dataList.clear();
268 }
269 
270 
271 QTEST_KDEMAIN_CORE(TestDataStructure)
TestDataStructure.h
KrossBackend.h
PointerType::Bidirectional
Definition: PointerType.h:48
DocumentManager.h
DocumentManager::self
static DocumentManager & self()
Definition: DocumentManager.cpp:57
DataStructurePtr
boost::shared_ptr< DataStructure > DataStructurePtr
Definition: CoreTypes.h:38
DocumentManager::activeDocument
Document * activeDocument() const
Returns the currently active document, or 0 if there document list is empty.
Definition: DocumentManager.cpp:96
DataStructure::create
static DataStructurePtr create(Document *parent=0)
Definition: DataStructure.cpp:68
DataList
QList< boost::shared_ptr< Data > > DataList
Definition: CoreTypes.h:30
QtScriptBackend.h
Data.h
Document.h
PointerType::Unidirectional
Definition: PointerType.h:47
DocumentManager::newDocument
Document * newDocument()
Creates and loads a new graph document.
Definition: DocumentManager.cpp:215
DataStructure.h
Document::addDataStructure
DataStructurePtr addDataStructure(const QString &name=QString())
Add data structure to graph document with name name.
Definition: Document.cpp:333
Document::setActiveDataStructure
void setActiveDataStructure(int index)
Sets the active data structure of graph document with index index in the data structure list...
Definition: Document.cpp:315
Document
Definition: Document.h:41
Pointer.h
DataPtr
boost::shared_ptr< Data > DataPtr
Definition: CoreTypes.h:34
TestDataStructure
Definition: TestDataStructure.h:30
DocumentManager::addDocument
void addDocument(Document *document)
Add document to document list and set this document as active document.
Definition: DocumentManager.cpp:106
TestDataStructure::TestDataStructure
TestDataStructure()
Definition: TestDataStructure.cpp:35
DataStructureBackendManager.h
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