1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
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. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
24 #include "smart_ptr.h"
25 #include "SavableHeaders.h"
26 #include "ChunkImporter.h"
28 #include "ChunkManager.h"
29 #include "StringUtilities.h"
30 #include "DatabaseMgr.h"
31 #include "SelectionMask.h"
32 #include "TableImpls.h"
33 #include "FieldValues.h"
34 #include "SqliteMgr.h"
37 #include "FieldImpls.h"
39 #include "RoomManager.h"
41 #include "DirectionParser.h"
43 extern bool g_shutdown
;
45 void TestChunkImporter()
47 value_type key
= mud::Managers::Get()->Chunk
->Add()->first()->getIntegerValue();
48 mud::ChunkPtr chunk
= mud::Managers::Get()->Chunk
->GetByKey(key
);
50 chunk
->setDescription("Some chunk thingy.");
51 chunk
->setTags("Chunk Thingy");
54 printf("Creating importer...\n");
56 input
.append("Algemene beschrijving.\n");
57 input
.append("*Eerste detail.\n");
58 input
.append("\tMet wat extra beschrijving.\n");
59 input
.append("\tDrie regels in totaal.\n");
60 input
.append("*Tweede detail.\n");
61 input
.append("*Derde detail.\n");
62 input
.append("**Met een subdetail\n");
63 input
.append("\tMet wat extra text in subdetail\n");
64 input
.append("**Nog een subdetail\n");
65 input
.append("*Vierde detail\n");
66 input
.append("**Subdetail\n");
67 input
.append("***Subsub detail\n");
68 input
.append("***Subsub detail twee\n");
69 input
.append("****Subsubsub detail\n");
70 input
.append("**Subdetail\n");
71 ChunkImporterPtr
importer(new ChunkImporter(input
));
72 printf(importer
->getResult().c_str());
75 printf("Applying chunk now...\n");
76 importer
->Apply(chunk
);
80 void TestStringUtilities()
83 input
.append("Dit is de eerste regel.\n");
84 input
.append("Dit is de tweede regel.\n");
85 input
.append("Derde regel.\n");
86 input
.append("Regel vier.\n");
87 input
.append("Laatste regel.\n");
89 Strings results
= String::Get()->lines(input
,"\n");
90 for(Strings::iterator it
= results
.begin(); it
!= results
.end(); it
++)
98 inputs
.push_back("Dit is de eerste regel.");
99 inputs
.push_back("Dit is de tweede regel.");
100 inputs
.push_back("Derde regel.");
101 inputs
.push_back("Regel vier.");
102 inputs
.push_back("Laatste regel.");
104 printf("\n=========\n");
105 printf(String::Get()->unlines(inputs
, "+", 0).c_str());
106 printf("\n=========\n");
107 printf(String::Get()->unlines(inputs
, "~", 1).c_str());
108 printf("\n=========\n");
109 printf(String::Get()->unlines(inputs
, "#", 2).c_str());
110 printf("\n=========\n");
111 printf(String::Get()->unlines(inputs
, "@", 3).c_str());
112 printf("\n=========\n");
113 printf(String::Get()->unlines(inputs
, "=", 4).c_str());
114 printf("\n=========\n");
115 printf(String::Get()->unlines(inputs
, "&", 5).c_str());
116 printf("\n=========\n");
117 printf(String::Get()->unlines(inputs
, "&", 6).c_str());
118 printf("\n=========\n");
122 inputs_two
.push_back("Dit is de eerste regel.\nMet wat extra's.");
123 inputs_two
.push_back("Dit is de tweede regel.");
124 inputs_two
.push_back("Derde regel.\nMet iets erachter.\nTwee ietsen zelfs");
125 inputs_two
.push_back("Regel vier.\nZit.\nVol.\nMet.\nExtra.\nRegels.\nZes in totaal.");
126 inputs_two
.push_back("Lege regels!\n\n\n\n\n\n\n\n");
127 inputs_two
.push_back("Laatste regel.");
129 Strings results_two
= String::Get()->unlines(inputs_two
);
131 printf("\n=========\n");
132 for(Strings::iterator it
= results_two
.begin(); it
!= results_two
.end(); it
++)
137 printf("\n=========\n");
140 printf("\n=========\n");
141 printf(String::Get()->box(inputs_two
).c_str());
142 printf("\n=========\n");
147 void printSavables(SavableManagersPtr result
)
149 for(SavableManagerVector::const_iterator it
= result
->begin(); it
!= result
->end(); it
++)
151 SavableManagerPtr manager
= *it
;
152 FieldValuePtr detailid
= manager
->getValue(db::TableImpls::Get()->DETAILS
->DETAILID
);
153 ValuePtr key
= manager
->getValue(db::TableImpls::Get()->DETAILS
->KEY
);
154 ValuePtr description
= manager
->getValue(db::TableImpls::Get()->DETAILS
->DESCRIPTION
);
156 printf("%s (%d): %s.\n", key
->getStringValue().c_str(), detailid
->getIntegerValue(), description
->getStringValue().c_str());
160 void printSavableRoom(SavableManagersPtr result
)
162 for(SavableManagerVector::const_iterator it
= result
->begin(); it
!= result
->end(); it
++)
164 SavableManagerPtr manager
= *it
;
165 FieldValuePtr roomid
= manager
->getValue(db::TableImpls::Get()->ROOMS
->ROOMID
);
166 ValuePtr name
= manager
->getValue(db::TableImpls::Get()->ROOMS
->NAME
);
167 ValuePtr description
= manager
->getValue(db::TableImpls::Get()->ROOMS
->DESCRIPTION
);
169 printf("%s (%d): %s.\n", name
->getStringValue().c_str(), roomid
->getIntegerValue(), description
->getStringValue().c_str());
173 void TestSelectionMask()
175 TableImplPtr table
= db::TableImpls::Get()->DETAILS
;
177 SelectionMaskPtr
mask(new SelectionMask(table
));
178 SqliteMgr::Get()->doSelectMulti(mask
.get());
180 printf("Full table:\n");
181 SavableManagersPtr result
= mask
->getResult();
182 printSavables(result
);
185 mask
.reset(new SelectionMask(table
));
186 FieldValuePtr
key(new FieldValue(db::TableImpls::Get()->DETAILS
->KEY
, "keythree"));
188 SqliteMgr::Get()->doSelectMulti(mask
.get());
190 printf("Just three\n");
191 result
= mask
->getResult();
192 printSavables(result
);
194 mask
.reset(new SelectionMask(table
));
196 TableImplPtr joinTable
= db::TableImpls::Get()->DETAILROOM
;
197 KeyImplPtr nativeKey
= db::TableImpls::Get()->DETAILS
->DETAILID
;
198 KeyImplPtr foreignKey
= db::TableImpls::Get()->DETAILROOM
->FKDETAILS
;
199 mask
->addJoin(joinTable
, nativeKey
, foreignKey
);
201 TableImplPtr roomTable
= db::TableImpls::Get()->ROOMS
;
202 KeyImplPtr reverseNativeKey
= db::TableImpls::Get()->DETAILROOM
->FKROOMS
;
203 KeyImplPtr reverseForeignKey
= db::TableImpls::Get()->ROOMS
->ROOMID
;
204 mask
->addJoin(joinTable
, roomTable
, reverseNativeKey
, reverseForeignKey
);
206 FieldValuePtr
foreignField(new FieldValue(db::TableImpls::Get()->ROOMS
->LENGTH
, 1));
207 mask
->addField(foreignField
);
209 SqliteMgr::Get()->doSelectMulti(mask
.get());
211 printf("Join restriction\n");
212 result
= mask
->getResult();
213 printSavables(result
);
216 void TestSelectionMaskJoin()
218 TableImplPtr table
= db::TableImpls::Get()->ROOMS
;
220 SelectionMaskPtr
mask(new SelectionMask(table
));
222 TableImplPtr joinTable
= db::TableImpls::Get()->CLUSTERS
;
223 KeyImplPtr nativeKey
= db::TableImpls::Get()->ROOMS
->FKCLUSTERS
;
224 KeyImplPtr foreignKey
= db::TableImpls::Get()->CLUSTERS
->CLUSTERID
;
225 mask
->addJoin(joinTable
, nativeKey
, foreignKey
);
227 FieldValuePtr
foreignField(new FieldValue(db::TableImpls::Get()->CLUSTERS
->NAME
, "Space"));
228 mask
->addField(foreignField
);
230 SqliteMgr::Get()->doSelectMulti(mask
.get());
232 printf("Join direct\n");
233 SavableManagersPtr result
= mask
->getResult();
234 printSavableRoom(result
);
237 void tableFormattingHelper(int* sizesarray
, int arraysize
)
239 std::vector
<int> sizes
;
240 for(int i
= 0; i
< arraysize
; i
++) {
241 sizes
.push_back(sizesarray
[i
]);
244 String::Get()->capFieldSize(sizes
, sizes
.size()*7);
248 for(std::vector
<int>::const_iterator it
= sizes
.begin(); it
!= sizes
.end(); it
++)
250 if(it
!= sizes
.begin())
257 printf("] = %d (%d).\n", totalsize
, sizes
.size()*7);
260 void TestTableFormatting()
262 int sizes1
[] = { 7, 1, 4, 6, 7, 9, 12, 24 };
263 tableFormattingHelper(sizes1
, 8);
265 int sizes2
[] = { 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 };
266 tableFormattingHelper(sizes2
, 10);
268 int sizes3
[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 100 };
269 tableFormattingHelper(sizes3
, 10);
271 int sizes4
[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 9, 15 };
272 tableFormattingHelper(sizes4
, 15);
277 Strings lines
= mud::Managers::Get()->Room
->List();
278 std::string result
= String::Get()->box(lines
, "Rooms");
279 printf("%s\n", result
.c_str());
284 SavableManagerPtr manager
= SavableManager::getnew(db::TableImpls::Get()->VERSION
);
285 bool locked
= manager
->lock();
286 Assert(locked
); // we're the only one active, should always be lockable
291 void printCoordinates(Coordinates coordinates
)
293 if(!coordinates
.size())
299 for(Coordinates::const_iterator it
= coordinates
.begin(); it
!= coordinates
.end(); it
++)
301 Coordinate coordinate
= *it
;
303 if(coordinate
.isDirection())
304 printf("%s\n", coordinate
.toDirectionString().c_str());
306 printf("%s\n", coordinate
.toString().c_str());
310 void TestDirections()
312 DirectionParser
n("n");
313 printCoordinates(n
.getResult());
315 DirectionParser
e("e");
316 printCoordinates(e
.getResult());
318 DirectionParser
s("s");
319 printCoordinates(s
.getResult());
321 DirectionParser
w("w");
322 printCoordinates(w
.getResult());
324 DirectionParser
ne("ne");
325 printCoordinates(ne
.getResult());
327 DirectionParser
ns("ns");
328 printCoordinates(ns
.getResult());
330 DirectionParser
nw("nw");
331 printCoordinates(nw
.getResult());
333 DirectionParser
nn("nn");
334 printCoordinates(nn
.getResult());
336 DirectionParser
nwu("nwu");
337 printCoordinates(nwu
.getResult());
339 DirectionParser
nwu_swd("nwu;swd");
340 printCoordinates(nwu_swd
.getResult());
345 printf("Initializing tables...\n");
346 db::TableImpls::Get()->Initialize();
348 printf("Opening database...\n");
349 std::string dbname
= "TestCase";
350 dbname
.append(".db");
351 DatabaseMgr::Initialize(dbname
);
355 printf("[starttest:Simple]\n");
357 printf("[endtest:Simple]\n");
360 printf("[starttest:Base]\n");
362 printf("[endtest:Simple]\n");
365 printf("[starttest:Derived]\n");
367 printf("[endtest:Simple]\n");
370 printf("[starttest:BaseDerived]\n");
372 printf("[endtest:BaseDerived]\n");
375 printf("[starttest:PointerPass]\n");
377 printf("[endtest:PointerPass]\n");
380 printf("[starttest:NullPointer]\n");
382 printf("[endtest:NullPointer]\n");
387 printf("[starttest:ChunkImporter]\n");
389 printf("[endtest:ChunkImporter]\n");
392 printf("[starttest:StringUtilities]\n");
393 TestStringUtilities();
394 printf("[endtest:StringUtilities]\n");
397 printf("[starttest:SelectionMask]\n");
399 printf("[endtest:SelectionMask]\n");
402 printf("[starttest:SelectionMaskJoin]\n");
403 TestSelectionMaskJoin();
404 printf("[endtest:SelectionMaskJoin]\n");
407 printf("[starttest:TableFormatting]\n");
408 TestTableFormatting();
409 printf("[endtest:TableFormatting]\n");
412 printf("[starttest:RoomList]\n");
414 printf("[endtest:RoomList]\n");
417 printf("[starttest:Inserting]\n");
419 printf("[endtest:Inserting]\n");
423 printf("[starttest:Directions]\n");
425 printf("[endtest:Directions]\n");
430 printf("Freeing global...\n");
432 printf("Freeing tables...\n");
435 printf("Exiting...\n");