| [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 |  | 
|---|
|  | 88 | static bool checkLock(boost::mutex &_mutex) | 
|---|
|  | 89 | { | 
|---|
|  | 90 | boost::mutex::scoped_lock lock(_mutex, boost::try_to_lock); | 
|---|
|  | 91 | return lock; | 
|---|
|  | 92 | } | 
|---|
|  | 93 |  | 
|---|
|  | 94 | void AtomicInstanceTest::ConstructionTest() | 
|---|
|  | 95 | { | 
|---|
|  | 96 | AtomicInstanceStub1 *ptr1_1 = new AtomicInstanceStub1(); | 
|---|
|  | 97 | AtomicInstanceStub2 *ptr2_1 = new AtomicInstanceStub2(); | 
|---|
|  | 98 | { | 
|---|
|  | 99 | CPPUNIT_ASSERT( checkLock(AtomicInstance<AtomicInstanceStub1>::atomicLock) ); | 
|---|
|  | 100 | AtomicInstance<AtomicInstanceStub1> atomic_ptr1_1(ptr1_1); | 
|---|
|  | 101 | CPPUNIT_ASSERT_EQUAL( ptr1_1, &(*atomic_ptr1_1) ); | 
|---|
|  | 102 | CPPUNIT_ASSERT( !checkLock(AtomicInstance<AtomicInstanceStub1>::atomicLock) ); | 
|---|
|  | 103 |  | 
|---|
|  | 104 | // will NULL no deadlock occurs | 
|---|
|  | 105 | { | 
|---|
|  | 106 | CPPUNIT_ASSERT( !checkLock(AtomicInstance<AtomicInstanceStub1>::atomicLock) ); | 
|---|
|  | 107 | AtomicInstance<AtomicInstanceStub1> atomic_ptr1_NULL(NULL); | 
|---|
|  | 108 | CPPUNIT_ASSERT( !checkLock(AtomicInstance<AtomicInstanceStub1>::atomicLock) ); | 
|---|
|  | 109 | } | 
|---|
|  | 110 |  | 
|---|
|  | 111 | CPPUNIT_ASSERT( checkLock(AtomicInstance<AtomicInstanceStub2>::atomicLock) ); | 
|---|
|  | 112 | const AtomicInstance<AtomicInstanceStub2> atomic_ptr2_1(ptr2_1); | 
|---|
|  | 113 | CPPUNIT_ASSERT_EQUAL( const_cast<const AtomicInstanceStub2 *>(ptr2_1), &(*atomic_ptr2_1) ); | 
|---|
|  | 114 | CPPUNIT_ASSERT( !checkLock(AtomicInstance<AtomicInstanceStub2>::atomicLock) ); | 
|---|
|  | 115 |  | 
|---|
|  | 116 | // move is ok | 
|---|
|  | 117 | CPPUNIT_ASSERT( !checkLock(AtomicInstance<AtomicInstanceStub1>::atomicLock) ); | 
|---|
|  | 118 | AtomicInstance<AtomicInstanceStub1> atomic_ptr1_2(atomic_ptr1_1); | 
|---|
|  | 119 | CPPUNIT_ASSERT( atomic_ptr1_1.content == (AtomicInstanceStub1 *)NULL); | 
|---|
|  | 120 | CPPUNIT_ASSERT( atomic_ptr1_2.content != (AtomicInstanceStub1 *)NULL); | 
|---|
|  | 121 | CPPUNIT_ASSERT( !checkLock(AtomicInstance<AtomicInstanceStub1>::atomicLock) ); | 
|---|
|  | 122 |  | 
|---|
|  | 123 | // this would cause deadlock | 
|---|
|  | 124 | //    AtomicInstance<AtomicInstanceStub1> atomic_ptr1_3(ptr1_1); | 
|---|
|  | 125 | } | 
|---|
|  | 126 | CPPUNIT_ASSERT( checkLock(AtomicInstance<AtomicInstanceStub1>::atomicLock) ); | 
|---|
|  | 127 | CPPUNIT_ASSERT( checkLock(AtomicInstance<AtomicInstanceStub2>::atomicLock) ); | 
|---|
|  | 128 | delete ptr1_1; | 
|---|
|  | 129 | delete ptr2_1; | 
|---|
|  | 130 | } | 
|---|
|  | 131 |  | 
|---|
|  | 132 | void AtomicInstanceTest::AssignmentTest() | 
|---|
|  | 133 | { | 
|---|
|  | 134 | AtomicInstanceStub1 *ptr1_1 = new AtomicInstanceStub1(); | 
|---|
|  | 135 | { | 
|---|
|  | 136 | // this does not lock | 
|---|
|  | 137 | CPPUNIT_ASSERT( checkLock(AtomicInstance<AtomicInstanceStub1>::atomicLock) ); | 
|---|
|  | 138 | AtomicInstance<AtomicInstanceStub1> atomic_ptr1_NULL(NULL); | 
|---|
|  | 139 | CPPUNIT_ASSERT( atomic_ptr1_NULL.content == (AtomicInstanceStub1 *)NULL ); | 
|---|
|  | 140 | CPPUNIT_ASSERT( checkLock(AtomicInstance<AtomicInstanceStub1>::atomicLock) ); | 
|---|
|  | 141 |  | 
|---|
|  | 142 | // this will lock | 
|---|
|  | 143 | AtomicInstance<AtomicInstanceStub1> atomic_ptr1_1(ptr1_1); | 
|---|
|  | 144 | CPPUNIT_ASSERT( atomic_ptr1_1.content != (AtomicInstanceStub1 *)NULL ); | 
|---|
|  | 145 | CPPUNIT_ASSERT( !checkLock(AtomicInstance<AtomicInstanceStub1>::atomicLock) ); | 
|---|
|  | 146 |  | 
|---|
|  | 147 | // now assign. Content has moved and should still be locked | 
|---|
|  | 148 | atomic_ptr1_NULL = atomic_ptr1_1; | 
|---|
|  | 149 | CPPUNIT_ASSERT( !checkLock(AtomicInstance<AtomicInstanceStub1>::atomicLock) ); | 
|---|
|  | 150 | CPPUNIT_ASSERT( atomic_ptr1_NULL.content != (AtomicInstanceStub1 *)NULL ); | 
|---|
|  | 151 | CPPUNIT_ASSERT( atomic_ptr1_1.content == (AtomicInstanceStub1 *)NULL ); | 
|---|
|  | 152 | } | 
|---|
|  | 153 | CPPUNIT_ASSERT( checkLock(AtomicInstance<AtomicInstanceStub1>::atomicLock) ); | 
|---|
|  | 154 |  | 
|---|
|  | 155 | delete ptr1_1; | 
|---|
|  | 156 | } | 
|---|