| [084729c] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
 | 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | /*
 | 
|---|
 | 9 |  * AtomicInstanceUnitTest.cpp
 | 
|---|
 | 10 |  *
 | 
|---|
 | 11 |  *  Created on: Apr 26, 2016
 | 
|---|
 | 12 |  *      Author: heber
 | 
|---|
 | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
 | 15 | // include config.h
 | 
|---|
 | 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 17 | #include <config.h>
 | 
|---|
 | 18 | #endif
 | 
|---|
 | 19 | 
 | 
|---|
 | 20 | #include "AtomicInstanceUnitTest.hpp"
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
 | 23 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
 | 24 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
 | 25 | #include <iostream>
 | 
|---|
 | 26 | 
 | 
|---|
 | 27 | #include "CodePatterns/AtomicInstance.hpp"
 | 
|---|
 | 28 | 
 | 
|---|
 | 29 | #ifdef HAVE_TESTRUNNER
 | 
|---|
 | 30 | #include "UnitTestMain.hpp"
 | 
|---|
 | 31 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
 | 32 | 
 | 
|---|
 | 33 | CPPUNIT_TEST_SUITE_REGISTRATION( AtomicInstanceTest );
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 | // some necessary stubs
 | 
|---|
 | 36 | class AtomicInstanceStub1 {
 | 
|---|
 | 37 | public:
 | 
|---|
 | 38 |   AtomicInstanceStub1(){
 | 
|---|
 | 39 |     count1++;
 | 
|---|
 | 40 |   }
 | 
|---|
 | 41 |   // explicit copy constructor to catch if this is ever called
 | 
|---|
 | 42 |   AtomicInstanceStub1(const AtomicInstanceStub1&){
 | 
|---|
 | 43 |     CPPUNIT_FAIL    ( "Copy constructor of AtomicInstance called" );
 | 
|---|
 | 44 |   }
 | 
|---|
 | 45 |   virtual ~AtomicInstanceStub1(){
 | 
|---|
 | 46 |     count2++;
 | 
|---|
 | 47 |   }
 | 
|---|
 | 48 | public:
 | 
|---|
 | 49 |   static int count1;
 | 
|---|
 | 50 |   static int count2;
 | 
|---|
 | 51 | };
 | 
|---|
 | 52 | 
 | 
|---|
 | 53 | int AtomicInstanceStub1::count1 = 0;
 | 
|---|
 | 54 | int AtomicInstanceStub1::count2 = 0;
 | 
|---|
 | 55 | 
 | 
|---|
 | 56 | //CONSTRUCT_ATOMIC(AtomicInstanceStub1)
 | 
|---|
 | 57 | 
 | 
|---|
 | 58 | // some necessary stubs
 | 
|---|
 | 59 | class AtomicInstanceStub2 {
 | 
|---|
 | 60 | public:
 | 
|---|
 | 61 |   AtomicInstanceStub2(){
 | 
|---|
 | 62 |     count1++;
 | 
|---|
 | 63 |   }
 | 
|---|
 | 64 |   // explicit copy constructor to catch if this is ever called
 | 
|---|
 | 65 |   AtomicInstanceStub2(const AtomicInstanceStub2&){
 | 
|---|
 | 66 |     CPPUNIT_FAIL    ( "Copy constructor of AtomicInstance called" );
 | 
|---|
 | 67 |   }
 | 
|---|
 | 68 |   virtual ~AtomicInstanceStub2(){
 | 
|---|
 | 69 |     count2++;
 | 
|---|
 | 70 |   }
 | 
|---|
 | 71 | public:
 | 
|---|
 | 72 |   static int count1;
 | 
|---|
 | 73 |   static int count2;
 | 
|---|
 | 74 | };
 | 
|---|
 | 75 | 
 | 
|---|
 | 76 | int AtomicInstanceStub2::count1 = 0;
 | 
|---|
 | 77 | int AtomicInstanceStub2::count2 = 0;
 | 
|---|
 | 78 | 
 | 
|---|
 | 79 | //CONSTRUCT_ATOMIC(AtomicInstanceStub2)
 | 
|---|
 | 80 | 
 | 
|---|
 | 81 | void AtomicInstanceTest::setUp()
 | 
|---|
 | 82 | {
 | 
|---|
 | 83 |   ASSERT_DO(Assert::Throw);
 | 
|---|
 | 84 | }
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 | void AtomicInstanceTest::tearDown(){}
 | 
|---|
 | 87 | 
 | 
|---|
| [2b88eb] | 88 | static bool isLockable(boost::mutex &_mutex)
 | 
|---|
| [084729c] | 89 | {
 | 
|---|
| [2b88eb] | 90 |   bool is_locked = _mutex.try_lock();
 | 
|---|
 | 91 |   if (is_locked)
 | 
|---|
 | 92 |     _mutex.unlock();
 | 
|---|
 | 93 |   return is_locked;
 | 
|---|
| [084729c] | 94 | }
 | 
|---|
 | 95 | 
 | 
|---|
 | 96 | void AtomicInstanceTest::ConstructionTest()
 | 
|---|
 | 97 | {
 | 
|---|
 | 98 |   AtomicInstanceStub1 *ptr1_1 = new AtomicInstanceStub1();
 | 
|---|
 | 99 |   AtomicInstanceStub2 *ptr2_1 = new AtomicInstanceStub2();
 | 
|---|
 | 100 |   {
 | 
|---|
| [2b88eb] | 101 |     CPPUNIT_ASSERT( isLockable(AtomicInstance<AtomicInstanceStub1>::atomicLock) );
 | 
|---|
| [084729c] | 102 |     AtomicInstance<AtomicInstanceStub1> atomic_ptr1_1(ptr1_1);
 | 
|---|
 | 103 |     CPPUNIT_ASSERT_EQUAL( ptr1_1, &(*atomic_ptr1_1) );
 | 
|---|
| [2b88eb] | 104 |     CPPUNIT_ASSERT( !isLockable(AtomicInstance<AtomicInstanceStub1>::atomicLock) );
 | 
|---|
| [084729c] | 105 | 
 | 
|---|
 | 106 |     // will NULL no deadlock occurs
 | 
|---|
 | 107 |     {
 | 
|---|
| [2b88eb] | 108 |       CPPUNIT_ASSERT( !isLockable(AtomicInstance<AtomicInstanceStub1>::atomicLock) );
 | 
|---|
| [084729c] | 109 |       AtomicInstance<AtomicInstanceStub1> atomic_ptr1_NULL(NULL);
 | 
|---|
| [2b88eb] | 110 |       CPPUNIT_ASSERT( !isLockable(AtomicInstance<AtomicInstanceStub1>::atomicLock) );
 | 
|---|
| [084729c] | 111 |     }
 | 
|---|
 | 112 | 
 | 
|---|
| [2b88eb] | 113 |     CPPUNIT_ASSERT( isLockable(AtomicInstance<AtomicInstanceStub2>::atomicLock) );
 | 
|---|
| [084729c] | 114 |     const AtomicInstance<AtomicInstanceStub2> atomic_ptr2_1(ptr2_1);
 | 
|---|
 | 115 |     CPPUNIT_ASSERT_EQUAL( const_cast<const AtomicInstanceStub2 *>(ptr2_1), &(*atomic_ptr2_1) );
 | 
|---|
| [2b88eb] | 116 |     CPPUNIT_ASSERT( !isLockable(AtomicInstance<AtomicInstanceStub2>::atomicLock) );
 | 
|---|
| [084729c] | 117 | 
 | 
|---|
 | 118 |     // move is ok
 | 
|---|
| [2b88eb] | 119 |     CPPUNIT_ASSERT( !isLockable(AtomicInstance<AtomicInstanceStub1>::atomicLock) );
 | 
|---|
| [084729c] | 120 |     AtomicInstance<AtomicInstanceStub1> atomic_ptr1_2(atomic_ptr1_1);
 | 
|---|
 | 121 |     CPPUNIT_ASSERT( atomic_ptr1_1.content == (AtomicInstanceStub1 *)NULL);
 | 
|---|
 | 122 |     CPPUNIT_ASSERT( atomic_ptr1_2.content != (AtomicInstanceStub1 *)NULL);
 | 
|---|
| [2b88eb] | 123 |     CPPUNIT_ASSERT( !isLockable(AtomicInstance<AtomicInstanceStub1>::atomicLock) );
 | 
|---|
| [084729c] | 124 | 
 | 
|---|
 | 125 |     // this would cause deadlock
 | 
|---|
 | 126 | //    AtomicInstance<AtomicInstanceStub1> atomic_ptr1_3(ptr1_1);
 | 
|---|
 | 127 |   }
 | 
|---|
| [2b88eb] | 128 |   CPPUNIT_ASSERT( isLockable(AtomicInstance<AtomicInstanceStub1>::atomicLock) );
 | 
|---|
 | 129 |   CPPUNIT_ASSERT( isLockable(AtomicInstance<AtomicInstanceStub2>::atomicLock) );
 | 
|---|
| [084729c] | 130 |   delete ptr1_1;
 | 
|---|
 | 131 |   delete ptr2_1;
 | 
|---|
 | 132 |  }
 | 
|---|
 | 133 | 
 | 
|---|
 | 134 | void AtomicInstanceTest::AssignmentTest()
 | 
|---|
 | 135 | {
 | 
|---|
 | 136 |   AtomicInstanceStub1 *ptr1_1 = new AtomicInstanceStub1();
 | 
|---|
 | 137 |   {
 | 
|---|
 | 138 |     // this does not lock
 | 
|---|
| [2b88eb] | 139 |     CPPUNIT_ASSERT( isLockable(AtomicInstance<AtomicInstanceStub1>::atomicLock) );
 | 
|---|
| [084729c] | 140 |     AtomicInstance<AtomicInstanceStub1> atomic_ptr1_NULL(NULL);
 | 
|---|
 | 141 |     CPPUNIT_ASSERT( atomic_ptr1_NULL.content == (AtomicInstanceStub1 *)NULL );
 | 
|---|
| [2b88eb] | 142 |     CPPUNIT_ASSERT( isLockable(AtomicInstance<AtomicInstanceStub1>::atomicLock) );
 | 
|---|
| [084729c] | 143 | 
 | 
|---|
 | 144 |     // this will lock
 | 
|---|
 | 145 |     AtomicInstance<AtomicInstanceStub1> atomic_ptr1_1(ptr1_1);
 | 
|---|
 | 146 |     CPPUNIT_ASSERT( atomic_ptr1_1.content != (AtomicInstanceStub1 *)NULL );
 | 
|---|
| [2b88eb] | 147 |     CPPUNIT_ASSERT( !isLockable(AtomicInstance<AtomicInstanceStub1>::atomicLock) );
 | 
|---|
| [084729c] | 148 | 
 | 
|---|
 | 149 |     // now assign. Content has moved and should still be locked
 | 
|---|
 | 150 |     atomic_ptr1_NULL = atomic_ptr1_1;
 | 
|---|
| [2b88eb] | 151 |     CPPUNIT_ASSERT( !isLockable(AtomicInstance<AtomicInstanceStub1>::atomicLock) );
 | 
|---|
| [084729c] | 152 |     CPPUNIT_ASSERT( atomic_ptr1_NULL.content != (AtomicInstanceStub1 *)NULL );
 | 
|---|
 | 153 |     CPPUNIT_ASSERT( atomic_ptr1_1.content == (AtomicInstanceStub1 *)NULL );
 | 
|---|
 | 154 |   }
 | 
|---|
| [2b88eb] | 155 |   CPPUNIT_ASSERT( isLockable(AtomicInstance<AtomicInstanceStub1>::atomicLock) );
 | 
|---|
| [084729c] | 156 | 
 | 
|---|
 | 157 |   delete ptr1_1;
 | 
|---|
 | 158 | }
 | 
|---|