00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "SortedCollections.h"
00026 #include "JTime.h"
00027 #include "Message.h"
00028
00029 namespace cmlabs {
00030
00031
00032
00033
00034
00035 SortedCollection::SortedCollection()
00036 {
00037 objectTable.type = "sortedcollection";
00038 objectTable.sorting = SORTBYVALUE;
00039 }
00040
00041 SortedCollection::SortedCollection(const JString& xml) : Collection(xml) {
00042 objectTable.type = "sortedcollection";
00043 objectTable.sorting = SORTBYVALUE;
00044 }
00045
00046 SortedCollection::SortedCollection(XMLNode *node) : Collection(node) {
00047 objectTable.type = "sortedcollection";
00048 objectTable.sorting = SORTBYVALUE;
00049 }
00050
00051
00052 SortedCollection::~SortedCollection()
00053 {
00054
00055 }
00056
00057 int SortedCollection::addAndReturnPosition(const JString& value) {
00058 return objectTable.addAndReturnPosition(NULL, value.clone());
00059 }
00060
00061 int SortedCollection::removeAllBetween(const JString& str1, const JString& str2) {
00062 return objectTable.removeAllEntriesBetween(&str1, &str2);
00063 }
00064
00065 SortedCollection SortedCollection::getAllBetween(const JString& str1, const JString& str2) {
00066 SortedCollection scol;
00067 ObjectCollection* col = objectTable.getAllEntriesBetween(&str1, &str2);
00068 if (col != NULL) {
00069 for (CollectionEntry* entry = (CollectionEntry*) col->getFirst(); entry != NULL; entry = (CollectionEntry*) col->getNext()) {
00070 scol.add(*(JString*)entry->value);
00071 }
00072 }
00073 delete(col);
00074 return scol;
00075 }
00076
00077 JString SortedCollection::getFirstAfter(const JString& str) {
00078 CollectionEntry* entry = objectTable.getFirstEntryAfter(&str);
00079 if (entry == NULL)
00080 return "";
00081 else
00082 return *(JString*) entry->value;
00083 }
00084
00085 JString SortedCollection::getLastBefore(const JString& str) {
00086 CollectionEntry* entry = objectTable.getLastEntryBefore(&str);
00087 if (entry == NULL)
00088 return "";
00089 else
00090 return *(JString*) entry->value;
00091 }
00092
00093
00094
00095
00096
00097
00098 bool SortedCollection::unitTest() {
00099
00100 SortedCollection* oc = new SortedCollection();
00101
00102 oc->add("Test1");
00103 oc->add("Test4");
00104 oc->add("Test2");
00105 oc->add("Test6");
00106 oc->add("Test3");
00107 oc->add("Test5");
00108
00109 if (!oc->getFirst().equals("Test1"))
00110 return false;
00111 if (!oc->getNext().equals("Test2"))
00112 return false;
00113 if (!oc->getNext().equals("Test3"))
00114 return false;
00115 if (!oc->getNext().equals("Test4"))
00116 return false;
00117 if (!oc->getNext().equals("Test5"))
00118 return false;
00119 if (!oc->getNext().equals("Test6"))
00120 return false;
00121
00122 SortedCollection oc2;
00123
00124 oc2 = *oc;
00125
00126 if (!oc2.getFirst().equals("Test1"))
00127 return false;
00128 if (!oc2.getNext().equals("Test2"))
00129 return false;
00130 if (!oc2.getNext().equals("Test3"))
00131 return false;
00132 if (!oc2.getNext().equals("Test4"))
00133 return false;
00134 if (!oc2.getNext().equals("Test5"))
00135 return false;
00136 if (!oc2.getNext().equals("Test6"))
00137 return false;
00138
00139 delete(oc);
00140 return true;
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172 SortedObjectCollection::SortedObjectCollection()
00173 {
00174 objectTable.type = "sortedobjectcollection";
00175 objectTable.sorting = SORTBYVALUE;
00176 }
00177
00178 SortedObjectCollection::SortedObjectCollection(const JString& xml) : ObjectCollection(xml) {
00179 objectTable.type = "sortedobjectcollection";
00180 objectTable.sorting = SORTBYVALUE;
00181 }
00182
00183 SortedObjectCollection::SortedObjectCollection(XMLNode *node) : ObjectCollection(node) {
00184 objectTable.type = "sortedobjectcollection";
00185 objectTable.sorting = SORTBYVALUE;
00186 }
00187
00188
00189 SortedObjectCollection::~SortedObjectCollection()
00190 {
00191
00192 }
00193
00194 int SortedObjectCollection::addAndReturnPosition(Object* value) {
00195 return objectTable.addAndReturnPosition(NULL, value);
00196 }
00197
00198 int SortedObjectCollection::removeAllBetween(const Object* value1, const Object* value2) {
00199 return objectTable.removeAllEntriesBetween(value1, value2);
00200 }
00201
00202 SortedObjectCollection SortedObjectCollection::getAllBetween(const Object* value1, const Object* value2) {
00203 SortedObjectCollection scol;
00204 scol.noDelete();
00205 ObjectCollection* col = objectTable.getAllEntriesBetween(value1, value2);
00206 if (col != NULL) {
00207 for (CollectionEntry* entry = (CollectionEntry*) col->getFirst(); entry != NULL; entry = (CollectionEntry*) col->getNext()) {
00208 scol.add(entry->value);
00209 }
00210 }
00211 delete(col);
00212 return scol;
00213 }
00214
00215 JString SortedObjectCollection::getFirstAfter(const Object* value) {
00216 CollectionEntry* entry = objectTable.getFirstEntryAfter(value);
00217 if (entry == NULL)
00218 return "";
00219 else
00220 return entry->value;
00221 }
00222
00223 JString SortedObjectCollection::getLastBefore(const Object* value) {
00224 CollectionEntry* entry = objectTable.getLastEntryBefore(value);
00225 if (entry == NULL)
00226 return "";
00227 else
00228 return entry->value;
00229 }
00230
00231
00232
00233
00234
00235
00236 bool SortedObjectCollection::unitTest() {
00237
00238 SortedObjectCollection* oc = new SortedObjectCollection();
00239
00240 oc->add(new JString("Test1"));
00241 oc->add(new JString("Test4"));
00242 oc->add(new JString("Test2"));
00243 oc->add(new JString("Test6"));
00244 oc->add(new JString("Test3"));
00245 oc->add(new JString("Test5"));
00246
00247 Object* obj;
00248 if ( (obj = oc->getFirst()) == NULL)
00249 return false;
00250 if (!((JString*)obj)->equals("Test1") )
00251 return false;
00252 if ( (obj = oc->getNext()) == NULL)
00253 return false;
00254 if (!((JString*)obj)->equals("Test2") )
00255 return false;
00256 if ( (obj = oc->getNext()) == NULL)
00257 return false;
00258 if (!((JString*)obj)->equals("Test3") )
00259 return false;
00260 if ( (obj = oc->getNext()) == NULL)
00261 return false;
00262 if (!((JString*)obj)->equals("Test4") )
00263 return false;
00264 if ( (obj = oc->getNext()) == NULL)
00265 return false;
00266 if (!((JString*)obj)->equals("Test5") )
00267 return false;
00268 if ( (obj = oc->getNext()) == NULL)
00269 return false;
00270 if (!((JString*)obj)->equals("Test6") )
00271 return false;
00272
00273 SortedObjectCollection oc2;
00274
00275 oc2 = *oc;
00276 oc2.noDelete();
00277
00278 if ( (obj = oc2.getFirst()) == NULL)
00279 return false;
00280 if (!((JString*)obj)->equals("Test1") )
00281 return false;
00282 if ( (obj = oc2.getNext()) == NULL)
00283 return false;
00284 if (!((JString*)obj)->equals("Test2") )
00285 return false;
00286 if ( (obj = oc2.getNext()) == NULL)
00287 return false;
00288 if (!((JString*)obj)->equals("Test3") )
00289 return false;
00290 if ( (obj = oc2.getNext()) == NULL)
00291 return false;
00292 if (!((JString*)obj)->equals("Test4") )
00293 return false;
00294 if ( (obj = oc2.getNext()) == NULL)
00295 return false;
00296 if (!((JString*)obj)->equals("Test5") )
00297 return false;
00298 if ( (obj = oc2.getNext()) == NULL)
00299 return false;
00300 if (!((JString*)obj)->equals("Test6") )
00301 return false;
00302
00303 oc->removeAll();
00304 oc2.removeAll();
00305
00306 if (oc->getCount() != 0)
00307 return false;
00308 if (oc2.getCount() != 0)
00309 return false;
00310
00311 delete(oc);
00312
00313
00314
00315
00316 oc = new SortedObjectCollection();
00317 oc->objectTable.sorting |= SORTREVERSE;
00318
00319 oc->add(new JString("Test1"));
00320 oc->add(new JString("Test4"));
00321 oc->add(new JString("Test2"));
00322 oc->add(new JString("Test6"));
00323 oc->add(new JString("Test3"));
00324 oc->add(new JString("Test5"));
00325
00326 if ( (obj = oc->getFirst()) == NULL)
00327 return false;
00328 if (!((JString*)obj)->equals("Test6") )
00329 return false;
00330 if ( (obj = oc->getNext()) == NULL)
00331 return false;
00332 if (!((JString*)obj)->equals("Test5") )
00333 return false;
00334 if ( (obj = oc->getNext()) == NULL)
00335 return false;
00336 if (!((JString*)obj)->equals("Test4") )
00337 return false;
00338 if ( (obj = oc->getNext()) == NULL)
00339 return false;
00340 if (!((JString*)obj)->equals("Test3") )
00341 return false;
00342 if ( (obj = oc->getNext()) == NULL)
00343 return false;
00344 if (!((JString*)obj)->equals("Test2") )
00345 return false;
00346 if ( (obj = oc->getNext()) == NULL)
00347 return false;
00348 if (!((JString*)obj)->equals("Test1") )
00349 return false;
00350
00351 delete(oc);
00352
00353
00354
00355
00356
00357 oc = new SortedObjectCollection();
00358 oc->objectTable.sorting |= FINDREVERSE;
00359
00360 oc->add(new JString("Test1"));
00361 oc->add(new JString("Test4"));
00362 oc->add(new JString("Test2"));
00363 oc->add(new JString("Test6"));
00364 oc->add(new JString("Test3"));
00365 oc->add(new JString("Test5"));
00366
00367 if ( (obj = oc->getFirst()) == NULL)
00368 return false;
00369 if (!((JString*)obj)->equals("Test1") )
00370 return false;
00371 if ( (obj = oc->getNext()) == NULL)
00372 return false;
00373 if (!((JString*)obj)->equals("Test2") )
00374 return false;
00375 if ( (obj = oc->getNext()) == NULL)
00376 return false;
00377 if (!((JString*)obj)->equals("Test3") )
00378 return false;
00379 if ( (obj = oc->getNext()) == NULL)
00380 return false;
00381 if (!((JString*)obj)->equals("Test4") )
00382 return false;
00383 if ( (obj = oc->getNext()) == NULL)
00384 return false;
00385 if (!((JString*)obj)->equals("Test5") )
00386 return false;
00387 if ( (obj = oc->getNext()) == NULL)
00388 return false;
00389 if (!((JString*)obj)->equals("Test6") )
00390 return false;
00391
00392 delete(oc);
00393
00394
00395
00396 oc = new SortedObjectCollection();
00397 oc->objectTable.sorting |= SORTREVERSE | FINDREVERSE;
00398
00399 oc->add(new JString("Test1"));
00400 oc->add(new JString("Test4"));
00401 oc->add(new JString("Test2"));
00402 oc->add(new JString("Test6"));
00403 oc->add(new JString("Test3"));
00404 oc->add(new JString("Test5"));
00405
00406 if ( (obj = oc->getFirst()) == NULL)
00407 return false;
00408 if (!((JString*)obj)->equals("Test6") )
00409 return false;
00410 if ( (obj = oc->getNext()) == NULL)
00411 return false;
00412 if (!((JString*)obj)->equals("Test5") )
00413 return false;
00414 if ( (obj = oc->getNext()) == NULL)
00415 return false;
00416 if (!((JString*)obj)->equals("Test4") )
00417 return false;
00418 if ( (obj = oc->getNext()) == NULL)
00419 return false;
00420 if (!((JString*)obj)->equals("Test3") )
00421 return false;
00422 if ( (obj = oc->getNext()) == NULL)
00423 return false;
00424 if (!((JString*)obj)->equals("Test2") )
00425 return false;
00426 if ( (obj = oc->getNext()) == NULL)
00427 return false;
00428 if (!((JString*)obj)->equals("Test1") )
00429 return false;
00430
00431 delete(oc);
00432
00433
00434
00435
00436 oc = new SortedObjectCollection();
00437 oc->objectTable.sorting |= SORTREVERSE;
00438
00439 int n;
00440 for (n=10; n<5000; n++)
00441 oc->add(JString::format("Test%.6d", n).clone());
00442 JTime t1;
00443 oc->add(new JString("Test000001"));
00444 JTime t2;
00445 long l1 = t2.microDifference(t1);
00446
00447 t1 = JTime();
00448 oc->add(new JString("Test000050"));
00449 t2 = JTime();
00450 long l2 = t2.microDifference(t1);
00451
00452 t1 = JTime();
00453 oc->add(new JString("Test000500"));
00454 t2 = JTime();
00455 long l3 = t2.microDifference(t1);
00456
00457 t1 = JTime();
00458 oc->add(new JString("Test005000"));
00459 t2 = JTime();
00460 long l4 = t2.microDifference(t1);
00461
00462 delete(oc);
00463
00464 JString text = JString::format("%d\t%d\t%d\t%d\t", l1, l2, l3, l4);
00465
00466
00467
00468 oc = new SortedObjectCollection();
00469 oc->objectTable.sorting |= SORTREVERSE | FINDREVERSE;
00470
00471 for (n=10; n<5000; n++)
00472 oc->add(JString::format("Test%.6d", n).clone());
00473 t1 = JTime();
00474 oc->add(new JString("Test000001"));
00475 t2 = JTime();
00476 l1 = t2.microDifference(t1);
00477
00478 t1 = JTime();
00479 oc->add(new JString("Test000050"));
00480 t2 = JTime();
00481 l2 = t2.microDifference(t1);
00482
00483 t1 = JTime();
00484 oc->add(new JString("Test000500"));
00485 t2 = JTime();
00486 l3 = t2.microDifference(t1);
00487
00488 t1 = JTime();
00489 oc->add(new JString("Test005000"));
00490 t2 = JTime();
00491 l4 = t2.microDifference(t1);
00492
00493 text = JString::format("%s\n%d\t%d\t%d\t%d", (char*) text, l1, l2, l3, l4);
00494
00495 delete(oc);
00496
00497
00498 oc = new SortedObjectCollection();
00499 oc->objectTable.sorting |= SORTREVERSE | FINDREVERSE;
00500
00501 for (n=10; n<5000; n++)
00502 oc->add(JString::format("Test000500").clone());
00503 t1 = JTime();
00504 oc->add(new JString("Test000001"));
00505 t2 = JTime();
00506 l1 = t2.microDifference(t1);
00507 oc->removeLast();
00508
00509 t1 = JTime();
00510 oc->add(new JString("Test000050"));
00511 t2 = JTime();
00512 l2 = t2.microDifference(t1);
00513 oc->removeLast();
00514
00515 t1 = JTime();
00516 oc->add(new JString("Test000500"));
00517 t2 = JTime();
00518 l3 = t2.microDifference(t1);
00519 oc->removeLast();
00520
00521 t1 = JTime();
00522 oc->add(new JString("Test005000"));
00523 t2 = JTime();
00524 l4 = t2.microDifference(t1);
00525 oc->removeFirst();
00526
00527 text = JString::format("%s\n%d\t%d\t%d\t%d", (char*) text, l1, l2, l3, l4);
00528
00529
00530
00531
00532
00533
00534
00535 delete(oc);
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545 Message* msg;
00546 JString expected;
00547 oc = new SortedObjectCollection();
00548
00549
00550 msg = new Message("", "", "1");
00551 oc->add(msg);
00552 msg = new Message("", "", "2");
00553 oc->add(msg);
00554 msg = new Message("", "", "3");
00555 oc->add(msg);
00556 msg = new Message("", "", "4");
00557 oc->add(msg);
00558 msg = new Message("", "", "5");
00559 oc->add(msg);
00560 msg = new Message("", "", "6");
00561 oc->add(msg);
00562
00563 expected = "1";
00564 msg = (Message*) oc->getFirst();
00565 if (!msg->type.equals(expected)) {
00566 addUnitTestLog(JString::format("Normal sorting failed when using identical entries... (%s)", (char*) expected));
00567 return false;
00568 }
00569 expected = "2";
00570 msg = (Message*) oc->getNext();
00571 if (!msg->type.equals(expected)) {
00572 addUnitTestLog(JString::format("Normal sorting failed when using identical entries... (%s)", (char*) expected));
00573 return false;
00574 }
00575 expected = "3";
00576 msg = (Message*) oc->getNext();
00577 if (!msg->type.equals(expected)) {
00578 addUnitTestLog(JString::format("Normal sorting failed when using identical entries... (%s)", (char*) expected));
00579 return false;
00580 }
00581 expected = "4";
00582 msg = (Message*) oc->getNext();
00583 if (!msg->type.equals(expected)) {
00584 addUnitTestLog(JString::format("Normal sorting failed when using identical entries... (%s)", (char*) expected));
00585 return false;
00586 }
00587 expected = "5";
00588 msg = (Message*) oc->getNext();
00589 if (!msg->type.equals(expected)) {
00590 addUnitTestLog(JString::format("Normal sorting failed when using identical entries... (%s)", (char*) expected));
00591 return false;
00592 }
00593 expected = "6";
00594 msg = (Message*) oc->getNext();
00595 if (!msg->type.equals(expected)) {
00596 addUnitTestLog(JString::format("Normal sorting failed when using identical entries... (%s)", (char*) expected));
00597 return false;
00598 }
00599
00600 delete(oc);
00601
00602
00603
00604
00605 oc = new SortedObjectCollection();
00606 oc->objectTable.sorting |= SORTREVERSE;
00607
00608 msg = new Message("", "", "1");
00609 oc->add(msg);
00610 msg = new Message("", "", "2");
00611 oc->add(msg);
00612 msg = new Message("", "", "3");
00613 oc->add(msg);
00614 msg = new Message("", "", "4");
00615 oc->add(msg);
00616 msg = new Message("", "", "5");
00617 oc->add(msg);
00618 msg = new Message("", "", "6");
00619 oc->add(msg);
00620
00621 expected = "1";
00622 msg = (Message*) oc->getFirst();
00623 if (!msg->type.equals(expected)) {
00624 addUnitTestLog(JString::format("Reverse sorting failed when using identical entries... (%s)", (char*) expected));
00625 return false;
00626 }
00627 expected = "2";
00628 msg = (Message*) oc->getNext();
00629 if (!msg->type.equals(expected)) {
00630 addUnitTestLog(JString::format("Reverse sorting failed when using identical entries... (%s)", (char*) expected));
00631 return false;
00632 }
00633 expected = "3";
00634 msg = (Message*) oc->getNext();
00635 if (!msg->type.equals(expected)) {
00636 addUnitTestLog(JString::format("Reverse sorting failed when using identical entries... (%s)", (char*) expected));
00637 return false;
00638 }
00639 expected = "4";
00640 msg = (Message*) oc->getNext();
00641 if (!msg->type.equals(expected)) {
00642 addUnitTestLog(JString::format("Reverse sorting failed when using identical entries... (%s)", (char*) expected));
00643 return false;
00644 }
00645 expected = "5";
00646 msg = (Message*) oc->getNext();
00647 if (!msg->type.equals(expected)) {
00648 addUnitTestLog(JString::format("Reverse sorting failed when using identical entries... (%s)", (char*) expected));
00649 return false;
00650 }
00651 expected = "6";
00652 msg = (Message*) oc->getNext();
00653 if (!msg->type.equals(expected)) {
00654 addUnitTestLog(JString::format("Reverse sorting failed when using identical entries... (%s)", (char*) expected));
00655 return false;
00656 }
00657
00658 delete(oc);
00659
00660
00661
00662
00663
00664
00665
00666
00667 oc = new SortedObjectCollection();
00668
00669
00670 msg = new Message("", "", "1");
00671 msg->priority = 1;
00672 oc->add(msg);
00673 msg = new Message("", "", "2");
00674 msg->priority = 2;
00675 oc->add(msg);
00676 msg = new Message("", "", "3");
00677 msg->priority = 1;
00678 oc->add(msg);
00679
00680 msg = new Message("", "", "4");
00681 msg->priority = 2;
00682 oc->add(msg);
00683
00684 msg = new Message("", "", "5");
00685 msg->priority = 1;
00686 oc->add(msg);
00687
00688 msg = new Message("", "", "6");
00689 msg->priority = 2;
00690 oc->add(msg);
00691
00692
00693 expected = "1";
00694 msg = (Message*) oc->getFirst();
00695 if (!msg->type.equals(expected)) {
00696 addUnitTestLog(JString::format("Normal sorting failed when using non-identical entries... (%s)\n%s", (char*) expected));
00697 return false;
00698 }
00699 expected = "3";
00700 msg = (Message*) oc->getNext();
00701 if (!msg->type.equals(expected)) {
00702 addUnitTestLog(JString::format("Normal sorting failed when using non-identical entries... (%s)", (char*) expected));
00703 return false;
00704 }
00705 expected = "5";
00706 msg = (Message*) oc->getNext();
00707 if (!msg->type.equals(expected)) {
00708 addUnitTestLog(JString::format("Normal sorting failed when using non-identical entries... (%s)", (char*) expected));
00709 return false;
00710 }
00711 expected = "2";
00712 msg = (Message*) oc->getNext();
00713 if (!msg->type.equals(expected)) {
00714 addUnitTestLog(JString::format("Normal sorting failed when using non-identical entries... (%s)", (char*) expected));
00715 return false;
00716 }
00717 expected = "4";
00718 msg = (Message*) oc->getNext();
00719 if (!msg->type.equals(expected)) {
00720 addUnitTestLog(JString::format("Normal sorting failed when using non-identical entries... (%s)", (char*) expected));
00721 return false;
00722 }
00723 expected = "6";
00724 msg = (Message*) oc->getNext();
00725 if (!msg->type.equals(expected)) {
00726 addUnitTestLog(JString::format("Normal sorting failed when using non-identical entries... (%s)", (char*) expected));
00727 return false;
00728 }
00729
00730 delete(oc);
00731
00732
00733
00734
00735
00736 oc = new SortedObjectCollection();
00737 oc->objectTable.sorting |= SORTREVERSE;
00738
00739 msg = new Message("", "", "1");
00740 msg->priority = 1;
00741 oc->add(msg);
00742 msg = new Message("", "", "2");
00743 msg->priority = 2;
00744 oc->add(msg);
00745
00746 msg = new Message("", "", "4");
00747 msg->priority = 2;
00748 oc->add(msg);
00749
00750 msg = new Message("", "", "6");
00751 msg->priority = 2;
00752 oc->add(msg);
00753
00754 msg = new Message("", "", "3");
00755 msg->priority = 1;
00756 oc->add(msg);
00757
00758 msg = new Message("", "", "5");
00759 msg->priority = 1;
00760 oc->add(msg);
00761
00762
00763 expected = "2";
00764 msg = (Message*) oc->getFirst();
00765 if (!msg->type.equals(expected)) {
00766 addUnitTestLog(JString::format("Reverse sorting failed when using non-identical entries... (%s)", (char*) expected));
00767 return false;
00768 }
00769 expected = "4";
00770 msg = (Message*) oc->getNext();
00771 if (!msg->type.equals(expected)) {
00772 addUnitTestLog(JString::format("Reverse sorting failed when using non-identical entries... (%s)", (char*) expected));
00773 return false;
00774 }
00775 expected = "6";
00776 msg = (Message*) oc->getNext();
00777 if (!msg->type.equals(expected)) {
00778 addUnitTestLog(JString::format("Reverse sorting failed when using non-identical entries... (%s)", (char*) expected));
00779 return false;
00780 }
00781 expected = "1";
00782 msg = (Message*) oc->getNext();
00783 if (!msg->type.equals(expected)) {
00784 addUnitTestLog(JString::format("Reverse sorting failed when using non-identical entries... (%s)", (char*) expected));
00785 return false;
00786 }
00787 expected = "3";
00788 msg = (Message*) oc->getNext();
00789 if (!msg->type.equals(expected)) {
00790 addUnitTestLog(JString::format("Reverse sorting failed when using non-identical entries... (%s)", (char*) expected));
00791 return false;
00792 }
00793 expected = "5";
00794 msg = (Message*) oc->getNext();
00795 if (!msg->type.equals(expected)) {
00796 addUnitTestLog(JString::format("Reverse sorting failed when using non-identical entries... (%s)", (char*) expected));
00797 return false;
00798 }
00799
00800 delete(oc);
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811 return true;
00812 }
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836 SortedDictionary::SortedDictionary()
00837 {
00838 objectTable.type = "sorteddictionary";
00839 objectTable.sorting = SORTBYVALUE;
00840 }
00841
00842 SortedDictionary::SortedDictionary(const JString& xml) : Dictionary(xml) {
00843 objectTable.type = "sorteddictionary";
00844 objectTable.sorting = SORTBYVALUE;
00845 }
00846
00847 SortedDictionary::SortedDictionary(XMLNode *node) : Dictionary(node) {
00848 objectTable.type = "sorteddictionary";
00849 objectTable.sorting = SORTBYVALUE;
00850 }
00851
00852
00853 SortedDictionary::~SortedDictionary()
00854 {
00855
00856 }
00857
00858 int SortedDictionary::putAndReturnPosition(const JString& key, const JString& value) {
00859 JString* keycopy = new JString(key);
00860 JString* valuecopy = new JString(value);
00861 if (objectTable.addAndReturnPosition(keycopy, valuecopy))
00862 return true;
00863 else {
00864 delete(keycopy);
00865 delete(valuecopy);
00866 return false;
00867 }
00868 }
00869
00870 int SortedDictionary::removeAllBetween(const JString& key1, const JString& key2) {
00871 return objectTable.removeAllEntriesBetweenKeys(&key1, &key2);
00872 }
00873
00874 Dictionary SortedDictionary::getAllBetween(const JString& key1, const JString& key2) {
00875 SortedDictionary scol;
00876 ObjectCollection* col = objectTable.getAllEntriesBetweenKeys(&key1, &key2);
00877 if (col != NULL) {
00878 for (CollectionEntry* entry = (CollectionEntry*) col->getFirst(); entry != NULL; entry = (CollectionEntry*) col->getNext()) {
00879 scol.put(*(JString*)entry->key, *(JString*)entry->value);
00880 }
00881 }
00882 delete(col);
00883 return scol;
00884 }
00885
00886 JString SortedDictionary::getFirstAfter(const JString& key) {
00887 CollectionEntry* entry = objectTable.getFirstEntryAfterKey(&key);
00888 if (entry == NULL)
00889 return "";
00890 else
00891 return *(JString*) entry->value;
00892 }
00893
00894 JString SortedDictionary::getLastBefore(const JString& key) {
00895 CollectionEntry* entry = objectTable.getLastEntryBeforeKey(&key);
00896 if (entry == NULL)
00897 return "";
00898 else
00899 return *(JString*) entry->value;
00900 }
00901
00902
00903
00904
00905
00906
00907 bool SortedDictionary::unitTest() {
00908
00909 SortedDictionary* oc = new SortedDictionary();
00910
00911 oc->put("Test1", "Test1");
00912 oc->put("Test4", "Test4");
00913 oc->put("Test2", "Test2");
00914 oc->put("Test6", "Test6");
00915 oc->put("Test3", "Test3");
00916 oc->put("Test5", "Test5");
00917
00918 if (!oc->getFirst().equals("Test1"))
00919 return false;
00920 if (!oc->getNext().equals("Test2"))
00921 return false;
00922 if (!oc->getNext().equals("Test3"))
00923 return false;
00924 if (!oc->getNext().equals("Test4"))
00925 return false;
00926 if (!oc->getNext().equals("Test5"))
00927 return false;
00928 if (!oc->getNext().equals("Test6"))
00929 return false;
00930
00931 SortedDictionary oc2;
00932
00933 oc2 = *oc;
00934
00935 if (!oc2.getFirst().equals("Test1"))
00936 return false;
00937 if (!oc2.getNext().equals("Test2"))
00938 return false;
00939 if (!oc2.getNext().equals("Test3"))
00940 return false;
00941 if (!oc2.getNext().equals("Test4"))
00942 return false;
00943 if (!oc2.getNext().equals("Test5"))
00944 return false;
00945 if (!oc2.getNext().equals("Test6"))
00946 return false;
00947
00948 delete(oc);
00949 return true;
00950 }
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981 SortedObjectDictionary::SortedObjectDictionary()
00982 {
00983 objectTable.type = "sortedobjectdictionary";
00984 objectTable.sorting = SORTBYVALUE;
00985 }
00986
00987 SortedObjectDictionary::SortedObjectDictionary(const JString& xml) : ObjectDictionary(xml) {
00988 objectTable.type = "sortedobjectdictionary";
00989 objectTable.sorting = SORTBYVALUE;
00990 }
00991
00992 SortedObjectDictionary::SortedObjectDictionary(XMLNode *node) : ObjectDictionary(node) {
00993 objectTable.type = "sortedobjectdictionary";
00994 objectTable.sorting = SORTBYVALUE;
00995 }
00996
00997
00998 SortedObjectDictionary::~SortedObjectDictionary()
00999 {
01000
01001 }
01002
01003 int SortedObjectDictionary::putAndReturnPosition(const JString& key, Object* value) {
01004 JString* keycopy = new JString(key);
01005 if (objectTable.addAndReturnPosition(keycopy, value))
01006 return true;
01007 else {
01008 delete(keycopy);
01009 return false;
01010 }
01011 }
01012
01013 int SortedObjectDictionary::removeAllBetween(const JString& key1, const JString& key2) {
01014 return objectTable.removeAllEntriesBetweenKeys(&key1, &key2);
01015 }
01016
01017 ObjectDictionary SortedObjectDictionary::getAllBetween(const JString& key1, const JString& key2) {
01018 SortedObjectDictionary scol;
01019 scol.noDelete();
01020 ObjectCollection* col = objectTable.getAllEntriesBetweenKeys(&key1, &key2);
01021 if (col != NULL) {
01022 for (CollectionEntry* entry = (CollectionEntry*) col->getFirst(); entry != NULL; entry = (CollectionEntry*) col->getNext()) {
01023 scol.put(*(JString*)entry->key, entry->value);
01024 }
01025 }
01026 delete(col);
01027 return scol;
01028 }
01029
01030 JString SortedObjectDictionary::getFirstAfter(const JString& key) {
01031 CollectionEntry* entry = objectTable.getFirstEntryAfterKey(&key);
01032 if (entry == NULL)
01033 return "";
01034 else
01035 return entry->value;
01036 }
01037
01038 JString SortedObjectDictionary::getLastBefore(const JString& key) {
01039 CollectionEntry* entry = objectTable.getLastEntryBeforeKey(&key);
01040 if (entry == NULL)
01041 return "";
01042 else
01043 return entry->value;
01044 }
01045
01046
01047
01048
01049
01050
01051 bool SortedObjectDictionary::unitTest() {
01052
01053 SortedObjectDictionary* oc = new SortedObjectDictionary();
01054
01055 oc->put("Test1", new JString("Test1"));
01056 oc->put("Test4", new JString("Test4"));
01057 oc->put("Test2", new JString("Test2"));
01058 oc->put("Test6", new JString("Test6"));
01059 oc->put("Test3", new JString("Test3"));
01060 oc->put("Test5", new JString("Test5"));
01061
01062 Object* obj;
01063 if ( (obj = oc->getFirst()) == NULL)
01064 return false;
01065 if (!((JString*)obj)->equals("Test1") )
01066 return false;
01067 if ( (obj = oc->getNext()) == NULL)
01068 return false;
01069 if (!((JString*)obj)->equals("Test2") )
01070 return false;
01071 if ( (obj = oc->getNext()) == NULL)
01072 return false;
01073 if (!((JString*)obj)->equals("Test3") )
01074 return false;
01075 if ( (obj = oc->getNext()) == NULL)
01076 return false;
01077 if (!((JString*)obj)->equals("Test4") )
01078 return false;
01079 if ( (obj = oc->getNext()) == NULL)
01080 return false;
01081 if (!((JString*)obj)->equals("Test5") )
01082 return false;
01083 if ( (obj = oc->getNext()) == NULL)
01084 return false;
01085 if (!((JString*)obj)->equals("Test6") )
01086 return false;
01087
01088 SortedObjectDictionary oc2;
01089
01090 oc2 = *oc;
01091 oc2.noDelete();
01092
01093 if ( (obj = oc2.getFirst()) == NULL)
01094 return false;
01095 if (!((JString*)obj)->equals("Test1") )
01096 return false;
01097 if ( (obj = oc2.getNext()) == NULL)
01098 return false;
01099 if (!((JString*)obj)->equals("Test2") )
01100 return false;
01101 if ( (obj = oc2.getNext()) == NULL)
01102 return false;
01103 if (!((JString*)obj)->equals("Test3") )
01104 return false;
01105 if ( (obj = oc2.getNext()) == NULL)
01106 return false;
01107 if (!((JString*)obj)->equals("Test4") )
01108 return false;
01109 if ( (obj = oc2.getNext()) == NULL)
01110 return false;
01111 if (!((JString*)obj)->equals("Test5") )
01112 return false;
01113 if ( (obj = oc2.getNext()) == NULL)
01114 return false;
01115 if (!((JString*)obj)->equals("Test6") )
01116 return false;
01117
01118 oc->removeAll();
01119 oc2.removeAll();
01120
01121 if (oc->getCount() != 0)
01122 return false;
01123 if (oc2.getCount() != 0)
01124 return false;
01125
01126 delete(oc);
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137 Message* msg;
01138 JString expected;
01139 ObjectTable* ot = new ObjectTable();
01140 ot->sorting |= SORTBYKEY;
01141
01142
01143 msg = new Message("", "", "1");
01144 ot->add(msg, msg->clone());
01145 msg = new Message("", "", "2");
01146 ot->add(msg, msg->clone());
01147 msg = new Message("", "", "3");
01148 ot->add(msg, msg->clone());
01149 msg = new Message("", "", "4");
01150 ot->add(msg, msg->clone());
01151 msg = new Message("", "", "5");
01152 ot->add(msg, msg->clone());
01153 msg = new Message("", "", "6");
01154 ot->add(msg, msg->clone());
01155
01156 expected = "1";
01157 msg = (Message*) ot->getFirst();
01158 if (!msg->type.equals(expected)) {
01159 addUnitTestLog(JString::format("Normal sorting failed when using identical entries... (%s)", (char*) expected));
01160 return false;
01161 }
01162 expected = "2";
01163 msg = (Message*) ot->getNext();
01164 if (!msg->type.equals(expected)) {
01165 addUnitTestLog(JString::format("Normal sorting failed when using identical entries... (%s)", (char*) expected));
01166 return false;
01167 }
01168 expected = "3";
01169 msg = (Message*) ot->getNext();
01170 if (!msg->type.equals(expected)) {
01171 addUnitTestLog(JString::format("Normal sorting failed when using identical entries... (%s)", (char*) expected));
01172 return false;
01173 }
01174 expected = "4";
01175 msg = (Message*) ot->getNext();
01176 if (!msg->type.equals(expected)) {
01177 addUnitTestLog(JString::format("Normal sorting failed when using identical entries... (%s)", (char*) expected));
01178 return false;
01179 }
01180 expected = "5";
01181 msg = (Message*) ot->getNext();
01182 if (!msg->type.equals(expected)) {
01183 addUnitTestLog(JString::format("Normal sorting failed when using identical entries... (%s)", (char*) expected));
01184 return false;
01185 }
01186 expected = "6";
01187 msg = (Message*) ot->getNext();
01188 if (!msg->type.equals(expected)) {
01189 addUnitTestLog(JString::format("Normal sorting failed when using identical entries... (%s)", (char*) expected));
01190 return false;
01191 }
01192
01193 delete(ot);
01194
01195
01196
01197
01198
01199 ot = new ObjectTable();
01200 ot->sorting |= SORTBYKEY;
01201 ot->sorting |= SORTREVERSE;
01202
01203 msg = new Message("", "", "1");
01204 ot->add(msg, msg->clone());
01205 msg = new Message("", "", "2");
01206 ot->add(msg, msg->clone());
01207
01208 msg = new Message("", "", "3");
01209 ot->add(msg, msg->clone());
01210
01211 msg = new Message("", "", "4");
01212 ot->add(msg, msg->clone());
01213
01214 msg = new Message("", "", "5");
01215 ot->add(msg, msg->clone());
01216
01217 msg = new Message("", "", "6");
01218 ot->add(msg, msg->clone());
01219
01220
01221 expected = "1";
01222 msg = (Message*) ot->getFirst();
01223 if (!msg->type.equals(expected)) {
01224 addUnitTestLog(JString::format("Reverse sorting failed when using identical entries... (%s)", (char*) expected));
01225 return false;
01226 }
01227 expected = "2";
01228 msg = (Message*) ot->getNext();
01229 if (!msg->type.equals(expected)) {
01230 addUnitTestLog(JString::format("Reverse sorting failed when using identical entries... (%s)", (char*) expected));
01231 return false;
01232 }
01233 expected = "3";
01234 msg = (Message*) ot->getNext();
01235 if (!msg->type.equals(expected)) {
01236 addUnitTestLog(JString::format("Reverse sorting failed when using identical entries... (%s)", (char*) expected));
01237 return false;
01238 }
01239 expected = "4";
01240 msg = (Message*) ot->getNext();
01241 if (!msg->type.equals(expected)) {
01242 addUnitTestLog(JString::format("Reverse sorting failed when using identical entries... (%s)", (char*) expected));
01243 return false;
01244 }
01245 expected = "5";
01246 msg = (Message*) ot->getNext();
01247 if (!msg->type.equals(expected)) {
01248 addUnitTestLog(JString::format("Reverse sorting failed when using identical entries... (%s)", (char*) expected));
01249 return false;
01250 }
01251 expected = "6";
01252 msg = (Message*) ot->getNext();
01253 if (!msg->type.equals(expected)) {
01254 addUnitTestLog(JString::format("Reverse sorting failed when using identical entries... (%s)", (char*) expected));
01255 return false;
01256 }
01257
01258 delete(ot);
01259
01260
01261
01262
01263
01264
01265
01266
01267 ot = new ObjectTable();
01268 ot->sorting |= SORTBYKEY;
01269
01270
01271 msg = new Message("", "", "1");
01272 msg->priority = 1;
01273 ot->add(msg, msg->clone());
01274 msg = new Message("", "", "2");
01275 msg->priority = 2;
01276 ot->add(msg, msg->clone());
01277 msg = new Message("", "", "3");
01278 msg->priority = 1;
01279 ot->add(msg, msg->clone());
01280
01281 msg = new Message("", "", "4");
01282 msg->priority = 2;
01283 ot->add(msg, msg->clone());
01284
01285 msg = new Message("", "", "5");
01286 msg->priority = 1;
01287 ot->add(msg, msg->clone());
01288
01289 msg = new Message("", "", "6");
01290 msg->priority = 2;
01291 ot->add(msg, msg->clone());
01292
01293
01294 expected = "1";
01295 msg = (Message*) ot->getFirst();
01296 if (!msg->type.equals(expected)) {
01297 addUnitTestLog(JString::format("Normal sorting failed when using non-identical entries... (%s)\n%s", (char*) expected));
01298 return false;
01299 }
01300 expected = "3";
01301 msg = (Message*) ot->getNext();
01302 if (!msg->type.equals(expected)) {
01303 addUnitTestLog(JString::format("Normal sorting failed when using non-identical entries... (%s)", (char*) expected));
01304 return false;
01305 }
01306 expected = "5";
01307 msg = (Message*) ot->getNext();
01308 if (!msg->type.equals(expected)) {
01309 addUnitTestLog(JString::format("Normal sorting failed when using non-identical entries... (%s)", (char*) expected));
01310 return false;
01311 }
01312 expected = "2";
01313 msg = (Message*) ot->getNext();
01314 if (!msg->type.equals(expected)) {
01315 addUnitTestLog(JString::format("Normal sorting failed when using non-identical entries... (%s)", (char*) expected));
01316 return false;
01317 }
01318 expected = "4";
01319 msg = (Message*) ot->getNext();
01320 if (!msg->type.equals(expected)) {
01321 addUnitTestLog(JString::format("Normal sorting failed when using non-identical entries... (%s)", (char*) expected));
01322 return false;
01323 }
01324 expected = "6";
01325 msg = (Message*) ot->getNext();
01326 if (!msg->type.equals(expected)) {
01327 addUnitTestLog(JString::format("Normal sorting failed when using non-identical entries... (%s)", (char*) expected));
01328 return false;
01329 }
01330
01331 delete(ot);
01332
01333
01334
01335
01336
01337 ot = new ObjectTable();
01338 ot->sorting |= SORTBYKEY;
01339 ot->sorting |= SORTREVERSE;
01340
01341 msg = new Message("", "", "1");
01342 msg->priority = 1;
01343 ot->add(msg, msg->clone());
01344 msg = new Message("", "", "2");
01345 msg->priority = 2;
01346 ot->add(msg, msg->clone());
01347
01348 msg = new Message("", "", "4");
01349 msg->priority = 2;
01350 ot->add(msg, msg->clone());
01351
01352 msg = new Message("", "", "6");
01353 msg->priority = 2;
01354 ot->add(msg, msg->clone());
01355
01356 msg = new Message("", "", "3");
01357 msg->priority = 1;
01358 ot->add(msg, msg->clone());
01359
01360 msg = new Message("", "", "5");
01361 msg->priority = 1;
01362 ot->add(msg, msg->clone());
01363
01364
01365 expected = "2";
01366 msg = (Message*) ot->getFirst();
01367 if (!msg->type.equals(expected)) {
01368 addUnitTestLog(JString::format("Reverse sorting failed when using non-identical entries... (%s)", (char*) expected));
01369 return false;
01370 }
01371 expected = "4";
01372 msg = (Message*) ot->getNext();
01373 if (!msg->type.equals(expected)) {
01374 addUnitTestLog(JString::format("Reverse sorting failed when using non-identical entries... (%s)", (char*) expected));
01375 return false;
01376 }
01377 expected = "6";
01378 msg = (Message*) ot->getNext();
01379 if (!msg->type.equals(expected)) {
01380 addUnitTestLog(JString::format("Reverse sorting failed when using non-identical entries... (%s)", (char*) expected));
01381 return false;
01382 }
01383 expected = "1";
01384 msg = (Message*) ot->getNext();
01385 if (!msg->type.equals(expected)) {
01386 addUnitTestLog(JString::format("Reverse sorting failed when using non-identical entries... (%s)", (char*) expected));
01387 return false;
01388 }
01389 expected = "3";
01390 msg = (Message*) ot->getNext();
01391 if (!msg->type.equals(expected)) {
01392 addUnitTestLog(JString::format("Reverse sorting failed when using non-identical entries... (%s)", (char*) expected));
01393 return false;
01394 }
01395 expected = "5";
01396 msg = (Message*) ot->getNext();
01397 if (!msg->type.equals(expected)) {
01398 addUnitTestLog(JString::format("Reverse sorting failed when using non-identical entries... (%s)", (char*) expected));
01399 return false;
01400 }
01401
01402 delete(ot);
01403
01404
01405
01406
01407
01408
01409
01410
01411
01412
01413
01414
01415
01416
01417 return true;
01418 }
01419
01420
01421 }