TestPointer.cpp

Go to the documentation of this file.
00001 
00002 //
00003 // This source file is a part of ParCompMark
00004 // Parallel Compositing Benchmark Framework
00005 //
00006 // for latest info see http://parcompmark.sourceforge.net
00007 
00008 //
00009 // Copyright (C) 2006 IT2 ParCompMark Dev. Team
00010 // 
00011 // This program is free software; you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License
00013 // as published by the Free Software Foundation; either version 2
00014 // of the License, or (at your option) any later version.
00015 // 
00016 // This program is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 // GNU General Public License for more details.
00020 // 
00021 // You should have received a copy of the GNU General Public License
00022 // along with this program; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00024 
00027 
00028 #include "../include/TestPointer.h"
00029 
00030 namespace ParCompMarkTest
00031 {
00032 
00033   void TestPointer::setUp()
00034   {
00035         // Pointer cannot log, beacuse the logger has a pointer attribute (mFp)
00036   }
00037 
00038  /*----------------------------------------------------------------------*/
00039 
00040   void TestPointer::tearDown()
00041   {
00042   }
00043 
00044  /*----------------------------------------------------------------------*/
00045 
00046   //
00047   // Constructor & destructor tests
00048   //
00049 
00050   void TestPointer::test_constructor()
00051   {
00052 
00053         /*
00054          * You have to verify the following:
00055          * 
00056          * Create a NULL pointer.
00057          */
00058 
00059         Pointer < int,
00060         DummyLock > p;
00061 
00062         CPPUNIT_ASSERT_MESSAGE("Null pointer initialization error", p.mMeta->ptr == 0);
00063         CPPUNIT_ASSERT_MESSAGE("Null pointer initialization error", p.mMeta->dead == true);
00064         CPPUNIT_ASSERT_MESSAGE("Null pointer initialization error", p.mMeta->usage == 1);
00065         CPPUNIT_ASSERT_MESSAGE("Null pointer initialization error", p.mMeta->ownMemory == false);
00066   }
00067 
00068  /*----------------------------------------------------------------------*/
00069 
00070   void TestPointer::test_constructor_cT_p_cbool()
00071   {
00072 
00073         /*
00074          * You have to verify the following:
00075          * 
00076          * Create smart pointer from a C style pointer.
00077          */
00078 
00079         // Take ownership
00080         int *_p = new int (1);
00081         Pointer < int,
00082         DummyLock > p(_p);
00083 
00084         CPPUNIT_ASSERT_MESSAGE("C style pointer initialization error", p.mMeta->ptr == _p);
00085         CPPUNIT_ASSERT_MESSAGE("C style pointer initialization error", p.mMeta->dead == false);
00086         CPPUNIT_ASSERT_MESSAGE("C style pointer initialization error", p.mMeta->usage == 1);
00087         CPPUNIT_ASSERT_MESSAGE("C style pointer initialization error", p.mMeta->ownMemory == true);
00088 
00089         // Do not take ownership
00090         Pointer < int,
00091         DummyLock > p2(_p, false);
00092 
00093         CPPUNIT_ASSERT_MESSAGE("C style pointer initialization error", p2.mMeta->ptr == _p);
00094         CPPUNIT_ASSERT_MESSAGE("C style pointer initialization error", p2.mMeta->dead == false);
00095         CPPUNIT_ASSERT_MESSAGE("C style pointer initialization error", p2.mMeta->usage == 1);
00096         CPPUNIT_ASSERT_MESSAGE("C style pointer initialization error", p2.mMeta->ownMemory == false);
00097   }
00098 
00099  /*----------------------------------------------------------------------*/
00100 
00101   void TestPointer::test_constructor_Pointer___T__Lock__()
00102   {
00103 
00104         /*
00105          * You have to verify the following:
00106          * 
00107          * Create smart pointer from an another smart pointer. (Copy constructor)
00108          */
00109 
00110         int *_p = new int (1);
00111         Pointer < int,
00112         DummyLock > p1(_p);
00113         Pointer < int,
00114         DummyLock > p2(p1);
00115 
00116         CPPUNIT_ASSERT_MESSAGE("Copy constructor initialization error", p1.mMeta == p2.mMeta);
00117         CPPUNIT_ASSERT_MESSAGE("Copy constructor initialization error", p1.mMeta->usage == 2);
00118   }
00119 
00120  /*----------------------------------------------------------------------*/
00121 
00122   void TestPointer::test_constructor_cPointer___T__Lock__()
00123   {
00124 
00125         /*
00126          * You have to verify the following:
00127          * 
00128          * Create smart pointer from an another const smart pointer. (Constantant copy constructor)
00129          */
00130 
00131         int *_p = new int (1);
00132         const Pointer < int,
00133         DummyLock > p1(_p);
00134         Pointer < int,
00135         DummyLock > p2(p1);
00136 
00137         CPPUNIT_ASSERT_MESSAGE("Copy constructor initialization error", p1.mMeta == p2.mMeta);
00138         CPPUNIT_ASSERT_MESSAGE("Copy constructor initialization error", p1.mMeta->usage == 2);
00139   }
00140 
00141  /*----------------------------------------------------------------------*/
00142 
00143   void TestPointer::test_destructor()
00144   {
00145         int *_p = new int (1);
00146         Pointer < int,
00147         DummyLock > p1(_p);
00148 
00149         do
00150         {
00151          Pointer < int,
00152           DummyLock > p2(p1);
00153         } while(false);
00154         CPPUNIT_ASSERT_MESSAGE("Copy constructor initialization error", p1.mMeta->usage == 1);
00155   }
00156 
00157  /*----------------------------------------------------------------------*/
00158 
00159   //
00160   // Operator tests
00161   //
00162 
00163   void TestPointer::test_operator_assignment_cT_p()
00164   {
00165 
00166         /*
00167          * You have to verify the following:
00168          * 
00169          * Assign a C style pointer.
00170          */
00171 
00172         int *_p1 = new int (1);
00173         Pointer < int,
00174         DummyLock > p(_p1);
00175         Pointer < int,
00176         DummyLock > p2(p);      // double reference
00177 
00178         // Assign self
00179         void *_meta = p.mMeta;
00180 
00181         CPPUNIT_ASSERT_NO_THROW(p = _p1);
00182         CPPUNIT_ASSERT_MESSAGE("C style pointer self assign error", p.mMeta->ptr == _p1);
00183         CPPUNIT_ASSERT_MESSAGE("C style pointer self assign error", p.mMeta->usage == 2);
00184         CPPUNIT_ASSERT_MESSAGE("C style pointer self assign error", p.mMeta == _meta);
00185 
00186         // Assign another
00187         int *_p2 = new int (2);
00188 
00189         CPPUNIT_ASSERT_NO_THROW(p = _p2);
00190         CPPUNIT_ASSERT_MESSAGE("C style pointer assign error", p.mMeta->ptr == _p2);
00191         CPPUNIT_ASSERT_MESSAGE("C style pointer assign error", p.mMeta->usage == 1);
00192   }
00193 
00194  /*----------------------------------------------------------------------*/
00195 
00196   void TestPointer::test_operator_assignment_Pointer___T__Lock__()
00197   {
00198 
00199         /*
00200          * You have to verify the following:
00201          * 
00202          * Assign another smart pointer.
00203          */
00204 
00205         typedef Pointer < int,
00206         DummyLock > MyPointer;
00207 
00208         int *_p = new int (1);
00209         MyPointer p1(_p);
00210 
00211         // Assign self
00212         void *_meta = p1.mMeta;
00213 
00214         CPPUNIT_ASSERT_NO_THROW(p1 = p1);
00215         CPPUNIT_ASSERT_MESSAGE("Self assign error", p1.mMeta->usage == 1);
00216         CPPUNIT_ASSERT_MESSAGE("Self assign error", p1.mMeta == _meta);
00217 
00218         // Assign another
00219         MyPointer p2;
00220 
00221         CPPUNIT_ASSERT_NO_THROW(p2 = p1);
00222         CPPUNIT_ASSERT_MESSAGE("Assign error", p1.mMeta == p2.mMeta);
00223         CPPUNIT_ASSERT_MESSAGE("Assign error", p1.mMeta->usage == 2);
00224 
00225         // Assing a just-in created smart pointer (null)
00226         CPPUNIT_ASSERT_NO_THROW(p2 = MyPointer());
00227         CPPUNIT_ASSERT_MESSAGE("Just-in created assign error", p2.mMeta->ptr == 0);
00228         CPPUNIT_ASSERT_MESSAGE("Just-in created assign error", p2.mMeta->dead == true);
00229         CPPUNIT_ASSERT_MESSAGE("Just-in created assign error", p2.mMeta->usage == 1);
00230         CPPUNIT_ASSERT_MESSAGE("Just-in created assign error", p2.mMeta->ownMemory == false);
00231 
00232         // Assing a just-in created smart pointer (not null)
00233         CPPUNIT_ASSERT_NO_THROW(p2 = MyPointer(new int (1234)));
00234 
00235         CPPUNIT_ASSERT_MESSAGE("Just-in created assign error", p2.mMeta->ptr != 0);
00236         CPPUNIT_ASSERT_MESSAGE("Just-in created assign error", *(p2.mMeta->ptr) == 1234);
00237         CPPUNIT_ASSERT_MESSAGE("Just-in created assign error", p2.mMeta->dead == false);
00238         CPPUNIT_ASSERT_MESSAGE("Just-in created assign error", p2.mMeta->usage == 1);
00239         CPPUNIT_ASSERT_MESSAGE("Just-in created assign error", p2.mMeta->ownMemory == true);
00240   }
00241 
00242  /*----------------------------------------------------------------------*/
00243 
00244   void TestPointer::test_operator_assignment_cPointer___T__Lock__()
00245   {
00246 
00247         /*
00248          * You have to verify the following:
00249          * 
00250          * Assign another smart pointer (const version).
00251          */
00252 
00253         // Testes in nonconst version
00254   }
00255 
00256  /*----------------------------------------------------------------------*/
00257 
00258   void TestPointer::test_operator_memberSelection()
00259   {
00260 
00261         /*
00262          * You have to verify the following:
00263          * 
00264          * Get referenced object.
00265          */
00266 
00267         int *_p = new int (1);
00268         Pointer < int,
00269         DummyLock > p(_p);
00270 
00271         CPPUNIT_ASSERT_MESSAGE("Member selection(->) error", p.operator->() == _p);
00272   }
00273 
00274  /*----------------------------------------------------------------------*/
00275 
00294  /*----------------------------------------------------------------------*/
00295 
00314  /*----------------------------------------------------------------------*/
00315 
00334  /*----------------------------------------------------------------------*/
00335 
00336   void TestPointer::test_operator_equality_cT_p()
00337   {
00338 
00339         /*
00340          * You have to verify the following:
00341          * 
00342          * Equality test operator on a C style pointer.
00343          */
00344 
00345         // ambiguous !
00346         // int *_p = new int (1);
00347         // Pointer < int, DummyLock > p(_p);
00348         // CPPUNIT_ASSERT(p == _p);
00349   }
00350 
00351  /*----------------------------------------------------------------------*/
00352 
00353   void TestPointer::test_operator_equality_Pointer___T__Lock__()
00354   {
00355 
00356         /*
00357          * You have to verify the following:
00358          * 
00359          * Equality test operator.
00360          */
00361 
00362         int *_p1 = new int (1);
00363         int *_p2 = new int (2);
00364         Pointer < int,
00365         DummyLock > p1(_p1);
00366         Pointer < int,
00367         DummyLock > p2(p1);
00368         Pointer < int,
00369         DummyLock > p3(_p2);
00370 
00371         CPPUNIT_ASSERT(p1 == p2);
00372         CPPUNIT_ASSERT(!(p1 == p3));
00373   }
00374 
00375  /*----------------------------------------------------------------------*/
00376 
00377   void TestPointer::test_operator_inequality_cT_p()
00378   {
00379 
00380         /*
00381          * You have to verify the following:
00382          * 
00383          * Equality test operator on a C style pointer.
00384          */
00385 
00386         // ambiguous !
00387         // int *_p = new int (1);
00388         // Pointer < int, DummyLock > p(_p);
00389         // CPPUNIT_ASSERT((p != _p));
00390   }
00391 
00392  /*----------------------------------------------------------------------*/
00393 
00394   void TestPointer::test_operator_inequality_Pointer___T__Lock__()
00395   {
00396 
00397         /*
00398          * You have to verify the following:
00399          * 
00400          * Equality test operator.
00401          */
00402 
00403         int *_p1 = new int (1);
00404         int *_p2 = new int (2);
00405         Pointer < int,
00406         DummyLock > p1(_p1);
00407         Pointer < int,
00408         DummyLock > p2(p1);
00409         Pointer < int,
00410         DummyLock > p3(_p2);
00411 
00412         CPPUNIT_ASSERT(!(p1 != p2));
00413         CPPUNIT_ASSERT(p1 != p3);
00414   }
00415 
00416  /*----------------------------------------------------------------------*/
00417 
00418   //
00419   // Method tests
00420   //
00421 
00422   void TestPointer::test_isNull()
00423   {
00424 
00425         /*
00426          * You have to verify the following:
00427          * 
00428          * Return true if the pointer does not reference anything.
00429          */
00430 
00431         Pointer < int,
00432         DummyLock > p1;
00433 
00434         int *_p = new int (1);
00435         Pointer < int,
00436         DummyLock > p2(_p);
00437 
00438         CPPUNIT_ASSERT(p1.isNull());
00439         CPPUNIT_ASSERT(!p2.isNull());
00440   }
00441 
00442  /*----------------------------------------------------------------------*/
00443 
00444   void TestPointer::test_isNotNull()
00445   {
00446 
00447         /*
00448          * You have to verify the following:
00449          * 
00450          * Return true if the pointer does reference something.
00451          */
00452 
00453         Pointer < int,
00454         DummyLock > p1;
00455 
00456         int *_p = new int (1);
00457         Pointer < int,
00458         DummyLock > p2(_p);
00459 
00460         CPPUNIT_ASSERT(!p1.isNotNull());
00461         CPPUNIT_ASSERT(p2.isNotNull());
00462   }
00463 
00464  /*----------------------------------------------------------------------*/
00465 
00466   void TestPointer::test_assignWithLock_Pointer___T__Lock__()
00467   {
00468 
00469         /*
00470          * You have to verify the following:
00471          * 
00472          * Assign another smart pointer with keeping it locked.
00473          */
00474 
00475         typedef Pointer < int,
00476         Mutex > MyPointer;
00477 
00478         int *_p = new int (1);
00479         MyPointer p1(_p);
00480 
00481         // Assign self
00482         void *_meta = p1.mMeta;
00483 
00484         CPPUNIT_ASSERT_NO_THROW(p1.assignWithLock(p1));
00485         CPPUNIT_ASSERT_MESSAGE("Self assign error", p1.mMeta->usage == 1);
00486         CPPUNIT_ASSERT_MESSAGE("Self assign error", p1.mMeta == _meta);
00487         // The pointed is still locked
00488         CPPUNIT_ASSERT_MESSAGE("Self assign error", p1.mMeta->lock.getLocked());
00489         p1.unlock();
00490 
00491         // Assign another
00492         MyPointer p2;
00493 
00494         CPPUNIT_ASSERT_NO_THROW(p2.assignWithLock(p1));
00495         CPPUNIT_ASSERT_MESSAGE("Assign error", p1.mMeta == p2.mMeta);
00496         CPPUNIT_ASSERT_MESSAGE("Assign error", p1.mMeta->usage == 2);
00497         // The pointed is still locked
00498         CPPUNIT_ASSERT_MESSAGE("Self assign error", p2.mMeta->lock.getLocked());
00499         p2.unlock();
00500   }
00501 
00502  /*----------------------------------------------------------------------*/
00503 
00504   void TestPointer::test_reference_cT_p()
00505   {
00506 
00507         /*
00508          * You have to verify the following:
00509          * 
00510          * Take reference from a C style pointer (does not delete referenced object when smart pointer dies).
00511          */
00512 
00513         int *_p1 = new int (1);
00514         Pointer < int,
00515         DummyLock > p(_p1);
00516         Pointer < int,
00517         DummyLock > p2(p);      // double reference
00518 
00519         // Assign self
00520         void *_meta = p.mMeta;
00521 
00522         CPPUNIT_ASSERT_NO_THROW(p = _p1);
00523         CPPUNIT_ASSERT_MESSAGE("C style pointer self assign error", p.mMeta->ptr == _p1);
00524         CPPUNIT_ASSERT_MESSAGE("C style pointer self assign error", p.mMeta->usage == 2);
00525         CPPUNIT_ASSERT_MESSAGE("C style pointer self assign error", p.mMeta == _meta);
00526 
00527         // Assign another
00528         int *_p2 = new int (2);
00529 
00530         CPPUNIT_ASSERT_NO_THROW(p = _p2);
00531         CPPUNIT_ASSERT_MESSAGE("C style pointer assign error", p.mMeta->ptr == _p2);
00532         CPPUNIT_ASSERT_MESSAGE("C style pointer assign error", p.mMeta->usage == 1);
00533   }
00534 
00535  /*----------------------------------------------------------------------*/
00536 
00537   void TestPointer::test_getPtr()
00538   {
00539 
00540         /*
00541          * You have to verify the following:
00542          * 
00543          * Return a C style pointer to the referenced object.
00544          */
00545 
00546         int *_p = new int (1);
00547         Pointer < int,
00548         DummyLock > p(_p);
00549 
00550         CPPUNIT_ASSERT_MESSAGE("C style pointer error", p.getPtr() == _p);
00551   }
00552 
00553  /*----------------------------------------------------------------------*/
00554 
00555   void TestPointer::test_kill_cbool()
00556   {
00557 
00558         /*
00559          * You have to verify the following:
00560          * 
00561          * Force deallocating referenced object.
00562          */
00563 
00564         // Nice kill
00565         int *_p = new int (1);
00566         Pointer < int,
00567         DummyLock > p1(_p);
00568         Pointer < int,
00569         DummyLock > p2(p1);
00570 
00571         CPPUNIT_ASSERT_NO_THROW(p2.kill());
00572         CPPUNIT_ASSERT_MESSAGE("Nice kill error", p1.isNotNull());
00573 
00574         // Force kill
00575         Pointer < int,
00576         DummyLock > p3(p1);
00577 
00578         CPPUNIT_ASSERT_NO_THROW(p3.kill(true));
00579         CPPUNIT_ASSERT_MESSAGE("Force kill error", p1.isNull());
00580   }
00581 
00582  /*----------------------------------------------------------------------*/
00583 
00584   void TestPointer::test_setNull_cbool()
00585   {
00586 
00587         /*
00588          * You have to verify the following:
00589          * 
00590          * Set the referenced object to null without trying to deallocate the actual object.
00591          * 
00592          * The typical usage of this method the external deallocating a pointer,
00593          * like fclose.
00594          * 
00595          */
00596 
00597         int *_p = new int (1);
00598 
00599         // Create a pointer referencing this object and set to null
00600         Pointer < int,
00601         DummyLock > p(_p);
00602 
00603         p.setNull();
00604 
00605         // Deallocate referenced object
00606         delete _p;
00607 
00608         // Destructor of will not crash
00609   }
00610 
00611  /*----------------------------------------------------------------------*/
00612 
00613   void TestPointer::test_lock()
00614   {
00615 
00616         /*
00617          * You have to verify the following:
00618          * 
00619          * Lock the referenced object.
00620          */
00621 
00622         // The functionality of this method is already tested
00623   }
00624 
00625  /*----------------------------------------------------------------------*/
00626 
00627   void TestPointer::test_trylock()
00628   {
00629 
00630         /*
00631          * You have to verify the following:
00632          * 
00633          * Try locking referenced object.
00634          */
00635 
00636         // The functionality of this method is already tested
00637   }
00638 
00639  /*----------------------------------------------------------------------*/
00640 
00641   void TestPointer::test_unlock()
00642   {
00643 
00644         /*
00645          * You have to verify the following:
00646          * 
00647          * Unlock the referenced object.
00648          */
00649 
00650         // The functionality of this method is already tested
00651   }
00652 
00653  /*----------------------------------------------------------------------*/
00654 
00655   void TestPointer::test_getLocked()
00656   {
00657 
00658         /*
00659          * You have to verify the following:
00660          * 
00661          * Return true, if the object is locked.
00662          */
00663 
00664         CPPUNIT_FAIL("Implement this test!");
00665   }
00666 
00667  /*----------------------------------------------------------------------*/
00668 
00669   void TestPointer::test__deletePointer_cbool_cbool()
00670   {
00671 
00672         /*
00673          * You have to verify the following:
00674          * 
00675          * Delete the pointer. Decrease reference counter, relese lock, free memory if needed.
00676          */
00677 
00678         // The functionality of this method is already tested
00679   }
00680 
00681  /*----------------------------------------------------------------------*/
00682 
00683   void TestPointer::test__assignCPointer_cT_p_cbool()
00684   {
00685 
00686         /*
00687          * You have to verify the following:
00688          * 
00689          * Assign a C style pointer.
00690          */
00691 
00692         // The functionality of this method is already tested
00693   }
00694 
00695  /*----------------------------------------------------------------------*/
00696 
00697   void TestPointer::test__assignPointer_Pointer___T__Lock___cbool()
00698   {
00699 
00700         /*
00701          * You have to verify the following:
00702          * 
00703          * Assign another smart pointer.
00704          */
00705 
00706         // The functionality of this method is already tested
00707   }
00708 
00709  /*----------------------------------------------------------------------*/
00710 
00711   void TestPointer::test__switchPointer_Pointer___T__Lock___cbool()
00712   {
00713 
00714         /*
00715          * You have to verify the following:
00716          * 
00717          * Switch to another smart pointer. Helps changing reference under continous lock.
00718          */
00719 
00720         CPPUNIT_FAIL("Implement this test!");
00721   }
00722 
00723  /*----------------------------------------------------------------------*/
00724 
00725   void TestPointer::test__equalsCPointer_cT_p()
00726   {
00727 
00728         /*
00729          * You have to verify the following:
00730          * 
00731          * Test indentity.
00732          */
00733 
00734         // The functionality of this method is already tested
00735   }
00736 
00737  /*----------------------------------------------------------------------*/
00738 
00739   void TestPointer::test__equalsPointer_Pointer___T__Lock__()
00740   {
00741 
00742         /*
00743          * You have to verify the following:
00744          * 
00745          * Test indentity.
00746          */
00747 
00748         // The functionality of this method is already tested
00749   }
00750 
00751  /*----------------------------------------------------------------------*/
00752 
00753   // To register the suite we add:
00755   CPPUNIT_TEST_SUITE_REGISTRATION(TestPointer);
00756 }