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
00026 #include <memory>
00027 #include <mpcl/memory/alloc_smart_pointer.hh>
00028 #include <mpcl/memory/smart_pointer.hh>
00029 #include <mpcl/util/general-functions.hh>
00030 #include <mpcl/test.h>
00031 #include <vector>
00032
00033
00034 using mpcl::memory::TAllocSmartPointer;
00035 using mpcl::memory::TSmartPointer;
00036 using std::allocator;
00037 using std::vector;
00038
00039
00040
00041
00042
00043
00048 class TMother
00049 {
00050
00051 protected:
00052
00053 int iMother;
00054
00055
00056 public:
00057
00058 TMother (void) :
00059 iMother (0) {}
00060
00061 };
00062
00063
00064 class TFather
00065 {
00066
00067 protected:
00068
00069 int& riValue;
00070
00071
00072 public:
00073
00074 TFather (int& riVALUE) :
00075 riValue (riVALUE)
00076 {
00077 riValue += 1;
00078 }
00079
00080 virtual ~TFather (void)
00081 {
00082 riValue -= 1;
00083 }
00084
00085 virtual bool isFather (void) const
00086 {
00087 return true;
00088 }
00089
00090 };
00091
00092
00093 class TSon :
00094 public TMother ,
00095 public TFather
00096 {
00097
00098 public:
00099
00100 int iAnotherValue;
00101
00102
00103 public:
00104
00105 TSon (int& riVALUE) :
00106 TMother () ,
00107 TFather (riVALUE) ,
00108 iAnotherValue (0)
00109 {
00110 riValue += 10;
00111 }
00112
00113 ~TSon (void)
00114 {
00115 riValue -= 10;
00116 }
00117
00118 bool isFather (void) const
00119 {
00120 return false;
00121 }
00122
00123 };
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 static vector<int*> _tIntArray;
00137 static vector<TSmartPointer<int> > _tIntSmartPointerVector;
00138 static vector<TAllocSmartPointer<int> > _tIntAllocSmartPointerVector;
00139
00140
00141
00142
00143
00144
00145 static void _Insert (int* piSOURCE)
00146 {
00147 _tIntArray.push_back (piSOURCE);
00148 }
00149
00150
00151 static void _InsertAllocSmartPointer (int* piSOURCE)
00152 {
00153 _tIntAllocSmartPointerVector.push_back (TAllocSmartPointer<int> (piSOURCE));
00154 }
00155
00156
00157 static void _InsertSmartPointer (int* piSOURCE)
00158 {
00159 _tIntSmartPointerVector.push_back (TSmartPointer<int> (piSOURCE));
00160 }
00161
00162
00163 static TFather* _ReturnPointerToFather (TFather* ptFATHER)
00164 {
00165 return ptFATHER;
00166 }
00167
00168
00169 static TAllocSmartPointer<TFather>
00170 _ReturnAllocSmartPointerToFather (TAllocSmartPointer<TFather>& rqtFATHER)
00171 {
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 return rqtFATHER;
00187
00188 }
00189
00190
00191 static TSmartPointer<TFather>
00192 _ReturnSmartPointerToFather (TSmartPointer<TFather>& rqtFATHER)
00193 {
00194 return rqtFATHER;
00195 }
00196
00197
00198 static TAllocSmartPointer<TFather>&
00199 _ReturnAllocSmartPointerReferenceToFather (TAllocSmartPointer<TFather>& rqtFATHER)
00200 {
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 return rqtFATHER;
00216
00217 }
00218
00219
00220 static TSmartPointer<TFather>&
00221 _ReturnSmartPointerReferenceToFather (TSmartPointer<TFather>& rqtFATHER)
00222 {
00223 return rqtFATHER;
00224 }
00225
00226
00227 static int _TestAllocators (void)
00228 {
00229
00230 TEST_INIT ("tests for class 'TAllocSmartPointer'");
00231
00232 TAllocSmartPointer<int, allocator> qtInt;
00233 allocator<int> tIntAllocator;
00234 int* ptInt = tIntAllocator.allocate (1);
00235
00236 tIntAllocator.construct (ptInt, 1);
00237 qtInt = ptInt;
00238 TEST_NUMBERS (1, *qtInt);
00239 qtInt = NULL;
00240
00241 TEST_MEMORY_STATUS;
00242 TEST_RETURN_CODE;
00243
00244 }
00245
00246
00247 static int _TestAssigns (void)
00248 {
00249
00250 typedef
00251 TSmartPointer<int>
00252 QTInt;
00253
00254 typedef
00255 TSmartPointer<const int>
00256 QKTInt;
00257
00258 TEST_INIT ("tests for assigns");
00259
00260 int iValue = 100;
00261
00262
00263
00264
00265 {
00266 TSmartPointer<TFather> qtFather;
00267 TSmartPointer<TSon> qtSon1;
00268 TSmartPointer<TSon> qtSon2;
00269 TSmartPointer<int> qiInt;
00270 TSon* ptSon = NULL;
00271
00272
00273
00274
00275
00276
00277
00278 qtFather = new TFather (iValue);
00279 TEST_NUMBERS (101, iValue);
00280
00281
00282
00283
00284
00285
00286
00287
00288 qtSon1 = new TSon (iValue);
00289 TEST_NUMBERS (112, iValue);
00290 TEST_NUMBERS (true, qtFather->isFather());
00291 TEST_NUMBERS (false, qtSon1->isFather());
00292
00293
00294
00295
00296
00297 try
00298 {
00299 qtFather.dynamicCast<TSon*>();
00300 TEST_FAIL;
00301 }
00302 catch (const mpcl::TConstraintException&)
00303 {
00304 TEST_PASS;
00305 }
00306
00307
00308
00309
00310
00311
00312
00313 qtFather = NULL;
00314 TEST_NUMBERS (111, iValue);
00315
00316
00317
00318
00319 qtSon1 = qtSon1;
00320 TEST_NUMBERS (111, iValue);
00321 TEST_NUMBERS (false, qtSon1->isFather());
00322 TEST_NUMBERS (1, qtSon1.references());
00323 TEST_NUMBERS (true, ( qtSon1 == qtSon1 ));
00324
00325
00326
00327
00328
00329
00330
00331
00332 qtSon2 = new TSon (iValue);
00333 TEST_NUMBERS (122, iValue);
00334
00335
00336
00337
00338 ptSon = qtSon1.dynamicCast<TSon*>();
00339 TEST_NUMBERS (122, iValue);
00340 TEST_NUMBERS (false, ptSon->isFather());
00341 TEST_NUMBERS (false, qtSon2->isFather());
00342 TEST_NUMBERS (1, qtSon2.references());
00343 }
00344 TEST_NUMBERS (100, iValue);
00345
00346
00347
00348
00349 {
00350 QTInt qtInt;
00351 QKTInt qktInt;
00352
00353 qtInt = new int (1);
00354 qktInt = new int (2);
00355 TEST_NUMBERS (*qtInt, 1);
00356 TEST_NUMBERS (*qktInt, 2);
00357 TEST_NUMBERS ((*QKTInt (qtInt)), 1);
00358 TEST_NUMBERS ((*QKTInt (qktInt)), 2);
00359 TEST_NUMBERS (QKTInt (qtInt).references(), 2);
00360 TEST_NUMBERS (QKTInt (qktInt).references(), 2);
00361 TEST_NUMBERS (true, ( qktInt.constCast<int*>() != NULL ));
00362 {
00363 QTInt qtInnerInt;
00364
00365 qtInnerInt = new int (3);
00366 qtInt = qtInnerInt;
00367 TEST_NUMBERS (*qtInt, 3);
00368 TEST_NUMBERS (*qtInnerInt, 3);
00369 TEST_NUMBERS (qtInt.references(), 2);
00370 TEST_NUMBERS (qtInnerInt.references(), 2);
00371 TEST_NUMBERS (qtInnerInt, qtInt);
00372 }
00373 TEST_NUMBERS (*qtInt, 3);
00374 TEST_NUMBERS (qtInt.references(), 1);
00375 qtInt = new int (4);
00376 TEST_NUMBERS (*qtInt, 4);
00377 TEST_NUMBERS (qtInt.references(), 1);
00378 }
00379
00380 TEST_MEMORY_STATUS;
00381 TEST_RETURN_CODE;
00382
00383 }
00384
00385
00386 static int _TestBenchmarks (void)
00387 {
00388
00389 vector<int*>::iterator I;
00390 int* piNewInt;
00391 TFather* ptAllocFather;
00392 TAllocSmartPointer<TFather> qtAllocFather;
00393 TSmartPointer<TFather> qtFather;
00394 allocator<TFather> tFatherAllocator;
00395 allocator<int> tIntAllocator;
00396 int iValue = 0;
00397 TFather* ptFather = new TFather (iValue);
00398
00399 TEST_INIT ("benchmarks for smart pointers insertion/returning");
00400
00401
00402
00403
00404 ptAllocFather = tFatherAllocator.allocate (1);
00405 tFatherAllocator.construct (ptAllocFather, iValue);
00406 qtAllocFather = ptAllocFather;
00407 qtFather = new TFather (iValue);
00408
00409 TEST_START_WATCH;
00410 for (size_t J = 0; ( J < 100000 ) ;++J)
00411 {
00412 _Insert (new int (J));
00413 }
00414 TEST_STOP_WATCH;
00415 TEST_DISPLAY_WATCH ("C++ standard pointer insertion");
00416
00417 TEST_START_WATCH;
00418 for (size_t J = 0; ( J < 100000 ) ;++J)
00419 {
00420 _InsertSmartPointer (new int (J));
00421 }
00422 TEST_STOP_WATCH;
00423 TEST_DISPLAY_WATCH ("TSmartPointer insertion");
00424
00425 TEST_START_WATCH;
00426 for (size_t J = 0; ( J < 100000 ) ;++J)
00427 {
00428 piNewInt = tIntAllocator.allocate (1);
00429 tIntAllocator.construct (piNewInt, J);
00430 _InsertAllocSmartPointer (piNewInt);
00431 }
00432 TEST_STOP_WATCH;
00433 TEST_DISPLAY_WATCH ("TAllocSmartPointer insertion");
00434
00435 TEST_START_WATCH;
00436 for (size_t J = 0; ( J < 400000 ) ;++J)
00437 {
00438 _ReturnPointerToFather (ptFather);
00439 }
00440 TEST_STOP_WATCH;
00441 TEST_DISPLAY_WATCH ("C++ standard pointer returning");
00442
00443 TEST_START_WATCH;
00444 for (size_t J = 0; ( J < 400000 ) ;++J)
00445 {
00446 _ReturnSmartPointerToFather (qtFather);
00447 }
00448 TEST_STOP_WATCH;
00449 TEST_DISPLAY_WATCH ("TSmartPointer returning");
00450
00451 TEST_START_WATCH;
00452 for (size_t J = 0; ( J < 400000 ) ;++J)
00453 {
00454 _ReturnAllocSmartPointerToFather (qtAllocFather);
00455 }
00456 TEST_STOP_WATCH;
00457 TEST_DISPLAY_WATCH ("TAllocSmartPointer returning");
00458
00459 TEST_START_WATCH;
00460 for (size_t J = 0; ( J < 400000 ) ;++J)
00461 {
00462 _ReturnSmartPointerReferenceToFather (qtFather);
00463 }
00464 TEST_STOP_WATCH;
00465 TEST_DISPLAY_WATCH ("TSmartPointer reference returning");
00466
00467 TEST_START_WATCH;
00468 for (size_t J = 0; ( J < 400000 ) ;++J)
00469 {
00470 _ReturnAllocSmartPointerReferenceToFather (qtAllocFather);
00471 }
00472 TEST_STOP_WATCH;
00473 TEST_DISPLAY_WATCH ("TAllocSmartPointer reference returning");
00474
00475
00476
00477
00478 delete ptFather;
00479 for (I = _tIntArray.begin(); ( I != _tIntArray.end() ) ;++I)
00480 {
00481 delete *I;
00482 }
00483
00484 TEST_MEMORY_STATUS;
00485 TEST_RETURN_CODE;
00486
00487 }
00488
00489
00490 static int _TestCastings (void)
00491 {
00492
00493 int iValue;
00494 TFather* ptFather;
00495 TSon* ptSon;
00496 TSmartPointer<TFather> qtFather (new TFather (iValue));
00497 TSmartPointer<TFather> qtSon (new TSon (iValue));
00498 TSmartPointer<TFather> qtAnotherFather (qtFather);
00499
00500 TEST_INIT ("test for castings");
00501 TEST_NUMBERS (true, qtAnotherFather->isFather());
00502 qtAnotherFather = qtSon;
00503 TEST_NUMBERS (false, qtAnotherFather->isFather());
00504
00505
00506
00507
00508
00509 try
00510 {
00511
00512
00513
00514 qtAnotherFather = qtFather;
00515 ptSon = qtAnotherFather.dynamicCast<TSon*>();
00516 TEST_FAIL;
00517 }
00518 catch (const mpcl::TConstraintException&)
00519 {
00520 TEST_PASS;
00521 }
00522
00523 try
00524 {
00525
00526
00527
00528 qtAnotherFather = qtSon;
00529 ptFather = qtAnotherFather.get();
00530 TEST_PASS;
00531 }
00532 catch (const mpcl::TConstraintException&)
00533 {
00534 TEST_FAIL;
00535 }
00536
00537
00538
00539
00540 TEST_START_WATCH;
00541 for (size_t J = 0; ( J < 1000000 ) ;++J)
00542 {
00543 ptSon = (TSon*) ptFather;
00544 }
00545 TEST_STOP_WATCH;
00546 TEST_DISPLAY_WATCH ("C++ standard casting");
00547
00548
00549
00550
00551 TEST_START_WATCH;
00552 qtAnotherFather = qtSon;
00553 for (size_t J = 0; ( J < 1000000 ) ;++J)
00554 {
00555 ptSon = qtAnotherFather.dynamicCast<TSon*>();
00556 }
00557 TEST_STOP_WATCH;
00558 TEST_DISPLAY_WATCH ("TSmartPointer casting");
00559 TEST_MEMORY_STATUS;
00560 TEST_RETURN_CODE;
00561
00562 }
00563
00564
00565 static int _TestContainers (void)
00566 {
00567
00568 TEST_INIT ("test for smart pointers and containers");
00569
00570 vector<TSmartPointer<TFather> > tFatherSmartPointerVector1;
00571 vector<TSmartPointer<TFather> > tFatherSmartPointerVector2;
00572 int iValue = 0;
00573
00574 TEST_START_WATCH;
00575 for (size_t J = 0; ( J < 100000 ) ;++J)
00576 {
00577 tFatherSmartPointerVector1.push_back (TSmartPointer<TFather> (new TFather (iValue)));
00578 tFatherSmartPointerVector2.push_back (TSmartPointer<TFather> (tFatherSmartPointerVector1.back()));
00579 tFatherSmartPointerVector1.push_back (TSmartPointer<TFather> (new TSon (iValue)));
00580 tFatherSmartPointerVector2.push_back (TSmartPointer<TFather> (tFatherSmartPointerVector1.back()));
00581 }
00582 TEST_STOP_WATCH;
00583 TEST_DISPLAY_WATCH ("container with 400.000 items");
00584 TEST_MEMORY_STATUS;
00585 TEST_RETURN_CODE;
00586
00587 }
00588
00589
00590 static int _TestCopyConstructors (TSmartPointer<int> qiSOURCE, int iREAL_VALUE)
00591 {
00592
00593 TEST_INIT ("test for copy constructors");
00594 TEST_NUMBERS (iREAL_VALUE, *qiSOURCE);
00595 TEST_MEMORY_STATUS;
00596 TEST_RETURN_CODE;
00597
00598 }
00599
00600
00602 int main (void)
00603 {
00604
00605 TEST_INIT ("tests for class 'TSmartPointer'");
00606
00607 TSmartPointer<int> qiAnotherInteger;
00608 TSmartPointer<int> qiOtherInteger;
00609 TSmartPointer<int> qiMyInteger (new int (10));
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644 TEST_NUMBERS (10, *qiMyInteger);
00645 TEST_NUMBERS (1, qiMyInteger.references());
00646 *qiMyInteger = 11;
00647 TEST_NUMBERS (11, *qiMyInteger);
00648 qiOtherInteger = qiMyInteger;
00649 qiAnotherInteger = qiOtherInteger;
00650 TEST_NUMBERS (3, qiMyInteger.references());
00651 TEST_NUMBERS (11, *qiMyInteger);
00652 TEST_NUMBERS (11, *qiOtherInteger);
00653 TEST_NUMBERS (11, *qiAnotherInteger);
00654
00655 *qiAnotherInteger = 9;
00656 TEST_NUMBERS (9, *qiMyInteger);
00657 TEST_NUMBERS (9, *qiOtherInteger);
00658 TEST_NUMBERS (9, *qiAnotherInteger);
00659 TEST_NUMBERS (qiMyInteger, qiMyInteger);
00660 TEST_NUMBERS (qiMyInteger, qiOtherInteger);
00661 TEST_NUMBERS (qiMyInteger, qiAnotherInteger);
00662 TEST_NUMBERS (3, qiMyInteger.references());
00663
00664 qiOtherInteger = new int (100);
00665 TEST_NUMBERS (9, *qiMyInteger);
00666 TEST_NUMBERS (100, *qiOtherInteger);
00667 TEST_NUMBERS (9, *qiAnotherInteger);
00668 TEST_NUMBERS (2, qiMyInteger.references());
00669
00670 qiAnotherInteger = qiOtherInteger;
00671 TEST_NUMBERS (9, *qiMyInteger);
00672 TEST_NUMBERS (100, *qiOtherInteger);
00673 TEST_NUMBERS (100, *qiAnotherInteger);
00674 TEST_NUMBERS (qiMyInteger, qiMyInteger);
00675 TEST_NUMBERS (qiOtherInteger, qiOtherInteger);
00676 TEST_NUMBERS (qiOtherInteger, qiAnotherInteger);
00677 TEST_NUMBERS (1, qiMyInteger.references());
00678 TEST_NUMBERS (2, qiOtherInteger.references());
00679
00680 *qiAnotherInteger = 101;
00681 TEST_NUMBERS ( 9, *qiMyInteger);
00682 TEST_NUMBERS (101, *qiOtherInteger);
00683 TEST_NUMBERS (101, *qiAnotherInteger);
00684 TEST_NUMBERS (qiMyInteger, qiMyInteger);
00685 TEST_NUMBERS (qiOtherInteger, qiOtherInteger);
00686 TEST_NUMBERS (qiOtherInteger, qiAnotherInteger);
00687
00688 qiMyInteger = new int (1000);
00689 qiMyInteger = new int (1001);
00690 qiMyInteger = new int (1002);
00691 TEST_NUMBERS (1002, *qiMyInteger);
00692 TEST_NUMBERS (101, *qiOtherInteger);
00693 TEST_NUMBERS (101, *qiAnotherInteger);
00694 TEST_NUMBERS (qiMyInteger, qiMyInteger);
00695 TEST_NUMBERS (qiOtherInteger, qiOtherInteger);
00696 TEST_NUMBERS (qiOtherInteger, qiAnotherInteger);
00697 TEST_NUMBERS (1, qiMyInteger.references());
00698
00699 TEST_FUNCTION (_TestAllocators());
00700 TEST_FUNCTION (_TestAssigns());
00701 TEST_FUNCTION (_TestBenchmarks());
00702 TEST_FUNCTION (_TestCastings());
00703 TEST_FUNCTION (_TestContainers());
00704 TEST_FUNCTION (_TestCopyConstructors (qiMyInteger, 1002));
00705 TEST_FUNCTION (_TestCopyConstructors (qiOtherInteger, 101));
00706 TEST_FUNCTION (_TestCopyConstructors (qiAnotherInteger, 101));
00707
00708 qiMyInteger = NULL;
00709 TEST_NUMBERS (true, !qiMyInteger);
00710 TEST_NUMBERS (false, !qiAnotherInteger);
00711
00712 TEST_MEMORY_STATUS;
00713 TEST_RETURN_CODE;
00714
00715 }