Used a more uniform of logging and added a commandline parser.
[UnsignedByte.git] / src / TestCases / main.cpp
blobb7b45046517cf81923f0eb5a5205909c5f745c71
1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
4 * *
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. *
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, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include <string>
22 #include <vector>
24 #include "smart_ptr.h"
25 #include "SavableHeaders.h"
26 #include "ChunkImporter.h"
27 #include "Chunk.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"
35 #include "Account.h"
36 #include "Join.h"
37 #include "FieldImpls.h"
38 #include "Array.h"
39 #include "RoomManager.h"
40 #include "Managers.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");
52 chunk->Save();
54 printf("Creating importer...\n");
55 std::string input;
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());
73 printf("\n");
75 printf("Applying chunk now...\n");
76 importer->Apply(chunk);
77 printf("\n");
80 void TestStringUtilities()
82 std::string input;
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++)
92 printf(it->c_str());
93 printf("|\n");
95 printf("\n\n");
97 Strings inputs;
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");
119 printf("\n\n");
121 Strings inputs_two;
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++)
134 printf(it->c_str());
135 printf("|\n");
137 printf("\n=========\n");
138 printf("\n\n");
140 printf("\n=========\n");
141 printf(String::Get()->box(inputs_two).c_str());
142 printf("\n=========\n");
144 printf("\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);
183 printf("\n");
185 mask.reset(new SelectionMask(table));
186 FieldValuePtr key(new FieldValue(db::TableImpls::Get()->DETAILS->KEY, "keythree"));
187 mask->addField(key);
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);
245 int totalsize = 0;
247 printf("[");
248 for(std::vector<int>::const_iterator it = sizes.begin(); it != sizes.end(); it++)
250 if(it != sizes.begin())
251 printf(" , ");
253 int size = *it;
254 printf("%d", size);
255 totalsize += size;
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);
275 void TestRoomList()
277 Strings lines = mud::Managers::Get()->Room->List();
278 std::string result = String::Get()->box(lines, "Rooms");
279 printf("%s\n", result.c_str());
282 void TestInserting()
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
287 manager->save();
288 manager->unlock();
291 void printCoordinates(Coordinates coordinates)
293 if(!coordinates.size())
295 printf("-\n");
296 return;
299 printf("[\n");
301 for(Coordinates::const_iterator it = coordinates.begin(); it != coordinates.end(); it++)
303 Coordinate coordinate = *it;
305 if(coordinate.isDirection())
306 printf("%s\n", coordinate.toDirectionString().c_str());
307 else
308 printf("%s\n", coordinate.toString().c_str());
311 printf("]\n");
314 void TestDirections()
316 DirectionParser n("n");
317 printCoordinates(n.getResult());
319 DirectionParser e("e");
320 printCoordinates(e.getResult());
322 DirectionParser s("s");
323 printCoordinates(s.getResult());
325 DirectionParser w("w");
326 printCoordinates(w.getResult());
328 DirectionParser ne("ne");
329 printCoordinates(ne.getResult());
331 // Should be rejected
332 DirectionParser ns("ns");
333 printCoordinates(ns.getResult());
335 DirectionParser nw("nw");
336 printCoordinates(nw.getResult());
338 // Should be rejected
339 DirectionParser nn("nn");
340 printCoordinates(nn.getResult());
342 DirectionParser nwu("nwu");
343 printCoordinates(nwu.getResult());
345 DirectionParser nwu_swd("nwu;swd");
346 printCoordinates(nwu_swd.getResult());
348 DirectionParser nsu_nuswd("nsu;nuswd");
349 printCoordinates(nsu_nuswd.getResult());
351 DirectionParser n_n_("n;n;");
352 printCoordinates(n_n_.getResult());
354 DirectionParser n_n__n("n;n;;n");
355 printCoordinates(n_n__n.getResult());
358 int main()
360 printf("Initializing tables...\n");
361 db::TableImpls::Get()->Initialize();
363 printf("Opening database...\n");
364 std::string dbname = "TestCase";
365 dbname.append(".db");
366 DatabaseMgr::Initialize(dbname);
367 printf("Done.\n");
370 printf("[starttest:Simple]\n");
371 TestSimple();
372 printf("[endtest:Simple]\n");
373 printf("\n");
375 printf("[starttest:Base]\n");
376 TestBase();
377 printf("[endtest:Simple]\n");
378 printf("\n");
380 printf("[starttest:Derived]\n");
381 TestDerived();
382 printf("[endtest:Simple]\n");
383 printf("\n");
385 printf("[starttest:BaseDerived]\n");
386 TestBaseDerived();
387 printf("[endtest:BaseDerived]\n");
388 printf("\n");
390 printf("[starttest:PointerPass]\n");
391 TestPointerPass();
392 printf("[endtest:PointerPass]\n");
393 printf("\n");
395 printf("[starttest:NullPointer]\n");
396 TestNullPtr();
397 printf("[endtest:NullPointer]\n");
398 printf("\n");
402 printf("[starttest:ChunkImporter]\n");
403 TestChunkImporter();
404 printf("[endtest:ChunkImporter]\n");
405 printf("\n");
407 printf("[starttest:StringUtilities]\n");
408 TestStringUtilities();
409 printf("[endtest:StringUtilities]\n");
410 printf("\n");
412 printf("[starttest:SelectionMask]\n");
413 TestSelectionMask();
414 printf("[endtest:SelectionMask]\n");
415 printf("\n");
417 printf("[starttest:SelectionMaskJoin]\n");
418 TestSelectionMaskJoin();
419 printf("[endtest:SelectionMaskJoin]\n");
420 printf("\n");
422 printf("[starttest:TableFormatting]\n");
423 TestTableFormatting();
424 printf("[endtest:TableFormatting]\n");
425 printf("\n");
427 printf("[starttest:RoomList]\n");
428 TestRoomList();
429 printf("[endtest:RoomList]\n");
430 printf("\n");
432 printf("[starttest:Inserting]\n");
433 TestInserting();
434 printf("[endtest:Inserting]\n");
435 printf("\n");
438 printf("[starttest:Directions]\n");
439 TestDirections();
440 printf("[endtest:Directions]\n");
441 printf("\n");
443 g_shutdown = true;
445 printf("Freeing global...\n");
446 Global::Free();
447 printf("Freeing tables...\n");
449 Tables::Free();
450 printf("Exiting...\n");
452 return 0;